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 |
4192
|
23 #if defined (__GNUG__) && defined (USE_PRAGMA_INTERFACE_IMPLEMENTATION) |
1297
|
24 #pragma implementation |
|
25 #endif |
|
26 |
240
|
27 #ifdef HAVE_CONFIG_H |
1192
|
28 #include <config.h> |
1
|
29 #endif |
|
30 |
1343
|
31 #include <cstdlib> |
|
32 |
3503
|
33 #include <iostream> |
164
|
34 |
1352
|
35 #include "Range.h" |
4650
|
36 #include "boolNDArray.h" |
1560
|
37 #include "dColVector.h" |
4650
|
38 #include "dNDArray.h" |
1352
|
39 |
1
|
40 #include "idx-vector.h" |
1560
|
41 #include "lo-error.h" |
2500
|
42 #include "lo-mappers.h" |
1
|
43 |
1560
|
44 #define IDX_VEC_REP idx_vector::idx_vector_rep |
|
45 |
|
46 IDX_VEC_REP::idx_vector_rep (const IDX_VEC_REP& a) |
1
|
47 { |
1135
|
48 data = 0; |
191
|
49 initialized = a.initialized; |
1560
|
50 frozen = a.frozen; |
|
51 colon_equiv_checked = a.colon_equiv_checked; |
|
52 colon_equiv = a.colon_equiv; |
|
53 |
|
54 colon = a.colon; |
|
55 |
|
56 orig_nr = a.orig_nr; |
|
57 orig_nc = a.orig_nc; |
191
|
58 |
1
|
59 len = a.len; |
|
60 if (len > 0) |
|
61 { |
|
62 data = new int [len]; |
|
63 for (int i = 0; i < len; i++) |
|
64 data[i] = a.data[i]; |
|
65 |
|
66 num_zeros = a.num_zeros; |
|
67 num_ones = a.num_ones; |
|
68 one_zero = a.one_zero; |
|
69 |
|
70 max_val = a.max_val; |
|
71 min_val = a.min_val; |
|
72 } |
|
73 } |
|
74 |
|
75 static inline int |
|
76 tree_to_mat_idx (double x) |
|
77 { |
3928
|
78 return (x > 0) |
|
79 ? (static_cast<int> (x + 0.5) - 1) |
|
80 : (static_cast<int> (x - 0.5) - 1); |
|
81 } |
|
82 |
|
83 static inline int |
|
84 tree_to_mat_idx (int i) |
|
85 { |
|
86 return i - 1; |
2500
|
87 } |
|
88 |
|
89 static inline bool |
|
90 idx_is_inf_or_nan (double x) |
|
91 { |
|
92 bool retval = false; |
|
93 |
|
94 if (xisnan (x)) |
|
95 { |
|
96 (*current_liboctave_error_handler) ("NaN invalid as index"); |
|
97 retval = true; |
|
98 } |
|
99 else if (xisinf (x)) |
|
100 { |
|
101 (*current_liboctave_error_handler) ("Inf invalid as index"); |
|
102 retval = true; |
|
103 } |
|
104 |
|
105 return retval; |
1
|
106 } |
|
107 |
1560
|
108 IDX_VEC_REP::idx_vector_rep (const ColumnVector& v) |
1
|
109 { |
1135
|
110 data = 0; |
191
|
111 initialized = 0; |
1560
|
112 frozen = 0; |
|
113 colon_equiv_checked = 0; |
|
114 colon_equiv = 0; |
|
115 colon = 0; |
2828
|
116 one_zero = 0; |
1
|
117 |
1650
|
118 len = v.length (); |
1560
|
119 |
|
120 orig_nr = len; |
|
121 orig_nc = 1; |
|
122 |
|
123 if (len == 0) |
1
|
124 { |
|
125 num_zeros = 0; |
|
126 num_ones = 0; |
1560
|
127 max_val = 0; |
|
128 min_val = 0; |
191
|
129 initialized = 1; |
1
|
130 return; |
|
131 } |
1560
|
132 else |
1
|
133 { |
|
134 data = new int [len]; |
2500
|
135 |
1
|
136 for (int i = 0; i < len; i++) |
2500
|
137 { |
|
138 double d = v.elem (i); |
|
139 |
|
140 if (idx_is_inf_or_nan (d)) |
|
141 return; |
|
142 else |
|
143 data[i] = tree_to_mat_idx (d); |
|
144 } |
1
|
145 } |
1560
|
146 |
|
147 init_state (); |
|
148 } |
|
149 |
4650
|
150 IDX_VEC_REP::idx_vector_rep (const NDArray& nda) |
1560
|
151 { |
|
152 data = 0; |
|
153 initialized = 0; |
|
154 frozen = 0; |
|
155 colon_equiv_checked = 0; |
|
156 colon_equiv = 0; |
|
157 colon = 0; |
2828
|
158 one_zero = 0; |
1560
|
159 |
4652
|
160 orig_nr = nda.rows (); |
|
161 orig_nc = nda.cols (); |
|
162 |
4650
|
163 len = nda.length (); |
1560
|
164 |
|
165 if (len == 0) |
1
|
166 { |
1560
|
167 num_zeros = 0; |
|
168 num_ones = 0; |
|
169 max_val = 0; |
|
170 min_val = 0; |
|
171 initialized = 1; |
|
172 return; |
1
|
173 } |
|
174 else |
|
175 { |
1560
|
176 int k = 0; |
|
177 data = new int [len]; |
2500
|
178 |
4650
|
179 for (int i = 0; i < len; i++) |
|
180 { |
|
181 double d = nda.elem (i); |
2500
|
182 |
4650
|
183 if (idx_is_inf_or_nan (d)) |
|
184 return; |
|
185 else |
|
186 data[k++] = tree_to_mat_idx (d); |
|
187 } |
1
|
188 } |
|
189 |
1560
|
190 init_state (); |
1
|
191 } |
|
192 |
1560
|
193 IDX_VEC_REP::idx_vector_rep (const Range& r) |
1
|
194 { |
1135
|
195 data = 0; |
191
|
196 initialized = 0; |
1560
|
197 frozen = 0; |
|
198 colon_equiv_checked = 0; |
|
199 colon_equiv = 0; |
|
200 colon = 0; |
2828
|
201 one_zero = 0; |
191
|
202 |
1
|
203 len = r.nelem (); |
|
204 |
1560
|
205 orig_nr = 1; |
|
206 orig_nc = len; |
|
207 |
191
|
208 if (len < 0) |
|
209 { |
1560
|
210 (*current_liboctave_error_handler) ("invalid range used as index"); |
191
|
211 return; |
|
212 } |
|
213 else if (len == 0) |
|
214 { |
|
215 num_zeros = 0; |
|
216 num_ones = 0; |
1560
|
217 max_val = 0; |
|
218 min_val = 0; |
191
|
219 initialized = 1; |
|
220 return; |
|
221 } |
1
|
222 |
|
223 double b = r.base (); |
|
224 double step = r.inc (); |
|
225 |
|
226 data = new int [len]; |
|
227 |
|
228 for (int i = 0; i < len; i++) |
|
229 { |
|
230 double val = b + i * step; |
2500
|
231 |
|
232 if (idx_is_inf_or_nan (val)) |
|
233 return; |
|
234 else |
|
235 data[i] = tree_to_mat_idx (val); |
1
|
236 } |
|
237 |
|
238 init_state (); |
|
239 } |
|
240 |
3928
|
241 IDX_VEC_REP::idx_vector_rep (double d) |
|
242 { |
|
243 data = 0; |
|
244 initialized = 0; |
|
245 frozen = 0; |
|
246 colon_equiv_checked = 0; |
|
247 colon_equiv = 0; |
|
248 colon = 0; |
|
249 one_zero = 0; |
|
250 |
|
251 len = 1; |
|
252 |
|
253 orig_nr = 1; |
|
254 orig_nc = 1; |
|
255 |
|
256 if (idx_is_inf_or_nan (d)) |
|
257 return; |
|
258 else |
|
259 { |
|
260 data = new int [len]; |
|
261 |
|
262 data[0] = tree_to_mat_idx (d); |
|
263 } |
|
264 |
|
265 init_state (); |
|
266 } |
|
267 |
|
268 IDX_VEC_REP::idx_vector_rep (int i) |
|
269 { |
|
270 data = 0; |
|
271 initialized = 0; |
|
272 frozen = 0; |
|
273 colon_equiv_checked = 0; |
|
274 colon_equiv = 0; |
|
275 colon = 0; |
|
276 one_zero = 0; |
|
277 |
|
278 len = 1; |
|
279 |
|
280 orig_nr = 1; |
|
281 orig_nc = 1; |
|
282 |
|
283 data = new int [len]; |
|
284 |
|
285 data[0] = tree_to_mat_idx (i); |
|
286 |
|
287 init_state (); |
|
288 } |
|
289 |
1560
|
290 IDX_VEC_REP::idx_vector_rep (char c) |
|
291 { |
|
292 assert (c == ':'); |
|
293 |
|
294 colon = 1; |
|
295 len = 0; |
|
296 num_zeros = 0; |
|
297 num_ones = 0; |
|
298 one_zero = 0; |
|
299 initialized = 0; |
|
300 frozen = 0; |
|
301 colon_equiv_checked = 0; |
|
302 colon_equiv = 0; |
|
303 data = 0; |
|
304 |
|
305 init_state (); |
|
306 } |
|
307 |
2828
|
308 IDX_VEC_REP::idx_vector_rep (bool b) |
|
309 { |
|
310 data = 0; |
|
311 initialized = 0; |
|
312 frozen = 0; |
|
313 colon_equiv_checked = 0; |
|
314 colon_equiv = 0; |
|
315 colon = 0; |
|
316 one_zero = 1; |
|
317 |
|
318 len = 1; |
|
319 |
|
320 orig_nr = 1; |
|
321 orig_nc = 1; |
|
322 |
|
323 data = new int [len]; |
|
324 |
|
325 data[0] = tree_to_mat_idx (b); |
|
326 |
|
327 init_state (); |
|
328 } |
|
329 |
4650
|
330 IDX_VEC_REP::idx_vector_rep (const boolNDArray& bnda) |
2828
|
331 { |
|
332 data = 0; |
|
333 initialized = 0; |
|
334 frozen = 0; |
|
335 colon_equiv_checked = 0; |
|
336 colon_equiv = 0; |
|
337 colon = 0; |
|
338 one_zero = 1; |
|
339 |
4652
|
340 orig_nr = bnda.rows (); |
|
341 orig_nc = bnda.cols (); |
|
342 |
4650
|
343 len = bnda.length (); |
2828
|
344 |
|
345 if (len == 0) |
|
346 { |
|
347 num_zeros = 0; |
|
348 num_ones = 0; |
|
349 one_zero = 0; |
|
350 max_val = 0; |
|
351 min_val = 0; |
|
352 initialized = 1; |
|
353 return; |
|
354 } |
|
355 else |
|
356 { |
|
357 int k = 0; |
|
358 data = new int [len]; |
|
359 |
4650
|
360 for (int i = 0; i < len; i++) |
|
361 data[k++] = tree_to_mat_idx (bnda.elem (i)); |
2828
|
362 } |
|
363 |
|
364 init_state (); |
|
365 } |
|
366 |
1560
|
367 IDX_VEC_REP& |
|
368 IDX_VEC_REP::operator = (const IDX_VEC_REP& a) |
1
|
369 { |
|
370 if (this != &a) |
|
371 { |
191
|
372 initialized = a.initialized; |
1560
|
373 frozen = a.frozen; |
|
374 colon_equiv_checked = a.colon_equiv_checked; |
|
375 colon_equiv = a.colon_equiv; |
|
376 |
|
377 colon = a.colon; |
|
378 |
|
379 orig_nr = a.orig_nr; |
|
380 orig_nc = a.orig_nc; |
191
|
381 |
1
|
382 delete [] data; |
|
383 len = a.len; |
|
384 data = new int [len]; |
|
385 for (int i = 0; i < len; i++) |
|
386 data[i] = a.data[i]; |
|
387 |
|
388 num_zeros = a.num_zeros; |
|
389 num_ones = a.num_ones; |
|
390 one_zero = a.one_zero; |
|
391 |
|
392 max_val = a.max_val; |
|
393 min_val = a.min_val; |
|
394 } |
|
395 return *this; |
|
396 } |
|
397 |
|
398 void |
1560
|
399 IDX_VEC_REP::init_state (void) |
1
|
400 { |
|
401 num_zeros = 0; |
|
402 num_ones = 0; |
|
403 |
1560
|
404 if (colon) |
1
|
405 { |
2828
|
406 min_val = 0; |
|
407 max_val = 0; |
1
|
408 } |
|
409 else |
|
410 { |
|
411 min_val = max_val = data[0]; |
|
412 |
1321
|
413 int i = 0; |
1
|
414 do |
|
415 { |
1560
|
416 if (data[i] == -1) |
|
417 num_zeros++; |
|
418 else if (data[i] == 0) |
|
419 num_ones++; |
|
420 |
1
|
421 if (data[i] > max_val) |
|
422 max_val = data[i]; |
|
423 |
|
424 if (data[i] < min_val) |
|
425 min_val = data[i]; |
|
426 } |
|
427 while (++i < len); |
|
428 } |
1560
|
429 |
|
430 initialized = 1; |
|
431 } |
|
432 |
|
433 void |
2828
|
434 IDX_VEC_REP::maybe_convert_one_zero_to_idx (int z_len) |
1560
|
435 { |
3680
|
436 if (one_zero && (z_len == len || z_len == 0)) |
1560
|
437 { |
|
438 if (num_ones == 0) |
|
439 { |
|
440 len = 0; |
|
441 max_val = 0; |
|
442 min_val = 0; |
|
443 delete [] data; |
|
444 data = 0; |
|
445 } |
|
446 else |
|
447 { |
|
448 assert (num_ones + num_zeros == len); |
|
449 |
|
450 int *new_data = new int [num_ones]; |
1650
|
451 int k = 0; |
1560
|
452 for (int i = 0; i < len; i++) |
|
453 if (data[i] == 0) |
1650
|
454 new_data[k++] = i; |
1560
|
455 |
|
456 delete [] data; |
|
457 len = num_ones; |
|
458 data = new_data; |
|
459 |
|
460 min_val = max_val = data[0]; |
|
461 |
|
462 int i = 0; |
|
463 do |
|
464 { |
|
465 if (data[i] > max_val) |
|
466 max_val = data[i]; |
|
467 |
|
468 if (data[i] < min_val) |
|
469 min_val = data[i]; |
|
470 } |
|
471 while (++i < len); |
|
472 } |
|
473 } |
1
|
474 } |
|
475 |
227
|
476 int |
1560
|
477 IDX_VEC_REP::checkelem (int n) const |
227
|
478 { |
|
479 if (n < 0 || n >= len) |
|
480 { |
1560
|
481 (*current_liboctave_error_handler) ("idx-vector: index out of range"); |
227
|
482 return 0; |
|
483 } |
|
484 |
|
485 return elem (n); |
|
486 } |
|
487 |
1552
|
488 static inline int |
3262
|
489 intcmp (const void *ii, const void *jj) |
1552
|
490 { |
3262
|
491 return (*(static_cast<const int *> (ii)) - *(static_cast<const int *> (jj))); |
1552
|
492 } |
|
493 |
1560
|
494 static inline void |
1650
|
495 sort_data (int *d, int l) |
1560
|
496 { |
2800
|
497 qsort (d, l, sizeof (int), intcmp); |
1560
|
498 } |
|
499 |
|
500 static inline int |
1650
|
501 make_uniq (int *d, int l) |
1560
|
502 { |
3125
|
503 if (l < 2) |
|
504 return l; |
|
505 |
1560
|
506 int k = 0; |
1759
|
507 for (int ii = 1; ii < l; ii++) |
1560
|
508 { |
1650
|
509 if (d[ii] != d[k]) |
1560
|
510 { |
|
511 k++; |
1650
|
512 d[k] = d[ii]; |
1560
|
513 } |
|
514 } |
|
515 return k+1; |
|
516 } |
|
517 |
|
518 static inline int * |
1650
|
519 copy_data (const int *d, int l) |
209
|
520 { |
1650
|
521 int *new_data = new int [l]; |
1560
|
522 |
1650
|
523 for (int ii = 0; ii < l; ii++) |
|
524 new_data[ii] = d[ii]; |
1560
|
525 |
|
526 return new_data; |
|
527 } |
|
528 |
|
529 int |
2356
|
530 IDX_VEC_REP::is_colon_equiv (int n, int sort_uniq) |
1560
|
531 { |
|
532 if (! colon_equiv_checked) |
|
533 { |
|
534 if (colon) |
|
535 { |
|
536 colon_equiv = 1; |
|
537 } |
3472
|
538 else if (len > 1) |
1560
|
539 { |
3472
|
540 if (one_zero) |
|
541 { |
|
542 colon_equiv = (len == n && ones_count () == n); |
|
543 } |
|
544 else if (sort_uniq) |
2356
|
545 { |
2966
|
546 int *tmp_data = copy_data (data, len); |
|
547 |
2356
|
548 sort_data (tmp_data, len); |
|
549 |
2966
|
550 int tmp_len = make_uniq (tmp_data, len); |
|
551 |
|
552 colon_equiv = (tmp_len == n |
|
553 && tmp_data[0] == 0 |
|
554 && tmp_data[tmp_len-1] == tmp_len - 1); |
1560
|
555 |
2966
|
556 delete [] tmp_data; |
|
557 } |
|
558 else |
|
559 { |
|
560 if (len == n) |
|
561 { |
|
562 colon_equiv = 1; |
1560
|
563 |
2966
|
564 for (int ii = 0; ii < n; ii++) |
|
565 if (data[ii] != ii) |
|
566 { |
|
567 colon_equiv = 0; |
|
568 break; |
|
569 } |
|
570 } |
|
571 } |
1560
|
572 } |
|
573 else |
2966
|
574 colon_equiv = (len == n && (n == 0 || (n == 1 && data[0] == 0))); |
1560
|
575 |
|
576 colon_equiv_checked = 1; |
|
577 } |
|
578 |
|
579 return colon_equiv; |
209
|
580 } |
|
581 |
417
|
582 void |
3079
|
583 IDX_VEC_REP::sort (bool uniq) |
|
584 { |
3125
|
585 if (len > 1) |
|
586 { |
|
587 sort_data (data, len); |
3079
|
588 |
3125
|
589 if (uniq) |
|
590 len = make_uniq (data, len); |
|
591 } |
3079
|
592 } |
|
593 |
|
594 void |
1560
|
595 IDX_VEC_REP::shorten (int n) |
434
|
596 { |
|
597 if (n > 0 && n <= len) |
|
598 len = n; |
|
599 else |
1560
|
600 (*current_liboctave_error_handler) |
|
601 ("idx_vector::shorten: internal error!"); |
434
|
602 } |
|
603 |
3504
|
604 std::ostream& |
|
605 IDX_VEC_REP::print (std::ostream& os) const |
1560
|
606 { |
1650
|
607 for (int ii = 0; ii < len; ii++) |
|
608 os << data[ii] << "\n"; |
1560
|
609 return os; |
|
610 } |
|
611 |
|
612 int |
4461
|
613 IDX_VEC_REP::freeze (int z_len, const char *tag, bool resize_ok, |
|
614 bool warn_resize) |
1
|
615 { |
1560
|
616 if (frozen) |
3680
|
617 return frozen_len; |
1560
|
618 |
|
619 frozen_len = -1; |
|
620 |
|
621 if (colon) |
|
622 frozen_len = z_len; |
|
623 else |
|
624 { |
|
625 if (len == 0) |
|
626 frozen_len = 0; |
|
627 else |
|
628 { |
2828
|
629 maybe_convert_one_zero_to_idx (z_len); |
1560
|
630 |
1650
|
631 max_val = max (); |
|
632 min_val = min (); |
1560
|
633 |
|
634 if (min_val < 0) |
|
635 { |
|
636 if (tag) |
|
637 (*current_liboctave_error_handler) |
|
638 ("invalid %s index = %d", tag, min_val+1); |
|
639 else |
|
640 (*current_liboctave_error_handler) |
|
641 ("invalid index = %d", min_val+1); |
|
642 |
|
643 initialized = 0; |
|
644 } |
|
645 else if (! resize_ok && max_val >= z_len) |
|
646 { |
|
647 if (tag) |
|
648 (*current_liboctave_error_handler) |
|
649 ("invalid %s index = %d", tag, max_val+1); |
|
650 else |
|
651 (*current_liboctave_error_handler) |
|
652 ("invalid index = %d", max_val+1); |
|
653 |
|
654 initialized = 0; |
|
655 } |
|
656 else |
4461
|
657 { |
|
658 if (warn_resize && max_val >= z_len) |
|
659 { |
|
660 if (tag) |
|
661 (*current_liboctave_error_handler) |
|
662 ("resizing object with %s index = %d out of bounds", |
|
663 tag, max_val+1); |
|
664 else |
|
665 (*current_liboctave_error_handler) |
|
666 ("resizing object with index = %d out of bounds", |
|
667 max_val+1); |
|
668 } |
|
669 |
|
670 frozen_len = length (z_len); |
|
671 } |
1560
|
672 } |
|
673 } |
|
674 |
|
675 frozen = 1; |
3680
|
676 |
|
677 frozen_at_z_len = z_len ? z_len : len; |
1560
|
678 |
|
679 return frozen_len; |
1
|
680 } |
|
681 |
|
682 /* |
|
683 ;;; Local Variables: *** |
|
684 ;;; mode: C++ *** |
|
685 ;;; End: *** |
|
686 */ |