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