1
|
1 /* |
|
2 |
2847
|
3 Copyright (C) 1996, 1997 John W. Eaton |
1
|
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 2, or (at your option) any |
|
10 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, write to the Free |
1315
|
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
1
|
20 |
|
21 */ |
|
22 |
383
|
23 #if !defined (octave_idx_vector_h) |
|
24 #define octave_idx_vector_h 1 |
1
|
25 |
4192
|
26 #if defined (__GNUG__) && defined (USE_PRAGMA_INTERFACE_IMPLEMENTATION) |
1297
|
27 #pragma interface |
|
28 #endif |
|
29 |
3503
|
30 #include <iostream> |
|
31 |
4653
|
32 #include "dim-vector.h" |
4938
|
33 #include "oct-inttypes.h" |
|
34 #include "intNDArray.h" |
4504
|
35 |
1560
|
36 class ColumnVector; |
4650
|
37 class boolNDArray; |
|
38 class NDArray; |
164
|
39 class Range; |
|
40 |
453
|
41 class |
|
42 idx_vector |
1
|
43 { |
1560
|
44 private: |
|
45 |
1879
|
46 class |
1560
|
47 idx_vector_rep |
1879
|
48 { |
|
49 public: |
|
50 |
2802
|
51 idx_vector_rep (void) |
4653
|
52 : data (0), len (0), num_zeros (0), num_ones (0), max_val (0), |
|
53 min_val (0), count (1), frozen_at_z_len (0), frozen_len (0), |
|
54 colon (0), one_zero (0), initialized (0), frozen (0), |
|
55 colon_equiv_checked (0), colon_equiv (0), orig_dims () { } |
1560
|
56 |
1879
|
57 idx_vector_rep (const ColumnVector& v); |
1560
|
58 |
4650
|
59 idx_vector_rep (const NDArray& nda); |
1879
|
60 |
4938
|
61 template <class U> |
|
62 idx_vector_rep (const intNDArray<U>& inda) |
|
63 : data (0), len (inda.length ()), num_zeros (0), num_ones (0), |
|
64 max_val (0), min_val (0), count (1), frozen_at_z_len (0), |
|
65 frozen_len (0), colon (0), one_zero (0), initialized (0), |
|
66 frozen (0), colon_equiv_checked (0), colon_equiv (0), |
|
67 orig_dims (inda.dims ()) |
|
68 { |
|
69 if (len == 0) |
|
70 { |
|
71 initialized = 1; |
|
72 return; |
|
73 } |
|
74 else |
|
75 { |
|
76 data = new int [len]; |
|
77 |
|
78 bool conversion_error = false; |
|
79 |
|
80 for (int i = 0; i < len; i++) |
|
81 data[i] = tree_to_mat_idx (inda.elem (i), conversion_error); |
|
82 |
|
83 if (conversion_error) |
|
84 return; |
|
85 } |
|
86 |
|
87 init_state (); |
|
88 } |
|
89 |
1879
|
90 idx_vector_rep (const Range& r); |
1560
|
91 |
2386
|
92 idx_vector_rep (double d); |
|
93 |
3928
|
94 idx_vector_rep (int i); |
|
95 |
1879
|
96 idx_vector_rep (char c); |
1560
|
97 |
2828
|
98 idx_vector_rep (bool b); |
|
99 |
4938
|
100 template <class U> |
|
101 idx_vector_rep (const octave_int<U>& i) |
|
102 : data (0), len (1), num_zeros (0), num_ones (0), |
|
103 max_val (0), min_val (0), count (1), frozen_at_z_len (0), |
|
104 frozen_len (0), colon (0), one_zero (0), initialized (0), |
|
105 frozen (0), colon_equiv_checked (0), colon_equiv (0), |
|
106 orig_dims (1, 1) |
|
107 { |
|
108 data = new int [len]; |
|
109 |
|
110 data[0] = tree_to_mat_idx (i); |
|
111 |
|
112 init_state (); |
|
113 } |
|
114 |
4650
|
115 idx_vector_rep (const boolNDArray& bnda); |
2828
|
116 |
1879
|
117 idx_vector_rep (const idx_vector_rep& a); |
1560
|
118 |
2802
|
119 ~idx_vector_rep (void) { delete [] data; } |
1560
|
120 |
1879
|
121 idx_vector_rep& operator = (const idx_vector_rep& a); |
1560
|
122 |
1879
|
123 int ok (void) { return initialized; } |
1560
|
124 |
1879
|
125 int capacity (void) const { return len; } |
|
126 int length (int colon_len) const { return colon ? colon_len : len; } |
1560
|
127 |
1879
|
128 int elem (int n) const { return colon ? n : data[n]; } |
1560
|
129 |
1879
|
130 int checkelem (int n) const; |
|
131 int operator () (int n) const { return checkelem (n); } |
1560
|
132 |
1879
|
133 int max (void) const { return max_val; } |
|
134 int min (void) const { return min_val; } |
|
135 |
|
136 int one_zero_only (void) const { return one_zero; } |
|
137 int zeros_count (void) const { return num_zeros; } |
|
138 int ones_count (void) const { return num_ones; } |
|
139 |
|
140 int is_colon (void) const { return colon; } |
2356
|
141 int is_colon_equiv (int n, int sort_uniq); |
1560
|
142 |
3079
|
143 void sort (bool uniq); |
|
144 |
4653
|
145 int orig_rows (void) const { return orig_dims(0); } |
|
146 int orig_columns (void) const { return orig_dims(1); } |
1560
|
147 |
4653
|
148 dim_vector orig_dimensions (void) const { return orig_dims; } |
4504
|
149 |
1879
|
150 // other stuff |
1560
|
151 |
1879
|
152 void shorten (int n); // Unsafe. Avoid at all cost. |
1560
|
153 |
4461
|
154 int freeze (int z_len, const char *tag, bool resize_ok, bool warn_resize); |
1560
|
155 |
1879
|
156 // i/o |
1560
|
157 |
3504
|
158 std::ostream& print (std::ostream& os) const; |
1560
|
159 |
1879
|
160 int *data; |
|
161 int len; |
|
162 int num_zeros; |
|
163 int num_ones; |
|
164 int max_val; |
|
165 int min_val; |
4504
|
166 |
1879
|
167 int count; |
|
168 int frozen_at_z_len; |
|
169 int 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 |
2828
|
182 void maybe_convert_one_zero_to_idx (int z_len); |
4938
|
183 |
|
184 int tree_to_mat_idx (double x, bool& conversion_error); |
|
185 |
|
186 int tree_to_mat_idx (int i) { return i - 1; } |
|
187 |
|
188 template <class U> int tree_to_mat_idx (const octave_int<U>& i) |
|
189 { return i.value () - 1; } |
1879
|
190 }; |
1560
|
191 |
1
|
192 public: |
1551
|
193 |
2802
|
194 idx_vector (void) |
1551
|
195 { |
1560
|
196 rep = new idx_vector_rep (); |
|
197 rep->count = 1; |
|
198 } |
|
199 |
|
200 idx_vector (const ColumnVector& v) |
|
201 { |
|
202 rep = new idx_vector_rep (v); |
|
203 rep->count = 1; |
|
204 } |
|
205 |
4650
|
206 idx_vector (const NDArray& nda) |
1560
|
207 { |
4650
|
208 rep = new idx_vector_rep (nda); |
1560
|
209 rep->count = 1; |
|
210 } |
|
211 |
4938
|
212 template <class U> |
|
213 idx_vector (const intNDArray<U>& inda) |
|
214 { |
|
215 rep = new idx_vector_rep (inda); |
|
216 rep->count = 1; |
|
217 } |
|
218 |
1560
|
219 idx_vector (const Range& r) |
|
220 { |
|
221 rep = new idx_vector_rep (r); |
|
222 rep->count = 1; |
|
223 } |
|
224 |
2386
|
225 idx_vector (double d) |
|
226 { |
|
227 rep = new idx_vector_rep (d); |
|
228 rep->count = 1; |
|
229 } |
|
230 |
3928
|
231 idx_vector (int i) |
|
232 { |
|
233 rep = new idx_vector_rep (i); |
|
234 rep->count = 1; |
|
235 } |
|
236 |
1560
|
237 idx_vector (char c) |
|
238 { |
|
239 rep = new idx_vector_rep (c); |
|
240 rep->count = 1; |
|
241 } |
|
242 |
2828
|
243 idx_vector (bool b) |
|
244 { |
|
245 rep = new idx_vector_rep (b); |
|
246 rep->count = 1; |
|
247 } |
|
248 |
4938
|
249 template <class U> |
|
250 idx_vector (const octave_int<U>& i) |
|
251 { |
|
252 rep = new idx_vector_rep (i); |
|
253 rep->count = 1; |
|
254 } |
|
255 |
4650
|
256 idx_vector (const boolNDArray& bnda) |
2828
|
257 { |
4650
|
258 rep = new idx_vector_rep (bnda); |
2828
|
259 rep->count = 1; |
|
260 } |
|
261 |
1560
|
262 idx_vector (const idx_vector& a) |
|
263 { |
|
264 rep = a.rep; |
|
265 rep->count++; |
1551
|
266 } |
|
267 |
2802
|
268 ~idx_vector (void) |
1560
|
269 { |
|
270 if (--rep->count <= 0) |
|
271 delete rep; |
|
272 } |
1
|
273 |
1560
|
274 idx_vector& operator = (const idx_vector& a) |
|
275 { |
|
276 if (this != &a) |
|
277 { |
|
278 if (--rep->count <= 0) |
|
279 delete rep; |
1
|
280 |
1560
|
281 rep = a.rep; |
|
282 rep->count++; |
|
283 } |
|
284 return *this; |
1551
|
285 } |
|
286 |
3145
|
287 operator bool () const { return rep->ok (); } |
1560
|
288 |
2802
|
289 int capacity (void) const { return rep->capacity (); } |
|
290 int length (int cl) const { return rep->length (cl); } |
191
|
291 |
2802
|
292 int elem (int n) const { return rep->elem (n); } |
|
293 int checkelem (int n) const { return rep->checkelem (n); } |
|
294 int operator () (int n) const { return rep->operator () (n); } |
1
|
295 |
2802
|
296 int max (void) const { return rep->max (); } |
|
297 int min (void) const { return rep->min (); } |
1551
|
298 |
2802
|
299 int one_zero_only (void) const { return rep->one_zero_only (); } |
|
300 int zeros_count (void) const { return rep->zeros_count (); } |
|
301 int ones_count (void) const { return rep->ones_count (); } |
1
|
302 |
1560
|
303 int is_colon (void) const { return rep->is_colon (); } |
2356
|
304 int is_colon_equiv (int n, int sort_uniq = 0) const |
|
305 { return rep->is_colon_equiv (n, sort_uniq); } |
1
|
306 |
3079
|
307 void sort (bool uniq = false) { rep->sort (uniq); } |
|
308 |
1560
|
309 int orig_rows (void) const { return rep->orig_rows (); } |
|
310 int orig_columns (void) const { return rep->orig_columns (); } |
208
|
311 |
4653
|
312 dim_vector orig_dimensions (void) const { return rep->orig_dimensions (); } |
4504
|
313 |
2663
|
314 int orig_empty (void) const |
4653
|
315 { return (! is_colon () && orig_dimensions().any_zero ()); } |
2663
|
316 |
4653
|
317 // Unsafe. Avoid at all cost. |
1560
|
318 void shorten (int n) { rep->shorten (n); } |
434
|
319 |
4653
|
320 // i/o |
1
|
321 |
4461
|
322 int freeze (int z_len, const char *tag, bool resize_ok = false, |
|
323 bool warn_resize = false) |
|
324 { return rep->freeze (z_len, tag, resize_ok, warn_resize); } |
1560
|
325 |
3504
|
326 std::ostream& print (std::ostream& os) const { return rep->print (os); } |
1560
|
327 |
3504
|
328 friend std::ostream& operator << (std::ostream& os, const idx_vector& a) |
1560
|
329 { return a.print (os); } |
|
330 |
2828
|
331 void maybe_convert_one_zero_to_idx (int z_len) |
|
332 { rep->maybe_convert_one_zero_to_idx (z_len); } |
1
|
333 |
|
334 private: |
|
335 |
1560
|
336 idx_vector_rep *rep; |
1
|
337 |
1560
|
338 void init_state (void) { rep->init_state (); } |
1
|
339 }; |
|
340 |
|
341 #endif |
|
342 |
|
343 /* |
|
344 ;;; Local Variables: *** |
|
345 ;;; mode: C++ *** |
|
346 ;;; End: *** |
|
347 */ |