GNU Octave  3.8.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-2013 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))
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 };
149 
150 extern OCTAVE_API Range operator - (const Range& r);
151 
152 extern OCTAVE_API Range operator + (double x, const Range& r);
153 
154 extern OCTAVE_API Range operator + (const Range& r, double x);
155 
156 extern OCTAVE_API Range operator - (double x, const Range& r);
157 
158 extern OCTAVE_API Range operator - (const Range& r, double x);
159 
160 extern OCTAVE_API Range operator * (double x, const Range& r);
161 
162 extern OCTAVE_API Range operator * (const Range& r, double x);
163 
164 #endif