1
|
1 /* |
|
2 |
7017
|
3 Copyright (C) 1993, 1994, 1995, 1996, 1997, 1998, 2000, 2002, 2003, |
|
4 2004, 2005, 2006, 2007 John W. Eaton |
1
|
5 |
|
6 This file is part of Octave. |
|
7 |
|
8 Octave is free software; you can redistribute it and/or modify it |
|
9 under the terms of the GNU General Public License as published by the |
7016
|
10 Free Software Foundation; either version 3 of the License, or (at your |
|
11 option) any later version. |
1
|
12 |
|
13 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
16 for more details. |
|
17 |
|
18 You should have received a copy of the GNU General Public License |
7016
|
19 along with Octave; see the file COPYING. If not, see |
|
20 <http://www.gnu.org/licenses/>. |
1
|
21 |
|
22 */ |
|
23 |
383
|
24 #if !defined (octave_idx_vector_h) |
|
25 #define octave_idx_vector_h 1 |
1
|
26 |
3503
|
27 #include <iostream> |
|
28 |
4653
|
29 #include "dim-vector.h" |
4938
|
30 #include "oct-inttypes.h" |
|
31 #include "intNDArray.h" |
4504
|
32 |
1560
|
33 class ColumnVector; |
4650
|
34 class boolNDArray; |
|
35 class NDArray; |
164
|
36 class Range; |
|
37 |
453
|
38 class |
6108
|
39 OCTAVE_API |
453
|
40 idx_vector |
1
|
41 { |
1560
|
42 private: |
|
43 |
1879
|
44 class |
6108
|
45 OCTAVE_API |
1560
|
46 idx_vector_rep |
1879
|
47 { |
|
48 public: |
|
49 |
2802
|
50 idx_vector_rep (void) |
4653
|
51 : data (0), len (0), num_zeros (0), num_ones (0), max_val (0), |
|
52 min_val (0), count (1), frozen_at_z_len (0), frozen_len (0), |
|
53 colon (0), one_zero (0), initialized (0), frozen (0), |
|
54 colon_equiv_checked (0), colon_equiv (0), orig_dims () { } |
1560
|
55 |
1879
|
56 idx_vector_rep (const ColumnVector& v); |
1560
|
57 |
4650
|
58 idx_vector_rep (const NDArray& nda); |
1879
|
59 |
4938
|
60 template <class U> |
|
61 idx_vector_rep (const intNDArray<U>& inda) |
|
62 : data (0), len (inda.length ()), num_zeros (0), num_ones (0), |
|
63 max_val (0), min_val (0), count (1), frozen_at_z_len (0), |
|
64 frozen_len (0), colon (0), one_zero (0), initialized (0), |
|
65 frozen (0), colon_equiv_checked (0), colon_equiv (0), |
|
66 orig_dims (inda.dims ()) |
|
67 { |
|
68 if (len == 0) |
|
69 { |
|
70 initialized = 1; |
|
71 return; |
|
72 } |
|
73 else |
|
74 { |
5275
|
75 data = new octave_idx_type [len]; |
4938
|
76 |
|
77 bool conversion_error = false; |
|
78 |
5275
|
79 for (octave_idx_type i = 0; i < len; i++) |
4938
|
80 data[i] = tree_to_mat_idx (inda.elem (i), conversion_error); |
|
81 |
|
82 if (conversion_error) |
|
83 return; |
|
84 } |
|
85 |
|
86 init_state (); |
|
87 } |
|
88 |
1879
|
89 idx_vector_rep (const Range& r); |
1560
|
90 |
2386
|
91 idx_vector_rep (double d); |
|
92 |
5275
|
93 idx_vector_rep (octave_idx_type i); |
3928
|
94 |
1879
|
95 idx_vector_rep (char c); |
1560
|
96 |
2828
|
97 idx_vector_rep (bool b); |
|
98 |
4938
|
99 template <class U> |
|
100 idx_vector_rep (const octave_int<U>& i) |
|
101 : data (0), len (1), num_zeros (0), num_ones (0), |
|
102 max_val (0), min_val (0), count (1), frozen_at_z_len (0), |
|
103 frozen_len (0), colon (0), one_zero (0), initialized (0), |
|
104 frozen (0), colon_equiv_checked (0), colon_equiv (0), |
|
105 orig_dims (1, 1) |
|
106 { |
5275
|
107 data = new octave_idx_type [len]; |
4938
|
108 |
|
109 data[0] = tree_to_mat_idx (i); |
|
110 |
|
111 init_state (); |
|
112 } |
|
113 |
4650
|
114 idx_vector_rep (const boolNDArray& bnda); |
2828
|
115 |
1879
|
116 idx_vector_rep (const idx_vector_rep& a); |
1560
|
117 |
2802
|
118 ~idx_vector_rep (void) { delete [] data; } |
1560
|
119 |
1879
|
120 idx_vector_rep& operator = (const idx_vector_rep& a); |
1560
|
121 |
1879
|
122 int ok (void) { return initialized; } |
1560
|
123 |
5275
|
124 octave_idx_type capacity (void) const { return len; } |
|
125 octave_idx_type length (octave_idx_type colon_len) const { return colon ? colon_len : len; } |
1560
|
126 |
5275
|
127 octave_idx_type elem (octave_idx_type n) const { return colon ? n : data[n]; } |
1560
|
128 |
5275
|
129 octave_idx_type checkelem (octave_idx_type n) const; |
|
130 octave_idx_type operator () (octave_idx_type n) const { return checkelem (n); } |
1560
|
131 |
5275
|
132 octave_idx_type max (void) const { return max_val; } |
|
133 octave_idx_type min (void) const { return min_val; } |
1879
|
134 |
|
135 int one_zero_only (void) const { return one_zero; } |
5275
|
136 octave_idx_type zeros_count (void) const { return num_zeros; } |
|
137 octave_idx_type ones_count (void) const { return num_ones; } |
1879
|
138 |
|
139 int is_colon (void) const { return colon; } |
5275
|
140 int is_colon_equiv (octave_idx_type n, int sort_uniq); |
1560
|
141 |
3079
|
142 void sort (bool uniq); |
|
143 |
5275
|
144 octave_idx_type orig_rows (void) const { return orig_dims(0); } |
|
145 octave_idx_type orig_columns (void) const { return orig_dims(1); } |
1560
|
146 |
4653
|
147 dim_vector orig_dimensions (void) const { return orig_dims; } |
4504
|
148 |
1879
|
149 // other stuff |
1560
|
150 |
5275
|
151 void shorten (octave_idx_type n); // Unsafe. Avoid at all cost. |
1560
|
152 |
5781
|
153 octave_idx_type freeze (octave_idx_type z_len, const char *tag, bool resize_ok); |
1560
|
154 |
1879
|
155 // i/o |
1560
|
156 |
3504
|
157 std::ostream& print (std::ostream& os) const; |
1560
|
158 |
5275
|
159 octave_idx_type *data; |
|
160 octave_idx_type len; |
|
161 octave_idx_type num_zeros; |
|
162 octave_idx_type num_ones; |
|
163 octave_idx_type max_val; |
|
164 octave_idx_type min_val; |
4504
|
165 |
1879
|
166 int count; |
5275
|
167 |
|
168 octave_idx_type frozen_at_z_len; |
|
169 octave_idx_type frozen_len; |
4653
|
170 |
1879
|
171 unsigned int colon : 1; |
|
172 unsigned int one_zero : 1; |
|
173 unsigned int initialized : 1; |
|
174 unsigned int frozen : 1; |
|
175 unsigned int colon_equiv_checked : 1; |
|
176 unsigned int colon_equiv : 1; |
1560
|
177 |
4653
|
178 dim_vector orig_dims; |
|
179 |
1879
|
180 void init_state (void); |
1560
|
181 |
5275
|
182 void maybe_convert_one_zero_to_idx (octave_idx_type z_len); |
4938
|
183 |
5275
|
184 octave_idx_type tree_to_mat_idx (double x, bool& conversion_error); |
4938
|
185 |
5275
|
186 octave_idx_type tree_to_mat_idx (octave_idx_type i) { return i - 1; } |
4938
|
187 |
5275
|
188 template <class U> octave_idx_type tree_to_mat_idx (const octave_int<U>& i) |
4938
|
189 { return i.value () - 1; } |
1879
|
190 }; |
1560
|
191 |
1
|
192 public: |
1551
|
193 |
4979
|
194 idx_vector (void) : rep (new idx_vector_rep ()) { } |
1560
|
195 |
4979
|
196 idx_vector (const ColumnVector& v) : rep (new idx_vector_rep (v)) { } |
1560
|
197 |
4979
|
198 idx_vector (const NDArray& nda) : rep (new idx_vector_rep (nda)) { } |
1560
|
199 |
4938
|
200 template <class U> |
4979
|
201 idx_vector (const intNDArray<U>& inda) : rep (new idx_vector_rep (inda)) { } |
4938
|
202 |
4979
|
203 idx_vector (const Range& r) : rep (new idx_vector_rep (r)) { } |
1560
|
204 |
4979
|
205 idx_vector (double d) : rep (new idx_vector_rep (d)) { } |
2386
|
206 |
5275
|
207 idx_vector (octave_idx_type i) : rep (new idx_vector_rep (i)) { } |
3928
|
208 |
4979
|
209 idx_vector (char c) : rep (new idx_vector_rep (c)) { } |
1560
|
210 |
4979
|
211 idx_vector (bool b) : rep (new idx_vector_rep (b)) { } |
2828
|
212 |
4938
|
213 template <class U> |
4979
|
214 idx_vector (const octave_int<U>& i) : rep (new idx_vector_rep (i)) { } |
4938
|
215 |
4979
|
216 idx_vector (const boolNDArray& bnda) : rep (new idx_vector_rep (bnda)) { } |
2828
|
217 |
4979
|
218 idx_vector (const idx_vector& a) : rep (a.rep) { rep->count++; } |
1551
|
219 |
2802
|
220 ~idx_vector (void) |
1560
|
221 { |
|
222 if (--rep->count <= 0) |
|
223 delete rep; |
|
224 } |
1
|
225 |
1560
|
226 idx_vector& operator = (const idx_vector& a) |
|
227 { |
|
228 if (this != &a) |
|
229 { |
|
230 if (--rep->count <= 0) |
|
231 delete rep; |
1
|
232 |
1560
|
233 rep = a.rep; |
|
234 rep->count++; |
|
235 } |
|
236 return *this; |
1551
|
237 } |
|
238 |
3145
|
239 operator bool () const { return rep->ok (); } |
1560
|
240 |
5275
|
241 octave_idx_type capacity (void) const { return rep->capacity (); } |
|
242 octave_idx_type length (octave_idx_type cl) const { return rep->length (cl); } |
191
|
243 |
5275
|
244 octave_idx_type elem (octave_idx_type n) const { return rep->elem (n); } |
|
245 octave_idx_type checkelem (octave_idx_type n) const { return rep->checkelem (n); } |
|
246 octave_idx_type operator () (octave_idx_type n) const { return rep->operator () (n); } |
1
|
247 |
5275
|
248 octave_idx_type max (void) const { return rep->max (); } |
|
249 octave_idx_type min (void) const { return rep->min (); } |
1551
|
250 |
2802
|
251 int one_zero_only (void) const { return rep->one_zero_only (); } |
5275
|
252 octave_idx_type zeros_count (void) const { return rep->zeros_count (); } |
|
253 octave_idx_type ones_count (void) const { return rep->ones_count (); } |
1
|
254 |
1560
|
255 int is_colon (void) const { return rep->is_colon (); } |
5275
|
256 int is_colon_equiv (octave_idx_type n, int sort_uniq = 0) const |
2356
|
257 { return rep->is_colon_equiv (n, sort_uniq); } |
1
|
258 |
3079
|
259 void sort (bool uniq = false) { rep->sort (uniq); } |
|
260 |
5275
|
261 octave_idx_type orig_rows (void) const { return rep->orig_rows (); } |
|
262 octave_idx_type orig_columns (void) const { return rep->orig_columns (); } |
208
|
263 |
4653
|
264 dim_vector orig_dimensions (void) const { return rep->orig_dimensions (); } |
4504
|
265 |
2663
|
266 int orig_empty (void) const |
4653
|
267 { return (! is_colon () && orig_dimensions().any_zero ()); } |
2663
|
268 |
4653
|
269 // Unsafe. Avoid at all cost. |
5275
|
270 void shorten (octave_idx_type n) { rep->shorten (n); } |
434
|
271 |
4653
|
272 // i/o |
1
|
273 |
5781
|
274 octave_idx_type freeze (octave_idx_type z_len, const char *tag, bool resize_ok = false) |
|
275 { return rep->freeze (z_len, tag, resize_ok); } |
1560
|
276 |
3504
|
277 std::ostream& print (std::ostream& os) const { return rep->print (os); } |
1560
|
278 |
3504
|
279 friend std::ostream& operator << (std::ostream& os, const idx_vector& a) |
1560
|
280 { return a.print (os); } |
|
281 |
5275
|
282 void maybe_convert_one_zero_to_idx (octave_idx_type z_len) |
2828
|
283 { rep->maybe_convert_one_zero_to_idx (z_len); } |
1
|
284 |
|
285 private: |
|
286 |
1560
|
287 idx_vector_rep *rep; |
1
|
288 |
1560
|
289 void init_state (void) { rep->init_state (); } |
1
|
290 }; |
|
291 |
|
292 #endif |
|
293 |
|
294 /* |
|
295 ;;; Local Variables: *** |
|
296 ;;; mode: C++ *** |
|
297 ;;; End: *** |
|
298 */ |