comparison liboctave/array/Range.h @ 20548:96153b16febe

Overhaul Range object in liboctave. * Range.h (Range (b, l), Range (b, l, i)): For 2- or 3-element form of constructor, set internal rng_limit equal to actual limit of range object. * Range.h (set_base, set_limit, set_inc): Change to prototypes only. Implementation moved to Range.cc * Range.cc (matrix_value): Return rng_base for first element, rng_limit for last element. * Range.cc (checkelem, elem, index): Simplify functions by returning rng_limit for last element. * Range.cc (sort_internal (bool), sort_internal (Array<octave_idx_t>, bool)): Simplify functions by simply swapping rng_base and rng_limit when sorting a range in the opposite direction. * Range.cc (operator <<): Simplify function by returning rng_limit for last element. * Range.cc (operator >>): Fix input stream operator so a correctly sync'ed range object is created with the correct rng_limit and rng_numel. * Range.cc (set_base, set_inc): Update rng_numel by calling numel_internal () after changing base or increment so that range object is correctly in sync with itself. Clip rng_limit to true limit of data after base or inc has been set. * Range.h (set_limit): Update rng_numel after changing internal rng_limit. Clip rng_limit to actual limit of range object.
author Rik <rik@octave.org>
date Fri, 25 Sep 2015 16:15:14 -0700
parents 00cf2847355d
children
comparison
equal deleted inserted replaced
20547:8164c580922b 20548:96153b16febe
41 : rng_base (r.rng_base), rng_limit (r.rng_limit), rng_inc (r.rng_inc), 41 : rng_base (r.rng_base), rng_limit (r.rng_limit), rng_inc (r.rng_inc),
42 rng_numel (r.rng_numel), cache (r.cache) { } 42 rng_numel (r.rng_numel), cache (r.cache) { }
43 43
44 Range (double b, double l) 44 Range (double b, double l)
45 : rng_base (b), rng_limit (l), rng_inc (1), 45 : rng_base (b), rng_limit (l), rng_inc (1),
46 rng_numel (numel_internal ()), cache () { } 46 rng_numel (numel_internal ()), cache ()
47 {
48 double tmplimit = rng_limit;
49
50 if (rng_inc > 0)
51 tmplimit = max ();
52 else
53 tmplimit = min ();
54
55 if (tmplimit != rng_limit)
56 rng_limit = tmplimit;
57 }
47 58
48 Range (double b, double l, double i) 59 Range (double b, double l, double i)
49 : rng_base (b), rng_limit (l), rng_inc (i), 60 : rng_base (b), rng_limit (l), rng_inc (i),
50 rng_numel (numel_internal ()), cache () { } 61 rng_numel (numel_internal ()), cache ()
62 {
63 double tmplimit = rng_limit;
64
65 if (rng_inc > 0)
66 tmplimit = max ();
67 else
68 tmplimit = min ();
69
70 if (tmplimit != rng_limit)
71 rng_limit = tmplimit;
72 }
51 73
52 // For operators' usage (to preserve element count). 74 // For operators' usage (to preserve element count).
53 Range (double b, double i, octave_idx_type n) 75 Range (double b, double i, octave_idx_type n)
54 : rng_base (b), rng_limit (b + (n-1) * i), rng_inc (i), 76 : rng_base (b), rng_limit (b + (n-1) * i), rng_inc (i),
55 rng_numel (n), cache () 77 rng_numel (n), cache ()
56 { 78 {
57 if (! xfinite (b) || ! xfinite (i) || ! xfinite (rng_limit)) 79 if (! xfinite (b) || ! xfinite (i) || ! xfinite (rng_limit))
58 rng_numel = -2; 80 rng_numel = -2;
81 else
82 {
83 // Code below is only needed if the resulting range must be 100%
84 // correctly constructed. If the Range object created is only
85 // a temporary one used by operators this may be unnecessary.
86 double tmplimit = rng_limit;
87
88 if (rng_inc > 0)
89 tmplimit = max ();
90 else
91 tmplimit = min ();
92
93 if (tmplimit != rng_limit)
94 rng_limit = tmplimit;
95 }
59 } 96 }
60 97
61 double base (void) const { return rng_base; } 98 double base (void) const { return rng_base; }
62 double limit (void) const { return rng_limit; } 99 double limit (void) const { return rng_limit; }
63 double inc (void) const { return rng_inc; } 100 double inc (void) const { return rng_inc; }
89 126
90 double elem (octave_idx_type i) const; 127 double elem (octave_idx_type i) const;
91 128
92 Array<double> index (const idx_vector& i) const; 129 Array<double> index (const idx_vector& i) const;
93 130
94 void set_base (double b) 131 void set_base (double b);
95 {
96 if (rng_base != b)
97 {
98 rng_base = b;
99 clear_cache ();
100 }
101 }
102 132
103 void set_limit (double l) 133 void set_limit (double l);
104 {
105 if (rng_limit != l)
106 {
107 rng_limit = l;
108 clear_cache ();
109 }
110 }
111 134
112 void set_inc (double i) 135 void set_inc (double i);
113 {
114 if (rng_inc != i)
115 {
116 rng_inc = i;
117 clear_cache ();
118 }
119 }
120 136
121 friend OCTAVE_API std::ostream& operator << (std::ostream& os, 137 friend OCTAVE_API std::ostream& operator << (std::ostream& os,
122 const Range& r); 138 const Range& r);
123 friend OCTAVE_API std::istream& operator >> (std::istream& is, Range& r); 139 friend OCTAVE_API std::istream& operator >> (std::istream& is, Range& r);
124 140