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