GNU Octave  4.0.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
Range.h
Go to the documentation of this file.
1 /*
2 
3 Copyright (C) 1993-2015 John W. Eaton
4 
5 This file is part of Octave.
6 
7 Octave is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by the
9 Free Software Foundation; either version 3 of the License, or (at your
10 option) any later version.
11 
12 Octave is distributed in the hope that it will be useful, but WITHOUT
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
16 
17 You should have received a copy of the GNU General Public License
18 along with Octave; see the file COPYING. If not, see
19 <http://www.gnu.org/licenses/>.
20 
21 */
22 
23 #if !defined (octave_Range_h)
24 #define octave_Range_h 1
25 
26 #include <iosfwd>
27 
28 #include "dMatrix.h"
29 #include "oct-sort.h"
30 
31 class
32 OCTAVE_API
33 Range
34 {
35 public:
36 
37  Range (void)
38  : rng_base (0), rng_limit (0), rng_inc (0), rng_nelem (0), cache (1, 0) { }
39 
40  Range (const Range& r)
41  : rng_base (r.rng_base), rng_limit (r.rng_limit), rng_inc (r.rng_inc),
42  rng_nelem (r.rng_nelem), cache (r.cache) { }
43 
44  Range (double b, double l)
45  : rng_base (b), rng_limit (l), rng_inc (1),
46  rng_nelem (nelem_internal ()), cache () { }
47 
48  Range (double b, double l, double i)
49  : rng_base (b), rng_limit (l), rng_inc (i),
50  rng_nelem (nelem_internal ()), cache () { }
51 
52  // For operators' usage (to preserve element count).
53  Range (double b, double i, octave_idx_type n)
54  : rng_base (b), rng_limit (b + (n-1) * i), rng_inc (i),
55  rng_nelem (n), cache ()
56  {
57  if (! xfinite (b) || ! xfinite (i) || ! xfinite (rng_limit))
58  rng_nelem = -2;
59  }
60 
61  double base (void) const { return rng_base; }
62  double limit (void) const { return rng_limit; }
63  double inc (void) const { return rng_inc; }
64  octave_idx_type nelem (void) const { return rng_nelem; }
65 
66  bool all_elements_are_ints (void) const;
67 
68  Matrix matrix_value (void) const;
69 
70  double min (void) const;
71  double max (void) const;
72 
73  void sort_internal (bool ascending = true);
74  void sort_internal (Array<octave_idx_type>& sidx, bool ascending = true);
75 
76  Matrix diag (octave_idx_type k = 0) const;
77 
78  Range sort (octave_idx_type dim = 0, sortmode mode = ASCENDING) const;
79 
80  Range sort (Array<octave_idx_type>& sidx, octave_idx_type dim = 0,
81  sortmode mode = ASCENDING) const;
82 
83  sortmode is_sorted (sortmode mode = ASCENDING) const;
84 
85  // Support for single-index subscripting, without generating matrix cache.
86 
87  double checkelem (octave_idx_type i) const;
88 
89  double elem (octave_idx_type i) const;
90 
91  Array<double> index (const idx_vector& i) const;
92 
93  void set_base (double b)
94  {
95  if (rng_base != b)
96  {
97  rng_base = b;
98  clear_cache ();
99  }
100  }
101 
102  void set_limit (double l)
103  {
104  if (rng_limit != l)
105  {
106  rng_limit = l;
107  clear_cache ();
108  }
109  }
110 
111  void set_inc (double i)
112  {
113  if (rng_inc != i)
114  {
115  rng_inc = i;
116  clear_cache ();
117  }
118  }
119 
120  friend OCTAVE_API std::ostream& operator << (std::ostream& os,
121  const Range& r);
122  friend OCTAVE_API std::istream& operator >> (std::istream& is, Range& r);
123 
124  friend OCTAVE_API Range operator - (const Range& r);
125  friend OCTAVE_API Range operator + (double x, const Range& r);
126  friend OCTAVE_API Range operator + (const Range& r, double x);
127  friend OCTAVE_API Range operator - (double x, const Range& r);
128  friend OCTAVE_API Range operator - (const Range& r, double x);
129  friend OCTAVE_API Range operator * (double x, const Range& r);
130  friend OCTAVE_API Range operator * (const Range& r, double x);
131 
132  void print_range (void);
133 
134 private:
135 
136  double rng_base;
137  double rng_limit;
138  double rng_inc;
139 
141 
142  mutable Matrix cache;
143 
144  octave_idx_type nelem_internal (void) const;
145 
146  void clear_cache (void) const { cache.resize (0, 0); }
147 
148 protected:
149 
150  // For operators' usage (to allow all values to be set directly).
151  Range (double b, double l, double i, octave_idx_type n)
152  : rng_base (b), rng_limit (l), rng_inc (i),
153  rng_nelem (n), cache ()
154  {
155  if (! xfinite (b) || ! xfinite (i) || ! xfinite (l))
156  rng_nelem = -2;
157  }
158 };
159 
160 extern OCTAVE_API Range operator - (const Range& r);
161 
162 extern OCTAVE_API Range operator + (double x, const Range& r);
163 
164 extern OCTAVE_API Range operator + (const Range& r, double x);
165 
166 extern OCTAVE_API Range operator - (double x, const Range& r);
167 
168 extern OCTAVE_API Range operator - (const Range& r, double x);
169 
170 extern OCTAVE_API Range operator * (double x, const Range& r);
171 
172 extern OCTAVE_API Range operator * (const Range& r, double x);
173 
174 #endif
OCTAVE_API Range operator-(const Range &r)
Definition: Range.cc:388
Matrix cache
Definition: Range.h:142
Range(void)
Definition: Range.h:37
void resize(octave_idx_type nr, octave_idx_type nc, double rfv=0)
Definition: dMatrix.h:130
std::istream & operator>>(std::istream &is, SparseBoolMatrix &a)
Definition: boolSparse.cc:279
double rng_limit
Definition: Range.h:137
sortmode
Definition: oct-sort.h:103
octave_idx_type nelem(void) const
Definition: Range.h:64
Definition: Range.h:31
double rng_base
Definition: Range.h:136
double limit(void) const
Definition: Range.h:62
void set_base(double b)
Definition: Range.h:93
double inc(void) const
Definition: Range.h:63
Range(const Range &r)
Definition: Range.h:40
static int elem
Definition: __contourc__.cc:49
static void cache(const std::string key, str_llist_type *value)
Definition: kpse.cc:2105
void set_limit(double l)
Definition: Range.h:102
Definition: dMatrix.h:35
Range(double b, double i, octave_idx_type n)
Definition: Range.h:53
Range(double b, double l, double i)
Definition: Range.h:48
charNDArray max(char d, const charNDArray &m)
Definition: chNDArray.cc:233
bool xfinite(double x)
Definition: lo-mappers.cc:152
void set_inc(double i)
Definition: Range.h:111
Range(double b, double l)
Definition: Range.h:44
template OCTAVE_API std::ostream & operator<<(std::ostream &, const Array< bool > &)
double base(void) const
Definition: Range.h:61
OCTAVE_API Range operator*(double x, const Range &r)
Definition: Range.cc:429
octave_idx_type rng_nelem
Definition: Range.h:140
OCTAVE_API Range operator+(double x, const Range &r)
Definition: Range.cc:393
double rng_inc
Definition: Range.h:138
Range(double b, double l, double i, octave_idx_type n)
Definition: Range.h:151
F77_RET_T const double * x
void clear_cache(void) const
Definition: Range.h:146
charNDArray min(char d, const charNDArray &m)
Definition: chNDArray.cc:210