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 |
1297
|
23 #if defined (__GNUG__) |
|
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" |
2828
|
36 #include "boolMatrix.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" |
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 |
|
150 IDX_VEC_REP::idx_vector_rep (const Matrix& m) |
|
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 |
|
160 orig_nr = m.rows (); |
|
161 orig_nc = m.columns (); |
|
162 |
|
163 len = orig_nr * orig_nc; |
|
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 |
1560
|
179 for (int j = 0; j < orig_nc; j++) |
|
180 for (int i = 0; i < orig_nr; i++) |
2500
|
181 { |
|
182 double d = m.elem (i, j); |
|
183 |
|
184 if (idx_is_inf_or_nan (d)) |
|
185 return; |
|
186 else |
|
187 data[k++] = tree_to_mat_idx (d); |
|
188 } |
1
|
189 } |
|
190 |
1560
|
191 init_state (); |
1
|
192 } |
|
193 |
1560
|
194 IDX_VEC_REP::idx_vector_rep (const Range& r) |
1
|
195 { |
1135
|
196 data = 0; |
191
|
197 initialized = 0; |
1560
|
198 frozen = 0; |
|
199 colon_equiv_checked = 0; |
|
200 colon_equiv = 0; |
|
201 colon = 0; |
2828
|
202 one_zero = 0; |
191
|
203 |
1
|
204 len = r.nelem (); |
|
205 |
1560
|
206 orig_nr = 1; |
|
207 orig_nc = len; |
|
208 |
191
|
209 if (len < 0) |
|
210 { |
1560
|
211 (*current_liboctave_error_handler) ("invalid range used as index"); |
191
|
212 return; |
|
213 } |
|
214 else if (len == 0) |
|
215 { |
|
216 num_zeros = 0; |
|
217 num_ones = 0; |
1560
|
218 max_val = 0; |
|
219 min_val = 0; |
191
|
220 initialized = 1; |
|
221 return; |
|
222 } |
1
|
223 |
|
224 double b = r.base (); |
|
225 double step = r.inc (); |
|
226 |
|
227 data = new int [len]; |
|
228 |
|
229 for (int i = 0; i < len; i++) |
|
230 { |
|
231 double val = b + i * step; |
2500
|
232 |
|
233 if (idx_is_inf_or_nan (val)) |
|
234 return; |
|
235 else |
|
236 data[i] = tree_to_mat_idx (val); |
1
|
237 } |
|
238 |
|
239 init_state (); |
|
240 } |
|
241 |
3928
|
242 IDX_VEC_REP::idx_vector_rep (double d) |
|
243 { |
|
244 data = 0; |
|
245 initialized = 0; |
|
246 frozen = 0; |
|
247 colon_equiv_checked = 0; |
|
248 colon_equiv = 0; |
|
249 colon = 0; |
|
250 one_zero = 0; |
|
251 |
|
252 len = 1; |
|
253 |
|
254 orig_nr = 1; |
|
255 orig_nc = 1; |
|
256 |
|
257 if (idx_is_inf_or_nan (d)) |
|
258 return; |
|
259 else |
|
260 { |
|
261 data = new int [len]; |
|
262 |
|
263 data[0] = tree_to_mat_idx (d); |
|
264 } |
|
265 |
|
266 init_state (); |
|
267 } |
|
268 |
|
269 IDX_VEC_REP::idx_vector_rep (int i) |
|
270 { |
|
271 data = 0; |
|
272 initialized = 0; |
|
273 frozen = 0; |
|
274 colon_equiv_checked = 0; |
|
275 colon_equiv = 0; |
|
276 colon = 0; |
|
277 one_zero = 0; |
|
278 |
|
279 len = 1; |
|
280 |
|
281 orig_nr = 1; |
|
282 orig_nc = 1; |
|
283 |
|
284 data = new int [len]; |
|
285 |
|
286 data[0] = tree_to_mat_idx (i); |
|
287 |
|
288 init_state (); |
|
289 } |
|
290 |
1560
|
291 IDX_VEC_REP::idx_vector_rep (char c) |
|
292 { |
|
293 assert (c == ':'); |
|
294 |
|
295 colon = 1; |
|
296 len = 0; |
|
297 num_zeros = 0; |
|
298 num_ones = 0; |
|
299 one_zero = 0; |
|
300 initialized = 0; |
|
301 frozen = 0; |
|
302 colon_equiv_checked = 0; |
|
303 colon_equiv = 0; |
|
304 data = 0; |
|
305 |
|
306 init_state (); |
|
307 } |
|
308 |
2828
|
309 IDX_VEC_REP::idx_vector_rep (bool b) |
|
310 { |
|
311 data = 0; |
|
312 initialized = 0; |
|
313 frozen = 0; |
|
314 colon_equiv_checked = 0; |
|
315 colon_equiv = 0; |
|
316 colon = 0; |
|
317 one_zero = 1; |
|
318 |
|
319 len = 1; |
|
320 |
|
321 orig_nr = 1; |
|
322 orig_nc = 1; |
|
323 |
|
324 data = new int [len]; |
|
325 |
|
326 data[0] = tree_to_mat_idx (b); |
|
327 |
|
328 init_state (); |
|
329 } |
|
330 |
|
331 IDX_VEC_REP::idx_vector_rep (const boolMatrix& bm) |
|
332 { |
|
333 data = 0; |
|
334 initialized = 0; |
|
335 frozen = 0; |
|
336 colon_equiv_checked = 0; |
|
337 colon_equiv = 0; |
|
338 colon = 0; |
|
339 one_zero = 1; |
|
340 |
|
341 orig_nr = bm.rows (); |
|
342 orig_nc = bm.columns (); |
|
343 |
|
344 len = orig_nr * orig_nc; |
|
345 |
|
346 if (len == 0) |
|
347 { |
|
348 num_zeros = 0; |
|
349 num_ones = 0; |
|
350 one_zero = 0; |
|
351 max_val = 0; |
|
352 min_val = 0; |
|
353 initialized = 1; |
|
354 return; |
|
355 } |
|
356 else |
|
357 { |
|
358 int k = 0; |
|
359 data = new int [len]; |
|
360 |
|
361 for (int j = 0; j < orig_nc; j++) |
|
362 for (int i = 0; i < orig_nr; i++) |
|
363 data[k++] = tree_to_mat_idx (bm.elem (i, j)); |
|
364 } |
|
365 |
|
366 init_state (); |
|
367 } |
|
368 |
1560
|
369 IDX_VEC_REP& |
|
370 IDX_VEC_REP::operator = (const IDX_VEC_REP& a) |
1
|
371 { |
|
372 if (this != &a) |
|
373 { |
191
|
374 initialized = a.initialized; |
1560
|
375 frozen = a.frozen; |
|
376 colon_equiv_checked = a.colon_equiv_checked; |
|
377 colon_equiv = a.colon_equiv; |
|
378 |
|
379 colon = a.colon; |
|
380 |
|
381 orig_nr = a.orig_nr; |
|
382 orig_nc = a.orig_nc; |
191
|
383 |
1
|
384 delete [] data; |
|
385 len = a.len; |
|
386 data = new int [len]; |
|
387 for (int i = 0; i < len; i++) |
|
388 data[i] = a.data[i]; |
|
389 |
|
390 num_zeros = a.num_zeros; |
|
391 num_ones = a.num_ones; |
|
392 one_zero = a.one_zero; |
|
393 |
|
394 max_val = a.max_val; |
|
395 min_val = a.min_val; |
|
396 } |
|
397 return *this; |
|
398 } |
|
399 |
|
400 void |
1560
|
401 IDX_VEC_REP::init_state (void) |
1
|
402 { |
|
403 num_zeros = 0; |
|
404 num_ones = 0; |
|
405 |
1560
|
406 if (colon) |
1
|
407 { |
2828
|
408 min_val = 0; |
|
409 max_val = 0; |
1
|
410 } |
|
411 else |
|
412 { |
|
413 min_val = max_val = data[0]; |
|
414 |
1321
|
415 int i = 0; |
1
|
416 do |
|
417 { |
1560
|
418 if (data[i] == -1) |
|
419 num_zeros++; |
|
420 else if (data[i] == 0) |
|
421 num_ones++; |
|
422 |
1
|
423 if (data[i] > max_val) |
|
424 max_val = data[i]; |
|
425 |
|
426 if (data[i] < min_val) |
|
427 min_val = data[i]; |
|
428 } |
|
429 while (++i < len); |
|
430 } |
1560
|
431 |
|
432 initialized = 1; |
|
433 } |
|
434 |
|
435 void |
2828
|
436 IDX_VEC_REP::maybe_convert_one_zero_to_idx (int z_len) |
1560
|
437 { |
3680
|
438 if (one_zero && (z_len == len || z_len == 0)) |
1560
|
439 { |
|
440 if (num_ones == 0) |
|
441 { |
|
442 len = 0; |
|
443 max_val = 0; |
|
444 min_val = 0; |
|
445 delete [] data; |
|
446 data = 0; |
|
447 } |
|
448 else |
|
449 { |
|
450 assert (num_ones + num_zeros == len); |
|
451 |
|
452 int *new_data = new int [num_ones]; |
1650
|
453 int k = 0; |
1560
|
454 for (int i = 0; i < len; i++) |
|
455 if (data[i] == 0) |
1650
|
456 new_data[k++] = i; |
1560
|
457 |
|
458 delete [] data; |
|
459 len = num_ones; |
|
460 data = new_data; |
|
461 |
|
462 min_val = max_val = data[0]; |
|
463 |
|
464 int i = 0; |
|
465 do |
|
466 { |
|
467 if (data[i] > max_val) |
|
468 max_val = data[i]; |
|
469 |
|
470 if (data[i] < min_val) |
|
471 min_val = data[i]; |
|
472 } |
|
473 while (++i < len); |
|
474 } |
|
475 } |
1
|
476 } |
|
477 |
227
|
478 int |
1560
|
479 IDX_VEC_REP::checkelem (int n) const |
227
|
480 { |
|
481 if (n < 0 || n >= len) |
|
482 { |
1560
|
483 (*current_liboctave_error_handler) ("idx-vector: index out of range"); |
227
|
484 return 0; |
|
485 } |
|
486 |
|
487 return elem (n); |
|
488 } |
|
489 |
1552
|
490 static inline int |
3262
|
491 intcmp (const void *ii, const void *jj) |
1552
|
492 { |
3262
|
493 return (*(static_cast<const int *> (ii)) - *(static_cast<const int *> (jj))); |
1552
|
494 } |
|
495 |
1560
|
496 static inline void |
1650
|
497 sort_data (int *d, int l) |
1560
|
498 { |
2800
|
499 qsort (d, l, sizeof (int), intcmp); |
1560
|
500 } |
|
501 |
|
502 static inline int |
1650
|
503 make_uniq (int *d, int l) |
1560
|
504 { |
3125
|
505 if (l < 2) |
|
506 return l; |
|
507 |
1560
|
508 int k = 0; |
1759
|
509 for (int ii = 1; ii < l; ii++) |
1560
|
510 { |
1650
|
511 if (d[ii] != d[k]) |
1560
|
512 { |
|
513 k++; |
1650
|
514 d[k] = d[ii]; |
1560
|
515 } |
|
516 } |
|
517 return k+1; |
|
518 } |
|
519 |
|
520 static inline int * |
1650
|
521 copy_data (const int *d, int l) |
209
|
522 { |
1650
|
523 int *new_data = new int [l]; |
1560
|
524 |
1650
|
525 for (int ii = 0; ii < l; ii++) |
|
526 new_data[ii] = d[ii]; |
1560
|
527 |
|
528 return new_data; |
|
529 } |
|
530 |
|
531 int |
2356
|
532 IDX_VEC_REP::is_colon_equiv (int n, int sort_uniq) |
1560
|
533 { |
|
534 if (! colon_equiv_checked) |
|
535 { |
|
536 if (colon) |
|
537 { |
|
538 colon_equiv = 1; |
|
539 } |
3472
|
540 else if (len > 1) |
1560
|
541 { |
3472
|
542 if (one_zero) |
|
543 { |
|
544 colon_equiv = (len == n && ones_count () == n); |
|
545 } |
|
546 else if (sort_uniq) |
2356
|
547 { |
2966
|
548 int *tmp_data = copy_data (data, len); |
|
549 |
2356
|
550 sort_data (tmp_data, len); |
|
551 |
2966
|
552 int tmp_len = make_uniq (tmp_data, len); |
|
553 |
|
554 colon_equiv = (tmp_len == n |
|
555 && tmp_data[0] == 0 |
|
556 && tmp_data[tmp_len-1] == tmp_len - 1); |
1560
|
557 |
2966
|
558 delete [] tmp_data; |
|
559 } |
|
560 else |
|
561 { |
|
562 if (len == n) |
|
563 { |
|
564 colon_equiv = 1; |
1560
|
565 |
2966
|
566 for (int ii = 0; ii < n; ii++) |
|
567 if (data[ii] != ii) |
|
568 { |
|
569 colon_equiv = 0; |
|
570 break; |
|
571 } |
|
572 } |
|
573 } |
1560
|
574 } |
|
575 else |
2966
|
576 colon_equiv = (len == n && (n == 0 || (n == 1 && data[0] == 0))); |
1560
|
577 |
|
578 colon_equiv_checked = 1; |
|
579 } |
|
580 |
|
581 return colon_equiv; |
209
|
582 } |
|
583 |
417
|
584 void |
3079
|
585 IDX_VEC_REP::sort (bool uniq) |
|
586 { |
3125
|
587 if (len > 1) |
|
588 { |
|
589 sort_data (data, len); |
3079
|
590 |
3125
|
591 if (uniq) |
|
592 len = make_uniq (data, len); |
|
593 } |
3079
|
594 } |
|
595 |
|
596 void |
1560
|
597 IDX_VEC_REP::shorten (int n) |
434
|
598 { |
|
599 if (n > 0 && n <= len) |
|
600 len = n; |
|
601 else |
1560
|
602 (*current_liboctave_error_handler) |
|
603 ("idx_vector::shorten: internal error!"); |
434
|
604 } |
|
605 |
3504
|
606 std::ostream& |
|
607 IDX_VEC_REP::print (std::ostream& os) const |
1560
|
608 { |
1650
|
609 for (int ii = 0; ii < len; ii++) |
|
610 os << data[ii] << "\n"; |
1560
|
611 return os; |
|
612 } |
|
613 |
|
614 int |
2830
|
615 IDX_VEC_REP::freeze (int z_len, const char *tag, int resize_ok) |
1
|
616 { |
1560
|
617 if (frozen) |
3680
|
618 return frozen_len; |
1560
|
619 |
|
620 frozen_len = -1; |
|
621 |
|
622 if (colon) |
|
623 frozen_len = z_len; |
|
624 else |
|
625 { |
|
626 if (len == 0) |
|
627 frozen_len = 0; |
|
628 else |
|
629 { |
2828
|
630 maybe_convert_one_zero_to_idx (z_len); |
1560
|
631 |
1650
|
632 max_val = max (); |
|
633 min_val = min (); |
1560
|
634 |
|
635 if (min_val < 0) |
|
636 { |
|
637 if (tag) |
|
638 (*current_liboctave_error_handler) |
|
639 ("invalid %s index = %d", tag, min_val+1); |
|
640 else |
|
641 (*current_liboctave_error_handler) |
|
642 ("invalid index = %d", min_val+1); |
|
643 |
|
644 initialized = 0; |
|
645 } |
|
646 else if (! resize_ok && max_val >= z_len) |
|
647 { |
|
648 if (tag) |
|
649 (*current_liboctave_error_handler) |
|
650 ("invalid %s index = %d", tag, max_val+1); |
|
651 else |
|
652 (*current_liboctave_error_handler) |
|
653 ("invalid index = %d", max_val+1); |
|
654 |
|
655 initialized = 0; |
|
656 } |
|
657 else |
|
658 frozen_len = length (z_len); |
|
659 } |
|
660 } |
|
661 |
|
662 frozen = 1; |
3680
|
663 |
|
664 frozen_at_z_len = z_len ? z_len : len; |
1560
|
665 |
|
666 return frozen_len; |
1
|
667 } |
|
668 |
|
669 /* |
|
670 ;;; Local Variables: *** |
|
671 ;;; mode: C++ *** |
|
672 ;;; End: *** |
|
673 */ |