comparison liboctave/array/Range.h @ 21321:79a51b7e00b6

eliminate some copy and paste code fragments in the Range class * Range.h, Range.cc (Range::limit_internal, Range::init): New functions. (Range::Range, Range::set_base, Range::set_limit, Range::set_inc): Use them to eliminate copy and paste code fragments.
author John W. Eaton <jwe@octave.org>
date Mon, 22 Feb 2016 21:28:22 -0500
parents 1473547f50f5
children 2aef506f3fec
comparison
equal deleted inserted replaced
21320:241666557fec 21321:79a51b7e00b6
45 45
46 Range (double b, double l) 46 Range (double b, double l)
47 : rng_base (b), rng_limit (l), rng_inc (1), 47 : rng_base (b), rng_limit (l), rng_inc (1),
48 rng_numel (numel_internal ()), cache () 48 rng_numel (numel_internal ()), cache ()
49 { 49 {
50 double tmplimit = rng_limit; 50 rng_limit = limit_internal ();
51
52 if (rng_inc > 0)
53 tmplimit = max ();
54 else
55 tmplimit = min ();
56
57 if (tmplimit != rng_limit)
58 rng_limit = tmplimit;
59 } 51 }
60 52
61 Range (double b, double l, double i) 53 Range (double b, double l, double i)
62 : rng_base (b), rng_limit (l), rng_inc (i), 54 : rng_base (b), rng_limit (l), rng_inc (i),
63 rng_numel (numel_internal ()), cache () 55 rng_numel (numel_internal ()), cache ()
64 { 56 {
65 double tmplimit = rng_limit; 57 rng_limit = limit_internal ();
66
67 if (rng_inc > 0)
68 tmplimit = max ();
69 else
70 tmplimit = min ();
71
72 if (tmplimit != rng_limit)
73 rng_limit = tmplimit;
74 } 58 }
75 59
76 // For operators' usage (to preserve element count). 60 // For operators' usage (to preserve element count).
77 Range (double b, double i, octave_idx_type n) 61 Range (double b, double i, octave_idx_type n)
78 : rng_base (b), rng_limit (b + (n-1) * i), rng_inc (i), 62 : rng_base (b), rng_limit (b + (n-1) * i), rng_inc (i),
83 else 67 else
84 { 68 {
85 // Code below is only needed if the resulting range must be 100% 69 // Code below is only needed if the resulting range must be 100%
86 // correctly constructed. If the Range object created is only 70 // correctly constructed. If the Range object created is only
87 // a temporary one used by operators this may be unnecessary. 71 // a temporary one used by operators this may be unnecessary.
88 double tmplimit = rng_limit;
89 72
90 if (rng_inc > 0) 73 rng_limit = limit_internal ();
91 tmplimit = max ();
92 else
93 tmplimit = min ();
94
95 if (tmplimit != rng_limit)
96 rng_limit = tmplimit;
97 } 74 }
98 } 75 }
99 76
100 double base (void) const { return rng_base; } 77 double base (void) const { return rng_base; }
101 double limit (void) const { return rng_limit; } 78 double limit (void) const { return rng_limit; }
163 140
164 mutable Matrix cache; 141 mutable Matrix cache;
165 142
166 octave_idx_type numel_internal (void) const; 143 octave_idx_type numel_internal (void) const;
167 144
145 double limit_internal (void) const;
146
147 void init (void);
148
168 void clear_cache (void) const { cache.resize (0, 0); } 149 void clear_cache (void) const { cache.resize (0, 0); }
169 150
170 protected: 151 protected:
171 152
172 // For operators' usage (to allow all values to be set directly). 153 // For operators' usage (to allow all values to be set directly).