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 |
1560
|
152 IDX_VEC_REP::idx_vector_rep (const Range& r) |
1
|
153 { |
1135
|
154 data = 0; |
191
|
155 initialized = 0; |
1560
|
156 frozen = 0; |
|
157 colon_equiv_checked = 0; |
|
158 colon_equiv = 0; |
|
159 colon = 0; |
191
|
160 |
1
|
161 len = r.nelem (); |
|
162 |
1560
|
163 orig_nr = 1; |
|
164 orig_nc = len; |
|
165 |
191
|
166 if (len < 0) |
|
167 { |
1560
|
168 (*current_liboctave_error_handler) ("invalid range used as index"); |
191
|
169 return; |
|
170 } |
|
171 else if (len == 0) |
|
172 { |
|
173 num_zeros = 0; |
|
174 num_ones = 0; |
|
175 one_zero = 0; |
1560
|
176 max_val = 0; |
|
177 min_val = 0; |
191
|
178 initialized = 1; |
|
179 return; |
|
180 } |
1
|
181 |
|
182 double b = r.base (); |
|
183 double step = r.inc (); |
|
184 |
|
185 data = new int [len]; |
|
186 |
|
187 for (int i = 0; i < len; i++) |
|
188 { |
|
189 double val = b + i * step; |
|
190 data[i] = tree_to_mat_idx (val); |
|
191 } |
|
192 |
|
193 init_state (); |
|
194 } |
|
195 |
1560
|
196 IDX_VEC_REP::idx_vector_rep (char c) |
|
197 { |
|
198 assert (c == ':'); |
|
199 |
|
200 colon = 1; |
|
201 len = 0; |
|
202 num_zeros = 0; |
|
203 num_ones = 0; |
|
204 one_zero = 0; |
|
205 initialized = 0; |
|
206 frozen = 0; |
|
207 colon_equiv_checked = 0; |
|
208 colon_equiv = 0; |
|
209 data = 0; |
|
210 |
|
211 init_state (); |
|
212 } |
|
213 |
|
214 IDX_VEC_REP& |
|
215 IDX_VEC_REP::operator = (const IDX_VEC_REP& a) |
1
|
216 { |
|
217 if (this != &a) |
|
218 { |
191
|
219 initialized = a.initialized; |
1560
|
220 frozen = a.frozen; |
|
221 colon_equiv_checked = a.colon_equiv_checked; |
|
222 colon_equiv = a.colon_equiv; |
|
223 |
|
224 colon = a.colon; |
|
225 |
|
226 orig_nr = a.orig_nr; |
|
227 orig_nc = a.orig_nc; |
191
|
228 |
1
|
229 delete [] data; |
|
230 len = a.len; |
|
231 data = new int [len]; |
|
232 for (int i = 0; i < len; i++) |
|
233 data[i] = a.data[i]; |
|
234 |
|
235 num_zeros = a.num_zeros; |
|
236 num_ones = a.num_ones; |
|
237 one_zero = a.one_zero; |
|
238 |
|
239 max_val = a.max_val; |
|
240 min_val = a.min_val; |
|
241 } |
|
242 return *this; |
|
243 } |
|
244 |
|
245 void |
1560
|
246 IDX_VEC_REP::init_state (void) |
1
|
247 { |
|
248 num_zeros = 0; |
|
249 num_ones = 0; |
|
250 |
1560
|
251 if (colon) |
1
|
252 { |
1560
|
253 one_zero = 0; |
|
254 min_val = max_val = 0; |
1
|
255 } |
|
256 else |
|
257 { |
1560
|
258 one_zero = 1; |
1
|
259 |
|
260 min_val = max_val = data[0]; |
|
261 |
1321
|
262 int i = 0; |
1
|
263 do |
|
264 { |
1560
|
265 if (data[i] == -1) |
|
266 num_zeros++; |
|
267 else if (data[i] == 0) |
|
268 num_ones++; |
|
269 |
|
270 if (one_zero && data[i] != -1 && data[i] != 0) |
|
271 one_zero = 0; |
|
272 |
1
|
273 if (data[i] > max_val) |
|
274 max_val = data[i]; |
|
275 |
|
276 if (data[i] < min_val) |
|
277 min_val = data[i]; |
|
278 } |
|
279 while (++i < len); |
|
280 } |
1560
|
281 |
|
282 initialized = 1; |
|
283 } |
|
284 |
|
285 void |
|
286 IDX_VEC_REP::maybe_convert_one_zero_to_idx (int z_len, int prefer_zero_one) |
|
287 { |
|
288 if (one_zero && z_len == len |
|
289 && (num_ones != len || prefer_zero_one)) |
|
290 { |
|
291 if (num_ones == 0) |
|
292 { |
|
293 len = 0; |
|
294 max_val = 0; |
|
295 min_val = 0; |
|
296 delete [] data; |
|
297 data = 0; |
|
298 } |
|
299 else |
|
300 { |
|
301 assert (num_ones + num_zeros == len); |
|
302 |
|
303 int *new_data = new int [num_ones]; |
1650
|
304 int k = 0; |
1560
|
305 for (int i = 0; i < len; i++) |
|
306 if (data[i] == 0) |
1650
|
307 new_data[k++] = i; |
1560
|
308 |
|
309 delete [] data; |
|
310 len = num_ones; |
|
311 data = new_data; |
|
312 |
|
313 min_val = max_val = data[0]; |
|
314 |
|
315 int i = 0; |
|
316 do |
|
317 { |
|
318 if (data[i] > max_val) |
|
319 max_val = data[i]; |
|
320 |
|
321 if (data[i] < min_val) |
|
322 min_val = data[i]; |
|
323 } |
|
324 while (++i < len); |
|
325 } |
|
326 } |
1
|
327 } |
|
328 |
227
|
329 int |
1560
|
330 IDX_VEC_REP::checkelem (int n) const |
227
|
331 { |
|
332 if (n < 0 || n >= len) |
|
333 { |
1560
|
334 (*current_liboctave_error_handler) ("idx-vector: index out of range"); |
227
|
335 return 0; |
|
336 } |
|
337 |
|
338 return elem (n); |
|
339 } |
|
340 |
1552
|
341 static inline int |
1650
|
342 intcmp (int *ii, int *jj) |
1552
|
343 { |
1650
|
344 return (*ii - *jj); |
1552
|
345 } |
|
346 |
1560
|
347 static inline void |
1650
|
348 sort_data (int *d, int l) |
1560
|
349 { |
1650
|
350 qsort ((void *) d, l, sizeof (int), |
1560
|
351 (int (*)(const void*, const void*)) intcmp); |
|
352 } |
|
353 |
|
354 static inline int |
1650
|
355 make_uniq (int *d, int l) |
1560
|
356 { |
|
357 int k = 0; |
1759
|
358 for (int ii = 1; ii < l; ii++) |
1560
|
359 { |
1650
|
360 if (d[ii] != d[k]) |
1560
|
361 { |
|
362 k++; |
1650
|
363 d[k] = d[ii]; |
1560
|
364 } |
|
365 } |
|
366 return k+1; |
|
367 } |
|
368 |
|
369 static inline int * |
1650
|
370 copy_data (const int *d, int l) |
209
|
371 { |
1650
|
372 int *new_data = new int [l]; |
1560
|
373 |
1650
|
374 for (int ii = 0; ii < l; ii++) |
|
375 new_data[ii] = d[ii]; |
1560
|
376 |
|
377 return new_data; |
|
378 } |
|
379 |
|
380 int |
|
381 IDX_VEC_REP::is_colon_equiv (int n, int sort) |
|
382 { |
|
383 if (! colon_equiv_checked) |
|
384 { |
|
385 if (colon) |
|
386 { |
|
387 colon_equiv = 1; |
|
388 } |
1595
|
389 else if (len > 0 && len > 1 && ! one_zero) |
1560
|
390 { |
|
391 int *tmp_data = copy_data (data, len); |
|
392 |
|
393 if (sort) |
|
394 sort_data (tmp_data, len); |
|
395 |
|
396 int tmp_len = make_uniq (tmp_data, len); |
|
397 |
|
398 colon_equiv = ((tmp_len == 0 && n == 0) |
|
399 || (tmp_len == n |
|
400 && tmp_data[0] == 0 |
|
401 && tmp_data[tmp_len-1] == tmp_len - 1)); |
|
402 |
|
403 delete [] tmp_data; |
|
404 } |
|
405 else |
|
406 colon_equiv = 0; |
|
407 |
|
408 colon_equiv_checked = 1; |
|
409 } |
|
410 |
|
411 return colon_equiv; |
209
|
412 } |
|
413 |
417
|
414 void |
1560
|
415 IDX_VEC_REP::shorten (int n) |
434
|
416 { |
|
417 if (n > 0 && n <= len) |
|
418 len = n; |
|
419 else |
1560
|
420 (*current_liboctave_error_handler) |
|
421 ("idx_vector::shorten: internal error!"); |
434
|
422 } |
|
423 |
1
|
424 ostream& |
1560
|
425 IDX_VEC_REP::print (ostream& os) const |
|
426 { |
1650
|
427 for (int ii = 0; ii < len; ii++) |
|
428 os << data[ii] << "\n"; |
1560
|
429 return os; |
|
430 } |
|
431 |
|
432 int |
|
433 IDX_VEC_REP::freeze (int z_len, const char *tag, |
|
434 int prefer_zero_one, int resize_ok) |
1
|
435 { |
1560
|
436 if (frozen) |
|
437 { |
|
438 assert (frozen_at_z_len == z_len); |
|
439 return frozen_len; |
|
440 } |
|
441 |
|
442 frozen_len = -1; |
|
443 |
|
444 if (colon) |
|
445 frozen_len = z_len; |
|
446 else |
|
447 { |
|
448 if (len == 0) |
|
449 frozen_len = 0; |
|
450 else |
|
451 { |
|
452 maybe_convert_one_zero_to_idx (z_len, prefer_zero_one); |
|
453 |
1650
|
454 max_val = max (); |
|
455 min_val = min (); |
1560
|
456 |
|
457 if (min_val < 0) |
|
458 { |
|
459 if (tag) |
|
460 (*current_liboctave_error_handler) |
|
461 ("invalid %s index = %d", tag, min_val+1); |
|
462 else |
|
463 (*current_liboctave_error_handler) |
|
464 ("invalid index = %d", min_val+1); |
|
465 |
|
466 initialized = 0; |
|
467 } |
|
468 else if (! resize_ok && max_val >= z_len) |
|
469 { |
|
470 if (tag) |
|
471 (*current_liboctave_error_handler) |
|
472 ("invalid %s index = %d", tag, max_val+1); |
|
473 else |
|
474 (*current_liboctave_error_handler) |
|
475 ("invalid index = %d", max_val+1); |
|
476 |
|
477 initialized = 0; |
|
478 } |
|
479 else |
|
480 frozen_len = length (z_len); |
|
481 } |
|
482 } |
|
483 |
|
484 frozen = 1; |
|
485 frozen_at_z_len = z_len; |
|
486 |
|
487 return frozen_len; |
1
|
488 } |
|
489 |
|
490 /* |
|
491 ;;; Local Variables: *** |
|
492 ;;; mode: C++ *** |
|
493 ;;; End: *** |
|
494 */ |