1 ////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright (C) 1993-2021 The Octave Project Developers
4 //
5 // See the file COPYRIGHT.md in the top-level directory of this
6 // distribution or <https://octave.org/copyright/>.
7 //
8 // This file is part of Octave.
9 //
10 // Octave is free software: you can redistribute it and/or modify it
11 // under the terms of the GNU General Public License as published by
12 // the Free Software Foundation, either version 3 of the License, or
13 // (at your option) any later version.
14 //
15 // Octave is distributed in the hope that it will be useful, but
16 // WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 // GNU General Public License for more details.
19 //
20 // You should have received a copy of the GNU General Public License
21 // along with Octave; see the file COPYING.  If not, see
22 // <https://www.gnu.org/licenses/>.
23 //
24 ////////////////////////////////////////////////////////////////////////
25 
26 #if ! defined (octave_Range_h)
27 #define octave_Range_h 1
28 
29 #include "octave-config.h"
30 
31 #include <iosfwd>
32 
33 #include "dMatrix.h"
34 #include "dim-vector.h"
35 #include "oct-sort.h"
36 
37 class
38 OCTAVE_API
39 Range
40 {
41 public:
42 
Range(void)43   Range (void)
44     : rng_base (0), rng_limit (0), rng_inc (0), rng_numel (0), cache (1, 0) { }
45 
46   Range (const Range& r) = default;
47 
48   Range& operator = (const Range& r) = default;
49 
50   ~Range (void) = default;
51 
Range(double b,double l)52   Range (double b, double l)
53     : rng_base (b), rng_limit (l), rng_inc (1),
54       rng_numel (numel_internal ()), cache ()
55   {
56     rng_limit = limit_internal ();
57   }
58 
Range(double b,double l,double i)59   Range (double b, double l, double i)
60     : rng_base (b), rng_limit (l), rng_inc (i),
61       rng_numel (numel_internal ()), cache ()
62   {
63     rng_limit = limit_internal ();
64   }
65 
66   // For operators' usage (to preserve element count).
Range(double b,double i,octave_idx_type n)67   Range (double b, double i, octave_idx_type n)
68     : rng_base (b), rng_limit (b + (n-1) * i), rng_inc (i),
69       rng_numel (n), cache ()
70   {
71     if (! octave::math::isfinite (b) || ! octave::math::isfinite (i)
72         || ! octave::math::isfinite (rng_limit))
73       rng_numel = -2;
74     else
75       {
76         // Code below is only needed if the resulting range must be 100%
77         // correctly constructed.  If the Range object created is only
78         // a temporary one used by operators this may be unnecessary.
79         rng_limit = limit_internal ();
80       }
81   }
82 
base(void)83   double base (void) const { return rng_base; }
limit(void)84   double limit (void) const { return rng_limit; }
inc(void)85   double inc (void) const { return rng_inc; }
86 
numel(void)87   octave_idx_type numel (void) const { return rng_numel; }
88 
dims(void)89   dim_vector dims (void) const { return dim_vector (1, rng_numel); }
90 
rows(void)91   octave_idx_type rows (void) const { return 1; }
92 
cols(void)93   octave_idx_type cols (void) const { return numel (); }
columns(void)94   octave_idx_type columns (void) const { return numel (); }
95 
isempty(void)96   bool isempty (void) const { return numel () == 0; }
97 
98   bool all_elements_are_ints (void) const;
99 
100   Matrix matrix_value (void) const;
101 
102   double min (void) const;
103   double max (void) const;
104 
105   void sort_internal (bool ascending = true);
106   void sort_internal (Array<octave_idx_type>& sidx, bool ascending = true);
107 
108   Matrix diag (octave_idx_type k = 0) const;
109 
110   Range sort (octave_idx_type dim = 0, sortmode mode = ASCENDING) const;
111   Range sort (Array<octave_idx_type>& sidx, octave_idx_type dim = 0,
112               sortmode mode = ASCENDING) const;
113 
114   sortmode issorted (sortmode mode = ASCENDING) const;
115 
116   octave_idx_type nnz (void) const;
117 
118   // Support for single-index subscripting, without generating matrix cache.
119 
120   double checkelem (octave_idx_type i) const;
121   double checkelem (octave_idx_type i, octave_idx_type j) const;
122 
123   double elem (octave_idx_type i) const;
elem(octave_idx_type,octave_idx_type j)124   double elem (octave_idx_type /* i */, octave_idx_type j) const
125   { return elem (j); }
126 
operator()127   double operator () (octave_idx_type i) const { return elem (i); }
operator()128   double operator () (octave_idx_type i, octave_idx_type j) const
129   { return elem (i, j); }
130 
131   Array<double> index (const idx_vector& i) const;
132 
133   void set_base (double b);
134 
135   void set_limit (double l);
136 
137   void set_inc (double i);
138 
139   friend OCTAVE_API std::ostream& operator << (std::ostream& os,
140                                                const Range& r);
141   friend OCTAVE_API std::istream& operator >> (std::istream& is, Range& r);
142 
143   friend OCTAVE_API Range operator - (const Range& r);
144   friend OCTAVE_API Range operator + (double x, const Range& r);
145   friend OCTAVE_API Range operator + (const Range& r, double x);
146   friend OCTAVE_API Range operator - (double x, const Range& r);
147   friend OCTAVE_API Range operator - (const Range& r, double x);
148   friend OCTAVE_API Range operator * (double x, const Range& r);
149   friend OCTAVE_API Range operator * (const Range& r, double x);
150 
151 private:
152 
153   double rng_base;
154   double rng_limit;
155   double rng_inc;
156 
157   octave_idx_type rng_numel;
158 
159   mutable Matrix cache;
160 
161   octave_idx_type numel_internal (void) const;
162 
163   double limit_internal (void) const;
164 
165   void init (void);
166 
clear_cache(void)167   void clear_cache (void) const { cache.resize (0, 0); }
168 
169 protected:
170 
171   // For operators' usage (to allow all values to be set directly).
Range(double b,double l,double i,octave_idx_type n)172   Range (double b, double l, double i, octave_idx_type n)
173     : rng_base (b), rng_limit (l), rng_inc (i),
174       rng_numel (n), cache ()
175   {
176     if (! octave::math::isfinite (b) || ! octave::math::isfinite (i)
177         || ! octave::math::isfinite (l))
178       rng_numel = -2;
179   }
180 };
181 
182 extern OCTAVE_API Range operator - (const Range& r);
183 extern OCTAVE_API Range operator + (double x, const Range& r);
184 extern OCTAVE_API Range operator + (const Range& r, double x);
185 extern OCTAVE_API Range operator - (double x, const Range& r);
186 extern OCTAVE_API Range operator - (const Range& r, double x);
187 extern OCTAVE_API Range operator * (double x, const Range& r);
188 extern OCTAVE_API Range operator * (const Range& r, double x);
189 
190 #endif
191