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 |
164
|
33 #include <iostream.h> |
|
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 { |
2500
|
78 return (x > 0) ? ((int) (x + 0.5) - 1) : ((int) (x - 0.5) - 1); |
|
79 } |
|
80 |
|
81 static inline bool |
|
82 idx_is_inf_or_nan (double x) |
|
83 { |
|
84 bool retval = false; |
|
85 |
|
86 if (xisnan (x)) |
|
87 { |
|
88 (*current_liboctave_error_handler) ("NaN invalid as index"); |
|
89 retval = true; |
|
90 } |
|
91 else if (xisinf (x)) |
|
92 { |
|
93 (*current_liboctave_error_handler) ("Inf invalid as index"); |
|
94 retval = true; |
|
95 } |
|
96 |
|
97 return retval; |
1
|
98 } |
|
99 |
1560
|
100 IDX_VEC_REP::idx_vector_rep (const ColumnVector& v) |
1
|
101 { |
1135
|
102 data = 0; |
191
|
103 initialized = 0; |
1560
|
104 frozen = 0; |
|
105 colon_equiv_checked = 0; |
|
106 colon_equiv = 0; |
|
107 colon = 0; |
2828
|
108 one_zero = 0; |
1
|
109 |
1650
|
110 len = v.length (); |
1560
|
111 |
|
112 orig_nr = len; |
|
113 orig_nc = 1; |
|
114 |
|
115 if (len == 0) |
1
|
116 { |
|
117 num_zeros = 0; |
|
118 num_ones = 0; |
1560
|
119 max_val = 0; |
|
120 min_val = 0; |
191
|
121 initialized = 1; |
1
|
122 return; |
|
123 } |
1560
|
124 else |
1
|
125 { |
|
126 data = new int [len]; |
2500
|
127 |
1
|
128 for (int i = 0; i < len; i++) |
2500
|
129 { |
|
130 double d = v.elem (i); |
|
131 |
|
132 if (idx_is_inf_or_nan (d)) |
|
133 return; |
|
134 else |
|
135 data[i] = tree_to_mat_idx (d); |
|
136 } |
1
|
137 } |
1560
|
138 |
|
139 init_state (); |
|
140 } |
|
141 |
|
142 IDX_VEC_REP::idx_vector_rep (const Matrix& m) |
|
143 { |
|
144 data = 0; |
|
145 initialized = 0; |
|
146 frozen = 0; |
|
147 colon_equiv_checked = 0; |
|
148 colon_equiv = 0; |
|
149 colon = 0; |
2828
|
150 one_zero = 0; |
1560
|
151 |
|
152 orig_nr = m.rows (); |
|
153 orig_nc = m.columns (); |
|
154 |
|
155 len = orig_nr * orig_nc; |
|
156 |
|
157 if (len == 0) |
1
|
158 { |
1560
|
159 num_zeros = 0; |
|
160 num_ones = 0; |
|
161 max_val = 0; |
|
162 min_val = 0; |
|
163 initialized = 1; |
|
164 return; |
1
|
165 } |
|
166 else |
|
167 { |
1560
|
168 int k = 0; |
|
169 data = new int [len]; |
2500
|
170 |
1560
|
171 for (int j = 0; j < orig_nc; j++) |
|
172 for (int i = 0; i < orig_nr; i++) |
2500
|
173 { |
|
174 double d = m.elem (i, j); |
|
175 |
|
176 if (idx_is_inf_or_nan (d)) |
|
177 return; |
|
178 else |
|
179 data[k++] = tree_to_mat_idx (d); |
|
180 } |
1
|
181 } |
|
182 |
1560
|
183 init_state (); |
1
|
184 } |
|
185 |
2386
|
186 IDX_VEC_REP::idx_vector_rep (double d) |
|
187 { |
|
188 data = 0; |
|
189 initialized = 0; |
|
190 frozen = 0; |
|
191 colon_equiv_checked = 0; |
|
192 colon_equiv = 0; |
|
193 colon = 0; |
2828
|
194 one_zero = 0; |
2386
|
195 |
|
196 len = 1; |
|
197 |
|
198 orig_nr = 1; |
|
199 orig_nc = 1; |
|
200 |
2500
|
201 if (idx_is_inf_or_nan (d)) |
|
202 return; |
|
203 else |
|
204 { |
|
205 data = new int [len]; |
2386
|
206 |
2500
|
207 data[0] = tree_to_mat_idx (d); |
|
208 } |
2508
|
209 |
|
210 init_state (); |
2386
|
211 } |
|
212 |
1560
|
213 IDX_VEC_REP::idx_vector_rep (const Range& r) |
1
|
214 { |
1135
|
215 data = 0; |
191
|
216 initialized = 0; |
1560
|
217 frozen = 0; |
|
218 colon_equiv_checked = 0; |
|
219 colon_equiv = 0; |
|
220 colon = 0; |
2828
|
221 one_zero = 0; |
191
|
222 |
1
|
223 len = r.nelem (); |
|
224 |
1560
|
225 orig_nr = 1; |
|
226 orig_nc = len; |
|
227 |
191
|
228 if (len < 0) |
|
229 { |
1560
|
230 (*current_liboctave_error_handler) ("invalid range used as index"); |
191
|
231 return; |
|
232 } |
|
233 else if (len == 0) |
|
234 { |
|
235 num_zeros = 0; |
|
236 num_ones = 0; |
1560
|
237 max_val = 0; |
|
238 min_val = 0; |
191
|
239 initialized = 1; |
|
240 return; |
|
241 } |
1
|
242 |
|
243 double b = r.base (); |
|
244 double step = r.inc (); |
|
245 |
|
246 data = new int [len]; |
|
247 |
|
248 for (int i = 0; i < len; i++) |
|
249 { |
|
250 double val = b + i * step; |
2500
|
251 |
|
252 if (idx_is_inf_or_nan (val)) |
|
253 return; |
|
254 else |
|
255 data[i] = tree_to_mat_idx (val); |
1
|
256 } |
|
257 |
|
258 init_state (); |
|
259 } |
|
260 |
1560
|
261 IDX_VEC_REP::idx_vector_rep (char c) |
|
262 { |
|
263 assert (c == ':'); |
|
264 |
|
265 colon = 1; |
|
266 len = 0; |
|
267 num_zeros = 0; |
|
268 num_ones = 0; |
|
269 one_zero = 0; |
|
270 initialized = 0; |
|
271 frozen = 0; |
|
272 colon_equiv_checked = 0; |
|
273 colon_equiv = 0; |
|
274 data = 0; |
|
275 |
|
276 init_state (); |
|
277 } |
|
278 |
2828
|
279 IDX_VEC_REP::idx_vector_rep (bool b) |
|
280 { |
|
281 data = 0; |
|
282 initialized = 0; |
|
283 frozen = 0; |
|
284 colon_equiv_checked = 0; |
|
285 colon_equiv = 0; |
|
286 colon = 0; |
|
287 one_zero = 1; |
|
288 |
|
289 len = 1; |
|
290 |
|
291 orig_nr = 1; |
|
292 orig_nc = 1; |
|
293 |
|
294 data = new int [len]; |
|
295 |
|
296 data[0] = tree_to_mat_idx (b); |
|
297 |
|
298 init_state (); |
|
299 } |
|
300 |
|
301 IDX_VEC_REP::idx_vector_rep (const boolMatrix& bm) |
|
302 { |
|
303 data = 0; |
|
304 initialized = 0; |
|
305 frozen = 0; |
|
306 colon_equiv_checked = 0; |
|
307 colon_equiv = 0; |
|
308 colon = 0; |
|
309 one_zero = 1; |
|
310 |
|
311 orig_nr = bm.rows (); |
|
312 orig_nc = bm.columns (); |
|
313 |
|
314 len = orig_nr * orig_nc; |
|
315 |
|
316 if (len == 0) |
|
317 { |
|
318 num_zeros = 0; |
|
319 num_ones = 0; |
|
320 one_zero = 0; |
|
321 max_val = 0; |
|
322 min_val = 0; |
|
323 initialized = 1; |
|
324 return; |
|
325 } |
|
326 else |
|
327 { |
|
328 int k = 0; |
|
329 data = new int [len]; |
|
330 |
|
331 for (int j = 0; j < orig_nc; j++) |
|
332 for (int i = 0; i < orig_nr; i++) |
|
333 data[k++] = tree_to_mat_idx (bm.elem (i, j)); |
|
334 } |
|
335 |
|
336 init_state (); |
|
337 } |
|
338 |
1560
|
339 IDX_VEC_REP& |
|
340 IDX_VEC_REP::operator = (const IDX_VEC_REP& a) |
1
|
341 { |
|
342 if (this != &a) |
|
343 { |
191
|
344 initialized = a.initialized; |
1560
|
345 frozen = a.frozen; |
|
346 colon_equiv_checked = a.colon_equiv_checked; |
|
347 colon_equiv = a.colon_equiv; |
|
348 |
|
349 colon = a.colon; |
|
350 |
|
351 orig_nr = a.orig_nr; |
|
352 orig_nc = a.orig_nc; |
191
|
353 |
1
|
354 delete [] data; |
|
355 len = a.len; |
|
356 data = new int [len]; |
|
357 for (int i = 0; i < len; i++) |
|
358 data[i] = a.data[i]; |
|
359 |
|
360 num_zeros = a.num_zeros; |
|
361 num_ones = a.num_ones; |
|
362 one_zero = a.one_zero; |
|
363 |
|
364 max_val = a.max_val; |
|
365 min_val = a.min_val; |
|
366 } |
|
367 return *this; |
|
368 } |
|
369 |
|
370 void |
1560
|
371 IDX_VEC_REP::init_state (void) |
1
|
372 { |
|
373 num_zeros = 0; |
|
374 num_ones = 0; |
|
375 |
1560
|
376 if (colon) |
1
|
377 { |
2828
|
378 min_val = 0; |
|
379 max_val = 0; |
1
|
380 } |
|
381 else |
|
382 { |
|
383 min_val = max_val = data[0]; |
|
384 |
1321
|
385 int i = 0; |
1
|
386 do |
|
387 { |
1560
|
388 if (data[i] == -1) |
|
389 num_zeros++; |
|
390 else if (data[i] == 0) |
|
391 num_ones++; |
|
392 |
1
|
393 if (data[i] > max_val) |
|
394 max_val = data[i]; |
|
395 |
|
396 if (data[i] < min_val) |
|
397 min_val = data[i]; |
|
398 } |
|
399 while (++i < len); |
|
400 } |
1560
|
401 |
|
402 initialized = 1; |
|
403 } |
|
404 |
|
405 void |
2828
|
406 IDX_VEC_REP::maybe_convert_one_zero_to_idx (int z_len) |
1560
|
407 { |
2828
|
408 if (one_zero && z_len == len) |
1560
|
409 { |
|
410 if (num_ones == 0) |
|
411 { |
|
412 len = 0; |
|
413 max_val = 0; |
|
414 min_val = 0; |
|
415 delete [] data; |
|
416 data = 0; |
|
417 } |
|
418 else |
|
419 { |
|
420 assert (num_ones + num_zeros == len); |
|
421 |
|
422 int *new_data = new int [num_ones]; |
1650
|
423 int k = 0; |
1560
|
424 for (int i = 0; i < len; i++) |
|
425 if (data[i] == 0) |
1650
|
426 new_data[k++] = i; |
1560
|
427 |
|
428 delete [] data; |
|
429 len = num_ones; |
|
430 data = new_data; |
|
431 |
|
432 min_val = max_val = data[0]; |
|
433 |
|
434 int i = 0; |
|
435 do |
|
436 { |
|
437 if (data[i] > max_val) |
|
438 max_val = data[i]; |
|
439 |
|
440 if (data[i] < min_val) |
|
441 min_val = data[i]; |
|
442 } |
|
443 while (++i < len); |
|
444 } |
|
445 } |
1
|
446 } |
|
447 |
227
|
448 int |
1560
|
449 IDX_VEC_REP::checkelem (int n) const |
227
|
450 { |
|
451 if (n < 0 || n >= len) |
|
452 { |
1560
|
453 (*current_liboctave_error_handler) ("idx-vector: index out of range"); |
227
|
454 return 0; |
|
455 } |
|
456 |
|
457 return elem (n); |
|
458 } |
|
459 |
1552
|
460 static inline int |
1650
|
461 intcmp (int *ii, int *jj) |
1552
|
462 { |
1650
|
463 return (*ii - *jj); |
1552
|
464 } |
|
465 |
1560
|
466 static inline void |
1650
|
467 sort_data (int *d, int l) |
1560
|
468 { |
2800
|
469 qsort (d, l, sizeof (int), intcmp); |
1560
|
470 } |
|
471 |
|
472 static inline int |
1650
|
473 make_uniq (int *d, int l) |
1560
|
474 { |
|
475 int k = 0; |
1759
|
476 for (int ii = 1; ii < l; ii++) |
1560
|
477 { |
1650
|
478 if (d[ii] != d[k]) |
1560
|
479 { |
|
480 k++; |
1650
|
481 d[k] = d[ii]; |
1560
|
482 } |
|
483 } |
|
484 return k+1; |
|
485 } |
|
486 |
|
487 static inline int * |
1650
|
488 copy_data (const int *d, int l) |
209
|
489 { |
1650
|
490 int *new_data = new int [l]; |
1560
|
491 |
1650
|
492 for (int ii = 0; ii < l; ii++) |
|
493 new_data[ii] = d[ii]; |
1560
|
494 |
|
495 return new_data; |
|
496 } |
|
497 |
|
498 int |
2356
|
499 IDX_VEC_REP::is_colon_equiv (int n, int sort_uniq) |
1560
|
500 { |
|
501 if (! colon_equiv_checked) |
|
502 { |
|
503 if (colon) |
|
504 { |
|
505 colon_equiv = 1; |
|
506 } |
1595
|
507 else if (len > 0 && len > 1 && ! one_zero) |
1560
|
508 { |
|
509 int *tmp_data = copy_data (data, len); |
|
510 |
2356
|
511 int tmp_len = len; |
1560
|
512 |
2356
|
513 if (sort_uniq) |
|
514 { |
|
515 sort_data (tmp_data, len); |
|
516 |
|
517 tmp_len = make_uniq (tmp_data, len); |
|
518 } |
1560
|
519 |
|
520 colon_equiv = ((tmp_len == 0 && n == 0) |
|
521 || (tmp_len == n |
|
522 && tmp_data[0] == 0 |
|
523 && tmp_data[tmp_len-1] == tmp_len - 1)); |
|
524 |
|
525 delete [] tmp_data; |
|
526 } |
|
527 else |
2714
|
528 colon_equiv = (len == 1 && n == 1 && data[0] == 0); |
1560
|
529 |
|
530 colon_equiv_checked = 1; |
|
531 } |
|
532 |
|
533 return colon_equiv; |
209
|
534 } |
|
535 |
417
|
536 void |
1560
|
537 IDX_VEC_REP::shorten (int n) |
434
|
538 { |
|
539 if (n > 0 && n <= len) |
|
540 len = n; |
|
541 else |
1560
|
542 (*current_liboctave_error_handler) |
|
543 ("idx_vector::shorten: internal error!"); |
434
|
544 } |
|
545 |
1
|
546 ostream& |
1560
|
547 IDX_VEC_REP::print (ostream& os) const |
|
548 { |
1650
|
549 for (int ii = 0; ii < len; ii++) |
|
550 os << data[ii] << "\n"; |
1560
|
551 return os; |
|
552 } |
|
553 |
|
554 int |
2830
|
555 IDX_VEC_REP::freeze (int z_len, const char *tag, int resize_ok) |
1
|
556 { |
1560
|
557 if (frozen) |
|
558 { |
|
559 assert (frozen_at_z_len == z_len); |
|
560 return frozen_len; |
|
561 } |
|
562 |
|
563 frozen_len = -1; |
|
564 |
|
565 if (colon) |
|
566 frozen_len = z_len; |
|
567 else |
|
568 { |
|
569 if (len == 0) |
|
570 frozen_len = 0; |
|
571 else |
|
572 { |
2828
|
573 maybe_convert_one_zero_to_idx (z_len); |
1560
|
574 |
1650
|
575 max_val = max (); |
|
576 min_val = min (); |
1560
|
577 |
|
578 if (min_val < 0) |
|
579 { |
|
580 if (tag) |
|
581 (*current_liboctave_error_handler) |
|
582 ("invalid %s index = %d", tag, min_val+1); |
|
583 else |
|
584 (*current_liboctave_error_handler) |
|
585 ("invalid index = %d", min_val+1); |
|
586 |
|
587 initialized = 0; |
|
588 } |
|
589 else if (! resize_ok && max_val >= z_len) |
|
590 { |
|
591 if (tag) |
|
592 (*current_liboctave_error_handler) |
|
593 ("invalid %s index = %d", tag, max_val+1); |
|
594 else |
|
595 (*current_liboctave_error_handler) |
|
596 ("invalid index = %d", max_val+1); |
|
597 |
|
598 initialized = 0; |
|
599 } |
|
600 else |
|
601 frozen_len = length (z_len); |
|
602 } |
|
603 } |
|
604 |
|
605 frozen = 1; |
|
606 frozen_at_z_len = z_len; |
|
607 |
|
608 return frozen_len; |
1
|
609 } |
|
610 |
|
611 /* |
|
612 ;;; Local Variables: *** |
|
613 ;;; mode: C++ *** |
|
614 ;;; End: *** |
|
615 */ |