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 |
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 |
4979
|
207 idx_vector (int 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 |
2802
|
241 int capacity (void) const { return rep->capacity (); } |
|
242 int length (int cl) const { return rep->length (cl); } |
191
|
243 |
2802
|
244 int elem (int n) const { return rep->elem (n); } |
|
245 int checkelem (int n) const { return rep->checkelem (n); } |
|
246 int operator () (int n) const { return rep->operator () (n); } |
1
|
247 |
2802
|
248 int max (void) const { return rep->max (); } |
|
249 int min (void) const { return rep->min (); } |
1551
|
250 |
2802
|
251 int one_zero_only (void) const { return rep->one_zero_only (); } |
|
252 int zeros_count (void) const { return rep->zeros_count (); } |
|
253 int ones_count (void) const { return rep->ones_count (); } |
1
|
254 |
1560
|
255 int is_colon (void) const { return rep->is_colon (); } |
2356
|
256 int is_colon_equiv (int n, int sort_uniq = 0) const |
|
257 { return rep->is_colon_equiv (n, sort_uniq); } |
1
|
258 |
3079
|
259 void sort (bool uniq = false) { rep->sort (uniq); } |
|
260 |
1560
|
261 int orig_rows (void) const { return rep->orig_rows (); } |
|
262 int 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. |
1560
|
270 void shorten (int n) { rep->shorten (n); } |
434
|
271 |
4653
|
272 // i/o |
1
|
273 |
4461
|
274 int freeze (int z_len, const char *tag, bool resize_ok = false, |
|
275 bool warn_resize = false) |
|
276 { return rep->freeze (z_len, tag, resize_ok, warn_resize); } |
1560
|
277 |
3504
|
278 std::ostream& print (std::ostream& os) const { return rep->print (os); } |
1560
|
279 |
3504
|
280 friend std::ostream& operator << (std::ostream& os, const idx_vector& a) |
1560
|
281 { return a.print (os); } |
|
282 |
2828
|
283 void maybe_convert_one_zero_to_idx (int z_len) |
|
284 { rep->maybe_convert_one_zero_to_idx (z_len); } |
1
|
285 |
|
286 private: |
|
287 |
1560
|
288 idx_vector_rep *rep; |
1
|
289 |
1560
|
290 void init_state (void) { rep->init_state (); } |
1
|
291 }; |
|
292 |
|
293 #endif |
|
294 |
|
295 /* |
|
296 ;;; Local Variables: *** |
|
297 ;;; mode: C++ *** |
|
298 ;;; End: *** |
|
299 */ |