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