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 |
5307
|
19 Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
|
20 02110-1301, USA. |
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 |
|
39 idx_vector |
1
|
40 { |
1560
|
41 private: |
|
42 |
1879
|
43 class |
1560
|
44 idx_vector_rep |
1879
|
45 { |
|
46 public: |
|
47 |
2802
|
48 idx_vector_rep (void) |
4653
|
49 : data (0), len (0), num_zeros (0), num_ones (0), max_val (0), |
|
50 min_val (0), count (1), frozen_at_z_len (0), frozen_len (0), |
|
51 colon (0), one_zero (0), initialized (0), frozen (0), |
|
52 colon_equiv_checked (0), colon_equiv (0), orig_dims () { } |
1560
|
53 |
1879
|
54 idx_vector_rep (const ColumnVector& v); |
1560
|
55 |
4650
|
56 idx_vector_rep (const NDArray& nda); |
1879
|
57 |
4938
|
58 template <class U> |
|
59 idx_vector_rep (const intNDArray<U>& inda) |
|
60 : data (0), len (inda.length ()), num_zeros (0), num_ones (0), |
|
61 max_val (0), min_val (0), count (1), frozen_at_z_len (0), |
|
62 frozen_len (0), colon (0), one_zero (0), initialized (0), |
|
63 frozen (0), colon_equiv_checked (0), colon_equiv (0), |
|
64 orig_dims (inda.dims ()) |
|
65 { |
|
66 if (len == 0) |
|
67 { |
|
68 initialized = 1; |
|
69 return; |
|
70 } |
|
71 else |
|
72 { |
5275
|
73 data = new octave_idx_type [len]; |
4938
|
74 |
|
75 bool conversion_error = false; |
|
76 |
5275
|
77 for (octave_idx_type i = 0; i < len; i++) |
4938
|
78 data[i] = tree_to_mat_idx (inda.elem (i), conversion_error); |
|
79 |
|
80 if (conversion_error) |
|
81 return; |
|
82 } |
|
83 |
|
84 init_state (); |
|
85 } |
|
86 |
1879
|
87 idx_vector_rep (const Range& r); |
1560
|
88 |
2386
|
89 idx_vector_rep (double d); |
|
90 |
5275
|
91 idx_vector_rep (octave_idx_type i); |
3928
|
92 |
1879
|
93 idx_vector_rep (char c); |
1560
|
94 |
2828
|
95 idx_vector_rep (bool b); |
|
96 |
4938
|
97 template <class U> |
|
98 idx_vector_rep (const octave_int<U>& i) |
|
99 : data (0), len (1), num_zeros (0), num_ones (0), |
|
100 max_val (0), min_val (0), count (1), frozen_at_z_len (0), |
|
101 frozen_len (0), colon (0), one_zero (0), initialized (0), |
|
102 frozen (0), colon_equiv_checked (0), colon_equiv (0), |
|
103 orig_dims (1, 1) |
|
104 { |
5275
|
105 data = new octave_idx_type [len]; |
4938
|
106 |
|
107 data[0] = tree_to_mat_idx (i); |
|
108 |
|
109 init_state (); |
|
110 } |
|
111 |
4650
|
112 idx_vector_rep (const boolNDArray& bnda); |
2828
|
113 |
1879
|
114 idx_vector_rep (const idx_vector_rep& a); |
1560
|
115 |
2802
|
116 ~idx_vector_rep (void) { delete [] data; } |
1560
|
117 |
1879
|
118 idx_vector_rep& operator = (const idx_vector_rep& a); |
1560
|
119 |
1879
|
120 int ok (void) { return initialized; } |
1560
|
121 |
5275
|
122 octave_idx_type capacity (void) const { return len; } |
|
123 octave_idx_type length (octave_idx_type colon_len) const { return colon ? colon_len : len; } |
1560
|
124 |
5275
|
125 octave_idx_type elem (octave_idx_type n) const { return colon ? n : data[n]; } |
1560
|
126 |
5275
|
127 octave_idx_type checkelem (octave_idx_type n) const; |
|
128 octave_idx_type operator () (octave_idx_type n) const { return checkelem (n); } |
1560
|
129 |
5275
|
130 octave_idx_type max (void) const { return max_val; } |
|
131 octave_idx_type min (void) const { return min_val; } |
1879
|
132 |
|
133 int one_zero_only (void) const { return one_zero; } |
5275
|
134 octave_idx_type zeros_count (void) const { return num_zeros; } |
|
135 octave_idx_type ones_count (void) const { return num_ones; } |
1879
|
136 |
|
137 int is_colon (void) const { return colon; } |
5275
|
138 int is_colon_equiv (octave_idx_type n, int sort_uniq); |
1560
|
139 |
3079
|
140 void sort (bool uniq); |
|
141 |
5275
|
142 octave_idx_type orig_rows (void) const { return orig_dims(0); } |
|
143 octave_idx_type orig_columns (void) const { return orig_dims(1); } |
1560
|
144 |
4653
|
145 dim_vector orig_dimensions (void) const { return orig_dims; } |
4504
|
146 |
1879
|
147 // other stuff |
1560
|
148 |
5275
|
149 void shorten (octave_idx_type n); // Unsafe. Avoid at all cost. |
1560
|
150 |
5781
|
151 octave_idx_type freeze (octave_idx_type z_len, const char *tag, bool resize_ok); |
1560
|
152 |
1879
|
153 // i/o |
1560
|
154 |
3504
|
155 std::ostream& print (std::ostream& os) const; |
1560
|
156 |
5275
|
157 octave_idx_type *data; |
|
158 octave_idx_type len; |
|
159 octave_idx_type num_zeros; |
|
160 octave_idx_type num_ones; |
|
161 octave_idx_type max_val; |
|
162 octave_idx_type min_val; |
4504
|
163 |
1879
|
164 int count; |
5275
|
165 |
|
166 octave_idx_type frozen_at_z_len; |
|
167 octave_idx_type frozen_len; |
4653
|
168 |
1879
|
169 unsigned int colon : 1; |
|
170 unsigned int one_zero : 1; |
|
171 unsigned int initialized : 1; |
|
172 unsigned int frozen : 1; |
|
173 unsigned int colon_equiv_checked : 1; |
|
174 unsigned int colon_equiv : 1; |
1560
|
175 |
4653
|
176 dim_vector orig_dims; |
|
177 |
1879
|
178 void init_state (void); |
1560
|
179 |
5275
|
180 void maybe_convert_one_zero_to_idx (octave_idx_type z_len); |
4938
|
181 |
5275
|
182 octave_idx_type tree_to_mat_idx (double x, bool& conversion_error); |
4938
|
183 |
5275
|
184 octave_idx_type tree_to_mat_idx (octave_idx_type i) { return i - 1; } |
4938
|
185 |
5275
|
186 template <class U> octave_idx_type tree_to_mat_idx (const octave_int<U>& i) |
4938
|
187 { return i.value () - 1; } |
1879
|
188 }; |
1560
|
189 |
1
|
190 public: |
1551
|
191 |
4979
|
192 idx_vector (void) : rep (new idx_vector_rep ()) { } |
1560
|
193 |
4979
|
194 idx_vector (const ColumnVector& v) : rep (new idx_vector_rep (v)) { } |
1560
|
195 |
4979
|
196 idx_vector (const NDArray& nda) : rep (new idx_vector_rep (nda)) { } |
1560
|
197 |
4938
|
198 template <class U> |
4979
|
199 idx_vector (const intNDArray<U>& inda) : rep (new idx_vector_rep (inda)) { } |
4938
|
200 |
4979
|
201 idx_vector (const Range& r) : rep (new idx_vector_rep (r)) { } |
1560
|
202 |
4979
|
203 idx_vector (double d) : rep (new idx_vector_rep (d)) { } |
2386
|
204 |
5275
|
205 idx_vector (octave_idx_type i) : rep (new idx_vector_rep (i)) { } |
3928
|
206 |
4979
|
207 idx_vector (char c) : rep (new idx_vector_rep (c)) { } |
1560
|
208 |
4979
|
209 idx_vector (bool b) : rep (new idx_vector_rep (b)) { } |
2828
|
210 |
4938
|
211 template <class U> |
4979
|
212 idx_vector (const octave_int<U>& i) : rep (new idx_vector_rep (i)) { } |
4938
|
213 |
4979
|
214 idx_vector (const boolNDArray& bnda) : rep (new idx_vector_rep (bnda)) { } |
2828
|
215 |
4979
|
216 idx_vector (const idx_vector& a) : rep (a.rep) { rep->count++; } |
1551
|
217 |
2802
|
218 ~idx_vector (void) |
1560
|
219 { |
|
220 if (--rep->count <= 0) |
|
221 delete rep; |
|
222 } |
1
|
223 |
1560
|
224 idx_vector& operator = (const idx_vector& a) |
|
225 { |
|
226 if (this != &a) |
|
227 { |
|
228 if (--rep->count <= 0) |
|
229 delete rep; |
1
|
230 |
1560
|
231 rep = a.rep; |
|
232 rep->count++; |
|
233 } |
|
234 return *this; |
1551
|
235 } |
|
236 |
3145
|
237 operator bool () const { return rep->ok (); } |
1560
|
238 |
5275
|
239 octave_idx_type capacity (void) const { return rep->capacity (); } |
|
240 octave_idx_type length (octave_idx_type cl) const { return rep->length (cl); } |
191
|
241 |
5275
|
242 octave_idx_type elem (octave_idx_type n) const { return rep->elem (n); } |
|
243 octave_idx_type checkelem (octave_idx_type n) const { return rep->checkelem (n); } |
|
244 octave_idx_type operator () (octave_idx_type n) const { return rep->operator () (n); } |
1
|
245 |
5275
|
246 octave_idx_type max (void) const { return rep->max (); } |
|
247 octave_idx_type min (void) const { return rep->min (); } |
1551
|
248 |
2802
|
249 int one_zero_only (void) const { return rep->one_zero_only (); } |
5275
|
250 octave_idx_type zeros_count (void) const { return rep->zeros_count (); } |
|
251 octave_idx_type ones_count (void) const { return rep->ones_count (); } |
1
|
252 |
1560
|
253 int is_colon (void) const { return rep->is_colon (); } |
5275
|
254 int is_colon_equiv (octave_idx_type n, int sort_uniq = 0) const |
2356
|
255 { return rep->is_colon_equiv (n, sort_uniq); } |
1
|
256 |
3079
|
257 void sort (bool uniq = false) { rep->sort (uniq); } |
|
258 |
5275
|
259 octave_idx_type orig_rows (void) const { return rep->orig_rows (); } |
|
260 octave_idx_type orig_columns (void) const { return rep->orig_columns (); } |
208
|
261 |
4653
|
262 dim_vector orig_dimensions (void) const { return rep->orig_dimensions (); } |
4504
|
263 |
2663
|
264 int orig_empty (void) const |
4653
|
265 { return (! is_colon () && orig_dimensions().any_zero ()); } |
2663
|
266 |
4653
|
267 // Unsafe. Avoid at all cost. |
5275
|
268 void shorten (octave_idx_type n) { rep->shorten (n); } |
434
|
269 |
4653
|
270 // i/o |
1
|
271 |
5781
|
272 octave_idx_type freeze (octave_idx_type z_len, const char *tag, bool resize_ok = false) |
|
273 { return rep->freeze (z_len, tag, resize_ok); } |
1560
|
274 |
3504
|
275 std::ostream& print (std::ostream& os) const { return rep->print (os); } |
1560
|
276 |
3504
|
277 friend std::ostream& operator << (std::ostream& os, const idx_vector& a) |
1560
|
278 { return a.print (os); } |
|
279 |
5275
|
280 void maybe_convert_one_zero_to_idx (octave_idx_type z_len) |
2828
|
281 { rep->maybe_convert_one_zero_to_idx (z_len); } |
1
|
282 |
|
283 private: |
|
284 |
1560
|
285 idx_vector_rep *rep; |
1
|
286 |
1560
|
287 void init_state (void) { rep->init_state (); } |
1
|
288 }; |
|
289 |
|
290 #endif |
|
291 |
|
292 /* |
|
293 ;;; Local Variables: *** |
|
294 ;;; mode: C++ *** |
|
295 ;;; End: *** |
|
296 */ |