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