519
|
1 // idx-vector.cc -*- C++ -*- |
1
|
2 /* |
|
3 |
1009
|
4 Copyright (C) 1992, 1993, 1994, 1995 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 |
|
10 Free Software Foundation; either version 2, or (at your option) any |
|
11 later version. |
|
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 |
|
19 along with Octave; see the file COPYING. If not, write to the Free |
1315
|
20 Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
1
|
21 |
|
22 */ |
|
23 |
1297
|
24 #if defined (__GNUG__) |
|
25 #pragma implementation |
|
26 #endif |
|
27 |
240
|
28 #ifdef HAVE_CONFIG_H |
1192
|
29 #include <config.h> |
1
|
30 #endif |
|
31 |
1343
|
32 #include <cstdlib> |
|
33 |
164
|
34 #include <iostream.h> |
|
35 |
1352
|
36 #include "Range.h" |
1560
|
37 #include "dColVector.h" |
453
|
38 #include "dMatrix.h" |
1352
|
39 |
1
|
40 #include "idx-vector.h" |
1560
|
41 #include "lo-error.h" |
1
|
42 |
1560
|
43 #define IDX_VEC_REP idx_vector::idx_vector_rep |
|
44 |
|
45 IDX_VEC_REP::idx_vector_rep (const IDX_VEC_REP& a) |
1
|
46 { |
1135
|
47 data = 0; |
191
|
48 initialized = a.initialized; |
1560
|
49 frozen = a.frozen; |
|
50 colon_equiv_checked = a.colon_equiv_checked; |
|
51 colon_equiv = a.colon_equiv; |
|
52 |
|
53 colon = a.colon; |
|
54 |
|
55 orig_nr = a.orig_nr; |
|
56 orig_nc = a.orig_nc; |
191
|
57 |
1
|
58 len = a.len; |
|
59 if (len > 0) |
|
60 { |
|
61 data = new int [len]; |
|
62 for (int i = 0; i < len; i++) |
|
63 data[i] = a.data[i]; |
|
64 |
|
65 num_zeros = a.num_zeros; |
|
66 num_ones = a.num_ones; |
|
67 one_zero = a.one_zero; |
|
68 |
|
69 max_val = a.max_val; |
|
70 min_val = a.min_val; |
|
71 } |
|
72 } |
|
73 |
|
74 static inline int |
|
75 tree_to_mat_idx (double x) |
|
76 { |
|
77 if (x > 0) |
|
78 return ((int) (x + 0.5) - 1); |
|
79 else |
|
80 return ((int) (x - 0.5) - 1); |
|
81 } |
|
82 |
1560
|
83 IDX_VEC_REP::idx_vector_rep (const ColumnVector& v) |
1
|
84 { |
1135
|
85 data = 0; |
191
|
86 initialized = 0; |
1560
|
87 frozen = 0; |
|
88 colon_equiv_checked = 0; |
|
89 colon_equiv = 0; |
|
90 colon = 0; |
1
|
91 |
1650
|
92 len = v.length (); |
1560
|
93 |
|
94 orig_nr = len; |
|
95 orig_nc = 1; |
|
96 |
|
97 if (len == 0) |
1
|
98 { |
|
99 num_zeros = 0; |
|
100 num_ones = 0; |
|
101 one_zero = 0; |
1560
|
102 max_val = 0; |
|
103 min_val = 0; |
191
|
104 initialized = 1; |
1
|
105 return; |
|
106 } |
1560
|
107 else |
1
|
108 { |
|
109 data = new int [len]; |
|
110 for (int i = 0; i < len; i++) |
1560
|
111 data[i] = tree_to_mat_idx (v.elem (i)); |
1
|
112 } |
1560
|
113 |
|
114 init_state (); |
|
115 } |
|
116 |
|
117 IDX_VEC_REP::idx_vector_rep (const Matrix& m) |
|
118 { |
|
119 data = 0; |
|
120 initialized = 0; |
|
121 frozen = 0; |
|
122 colon_equiv_checked = 0; |
|
123 colon_equiv = 0; |
|
124 colon = 0; |
|
125 |
|
126 orig_nr = m.rows (); |
|
127 orig_nc = m.columns (); |
|
128 |
|
129 len = orig_nr * orig_nc; |
|
130 |
|
131 if (len == 0) |
1
|
132 { |
1560
|
133 num_zeros = 0; |
|
134 num_ones = 0; |
|
135 one_zero = 0; |
|
136 max_val = 0; |
|
137 min_val = 0; |
|
138 initialized = 1; |
|
139 return; |
1
|
140 } |
|
141 else |
|
142 { |
1560
|
143 int k = 0; |
|
144 data = new int [len]; |
|
145 for (int j = 0; j < orig_nc; j++) |
|
146 for (int i = 0; i < orig_nr; i++) |
|
147 data[k++] = tree_to_mat_idx (m.elem (i, j)); |
1
|
148 } |
|
149 |
1560
|
150 init_state (); |
1
|
151 } |
|
152 |
1560
|
153 IDX_VEC_REP::idx_vector_rep (const Range& r) |
1
|
154 { |
1135
|
155 data = 0; |
191
|
156 initialized = 0; |
1560
|
157 frozen = 0; |
|
158 colon_equiv_checked = 0; |
|
159 colon_equiv = 0; |
|
160 colon = 0; |
191
|
161 |
1
|
162 len = r.nelem (); |
|
163 |
1560
|
164 orig_nr = 1; |
|
165 orig_nc = len; |
|
166 |
191
|
167 if (len < 0) |
|
168 { |
1560
|
169 (*current_liboctave_error_handler) ("invalid range used as index"); |
191
|
170 return; |
|
171 } |
|
172 else if (len == 0) |
|
173 { |
|
174 num_zeros = 0; |
|
175 num_ones = 0; |
|
176 one_zero = 0; |
1560
|
177 max_val = 0; |
|
178 min_val = 0; |
191
|
179 initialized = 1; |
|
180 return; |
|
181 } |
1
|
182 |
|
183 double b = r.base (); |
|
184 double step = r.inc (); |
|
185 |
|
186 data = new int [len]; |
|
187 |
|
188 for (int i = 0; i < len; i++) |
|
189 { |
|
190 double val = b + i * step; |
|
191 data[i] = tree_to_mat_idx (val); |
|
192 } |
|
193 |
|
194 init_state (); |
|
195 } |
|
196 |
1560
|
197 IDX_VEC_REP::idx_vector_rep (char c) |
|
198 { |
|
199 assert (c == ':'); |
|
200 |
|
201 colon = 1; |
|
202 len = 0; |
|
203 num_zeros = 0; |
|
204 num_ones = 0; |
|
205 one_zero = 0; |
|
206 initialized = 0; |
|
207 frozen = 0; |
|
208 colon_equiv_checked = 0; |
|
209 colon_equiv = 0; |
|
210 data = 0; |
|
211 |
|
212 init_state (); |
|
213 } |
|
214 |
|
215 IDX_VEC_REP& |
|
216 IDX_VEC_REP::operator = (const IDX_VEC_REP& a) |
1
|
217 { |
|
218 if (this != &a) |
|
219 { |
191
|
220 initialized = a.initialized; |
1560
|
221 frozen = a.frozen; |
|
222 colon_equiv_checked = a.colon_equiv_checked; |
|
223 colon_equiv = a.colon_equiv; |
|
224 |
|
225 colon = a.colon; |
|
226 |
|
227 orig_nr = a.orig_nr; |
|
228 orig_nc = a.orig_nc; |
191
|
229 |
1
|
230 delete [] data; |
|
231 len = a.len; |
|
232 data = new int [len]; |
|
233 for (int i = 0; i < len; i++) |
|
234 data[i] = a.data[i]; |
|
235 |
|
236 num_zeros = a.num_zeros; |
|
237 num_ones = a.num_ones; |
|
238 one_zero = a.one_zero; |
|
239 |
|
240 max_val = a.max_val; |
|
241 min_val = a.min_val; |
|
242 } |
|
243 return *this; |
|
244 } |
|
245 |
|
246 void |
1560
|
247 IDX_VEC_REP::init_state (void) |
1
|
248 { |
|
249 num_zeros = 0; |
|
250 num_ones = 0; |
|
251 |
1560
|
252 if (colon) |
1
|
253 { |
1560
|
254 one_zero = 0; |
|
255 min_val = max_val = 0; |
1
|
256 } |
|
257 else |
|
258 { |
1560
|
259 one_zero = 1; |
1
|
260 |
|
261 min_val = max_val = data[0]; |
|
262 |
1321
|
263 int i = 0; |
1
|
264 do |
|
265 { |
1560
|
266 if (data[i] == -1) |
|
267 num_zeros++; |
|
268 else if (data[i] == 0) |
|
269 num_ones++; |
|
270 |
|
271 if (one_zero && data[i] != -1 && data[i] != 0) |
|
272 one_zero = 0; |
|
273 |
1
|
274 if (data[i] > max_val) |
|
275 max_val = data[i]; |
|
276 |
|
277 if (data[i] < min_val) |
|
278 min_val = data[i]; |
|
279 } |
|
280 while (++i < len); |
|
281 } |
1560
|
282 |
|
283 initialized = 1; |
|
284 } |
|
285 |
|
286 void |
|
287 IDX_VEC_REP::maybe_convert_one_zero_to_idx (int z_len, int prefer_zero_one) |
|
288 { |
|
289 if (one_zero && z_len == len |
|
290 && (num_ones != len || prefer_zero_one)) |
|
291 { |
|
292 if (num_ones == 0) |
|
293 { |
|
294 len = 0; |
|
295 max_val = 0; |
|
296 min_val = 0; |
|
297 delete [] data; |
|
298 data = 0; |
|
299 } |
|
300 else |
|
301 { |
|
302 assert (num_ones + num_zeros == len); |
|
303 |
|
304 int *new_data = new int [num_ones]; |
1650
|
305 int k = 0; |
1560
|
306 for (int i = 0; i < len; i++) |
|
307 if (data[i] == 0) |
1650
|
308 new_data[k++] = i; |
1560
|
309 |
|
310 delete [] data; |
|
311 len = num_ones; |
|
312 data = new_data; |
|
313 |
|
314 min_val = max_val = data[0]; |
|
315 |
|
316 int i = 0; |
|
317 do |
|
318 { |
|
319 if (data[i] > max_val) |
|
320 max_val = data[i]; |
|
321 |
|
322 if (data[i] < min_val) |
|
323 min_val = data[i]; |
|
324 } |
|
325 while (++i < len); |
|
326 } |
|
327 } |
1
|
328 } |
|
329 |
227
|
330 int |
1560
|
331 IDX_VEC_REP::checkelem (int n) const |
227
|
332 { |
|
333 if (n < 0 || n >= len) |
|
334 { |
1560
|
335 (*current_liboctave_error_handler) ("idx-vector: index out of range"); |
227
|
336 return 0; |
|
337 } |
|
338 |
|
339 return elem (n); |
|
340 } |
|
341 |
1552
|
342 static inline int |
1650
|
343 intcmp (int *ii, int *jj) |
1552
|
344 { |
1650
|
345 return (*ii - *jj); |
1552
|
346 } |
|
347 |
1560
|
348 static inline void |
1650
|
349 sort_data (int *d, int l) |
1560
|
350 { |
1650
|
351 qsort ((void *) d, l, sizeof (int), |
1560
|
352 (int (*)(const void*, const void*)) intcmp); |
|
353 } |
|
354 |
|
355 static inline int |
1650
|
356 make_uniq (int *d, int l) |
1560
|
357 { |
|
358 int k = 0; |
1759
|
359 for (int ii = 1; ii < l; ii++) |
1560
|
360 { |
1650
|
361 if (d[ii] != d[k]) |
1560
|
362 { |
|
363 k++; |
1650
|
364 d[k] = d[ii]; |
1560
|
365 } |
|
366 } |
|
367 return k+1; |
|
368 } |
|
369 |
|
370 static inline int * |
1650
|
371 copy_data (const int *d, int l) |
209
|
372 { |
1650
|
373 int *new_data = new int [l]; |
1560
|
374 |
1650
|
375 for (int ii = 0; ii < l; ii++) |
|
376 new_data[ii] = d[ii]; |
1560
|
377 |
|
378 return new_data; |
|
379 } |
|
380 |
|
381 int |
|
382 IDX_VEC_REP::is_colon_equiv (int n, int sort) |
|
383 { |
|
384 if (! colon_equiv_checked) |
|
385 { |
|
386 if (colon) |
|
387 { |
|
388 colon_equiv = 1; |
|
389 } |
1595
|
390 else if (len > 0 && len > 1 && ! one_zero) |
1560
|
391 { |
|
392 int *tmp_data = copy_data (data, len); |
|
393 |
|
394 if (sort) |
|
395 sort_data (tmp_data, len); |
|
396 |
|
397 int tmp_len = make_uniq (tmp_data, len); |
|
398 |
|
399 colon_equiv = ((tmp_len == 0 && n == 0) |
|
400 || (tmp_len == n |
|
401 && tmp_data[0] == 0 |
|
402 && tmp_data[tmp_len-1] == tmp_len - 1)); |
|
403 |
|
404 delete [] tmp_data; |
|
405 } |
|
406 else |
|
407 colon_equiv = 0; |
|
408 |
|
409 colon_equiv_checked = 1; |
|
410 } |
|
411 |
|
412 return colon_equiv; |
209
|
413 } |
|
414 |
417
|
415 void |
1560
|
416 IDX_VEC_REP::shorten (int n) |
434
|
417 { |
|
418 if (n > 0 && n <= len) |
|
419 len = n; |
|
420 else |
1560
|
421 (*current_liboctave_error_handler) |
|
422 ("idx_vector::shorten: internal error!"); |
434
|
423 } |
|
424 |
1
|
425 ostream& |
1560
|
426 IDX_VEC_REP::print (ostream& os) const |
|
427 { |
1650
|
428 for (int ii = 0; ii < len; ii++) |
|
429 os << data[ii] << "\n"; |
1560
|
430 return os; |
|
431 } |
|
432 |
|
433 int |
|
434 IDX_VEC_REP::freeze (int z_len, const char *tag, |
|
435 int prefer_zero_one, int resize_ok) |
1
|
436 { |
1560
|
437 if (frozen) |
|
438 { |
|
439 assert (frozen_at_z_len == z_len); |
|
440 return frozen_len; |
|
441 } |
|
442 |
|
443 frozen_len = -1; |
|
444 |
|
445 if (colon) |
|
446 frozen_len = z_len; |
|
447 else |
|
448 { |
|
449 if (len == 0) |
|
450 frozen_len = 0; |
|
451 else |
|
452 { |
|
453 maybe_convert_one_zero_to_idx (z_len, prefer_zero_one); |
|
454 |
1650
|
455 max_val = max (); |
|
456 min_val = min (); |
1560
|
457 |
|
458 if (min_val < 0) |
|
459 { |
|
460 if (tag) |
|
461 (*current_liboctave_error_handler) |
|
462 ("invalid %s index = %d", tag, min_val+1); |
|
463 else |
|
464 (*current_liboctave_error_handler) |
|
465 ("invalid index = %d", min_val+1); |
|
466 |
|
467 initialized = 0; |
|
468 } |
|
469 else if (! resize_ok && max_val >= z_len) |
|
470 { |
|
471 if (tag) |
|
472 (*current_liboctave_error_handler) |
|
473 ("invalid %s index = %d", tag, max_val+1); |
|
474 else |
|
475 (*current_liboctave_error_handler) |
|
476 ("invalid index = %d", max_val+1); |
|
477 |
|
478 initialized = 0; |
|
479 } |
|
480 else |
|
481 frozen_len = length (z_len); |
|
482 } |
|
483 } |
|
484 |
|
485 frozen = 1; |
|
486 frozen_at_z_len = z_len; |
|
487 |
|
488 return frozen_len; |
1
|
489 } |
|
490 |
|
491 /* |
|
492 ;;; Local Variables: *** |
|
493 ;;; mode: C++ *** |
|
494 ;;; page-delimiter: "^/\\*" *** |
|
495 ;;; End: *** |
|
496 */ |