Mercurial > octave-nkf
annotate liboctave/idx-vector.cc @ 7727:c8da61051ea2
idx-vector.cc: fix for-loop condition
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Mon, 21 Apr 2008 11:51:34 -0400 |
parents | 755bf7ecc29b |
children | ce52af0e4a10 |
rev | line source |
---|---|
1 | 1 /* |
2 | |
7017 | 3 Copyright (C) 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2002, |
4 2003, 2004, 2005, 2006, 2007 John W. Eaton | |
1 | 5 |
6 This file is part of Octave. | |
7 | |
8 Octave is free software; you can redistribute it and/or modify it | |
9 under the terms of the GNU General Public License as published by the | |
7016 | 10 Free Software Foundation; either version 3 of the License, or (at your |
11 option) any later version. | |
1 | 12 |
13 Octave is distributed in the hope that it will be useful, but WITHOUT | |
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
16 for more details. | |
17 | |
18 You should have received a copy of the GNU General Public License | |
7016 | 19 along with Octave; see the file COPYING. If not, see |
20 <http://www.gnu.org/licenses/>. | |
1 | 21 |
22 */ | |
23 | |
240 | 24 #ifdef HAVE_CONFIG_H |
1192 | 25 #include <config.h> |
1 | 26 #endif |
27 | |
1343 | 28 #include <cstdlib> |
29 | |
3503 | 30 #include <iostream> |
164 | 31 |
1352 | 32 #include "Range.h" |
4650 | 33 #include "boolNDArray.h" |
1560 | 34 #include "dColVector.h" |
4650 | 35 #include "dNDArray.h" |
1352 | 36 |
1 | 37 #include "idx-vector.h" |
1560 | 38 #include "lo-error.h" |
2500 | 39 #include "lo-mappers.h" |
1 | 40 |
1560 | 41 #define IDX_VEC_REP idx_vector::idx_vector_rep |
42 | |
43 IDX_VEC_REP::idx_vector_rep (const IDX_VEC_REP& a) | |
4653 | 44 : data (0), len (a.len), num_zeros (a.num_zeros), num_ones (a.num_ones), |
7422 | 45 range_base (a.range_base), range_step (a.range_step), |
4653 | 46 max_val (a.max_val), min_val (a.min_val), |
47 frozen_at_z_len (a.frozen_at_z_len), frozen_len (a.frozen_len), | |
7573
755bf7ecc29b
eliminate one_zero stuff from idx_vector
John W. Eaton <jwe@octave.org>
parents:
7572
diff
changeset
|
48 colon (a.colon), range(a.range), initialized (a.initialized), |
755bf7ecc29b
eliminate one_zero stuff from idx_vector
John W. Eaton <jwe@octave.org>
parents:
7572
diff
changeset
|
49 frozen (a.frozen), colon_equiv_checked (a.colon_equiv_checked), |
4653 | 50 colon_equiv (a.colon_equiv), orig_dims (a.orig_dims) |
1 | 51 { |
52 if (len > 0) | |
53 { | |
7422 | 54 if (! range) |
55 { | |
56 data = new octave_idx_type [len]; | |
57 | |
58 for (octave_idx_type i = 0; i < len; i++) | |
59 data[i] = a.data[i]; | |
60 } | |
1 | 61 } |
62 } | |
63 | |
5275 | 64 octave_idx_type |
4938 | 65 IDX_VEC_REP::tree_to_mat_idx (double x, bool& conversion_error) |
1 | 66 { |
5275 | 67 octave_idx_type retval = -1; |
4732 | 68 |
69 conversion_error = false; | |
70 | |
71 if (D_NINT (x) != x) | |
72 { | |
73 (*current_liboctave_error_handler) | |
74 ("expecting integer index, found %f", x); | |
75 | |
76 conversion_error = true; | |
77 } | |
78 else | |
5275 | 79 retval = static_cast<octave_idx_type> (x - 1); |
4732 | 80 |
81 return retval; | |
3928 | 82 } |
83 | |
2500 | 84 static inline bool |
85 idx_is_inf_or_nan (double x) | |
86 { | |
87 bool retval = false; | |
88 | |
89 if (xisnan (x)) | |
90 { | |
91 (*current_liboctave_error_handler) ("NaN invalid as index"); | |
92 retval = true; | |
93 } | |
94 else if (xisinf (x)) | |
95 { | |
96 (*current_liboctave_error_handler) ("Inf invalid as index"); | |
97 retval = true; | |
98 } | |
99 | |
100 return retval; | |
1 | 101 } |
102 | |
1560 | 103 IDX_VEC_REP::idx_vector_rep (const ColumnVector& v) |
7422 | 104 : data (0), len (v.length ()), num_zeros (0), num_ones (0), |
105 range_base (0), range_step (0), max_val (0), min_val (0), count (1), | |
106 frozen_at_z_len (0), frozen_len (0), colon (0), range(0), | |
7573
755bf7ecc29b
eliminate one_zero stuff from idx_vector
John W. Eaton <jwe@octave.org>
parents:
7572
diff
changeset
|
107 initialized (0), frozen (0), colon_equiv_checked (0), |
7422 | 108 colon_equiv (0), orig_dims (len, 1) |
1 | 109 { |
1560 | 110 if (len == 0) |
1 | 111 { |
191 | 112 initialized = 1; |
1 | 113 return; |
114 } | |
1560 | 115 else |
1 | 116 { |
5275 | 117 data = new octave_idx_type [len]; |
2500 | 118 |
4732 | 119 bool conversion_error = false; |
120 | |
5275 | 121 for (octave_idx_type i = 0; i < len; i++) |
2500 | 122 { |
123 double d = v.elem (i); | |
124 | |
125 if (idx_is_inf_or_nan (d)) | |
126 return; | |
127 else | |
4732 | 128 data[i] = tree_to_mat_idx (d, conversion_error); |
129 | |
130 if (conversion_error) | |
131 return; | |
2500 | 132 } |
1 | 133 } |
1560 | 134 |
135 init_state (); | |
136 } | |
137 | |
4650 | 138 IDX_VEC_REP::idx_vector_rep (const NDArray& nda) |
4653 | 139 : data (0), len (nda.length ()), num_zeros (0), num_ones (0), |
7422 | 140 range_base (0), range_step (0), max_val (0), min_val (0), count (1), |
141 frozen_at_z_len (0), frozen_len (0), colon (0), range(0), | |
7573
755bf7ecc29b
eliminate one_zero stuff from idx_vector
John W. Eaton <jwe@octave.org>
parents:
7572
diff
changeset
|
142 initialized (0), frozen (0), colon_equiv_checked (0), |
7422 | 143 colon_equiv (0), orig_dims (nda.dims ()) |
1560 | 144 { |
145 if (len == 0) | |
1 | 146 { |
1560 | 147 initialized = 1; |
148 return; | |
1 | 149 } |
150 else | |
151 { | |
5275 | 152 octave_idx_type k = 0; |
153 data = new octave_idx_type [len]; | |
2500 | 154 |
4732 | 155 bool conversion_error = false; |
156 | |
5275 | 157 for (octave_idx_type i = 0; i < len; i++) |
4650 | 158 { |
159 double d = nda.elem (i); | |
2500 | 160 |
4650 | 161 if (idx_is_inf_or_nan (d)) |
162 return; | |
163 else | |
4732 | 164 data[k++] = tree_to_mat_idx (d, conversion_error); |
165 | |
166 if (conversion_error) | |
167 return; | |
4650 | 168 } |
1 | 169 } |
170 | |
1560 | 171 init_state (); |
1 | 172 } |
173 | |
1560 | 174 IDX_VEC_REP::idx_vector_rep (const Range& r) |
4653 | 175 : data (0), len (r.nelem ()), num_zeros (0), num_ones (0), |
7422 | 176 range_base (0), range_step (0), max_val (0), min_val (0), |
177 count (1), frozen_at_z_len (0), frozen_len (0), colon (0), | |
7573
755bf7ecc29b
eliminate one_zero stuff from idx_vector
John W. Eaton <jwe@octave.org>
parents:
7572
diff
changeset
|
178 range(1), initialized (0), frozen (0), |
7422 | 179 colon_equiv_checked (0), colon_equiv (0), orig_dims (1, len) |
1 | 180 { |
191 | 181 if (len < 0) |
182 { | |
1560 | 183 (*current_liboctave_error_handler) ("invalid range used as index"); |
191 | 184 return; |
185 } | |
186 else if (len == 0) | |
187 { | |
188 initialized = 1; | |
189 return; | |
190 } | |
1 | 191 |
6457 | 192 if (r.all_elements_are_ints ()) |
193 { | |
7422 | 194 range_base = static_cast<octave_idx_type> (r.base () - 1); |
195 range_step = static_cast<octave_idx_type> (r.inc ()); | |
4732 | 196 |
7422 | 197 init_state (); |
1 | 198 } |
6457 | 199 else |
200 (*current_liboctave_error_handler) | |
201 ("expecting integer index, found non integer Range"); | |
1 | 202 } |
203 | |
3928 | 204 IDX_VEC_REP::idx_vector_rep (double d) |
4653 | 205 : data (0), len (1), num_zeros (0), num_ones (0), |
7422 | 206 range_base (0), range_step (0), max_val (0), min_val (0), |
207 count (1), frozen_at_z_len (0), frozen_len (0), colon (0), | |
7573
755bf7ecc29b
eliminate one_zero stuff from idx_vector
John W. Eaton <jwe@octave.org>
parents:
7572
diff
changeset
|
208 range(1), initialized (0), frozen (0), colon_equiv_checked (0), |
755bf7ecc29b
eliminate one_zero stuff from idx_vector
John W. Eaton <jwe@octave.org>
parents:
7572
diff
changeset
|
209 colon_equiv (0), orig_dims (1, 1) |
3928 | 210 { |
211 if (idx_is_inf_or_nan (d)) | |
212 return; | |
213 else | |
214 { | |
4732 | 215 bool conversion_error = false; |
216 | |
7422 | 217 range_base = tree_to_mat_idx (d, conversion_error); |
218 range_step = 1; | |
4732 | 219 |
220 if (conversion_error) | |
221 return; | |
3928 | 222 } |
223 | |
224 init_state (); | |
225 } | |
226 | |
5275 | 227 IDX_VEC_REP::idx_vector_rep (octave_idx_type i) |
4653 | 228 : data (0), len (1), num_zeros (0), num_ones (0), |
7422 | 229 range_base (tree_to_mat_idx (i)), range_step (1), |
4653 | 230 max_val (0), min_val (0), count (1), frozen_at_z_len (0), |
7573
755bf7ecc29b
eliminate one_zero stuff from idx_vector
John W. Eaton <jwe@octave.org>
parents:
7572
diff
changeset
|
231 frozen_len (0), colon (0), range(1), initialized (0), |
755bf7ecc29b
eliminate one_zero stuff from idx_vector
John W. Eaton <jwe@octave.org>
parents:
7572
diff
changeset
|
232 frozen (0), colon_equiv_checked (0), colon_equiv (0), orig_dims (1, 1) |
3928 | 233 { |
234 init_state (); | |
235 } | |
236 | |
1560 | 237 IDX_VEC_REP::idx_vector_rep (char c) |
7422 | 238 : data (0), len (0), num_zeros (0), num_ones (0), range_base (0), |
239 range_step (0), max_val (0), min_val (0), count (1), | |
240 frozen_at_z_len (0), frozen_len (0), colon (1), range(0), | |
7573
755bf7ecc29b
eliminate one_zero stuff from idx_vector
John W. Eaton <jwe@octave.org>
parents:
7572
diff
changeset
|
241 initialized (0), frozen (0), colon_equiv_checked (0), |
7422 | 242 colon_equiv (0), orig_dims (0, 0) |
1560 | 243 { |
244 assert (c == ':'); | |
245 | |
246 init_state (); | |
247 } | |
248 | |
2828 | 249 IDX_VEC_REP::idx_vector_rep (bool b) |
7572
85da2ab0c6fd
logical indexing compatibility fixes
John W. Eaton <jwe@octave.org>
parents:
7425
diff
changeset
|
250 : data (0), len (b ? 1 : 0), num_zeros (0), num_ones (0), range_base (0), |
7422 | 251 range_step (0), max_val (0), min_val (0), count (1), |
252 frozen_at_z_len (0), frozen_len (0), colon (0), range(0), | |
7573
755bf7ecc29b
eliminate one_zero stuff from idx_vector
John W. Eaton <jwe@octave.org>
parents:
7572
diff
changeset
|
253 initialized (0), frozen (0), colon_equiv_checked (0), |
7572
85da2ab0c6fd
logical indexing compatibility fixes
John W. Eaton <jwe@octave.org>
parents:
7425
diff
changeset
|
254 colon_equiv (0), orig_dims (len, len) |
2828 | 255 { |
7572
85da2ab0c6fd
logical indexing compatibility fixes
John W. Eaton <jwe@octave.org>
parents:
7425
diff
changeset
|
256 if (len == 0) |
85da2ab0c6fd
logical indexing compatibility fixes
John W. Eaton <jwe@octave.org>
parents:
7425
diff
changeset
|
257 initialized = 1; |
85da2ab0c6fd
logical indexing compatibility fixes
John W. Eaton <jwe@octave.org>
parents:
7425
diff
changeset
|
258 else |
85da2ab0c6fd
logical indexing compatibility fixes
John W. Eaton <jwe@octave.org>
parents:
7425
diff
changeset
|
259 { |
85da2ab0c6fd
logical indexing compatibility fixes
John W. Eaton <jwe@octave.org>
parents:
7425
diff
changeset
|
260 data = new octave_idx_type [len]; |
85da2ab0c6fd
logical indexing compatibility fixes
John W. Eaton <jwe@octave.org>
parents:
7425
diff
changeset
|
261 data[0] = 0; |
85da2ab0c6fd
logical indexing compatibility fixes
John W. Eaton <jwe@octave.org>
parents:
7425
diff
changeset
|
262 init_state (); |
85da2ab0c6fd
logical indexing compatibility fixes
John W. Eaton <jwe@octave.org>
parents:
7425
diff
changeset
|
263 } |
2828 | 264 } |
265 | |
4650 | 266 IDX_VEC_REP::idx_vector_rep (const boolNDArray& bnda) |
7572
85da2ab0c6fd
logical indexing compatibility fixes
John W. Eaton <jwe@octave.org>
parents:
7425
diff
changeset
|
267 : data (0), len (bnda.nnz ()), num_zeros (0), num_ones (0), |
7422 | 268 range_base (0), range_step (0), max_val (0), min_val (0), |
269 count (1), frozen_at_z_len (0), frozen_len (0), colon (0), | |
7573
755bf7ecc29b
eliminate one_zero stuff from idx_vector
John W. Eaton <jwe@octave.org>
parents:
7572
diff
changeset
|
270 range(0), initialized (0), frozen (0), |
7572
85da2ab0c6fd
logical indexing compatibility fixes
John W. Eaton <jwe@octave.org>
parents:
7425
diff
changeset
|
271 colon_equiv_checked (0), colon_equiv (0), orig_dims () |
2828 | 272 { |
273 if (len == 0) | |
274 { | |
7572
85da2ab0c6fd
logical indexing compatibility fixes
John W. Eaton <jwe@octave.org>
parents:
7425
diff
changeset
|
275 orig_dims = dim_vector (0, 0); |
2828 | 276 initialized = 1; |
277 } | |
278 else | |
279 { | |
5275 | 280 data = new octave_idx_type [len]; |
2828 | 281 |
7572
85da2ab0c6fd
logical indexing compatibility fixes
John W. Eaton <jwe@octave.org>
parents:
7425
diff
changeset
|
282 octave_idx_type ntot = bnda.length (); |
85da2ab0c6fd
logical indexing compatibility fixes
John W. Eaton <jwe@octave.org>
parents:
7425
diff
changeset
|
283 |
7727
c8da61051ea2
idx-vector.cc: fix for-loop condition
John W. Eaton <jwe@octave.org>
parents:
7573
diff
changeset
|
284 for (octave_idx_type i = 0, k = 0; i < ntot && k < len; i++) |
7572
85da2ab0c6fd
logical indexing compatibility fixes
John W. Eaton <jwe@octave.org>
parents:
7425
diff
changeset
|
285 if (bnda.elem (i)) |
85da2ab0c6fd
logical indexing compatibility fixes
John W. Eaton <jwe@octave.org>
parents:
7425
diff
changeset
|
286 data[k++] = i; |
85da2ab0c6fd
logical indexing compatibility fixes
John W. Eaton <jwe@octave.org>
parents:
7425
diff
changeset
|
287 |
85da2ab0c6fd
logical indexing compatibility fixes
John W. Eaton <jwe@octave.org>
parents:
7425
diff
changeset
|
288 dim_vector dv = bnda.dims (); |
85da2ab0c6fd
logical indexing compatibility fixes
John W. Eaton <jwe@octave.org>
parents:
7425
diff
changeset
|
289 |
85da2ab0c6fd
logical indexing compatibility fixes
John W. Eaton <jwe@octave.org>
parents:
7425
diff
changeset
|
290 orig_dims = ((dv.length () == 2 && dv(0) == 1) |
85da2ab0c6fd
logical indexing compatibility fixes
John W. Eaton <jwe@octave.org>
parents:
7425
diff
changeset
|
291 ? dim_vector (1, len) : orig_dims = dim_vector (len, 1)); |
85da2ab0c6fd
logical indexing compatibility fixes
John W. Eaton <jwe@octave.org>
parents:
7425
diff
changeset
|
292 |
85da2ab0c6fd
logical indexing compatibility fixes
John W. Eaton <jwe@octave.org>
parents:
7425
diff
changeset
|
293 init_state (); |
2828 | 294 } |
295 } | |
296 | |
1560 | 297 IDX_VEC_REP& |
298 IDX_VEC_REP::operator = (const IDX_VEC_REP& a) | |
1 | 299 { |
300 if (this != &a) | |
301 { | |
302 delete [] data; | |
303 len = a.len; | |
7422 | 304 |
305 if (a.data) | |
306 { | |
307 data = new octave_idx_type [len]; | |
308 | |
309 for (octave_idx_type i = 0; i < len; i++) | |
310 data[i] = a.data[i]; | |
311 } | |
312 else | |
313 data = 0; | |
1 | 314 |
315 num_zeros = a.num_zeros; | |
316 num_ones = a.num_ones; | |
7425 | 317 range_base = a.range_base; |
318 range_step = a.range_step; | |
1 | 319 max_val = a.max_val; |
320 min_val = a.min_val; | |
4653 | 321 frozen_at_z_len = a.frozen_at_z_len; |
322 frozen_len = a.frozen_len; | |
323 colon = a.colon; | |
7422 | 324 range = a.range; |
4653 | 325 initialized = a.initialized; |
326 frozen = a.frozen; | |
327 colon_equiv_checked = a.colon_equiv_checked; | |
328 colon_equiv = a.colon_equiv; | |
329 orig_dims = a.orig_dims; | |
1 | 330 } |
4653 | 331 |
1 | 332 return *this; |
333 } | |
334 | |
335 void | |
1560 | 336 IDX_VEC_REP::init_state (void) |
1 | 337 { |
338 num_zeros = 0; | |
339 num_ones = 0; | |
340 | |
1560 | 341 if (colon) |
1 | 342 { |
2828 | 343 min_val = 0; |
344 max_val = 0; | |
1 | 345 } |
7422 | 346 else if (range) |
347 { | |
348 if (range_step >= 0) | |
349 { | |
350 min_val = range_base; | |
351 max_val = (len > 0) ? range_base + (len-1)*range_step : range_base; | |
352 } | |
353 else | |
354 { | |
355 max_val = range_base; | |
356 min_val = (len > 0) ? range_base + (len-1)*range_step : range_base; | |
357 } | |
358 | |
359 if ((range_base <= 0 && range_step > 0) | |
360 || (range_base >= 0 && range_step < 0)) | |
361 num_zeros = 1; | |
362 | |
363 if ((range_base <= 1 && range_step > 0) | |
364 || (range_base >= 1 && range_step < 0)) | |
365 num_zeros = 0; | |
366 } | |
1 | 367 else |
368 { | |
369 min_val = max_val = data[0]; | |
370 | |
5275 | 371 octave_idx_type i = 0; |
1 | 372 do |
373 { | |
1560 | 374 if (data[i] == -1) |
375 num_zeros++; | |
376 else if (data[i] == 0) | |
377 num_ones++; | |
378 | |
1 | 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 } | |
1560 | 387 |
388 initialized = 1; | |
389 } | |
390 | |
5275 | 391 octave_idx_type |
392 IDX_VEC_REP::checkelem (octave_idx_type n) const | |
227 | 393 { |
394 if (n < 0 || n >= len) | |
395 { | |
1560 | 396 (*current_liboctave_error_handler) ("idx-vector: index out of range"); |
227 | 397 return 0; |
398 } | |
399 | |
400 return elem (n); | |
401 } | |
402 | |
1552 | 403 static inline int |
3262 | 404 intcmp (const void *ii, const void *jj) |
1552 | 405 { |
5275 | 406 return (*(static_cast<const octave_idx_type *> (ii)) - *(static_cast<const octave_idx_type *> (jj))); |
1552 | 407 } |
408 | |
1560 | 409 static inline void |
5275 | 410 sort_data (octave_idx_type *d, octave_idx_type l) |
1560 | 411 { |
5275 | 412 qsort (d, l, sizeof (octave_idx_type), intcmp); |
1560 | 413 } |
414 | |
5275 | 415 static inline octave_idx_type |
416 make_uniq (octave_idx_type *d, octave_idx_type l) | |
1560 | 417 { |
3125 | 418 if (l < 2) |
419 return l; | |
420 | |
5275 | 421 octave_idx_type k = 0; |
422 for (octave_idx_type ii = 1; ii < l; ii++) | |
1560 | 423 { |
1650 | 424 if (d[ii] != d[k]) |
1560 | 425 { |
426 k++; | |
1650 | 427 d[k] = d[ii]; |
1560 | 428 } |
429 } | |
430 return k+1; | |
431 } | |
432 | |
5275 | 433 static inline octave_idx_type * |
434 copy_data (const octave_idx_type *d, octave_idx_type l) | |
209 | 435 { |
5275 | 436 octave_idx_type *new_data = new octave_idx_type [l]; |
1560 | 437 |
5275 | 438 for (octave_idx_type ii = 0; ii < l; ii++) |
1650 | 439 new_data[ii] = d[ii]; |
1560 | 440 |
441 return new_data; | |
442 } | |
443 | |
444 int | |
5275 | 445 IDX_VEC_REP::is_colon_equiv (octave_idx_type n, int sort_uniq) |
1560 | 446 { |
447 if (! colon_equiv_checked) | |
448 { | |
449 if (colon) | |
450 { | |
451 colon_equiv = 1; | |
452 } | |
7422 | 453 else if (range) |
454 { | |
455 colon_equiv = (range_base == 0 | |
456 && len == n | |
457 && (range_step == 1 | |
458 || (range_step == -1 && sort_uniq))); | |
459 } | |
5275 | 460 else if (static_cast<octave_idx_type> (len) > 1) |
1560 | 461 { |
7573
755bf7ecc29b
eliminate one_zero stuff from idx_vector
John W. Eaton <jwe@octave.org>
parents:
7572
diff
changeset
|
462 if (sort_uniq) |
2356 | 463 { |
5275 | 464 octave_idx_type *tmp_data = copy_data (data, len); |
2966 | 465 |
2356 | 466 sort_data (tmp_data, len); |
467 | |
5275 | 468 octave_idx_type tmp_len = make_uniq (tmp_data, len); |
2966 | 469 |
470 colon_equiv = (tmp_len == n | |
471 && tmp_data[0] == 0 | |
472 && tmp_data[tmp_len-1] == tmp_len - 1); | |
1560 | 473 |
2966 | 474 delete [] tmp_data; |
475 } | |
476 else | |
477 { | |
478 if (len == n) | |
479 { | |
480 colon_equiv = 1; | |
1560 | 481 |
5275 | 482 for (octave_idx_type ii = 0; ii < n; ii++) |
2966 | 483 if (data[ii] != ii) |
484 { | |
485 colon_equiv = 0; | |
486 break; | |
487 } | |
488 } | |
489 } | |
1560 | 490 } |
491 else | |
2966 | 492 colon_equiv = (len == n && (n == 0 || (n == 1 && data[0] == 0))); |
1560 | 493 |
494 colon_equiv_checked = 1; | |
495 } | |
496 | |
497 return colon_equiv; | |
209 | 498 } |
499 | |
417 | 500 void |
3079 | 501 IDX_VEC_REP::sort (bool uniq) |
502 { | |
7422 | 503 if (range && len) |
504 { | |
505 if (range_step < 0) | |
506 { | |
507 range_base += (len-1)*(range_step); | |
508 range_step = -range_step; | |
509 } | |
510 } | |
511 else if (len > 1) | |
3125 | 512 { |
513 sort_data (data, len); | |
3079 | 514 |
3125 | 515 if (uniq) |
516 len = make_uniq (data, len); | |
517 } | |
3079 | 518 } |
519 | |
520 void | |
5275 | 521 IDX_VEC_REP::shorten (octave_idx_type n) |
434 | 522 { |
523 if (n > 0 && n <= len) | |
524 len = n; | |
525 else | |
1560 | 526 (*current_liboctave_error_handler) |
527 ("idx_vector::shorten: internal error!"); | |
434 | 528 } |
529 | |
3504 | 530 std::ostream& |
531 IDX_VEC_REP::print (std::ostream& os) const | |
1560 | 532 { |
7425 | 533 if (colon) |
534 os << "colon" << std::endl; | |
535 else if (range) | |
536 os << "range_base: " << range_base | |
537 << ", range_step: " << range_step << std::endl; | |
538 else | |
539 { | |
540 for (octave_idx_type ii = 0; ii < len; ii++) | |
541 os << data[ii] << "\n"; | |
542 } | |
543 | |
1560 | 544 return os; |
545 } | |
546 | |
5275 | 547 octave_idx_type |
5781 | 548 IDX_VEC_REP::freeze (octave_idx_type z_len, const char *tag, bool resize_ok) |
1 | 549 { |
1560 | 550 if (frozen) |
3680 | 551 return frozen_len; |
1560 | 552 |
553 frozen_len = -1; | |
554 | |
555 if (colon) | |
556 frozen_len = z_len; | |
557 else | |
558 { | |
559 if (len == 0) | |
560 frozen_len = 0; | |
561 else | |
562 { | |
1650 | 563 max_val = max (); |
564 min_val = min (); | |
1560 | 565 |
566 if (min_val < 0) | |
567 { | |
568 if (tag) | |
569 (*current_liboctave_error_handler) | |
570 ("invalid %s index = %d", tag, min_val+1); | |
571 else | |
572 (*current_liboctave_error_handler) | |
573 ("invalid index = %d", min_val+1); | |
574 | |
575 initialized = 0; | |
576 } | |
577 else if (! resize_ok && max_val >= z_len) | |
578 { | |
579 if (tag) | |
580 (*current_liboctave_error_handler) | |
581 ("invalid %s index = %d", tag, max_val+1); | |
582 else | |
583 (*current_liboctave_error_handler) | |
584 ("invalid index = %d", max_val+1); | |
585 | |
586 initialized = 0; | |
587 } | |
588 else | |
4461 | 589 { |
5781 | 590 if (max_val >= z_len) |
4461 | 591 { |
592 if (tag) | |
5781 | 593 (*current_liboctave_warning_with_id_handler) |
594 ("Octave:resize-on-range-error", | |
595 "resizing object with %s index = %d out of bounds", | |
4461 | 596 tag, max_val+1); |
597 else | |
5781 | 598 (*current_liboctave_warning_with_id_handler) |
599 ("Octave:resize-on-range-error", | |
600 "resizing object with index = %d out of bounds", | |
4461 | 601 max_val+1); |
602 } | |
603 | |
604 frozen_len = length (z_len); | |
605 } | |
1560 | 606 } |
607 } | |
608 | |
609 frozen = 1; | |
3680 | 610 |
611 frozen_at_z_len = z_len ? z_len : len; | |
1560 | 612 |
613 return frozen_len; | |
1 | 614 } |
615 | |
616 /* | |
617 ;;; Local Variables: *** | |
618 ;;; mode: C++ *** | |
619 ;;; End: *** | |
620 */ |