Mercurial > hg > octave-nkf
annotate liboctave/Array.cc @ 7597:6b2a99e44ff2
shortened empty indexing fix
author | David Bateman <dbateman@free.fr> |
---|---|
date | Tue, 18 Mar 2008 15:39:13 -0400 |
parents | 755bf7ecc29b |
children | 48488cca0006 |
rev | line source |
---|---|
1993 | 1 // Template array classes |
237 | 2 /* |
3 | |
7017 | 4 Copyright (C) 1993, 1994, 1995, 1996, 1997, 2000, 2002, 2003, 2004, |
5 2005, 2006, 2007 John W. Eaton | |
237 | 6 |
7 This file is part of Octave. | |
8 | |
9 Octave is free software; you can redistribute it and/or modify it | |
10 under the terms of the GNU General Public License as published by the | |
7016 | 11 Free Software Foundation; either version 3 of the License, or (at your |
12 option) any later version. | |
237 | 13 |
14 Octave is distributed in the hope that it will be useful, but WITHOUT | |
15 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
16 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
17 for more details. | |
18 | |
19 You should have received a copy of the GNU General Public License | |
7016 | 20 along with Octave; see the file COPYING. If not, see |
21 <http://www.gnu.org/licenses/>. | |
237 | 22 |
23 */ | |
24 | |
25 #ifdef HAVE_CONFIG_H | |
1192 | 26 #include <config.h> |
237 | 27 #endif |
28 | |
1367 | 29 #include <cassert> |
4518 | 30 #include <climits> |
449 | 31 |
3503 | 32 #include <iostream> |
5765 | 33 #include <sstream> |
5607 | 34 #include <vector> |
6674 | 35 #include <new> |
1560 | 36 |
237 | 37 #include "Array.h" |
4588 | 38 #include "Array-util.h" |
1560 | 39 #include "idx-vector.h" |
40 #include "lo-error.h" | |
41 | |
1360 | 42 // One dimensional array class. Handles the reference counting for |
43 // all the derived classes. | |
237 | 44 |
45 template <class T> | |
4834 | 46 Array<T>::Array (const Array<T>& a, const dim_vector& dv) |
47 : rep (a.rep), dimensions (dv), idx (0), idx_count (0) | |
48 { | |
49 rep->count++; | |
50 | |
51 if (a.numel () < dv.numel ()) | |
52 (*current_liboctave_error_handler) | |
53 ("Array::Array (const Array&, const dim_vector&): dimension mismatch"); | |
54 } | |
55 | |
56 template <class T> | |
1619 | 57 Array<T>::~Array (void) |
58 { | |
59 if (--rep->count <= 0) | |
60 delete rep; | |
61 | |
62 delete [] idx; | |
4513 | 63 } |
64 | |
4532 | 65 template <class T> |
66 Array<T> | |
67 Array<T>::squeeze (void) const | |
68 { | |
69 Array<T> retval = *this; | |
70 | |
4929 | 71 if (ndims () > 2) |
4532 | 72 { |
4929 | 73 bool dims_changed = false; |
74 | |
75 dim_vector new_dimensions = dimensions; | |
76 | |
77 int k = 0; | |
78 | |
79 for (int i = 0; i < ndims (); i++) | |
4759 | 80 { |
4929 | 81 if (dimensions(i) == 1) |
82 dims_changed = true; | |
83 else | |
84 new_dimensions(k++) = dimensions(i); | |
85 } | |
86 | |
87 if (dims_changed) | |
88 { | |
89 switch (k) | |
90 { | |
91 case 0: | |
92 new_dimensions = dim_vector (1, 1); | |
93 break; | |
94 | |
95 case 1: | |
4759 | 96 { |
5275 | 97 octave_idx_type tmp = new_dimensions(0); |
4929 | 98 |
99 new_dimensions.resize (2); | |
100 | |
4759 | 101 new_dimensions(0) = tmp; |
102 new_dimensions(1) = 1; | |
103 } | |
4929 | 104 break; |
105 | |
106 default: | |
107 new_dimensions.resize (k); | |
108 break; | |
109 } | |
4759 | 110 } |
4532 | 111 |
5775 | 112 // FIXME -- it would be better if we did not have to do |
5047 | 113 // this, so we could share the data while still having different |
114 // dimension vectors. | |
115 | |
4532 | 116 retval.make_unique (); |
117 | |
118 retval.dimensions = new_dimensions; | |
119 } | |
120 | |
121 return retval; | |
122 } | |
123 | |
6674 | 124 // KLUGE |
125 | |
126 // The following get_size functions will throw a std::bad_alloc () | |
127 // exception if the requested size is larger than can be indexed by | |
128 // octave_idx_type. This may be smaller than the actual amount of | |
129 // memory that can be safely allocated on a system. However, if we | |
130 // don't fail here, we can end up with a mysterious crash inside a | |
131 // function that is iterating over an array using octave_idx_type | |
132 // indices. | |
133 | |
4513 | 134 // A guess (should be quite conservative). |
135 #define MALLOC_OVERHEAD 1024 | |
136 | |
137 template <class T> | |
5275 | 138 octave_idx_type |
139 Array<T>::get_size (octave_idx_type r, octave_idx_type c) | |
4513 | 140 { |
141 static int nl; | |
142 static double dl | |
5275 | 143 = frexp (static_cast<double> |
144 (std::numeric_limits<octave_idx_type>::max() - MALLOC_OVERHEAD) / sizeof (T), &nl); | |
4513 | 145 |
146 int nr, nc; | |
5275 | 147 double dr = frexp (static_cast<double> (r), &nr); // r = dr * 2^nr |
148 double dc = frexp (static_cast<double> (c), &nc); // c = dc * 2^nc | |
4513 | 149 |
150 int nt = nr + nc; | |
151 double dt = dr * dc; | |
152 | |
4532 | 153 if (dt < 0.5) |
4513 | 154 { |
155 nt--; | |
156 dt *= 2; | |
157 } | |
158 | |
6674 | 159 if (nt < nl || (nt == nl && dt < dl)) |
160 return r * c; | |
161 else | |
162 { | |
163 throw std::bad_alloc (); | |
164 return 0; | |
165 } | |
237 | 166 } |
167 | |
168 template <class T> | |
5275 | 169 octave_idx_type |
170 Array<T>::get_size (octave_idx_type r, octave_idx_type c, octave_idx_type p) | |
237 | 171 { |
4513 | 172 static int nl; |
173 static double dl | |
174 = frexp (static_cast<double> | |
5275 | 175 (std::numeric_limits<octave_idx_type>::max() - MALLOC_OVERHEAD) / sizeof (T), &nl); |
4513 | 176 |
177 int nr, nc, np; | |
178 double dr = frexp (static_cast<double> (r), &nr); | |
179 double dc = frexp (static_cast<double> (c), &nc); | |
180 double dp = frexp (static_cast<double> (p), &np); | |
181 | |
182 int nt = nr + nc + np; | |
183 double dt = dr * dc * dp; | |
184 | |
4532 | 185 if (dt < 0.5) |
659 | 186 { |
4513 | 187 nt--; |
188 dt *= 2; | |
237 | 189 |
4532 | 190 if (dt < 0.5) |
191 { | |
192 nt--; | |
193 dt *= 2; | |
194 } | |
659 | 195 } |
1619 | 196 |
6674 | 197 if (nt < nl || (nt == nl && dt < dl)) |
198 return r * c * p; | |
199 else | |
200 { | |
201 throw std::bad_alloc (); | |
202 return 0; | |
203 } | |
237 | 204 } |
205 | |
206 template <class T> | |
5275 | 207 octave_idx_type |
4513 | 208 Array<T>::get_size (const dim_vector& ra_idx) |
237 | 209 { |
4513 | 210 static int nl; |
211 static double dl | |
212 = frexp (static_cast<double> | |
5275 | 213 (std::numeric_limits<octave_idx_type>::max() - MALLOC_OVERHEAD) / sizeof (T), &nl); |
4513 | 214 |
215 int n = ra_idx.length (); | |
216 | |
217 int nt = 0; | |
218 double dt = 1; | |
219 | |
220 for (int i = 0; i < n; i++) | |
237 | 221 { |
4513 | 222 int nra_idx; |
223 double dra_idx = frexp (static_cast<double> (ra_idx(i)), &nra_idx); | |
224 | |
225 nt += nra_idx; | |
226 dt *= dra_idx; | |
4532 | 227 |
228 if (dt < 0.5) | |
229 { | |
230 nt--; | |
231 dt *= 2; | |
232 } | |
237 | 233 } |
234 | |
4513 | 235 if (nt < nl || (nt == nl && dt < dl)) |
236 { | |
6674 | 237 octave_idx_type retval = 1; |
4513 | 238 |
239 for (int i = 0; i < n; i++) | |
240 retval *= ra_idx(i); | |
6674 | 241 |
242 return retval; | |
4513 | 243 } |
6674 | 244 else |
245 { | |
246 throw std::bad_alloc (); | |
247 return 0; | |
248 } | |
237 | 249 } |
250 | |
4513 | 251 #undef MALLOC_OVERHEAD |
252 | |
237 | 253 template <class T> |
5275 | 254 octave_idx_type |
255 Array<T>::compute_index (const Array<octave_idx_type>& ra_idx) const | |
237 | 256 { |
5275 | 257 octave_idx_type retval = -1; |
4513 | 258 |
259 int n = dimensions.length (); | |
260 | |
261 if (n > 0 && n == ra_idx.length ()) | |
237 | 262 { |
4513 | 263 retval = ra_idx(--n); |
237 | 264 |
4513 | 265 while (--n >= 0) |
266 { | |
267 retval *= dimensions(n); | |
268 retval += ra_idx(n); | |
269 } | |
270 } | |
271 else | |
272 (*current_liboctave_error_handler) | |
273 ("Array<T>::compute_index: invalid ra_idxing operation"); | |
237 | 274 |
4513 | 275 return retval; |
237 | 276 } |
277 | |
2049 | 278 template <class T> |
279 T | |
5275 | 280 Array<T>::range_error (const char *fcn, octave_idx_type n) const |
2049 | 281 { |
2109 | 282 (*current_liboctave_error_handler) ("%s (%d): range error", fcn, n); |
2049 | 283 return T (); |
284 } | |
285 | |
286 template <class T> | |
287 T& | |
5275 | 288 Array<T>::range_error (const char *fcn, octave_idx_type n) |
2049 | 289 { |
2109 | 290 (*current_liboctave_error_handler) ("%s (%d): range error", fcn, n); |
2049 | 291 static T foo; |
292 return foo; | |
293 } | |
294 | |
3933 | 295 template <class T> |
4513 | 296 T |
5275 | 297 Array<T>::range_error (const char *fcn, octave_idx_type i, octave_idx_type j) const |
4513 | 298 { |
299 (*current_liboctave_error_handler) | |
300 ("%s (%d, %d): range error", fcn, i, j); | |
301 return T (); | |
302 } | |
303 | |
304 template <class T> | |
305 T& | |
5275 | 306 Array<T>::range_error (const char *fcn, octave_idx_type i, octave_idx_type j) |
4513 | 307 { |
308 (*current_liboctave_error_handler) | |
309 ("%s (%d, %d): range error", fcn, i, j); | |
310 static T foo; | |
311 return foo; | |
312 } | |
313 | |
314 template <class T> | |
315 T | |
5275 | 316 Array<T>::range_error (const char *fcn, octave_idx_type i, octave_idx_type j, octave_idx_type k) const |
4513 | 317 { |
318 (*current_liboctave_error_handler) | |
319 ("%s (%d, %d, %d): range error", fcn, i, j, k); | |
320 return T (); | |
321 } | |
322 | |
323 template <class T> | |
324 T& | |
5275 | 325 Array<T>::range_error (const char *fcn, octave_idx_type i, octave_idx_type j, octave_idx_type k) |
4513 | 326 { |
327 (*current_liboctave_error_handler) | |
328 ("%s (%d, %d, %d): range error", fcn, i, j, k); | |
329 static T foo; | |
330 return foo; | |
331 } | |
332 | |
333 template <class T> | |
334 T | |
6867 | 335 Array<T>::range_error (const char *fcn, const Array<octave_idx_type>& ra_idx) const |
4513 | 336 { |
5765 | 337 std::ostringstream buf; |
4661 | 338 |
339 buf << fcn << " ("; | |
340 | |
5275 | 341 octave_idx_type n = ra_idx.length (); |
4661 | 342 |
343 if (n > 0) | |
344 buf << ra_idx(0); | |
345 | |
5275 | 346 for (octave_idx_type i = 1; i < n; i++) |
4661 | 347 buf << ", " << ra_idx(i); |
348 | |
349 buf << "): range error"; | |
350 | |
5765 | 351 std::string buf_str = buf.str (); |
352 | |
353 (*current_liboctave_error_handler) (buf_str.c_str ()); | |
4513 | 354 |
355 return T (); | |
356 } | |
357 | |
358 template <class T> | |
359 T& | |
6867 | 360 Array<T>::range_error (const char *fcn, const Array<octave_idx_type>& ra_idx) |
4513 | 361 { |
5765 | 362 std::ostringstream buf; |
4661 | 363 |
364 buf << fcn << " ("; | |
365 | |
5275 | 366 octave_idx_type n = ra_idx.length (); |
4661 | 367 |
368 if (n > 0) | |
369 buf << ra_idx(0); | |
370 | |
5275 | 371 for (octave_idx_type i = 1; i < n; i++) |
4661 | 372 buf << ", " << ra_idx(i); |
373 | |
374 buf << "): range error"; | |
375 | |
5765 | 376 std::string buf_str = buf.str (); |
377 | |
378 (*current_liboctave_error_handler) (buf_str.c_str ()); | |
4513 | 379 |
380 static T foo; | |
381 return foo; | |
382 } | |
383 | |
384 template <class T> | |
4567 | 385 Array<T> |
386 Array<T>::reshape (const dim_vector& new_dims) const | |
387 { | |
388 Array<T> retval; | |
389 | |
390 if (dimensions != new_dims) | |
391 { | |
392 if (dimensions.numel () == new_dims.numel ()) | |
393 retval = Array<T> (*this, new_dims); | |
394 else | |
395 (*current_liboctave_error_handler) ("reshape: size mismatch"); | |
396 } | |
4916 | 397 else |
398 retval = *this; | |
4567 | 399 |
400 return retval; | |
401 } | |
402 | |
7241 | 403 |
5607 | 404 |
4567 | 405 template <class T> |
4593 | 406 Array<T> |
5607 | 407 Array<T>::permute (const Array<octave_idx_type>& perm_vec_arg, bool inv) const |
4593 | 408 { |
409 Array<T> retval; | |
410 | |
5607 | 411 Array<octave_idx_type> perm_vec = perm_vec_arg; |
412 | |
4593 | 413 dim_vector dv = dims (); |
414 dim_vector dv_new; | |
415 | |
5148 | 416 int perm_vec_len = perm_vec.length (); |
417 | |
418 if (perm_vec_len < dv.length ()) | |
419 (*current_liboctave_error_handler) | |
420 ("%s: invalid permutation vector", inv ? "ipermute" : "permute"); | |
421 | |
422 dv_new.resize (perm_vec_len); | |
423 | |
424 // Append singleton dimensions as needed. | |
425 dv.resize (perm_vec_len, 1); | |
426 | |
4593 | 427 // Need this array to check for identical elements in permutation array. |
5148 | 428 Array<bool> checked (perm_vec_len, false); |
4593 | 429 |
430 // Find dimension vector of permuted array. | |
5148 | 431 for (int i = 0; i < perm_vec_len; i++) |
4593 | 432 { |
5275 | 433 octave_idx_type perm_elt = perm_vec.elem (i); |
5148 | 434 |
435 if (perm_elt >= perm_vec_len || perm_elt < 0) | |
4593 | 436 { |
437 (*current_liboctave_error_handler) | |
5148 | 438 ("%s: permutation vector contains an invalid element", |
439 inv ? "ipermute" : "permute"); | |
4593 | 440 |
441 return retval; | |
442 } | |
443 | |
5148 | 444 if (checked.elem(perm_elt)) |
4593 | 445 { |
446 (*current_liboctave_error_handler) | |
5148 | 447 ("%s: permutation vector cannot contain identical elements", |
448 inv ? "ipermute" : "permute"); | |
4593 | 449 |
450 return retval; | |
451 } | |
452 else | |
5148 | 453 checked.elem(perm_elt) = true; |
454 | |
455 dv_new(i) = dv(perm_elt); | |
4593 | 456 } |
457 | |
5607 | 458 int nd = dv.length (); |
459 | |
5775 | 460 // FIXME -- it would be nice to have a sort method in the |
5607 | 461 // Array class that also returns the sort indices. |
462 | |
463 if (inv) | |
464 { | |
465 OCTAVE_LOCAL_BUFFER (permute_vector, pvec, nd); | |
466 | |
467 for (int i = 0; i < nd; i++) | |
468 { | |
469 pvec[i].pidx = perm_vec(i); | |
470 pvec[i].iidx = i; | |
471 } | |
472 | |
473 octave_qsort (pvec, static_cast<size_t> (nd), | |
474 sizeof (permute_vector), permute_vector_compare); | |
475 | |
476 for (int i = 0; i < nd; i++) | |
477 { | |
478 perm_vec(i) = pvec[i].iidx; | |
479 dv_new(i) = dv(perm_vec(i)); | |
480 } | |
481 } | |
482 | |
4593 | 483 retval.resize (dv_new); |
484 | |
5940 | 485 if (numel () > 0) |
5607 | 486 { |
5940 | 487 Array<octave_idx_type> cp (nd+1, 1); |
488 for (octave_idx_type i = 1; i < nd+1; i++) | |
489 cp(i) = cp(i-1) * dv(i-1); | |
490 | |
491 octave_idx_type incr = cp(perm_vec(0)); | |
492 | |
493 Array<octave_idx_type> base_delta (nd-1, 0); | |
494 Array<octave_idx_type> base_delta_max (nd-1); | |
495 Array<octave_idx_type> base_incr (nd-1); | |
496 for (octave_idx_type i = 0; i < nd-1; i++) | |
5607 | 497 { |
5940 | 498 base_delta_max(i) = dv_new(i+1); |
499 base_incr(i) = cp(perm_vec(i+1)); | |
5607 | 500 } |
501 | |
5940 | 502 octave_idx_type nr_new = dv_new(0); |
503 octave_idx_type nel_new = dv_new.numel (); | |
504 octave_idx_type n = nel_new / nr_new; | |
505 | |
506 octave_idx_type k = 0; | |
507 | |
508 for (octave_idx_type i = 0; i < n; i++) | |
5607 | 509 { |
5940 | 510 octave_idx_type iidx = 0; |
511 for (octave_idx_type kk = 0; kk < nd-1; kk++) | |
512 iidx += base_delta(kk) * base_incr(kk); | |
513 | |
514 for (octave_idx_type j = 0; j < nr_new; j++) | |
5607 | 515 { |
5940 | 516 OCTAVE_QUIT; |
517 | |
518 retval(k++) = elem(iidx); | |
519 iidx += incr; | |
520 } | |
521 | |
522 base_delta(0)++; | |
523 | |
524 for (octave_idx_type kk = 0; kk < nd-2; kk++) | |
525 { | |
526 if (base_delta(kk) == base_delta_max(kk)) | |
527 { | |
528 base_delta(kk) = 0; | |
529 base_delta(kk+1)++; | |
530 } | |
5607 | 531 } |
532 } | |
4593 | 533 } |
534 | |
5340 | 535 retval.chop_trailing_singletons (); |
5338 | 536 |
4593 | 537 return retval; |
538 } | |
539 | |
540 template <class T> | |
4513 | 541 void |
5275 | 542 Array<T>::resize_no_fill (octave_idx_type n) |
4513 | 543 { |
544 if (n < 0) | |
545 { | |
546 (*current_liboctave_error_handler) | |
547 ("can't resize to negative dimension"); | |
548 return; | |
549 } | |
550 | |
551 if (n == length ()) | |
552 return; | |
553 | |
554 typename Array<T>::ArrayRep *old_rep = rep; | |
555 const T *old_data = data (); | |
5275 | 556 octave_idx_type old_len = length (); |
4513 | 557 |
558 rep = new typename Array<T>::ArrayRep (n); | |
559 | |
560 dimensions = dim_vector (n); | |
561 | |
4747 | 562 if (n > 0 && old_data && old_len > 0) |
4513 | 563 { |
5275 | 564 octave_idx_type min_len = old_len < n ? old_len : n; |
565 | |
566 for (octave_idx_type i = 0; i < min_len; i++) | |
4513 | 567 xelem (i) = old_data[i]; |
568 } | |
569 | |
570 if (--old_rep->count <= 0) | |
571 delete old_rep; | |
572 } | |
573 | |
574 template <class T> | |
575 void | |
4587 | 576 Array<T>::resize_no_fill (const dim_vector& dv) |
4513 | 577 { |
5275 | 578 octave_idx_type n = dv.length (); |
579 | |
580 for (octave_idx_type i = 0; i < n; i++) | |
4513 | 581 { |
4587 | 582 if (dv(i) < 0) |
4513 | 583 { |
584 (*current_liboctave_error_handler) | |
585 ("can't resize to negative dimension"); | |
586 return; | |
587 } | |
588 } | |
589 | |
4548 | 590 bool same_size = true; |
591 | |
592 if (dimensions.length () != n) | |
593 { | |
594 same_size = false; | |
595 } | |
596 else | |
4513 | 597 { |
5275 | 598 for (octave_idx_type i = 0; i < n; i++) |
4513 | 599 { |
4587 | 600 if (dv(i) != dimensions(i)) |
4548 | 601 { |
602 same_size = false; | |
603 break; | |
604 } | |
4513 | 605 } |
606 } | |
607 | |
4548 | 608 if (same_size) |
4513 | 609 return; |
610 | |
611 typename Array<T>::ArrayRep *old_rep = rep; | |
612 const T *old_data = data (); | |
613 | |
5275 | 614 octave_idx_type ts = get_size (dv); |
4747 | 615 |
616 rep = new typename Array<T>::ArrayRep (ts); | |
4587 | 617 |
4870 | 618 dim_vector dv_old = dimensions; |
5275 | 619 octave_idx_type dv_old_orig_len = dv_old.length (); |
4587 | 620 dimensions = dv; |
5275 | 621 octave_idx_type ts_old = get_size (dv_old); |
4915 | 622 |
623 if (ts > 0 && ts_old > 0 && dv_old_orig_len > 0) | |
4513 | 624 { |
5275 | 625 Array<octave_idx_type> ra_idx (dimensions.length (), 0); |
4747 | 626 |
4870 | 627 if (n > dv_old_orig_len) |
4747 | 628 { |
4870 | 629 dv_old.resize (n); |
630 | |
5275 | 631 for (octave_idx_type i = dv_old_orig_len; i < n; i++) |
4870 | 632 dv_old.elem (i) = 1; |
633 } | |
634 | |
5275 | 635 for (octave_idx_type i = 0; i < ts; i++) |
4870 | 636 { |
637 if (index_in_bounds (ra_idx, dv_old)) | |
638 rep->elem (i) = old_data[get_scalar_idx (ra_idx, dv_old)]; | |
4747 | 639 |
640 increment_index (ra_idx, dimensions); | |
641 } | |
4513 | 642 } |
643 | |
644 if (--old_rep->count <= 0) | |
645 delete old_rep; | |
646 } | |
647 | |
648 template <class T> | |
649 void | |
5275 | 650 Array<T>::resize_no_fill (octave_idx_type r, octave_idx_type c) |
4513 | 651 { |
652 if (r < 0 || c < 0) | |
653 { | |
654 (*current_liboctave_error_handler) | |
655 ("can't resize to negative dimension"); | |
656 return; | |
657 } | |
658 | |
4548 | 659 int n = ndims (); |
660 | |
661 if (n == 0) | |
662 dimensions = dim_vector (0, 0); | |
663 | |
664 assert (ndims () == 2); | |
665 | |
4513 | 666 if (r == dim1 () && c == dim2 ()) |
667 return; | |
668 | |
669 typename Array<T>::ArrayRep *old_rep = Array<T>::rep; | |
670 const T *old_data = data (); | |
671 | |
5275 | 672 octave_idx_type old_d1 = dim1 (); |
673 octave_idx_type old_d2 = dim2 (); | |
674 octave_idx_type old_len = length (); | |
675 | |
676 octave_idx_type ts = get_size (r, c); | |
4747 | 677 |
678 rep = new typename Array<T>::ArrayRep (ts); | |
4513 | 679 |
680 dimensions = dim_vector (r, c); | |
681 | |
4747 | 682 if (ts > 0 && old_data && old_len > 0) |
4513 | 683 { |
5275 | 684 octave_idx_type min_r = old_d1 < r ? old_d1 : r; |
685 octave_idx_type min_c = old_d2 < c ? old_d2 : c; | |
686 | |
687 for (octave_idx_type j = 0; j < min_c; j++) | |
688 for (octave_idx_type i = 0; i < min_r; i++) | |
4513 | 689 xelem (i, j) = old_data[old_d1*j+i]; |
690 } | |
691 | |
692 if (--old_rep->count <= 0) | |
693 delete old_rep; | |
694 } | |
695 | |
696 template <class T> | |
697 void | |
5275 | 698 Array<T>::resize_no_fill (octave_idx_type r, octave_idx_type c, octave_idx_type p) |
4513 | 699 { |
700 if (r < 0 || c < 0 || p < 0) | |
701 { | |
702 (*current_liboctave_error_handler) | |
703 ("can't resize to negative dimension"); | |
704 return; | |
705 } | |
706 | |
4548 | 707 int n = ndims (); |
708 | |
709 if (n == 0) | |
710 dimensions = dim_vector (0, 0, 0); | |
711 | |
712 assert (ndims () == 3); | |
713 | |
4513 | 714 if (r == dim1 () && c == dim2 () && p == dim3 ()) |
715 return; | |
716 | |
717 typename Array<T>::ArrayRep *old_rep = rep; | |
718 const T *old_data = data (); | |
719 | |
5275 | 720 octave_idx_type old_d1 = dim1 (); |
721 octave_idx_type old_d2 = dim2 (); | |
722 octave_idx_type old_d3 = dim3 (); | |
723 octave_idx_type old_len = length (); | |
724 | |
725 octave_idx_type ts = get_size (get_size (r, c), p); | |
4513 | 726 |
727 rep = new typename Array<T>::ArrayRep (ts); | |
728 | |
729 dimensions = dim_vector (r, c, p); | |
730 | |
4747 | 731 if (ts > 0 && old_data && old_len > 0) |
4513 | 732 { |
5275 | 733 octave_idx_type min_r = old_d1 < r ? old_d1 : r; |
734 octave_idx_type min_c = old_d2 < c ? old_d2 : c; | |
735 octave_idx_type min_p = old_d3 < p ? old_d3 : p; | |
736 | |
737 for (octave_idx_type k = 0; k < min_p; k++) | |
738 for (octave_idx_type j = 0; j < min_c; j++) | |
739 for (octave_idx_type i = 0; i < min_r; i++) | |
4513 | 740 xelem (i, j, k) = old_data[old_d1*(old_d2*k+j)+i]; |
741 } | |
742 | |
743 if (--old_rep->count <= 0) | |
744 delete old_rep; | |
745 } | |
746 | |
747 template <class T> | |
748 void | |
5275 | 749 Array<T>::resize_and_fill (octave_idx_type n, const T& val) |
4513 | 750 { |
751 if (n < 0) | |
752 { | |
753 (*current_liboctave_error_handler) | |
754 ("can't resize to negative dimension"); | |
755 return; | |
756 } | |
757 | |
758 if (n == length ()) | |
759 return; | |
760 | |
761 typename Array<T>::ArrayRep *old_rep = rep; | |
762 const T *old_data = data (); | |
5275 | 763 octave_idx_type old_len = length (); |
4513 | 764 |
765 rep = new typename Array<T>::ArrayRep (n); | |
766 | |
767 dimensions = dim_vector (n); | |
768 | |
4747 | 769 if (n > 0) |
4513 | 770 { |
5275 | 771 octave_idx_type min_len = old_len < n ? old_len : n; |
4747 | 772 |
773 if (old_data && old_len > 0) | |
774 { | |
5275 | 775 for (octave_idx_type i = 0; i < min_len; i++) |
4747 | 776 xelem (i) = old_data[i]; |
777 } | |
778 | |
5275 | 779 for (octave_idx_type i = old_len; i < n; i++) |
4747 | 780 xelem (i) = val; |
4513 | 781 } |
782 | |
783 if (--old_rep->count <= 0) | |
784 delete old_rep; | |
785 } | |
786 | |
787 template <class T> | |
788 void | |
5275 | 789 Array<T>::resize_and_fill (octave_idx_type r, octave_idx_type c, const T& val) |
4513 | 790 { |
791 if (r < 0 || c < 0) | |
792 { | |
793 (*current_liboctave_error_handler) | |
794 ("can't resize to negative dimension"); | |
795 return; | |
796 } | |
797 | |
4548 | 798 if (ndims () == 0) |
799 dimensions = dim_vector (0, 0); | |
800 | |
801 assert (ndims () == 2); | |
802 | |
4513 | 803 if (r == dim1 () && c == dim2 ()) |
804 return; | |
805 | |
806 typename Array<T>::ArrayRep *old_rep = Array<T>::rep; | |
807 const T *old_data = data (); | |
808 | |
5275 | 809 octave_idx_type old_d1 = dim1 (); |
810 octave_idx_type old_d2 = dim2 (); | |
811 octave_idx_type old_len = length (); | |
812 | |
813 octave_idx_type ts = get_size (r, c); | |
4747 | 814 |
815 rep = new typename Array<T>::ArrayRep (ts); | |
4513 | 816 |
817 dimensions = dim_vector (r, c); | |
818 | |
4747 | 819 if (ts > 0) |
4513 | 820 { |
5275 | 821 octave_idx_type min_r = old_d1 < r ? old_d1 : r; |
822 octave_idx_type min_c = old_d2 < c ? old_d2 : c; | |
4747 | 823 |
824 if (old_data && old_len > 0) | |
825 { | |
5275 | 826 for (octave_idx_type j = 0; j < min_c; j++) |
827 for (octave_idx_type i = 0; i < min_r; i++) | |
4747 | 828 xelem (i, j) = old_data[old_d1*j+i]; |
829 } | |
830 | |
5275 | 831 for (octave_idx_type j = 0; j < min_c; j++) |
832 for (octave_idx_type i = min_r; i < r; i++) | |
4747 | 833 xelem (i, j) = val; |
834 | |
5275 | 835 for (octave_idx_type j = min_c; j < c; j++) |
836 for (octave_idx_type i = 0; i < r; i++) | |
4747 | 837 xelem (i, j) = val; |
4513 | 838 } |
839 | |
840 if (--old_rep->count <= 0) | |
841 delete old_rep; | |
842 } | |
843 | |
844 template <class T> | |
845 void | |
5275 | 846 Array<T>::resize_and_fill (octave_idx_type r, octave_idx_type c, octave_idx_type p, const T& val) |
4513 | 847 { |
848 if (r < 0 || c < 0 || p < 0) | |
849 { | |
850 (*current_liboctave_error_handler) | |
851 ("can't resize to negative dimension"); | |
852 return; | |
853 } | |
854 | |
4548 | 855 if (ndims () == 0) |
856 dimensions = dim_vector (0, 0, 0); | |
857 | |
858 assert (ndims () == 3); | |
859 | |
4513 | 860 if (r == dim1 () && c == dim2 () && p == dim3 ()) |
861 return; | |
862 | |
863 typename Array<T>::ArrayRep *old_rep = rep; | |
864 const T *old_data = data (); | |
865 | |
5275 | 866 octave_idx_type old_d1 = dim1 (); |
867 octave_idx_type old_d2 = dim2 (); | |
868 octave_idx_type old_d3 = dim3 (); | |
869 | |
870 octave_idx_type old_len = length (); | |
871 | |
872 octave_idx_type ts = get_size (get_size (r, c), p); | |
4513 | 873 |
874 rep = new typename Array<T>::ArrayRep (ts); | |
875 | |
876 dimensions = dim_vector (r, c, p); | |
877 | |
4747 | 878 if (ts > 0) |
879 { | |
5275 | 880 octave_idx_type min_r = old_d1 < r ? old_d1 : r; |
881 octave_idx_type min_c = old_d2 < c ? old_d2 : c; | |
882 octave_idx_type min_p = old_d3 < p ? old_d3 : p; | |
4747 | 883 |
884 if (old_data && old_len > 0) | |
5275 | 885 for (octave_idx_type k = 0; k < min_p; k++) |
886 for (octave_idx_type j = 0; j < min_c; j++) | |
887 for (octave_idx_type i = 0; i < min_r; i++) | |
4747 | 888 xelem (i, j, k) = old_data[old_d1*(old_d2*k+j)+i]; |
889 | |
5775 | 890 // FIXME -- if the copy constructor is expensive, this |
4747 | 891 // may win. Otherwise, it may make more sense to just copy the |
892 // value everywhere when making the new ArrayRep. | |
893 | |
5275 | 894 for (octave_idx_type k = 0; k < min_p; k++) |
895 for (octave_idx_type j = min_c; j < c; j++) | |
896 for (octave_idx_type i = 0; i < min_r; i++) | |
4747 | 897 xelem (i, j, k) = val; |
898 | |
5275 | 899 for (octave_idx_type k = 0; k < min_p; k++) |
900 for (octave_idx_type j = 0; j < c; j++) | |
901 for (octave_idx_type i = min_r; i < r; i++) | |
4747 | 902 xelem (i, j, k) = val; |
903 | |
5275 | 904 for (octave_idx_type k = min_p; k < p; k++) |
905 for (octave_idx_type j = 0; j < c; j++) | |
906 for (octave_idx_type i = 0; i < r; i++) | |
4747 | 907 xelem (i, j, k) = val; |
908 } | |
4513 | 909 |
910 if (--old_rep->count <= 0) | |
911 delete old_rep; | |
912 } | |
913 | |
914 template <class T> | |
915 void | |
4587 | 916 Array<T>::resize_and_fill (const dim_vector& dv, const T& val) |
4513 | 917 { |
5275 | 918 octave_idx_type n = dv.length (); |
919 | |
920 for (octave_idx_type i = 0; i < n; i++) | |
4513 | 921 { |
4587 | 922 if (dv(i) < 0) |
4513 | 923 { |
924 (*current_liboctave_error_handler) | |
925 ("can't resize to negative dimension"); | |
926 return; | |
927 } | |
928 } | |
929 | |
4553 | 930 bool same_size = true; |
931 | |
932 if (dimensions.length () != n) | |
933 { | |
934 same_size = false; | |
935 } | |
936 else | |
4513 | 937 { |
5275 | 938 for (octave_idx_type i = 0; i < n; i++) |
4513 | 939 { |
4587 | 940 if (dv(i) != dimensions(i)) |
4553 | 941 { |
942 same_size = false; | |
943 break; | |
944 } | |
4513 | 945 } |
946 } | |
947 | |
4553 | 948 if (same_size) |
4513 | 949 return; |
950 | |
951 typename Array<T>::ArrayRep *old_rep = rep; | |
952 const T *old_data = data (); | |
953 | |
5275 | 954 octave_idx_type len = get_size (dv); |
4709 | 955 |
4513 | 956 rep = new typename Array<T>::ArrayRep (len); |
957 | |
4707 | 958 dim_vector dv_old = dimensions; |
5275 | 959 octave_idx_type dv_old_orig_len = dv_old.length (); |
4587 | 960 dimensions = dv; |
4513 | 961 |
4870 | 962 if (len > 0 && dv_old_orig_len > 0) |
4513 | 963 { |
5275 | 964 Array<octave_idx_type> ra_idx (dimensions.length (), 0); |
4870 | 965 |
966 if (n > dv_old_orig_len) | |
967 { | |
968 dv_old.resize (n); | |
969 | |
5275 | 970 for (octave_idx_type i = dv_old_orig_len; i < n; i++) |
4870 | 971 dv_old.elem (i) = 1; |
972 } | |
4747 | 973 |
5275 | 974 for (octave_idx_type i = 0; i < len; i++) |
4747 | 975 { |
976 if (index_in_bounds (ra_idx, dv_old)) | |
4870 | 977 rep->elem (i) = old_data[get_scalar_idx (ra_idx, dv_old)]; |
978 else | |
979 rep->elem (i) = val; | |
980 | |
981 increment_index (ra_idx, dimensions); | |
4747 | 982 } |
4513 | 983 } |
4870 | 984 else |
5275 | 985 for (octave_idx_type i = 0; i < len; i++) |
4870 | 986 rep->elem (i) = val; |
4513 | 987 |
988 if (--old_rep->count <= 0) | |
989 delete old_rep; | |
990 } | |
991 | |
992 template <class T> | |
993 Array<T>& | |
5275 | 994 Array<T>::insert (const Array<T>& a, octave_idx_type r, octave_idx_type c) |
4513 | 995 { |
4786 | 996 if (ndims () == 2 && a.ndims () == 2) |
997 insert2 (a, r, c); | |
998 else | |
999 insertN (a, r, c); | |
1000 | |
1001 return *this; | |
1002 } | |
1003 | |
1004 | |
1005 template <class T> | |
1006 Array<T>& | |
5275 | 1007 Array<T>::insert2 (const Array<T>& a, octave_idx_type r, octave_idx_type c) |
4786 | 1008 { |
5275 | 1009 octave_idx_type a_rows = a.rows (); |
1010 octave_idx_type a_cols = a.cols (); | |
4786 | 1011 |
1012 if (r < 0 || r + a_rows > rows () || c < 0 || c + a_cols > cols ()) | |
1013 { | |
1014 (*current_liboctave_error_handler) ("range error for insert"); | |
1015 return *this; | |
1016 } | |
1017 | |
5275 | 1018 for (octave_idx_type j = 0; j < a_cols; j++) |
1019 for (octave_idx_type i = 0; i < a_rows; i++) | |
4786 | 1020 elem (r+i, c+j) = a.elem (i, j); |
1021 | |
1022 return *this; | |
1023 } | |
1024 | |
1025 template <class T> | |
1026 Array<T>& | |
5275 | 1027 Array<T>::insertN (const Array<T>& a, octave_idx_type r, octave_idx_type c) |
4786 | 1028 { |
4806 | 1029 dim_vector dv = dims (); |
1030 | |
4765 | 1031 dim_vector a_dv = a.dims (); |
1032 | |
1033 int n = a_dv.length (); | |
1034 | |
1035 if (n == dimensions.length ()) | |
4513 | 1036 { |
5275 | 1037 Array<octave_idx_type> a_ra_idx (a_dv.length (), 0); |
4765 | 1038 |
1039 a_ra_idx.elem (0) = r; | |
1040 a_ra_idx.elem (1) = c; | |
1041 | |
1042 for (int i = 0; i < n; i++) | |
1043 { | |
4806 | 1044 if (a_ra_idx(i) < 0 || (a_ra_idx(i) + a_dv(i)) > dv(i)) |
4765 | 1045 { |
1046 (*current_liboctave_error_handler) | |
1047 ("Array<T>::insert: range error for insert"); | |
1048 return *this; | |
1049 } | |
1050 } | |
1051 | |
5275 | 1052 octave_idx_type n_elt = a.numel (); |
4806 | 1053 |
1054 const T *a_data = a.data (); | |
1055 | |
5275 | 1056 octave_idx_type iidx = 0; |
4806 | 1057 |
5275 | 1058 octave_idx_type a_rows = a_dv(0); |
1059 | |
1060 octave_idx_type this_rows = dv(0); | |
4806 | 1061 |
5275 | 1062 octave_idx_type numel_page = a_dv(0) * a_dv(1); |
1063 | |
1064 octave_idx_type count_pages = 0; | |
4806 | 1065 |
5275 | 1066 for (octave_idx_type i = 0; i < n_elt; i++) |
4765 | 1067 { |
4806 | 1068 if (i != 0 && i % a_rows == 0) |
1069 iidx += (this_rows - a_rows); | |
1070 | |
1071 if (i % numel_page == 0) | |
1072 iidx = c * dv(0) + r + dv(0) * dv(1) * count_pages++; | |
1073 | |
1074 elem (iidx++) = a_data[i]; | |
4765 | 1075 } |
4513 | 1076 } |
4765 | 1077 else |
1078 (*current_liboctave_error_handler) | |
1079 ("Array<T>::insert: invalid indexing operation"); | |
4513 | 1080 |
1081 return *this; | |
1082 } | |
1083 | |
1084 template <class T> | |
1085 Array<T>& | |
5275 | 1086 Array<T>::insert (const Array<T>& a, const Array<octave_idx_type>& ra_idx) |
4513 | 1087 { |
5275 | 1088 octave_idx_type n = ra_idx.length (); |
4513 | 1089 |
1090 if (n == dimensions.length ()) | |
1091 { | |
4915 | 1092 dim_vector dva = a.dims (); |
1093 dim_vector dv = dims (); | |
1094 int len_a = dva.length (); | |
5120 | 1095 int non_full_dim = 0; |
4513 | 1096 |
5275 | 1097 for (octave_idx_type i = 0; i < n; i++) |
4513 | 1098 { |
4915 | 1099 if (ra_idx(i) < 0 || (ra_idx(i) + |
1100 (i < len_a ? dva(i) : 1)) > dimensions(i)) | |
4513 | 1101 { |
1102 (*current_liboctave_error_handler) | |
1103 ("Array<T>::insert: range error for insert"); | |
1104 return *this; | |
1105 } | |
5120 | 1106 |
1107 if (dv(i) != (i < len_a ? dva(i) : 1)) | |
1108 non_full_dim++; | |
4513 | 1109 } |
1110 | |
4915 | 1111 if (dva.numel ()) |
1112 { | |
5120 | 1113 if (non_full_dim < 2) |
4915 | 1114 { |
5120 | 1115 // Special case for fast concatenation |
1116 const T *a_data = a.data (); | |
5275 | 1117 octave_idx_type numel_to_move = 1; |
1118 octave_idx_type skip = 0; | |
5120 | 1119 for (int i = 0; i < len_a; i++) |
1120 if (ra_idx(i) == 0 && dva(i) == dv(i)) | |
1121 numel_to_move *= dva(i); | |
1122 else | |
1123 { | |
1124 skip = numel_to_move * (dv(i) - dva(i)); | |
1125 numel_to_move *= dva(i); | |
1126 break; | |
1127 } | |
1128 | |
5275 | 1129 octave_idx_type jidx = ra_idx(n-1); |
5120 | 1130 for (int i = n-2; i >= 0; i--) |
1131 { | |
1132 jidx *= dv(i); | |
1133 jidx += ra_idx(i); | |
1134 } | |
1135 | |
5275 | 1136 octave_idx_type iidx = 0; |
1137 octave_idx_type moves = dva.numel () / numel_to_move; | |
1138 for (octave_idx_type i = 0; i < moves; i++) | |
5120 | 1139 { |
5275 | 1140 for (octave_idx_type j = 0; j < numel_to_move; j++) |
5120 | 1141 elem (jidx++) = a_data[iidx++]; |
1142 jidx += skip; | |
1143 } | |
4915 | 1144 } |
5120 | 1145 else |
4915 | 1146 { |
5120 | 1147 // Generic code |
1148 const T *a_data = a.data (); | |
1149 int nel = a.numel (); | |
5275 | 1150 Array<octave_idx_type> a_idx (n, 0); |
5120 | 1151 |
1152 for (int i = 0; i < nel; i++) | |
1153 { | |
1154 int iidx = a_idx(n-1) + ra_idx(n-1); | |
1155 for (int j = n-2; j >= 0; j--) | |
1156 { | |
1157 iidx *= dv(j); | |
1158 iidx += a_idx(j) + ra_idx(j); | |
1159 } | |
1160 | |
1161 elem (iidx) = a_data[i]; | |
1162 | |
1163 increment_index (a_idx, dva); | |
1164 } | |
4915 | 1165 } |
1166 } | |
4513 | 1167 } |
1168 else | |
1169 (*current_liboctave_error_handler) | |
1170 ("Array<T>::insert: invalid indexing operation"); | |
1171 | |
1172 return *this; | |
1173 } | |
1174 | |
1175 template <class T> | |
1176 Array<T> | |
1177 Array<T>::transpose (void) const | |
1178 { | |
4548 | 1179 assert (ndims () == 2); |
1180 | |
5275 | 1181 octave_idx_type nr = dim1 (); |
1182 octave_idx_type nc = dim2 (); | |
4513 | 1183 |
1184 if (nr > 1 && nc > 1) | |
1185 { | |
1186 Array<T> result (dim_vector (nc, nr)); | |
1187 | |
5275 | 1188 for (octave_idx_type j = 0; j < nc; j++) |
1189 for (octave_idx_type i = 0; i < nr; i++) | |
4513 | 1190 result.xelem (j, i) = xelem (i, j); |
1191 | |
1192 return result; | |
1193 } | |
1194 else | |
1195 { | |
1196 // Fast transpose for vectors and empty matrices | |
1197 return Array<T> (*this, dim_vector (nc, nr)); | |
1198 } | |
1199 } | |
1200 | |
1201 template <class T> | |
1202 T * | |
1203 Array<T>::fortran_vec (void) | |
1204 { | |
6881 | 1205 make_unique (); |
1206 | |
4513 | 1207 return rep->data; |
1208 } | |
1209 | |
1210 template <class T> | |
3933 | 1211 void |
4517 | 1212 Array<T>::maybe_delete_dims (void) |
1213 { | |
4587 | 1214 int nd = dimensions.length (); |
4517 | 1215 |
1216 dim_vector new_dims (1, 1); | |
1217 | |
1218 bool delete_dims = true; | |
1219 | |
4587 | 1220 for (int i = nd - 1; i >= 0; i--) |
4517 | 1221 { |
1222 if (delete_dims) | |
1223 { | |
1224 if (dimensions(i) != 1) | |
1225 { | |
1226 delete_dims = false; | |
1227 | |
1228 new_dims = dim_vector (i + 1, dimensions(i)); | |
1229 } | |
1230 } | |
1231 else | |
1232 new_dims(i) = dimensions(i); | |
1233 } | |
4530 | 1234 |
4587 | 1235 if (nd != new_dims.length ()) |
4517 | 1236 dimensions = new_dims; |
1237 } | |
1238 | |
1239 template <class T> | |
1240 void | |
6881 | 1241 Array<T>::clear_index (void) const |
4517 | 1242 { |
1243 delete [] idx; | |
1244 idx = 0; | |
1245 idx_count = 0; | |
1246 } | |
1247 | |
1248 template <class T> | |
1249 void | |
6881 | 1250 Array<T>::set_index (const idx_vector& idx_arg) const |
4517 | 1251 { |
1252 int nd = ndims (); | |
1253 | |
1254 if (! idx && nd > 0) | |
1255 idx = new idx_vector [nd]; | |
1256 | |
1257 if (idx_count < nd) | |
1258 { | |
1259 idx[idx_count++] = idx_arg; | |
1260 } | |
1261 else | |
1262 { | |
1263 idx_vector *new_idx = new idx_vector [idx_count+1]; | |
1264 | |
1265 for (int i = 0; i < idx_count; i++) | |
1266 new_idx[i] = idx[i]; | |
1267 | |
1268 new_idx[idx_count++] = idx_arg; | |
1269 | |
1270 delete [] idx; | |
1271 | |
1272 idx = new_idx; | |
1273 } | |
1274 } | |
1275 | |
1276 template <class T> | |
1277 void | |
1278 Array<T>::maybe_delete_elements (idx_vector& idx_arg) | |
1279 { | |
1280 switch (ndims ()) | |
1281 { | |
1282 case 1: | |
1283 maybe_delete_elements_1 (idx_arg); | |
1284 break; | |
1285 | |
1286 case 2: | |
1287 maybe_delete_elements_2 (idx_arg); | |
1288 break; | |
1289 | |
1290 default: | |
1291 (*current_liboctave_error_handler) | |
1292 ("Array<T>::maybe_delete_elements: invalid operation"); | |
1293 break; | |
1294 } | |
1295 } | |
1296 | |
1297 template <class T> | |
1298 void | |
1299 Array<T>::maybe_delete_elements_1 (idx_vector& idx_arg) | |
1300 { | |
5275 | 1301 octave_idx_type len = length (); |
4517 | 1302 |
1303 if (len == 0) | |
1304 return; | |
1305 | |
1306 if (idx_arg.is_colon_equiv (len, 1)) | |
1307 resize_no_fill (0); | |
1308 else | |
1309 { | |
1310 int num_to_delete = idx_arg.length (len); | |
1311 | |
1312 if (num_to_delete != 0) | |
1313 { | |
5275 | 1314 octave_idx_type new_len = len; |
1315 | |
1316 octave_idx_type iidx = 0; | |
1317 | |
1318 for (octave_idx_type i = 0; i < len; i++) | |
4517 | 1319 if (i == idx_arg.elem (iidx)) |
1320 { | |
1321 iidx++; | |
1322 new_len--; | |
1323 | |
1324 if (iidx == num_to_delete) | |
1325 break; | |
1326 } | |
1327 | |
1328 if (new_len > 0) | |
1329 { | |
1330 T *new_data = new T [new_len]; | |
1331 | |
5275 | 1332 octave_idx_type ii = 0; |
4517 | 1333 iidx = 0; |
5275 | 1334 for (octave_idx_type i = 0; i < len; i++) |
4517 | 1335 { |
1336 if (iidx < num_to_delete && i == idx_arg.elem (iidx)) | |
1337 iidx++; | |
1338 else | |
1339 { | |
6884 | 1340 new_data[ii] = xelem (i); |
4517 | 1341 ii++; |
1342 } | |
1343 } | |
1344 | |
1345 if (--rep->count <= 0) | |
1346 delete rep; | |
1347 | |
1348 rep = new typename Array<T>::ArrayRep (new_data, new_len); | |
1349 | |
1350 dimensions.resize (1); | |
1351 dimensions(0) = new_len; | |
1352 } | |
1353 else | |
1354 (*current_liboctave_error_handler) | |
1355 ("A(idx) = []: index out of range"); | |
1356 } | |
1357 } | |
1358 } | |
1359 | |
1360 template <class T> | |
1361 void | |
1362 Array<T>::maybe_delete_elements_2 (idx_vector& idx_arg) | |
1363 { | |
4548 | 1364 assert (ndims () == 2); |
1365 | |
5275 | 1366 octave_idx_type nr = dim1 (); |
1367 octave_idx_type nc = dim2 (); | |
4517 | 1368 |
5275 | 1369 octave_idx_type n; |
4517 | 1370 if (nr == 1) |
1371 n = nc; | |
1372 else if (nc == 1) | |
1373 n = nr; | |
1374 else | |
1375 { | |
4756 | 1376 // Reshape to row vector for Matlab compatibility. |
1377 | |
1378 n = nr * nc; | |
1379 nr = 1; | |
1380 nc = n; | |
4517 | 1381 } |
1382 | |
6525 | 1383 if (nr > 0 && nc > 0 && idx_arg.is_colon_equiv (n, 1)) |
4517 | 1384 { |
1385 // Either A(:) = [] or A(idx) = [] with idx enumerating all | |
1386 // elements, so we delete all elements and return [](0x0). To | |
1387 // preserve the orientation of the vector, you have to use | |
1388 // A(idx,:) = [] (delete rows) or A(:,idx) (delete columns). | |
1389 | |
1390 resize_no_fill (0, 0); | |
1391 return; | |
1392 } | |
1393 | |
1394 idx_arg.sort (true); | |
1395 | |
5275 | 1396 octave_idx_type num_to_delete = idx_arg.length (n); |
4517 | 1397 |
1398 if (num_to_delete != 0) | |
1399 { | |
5275 | 1400 octave_idx_type new_n = n; |
1401 | |
1402 octave_idx_type iidx = 0; | |
1403 | |
1404 for (octave_idx_type i = 0; i < n; i++) | |
4517 | 1405 if (i == idx_arg.elem (iidx)) |
1406 { | |
1407 iidx++; | |
1408 new_n--; | |
1409 | |
1410 if (iidx == num_to_delete) | |
1411 break; | |
1412 } | |
1413 | |
1414 if (new_n > 0) | |
1415 { | |
1416 T *new_data = new T [new_n]; | |
1417 | |
5275 | 1418 octave_idx_type ii = 0; |
4517 | 1419 iidx = 0; |
5275 | 1420 for (octave_idx_type i = 0; i < n; i++) |
4517 | 1421 { |
1422 if (iidx < num_to_delete && i == idx_arg.elem (iidx)) | |
1423 iidx++; | |
1424 else | |
1425 { | |
6884 | 1426 new_data[ii] = xelem (i); |
4517 | 1427 |
1428 ii++; | |
1429 } | |
1430 } | |
1431 | |
1432 if (--(Array<T>::rep)->count <= 0) | |
1433 delete Array<T>::rep; | |
1434 | |
1435 Array<T>::rep = new typename Array<T>::ArrayRep (new_data, new_n); | |
1436 | |
1437 dimensions.resize (2); | |
1438 | |
1439 if (nr == 1) | |
1440 { | |
1441 dimensions(0) = 1; | |
1442 dimensions(1) = new_n; | |
1443 } | |
1444 else | |
1445 { | |
1446 dimensions(0) = new_n; | |
1447 dimensions(1) = 1; | |
1448 } | |
1449 } | |
1450 else | |
1451 (*current_liboctave_error_handler) | |
1452 ("A(idx) = []: index out of range"); | |
1453 } | |
1454 } | |
1455 | |
1456 template <class T> | |
1457 void | |
1458 Array<T>::maybe_delete_elements (idx_vector& idx_i, idx_vector& idx_j) | |
1459 { | |
4548 | 1460 assert (ndims () == 2); |
1461 | |
5275 | 1462 octave_idx_type nr = dim1 (); |
1463 octave_idx_type nc = dim2 (); | |
4517 | 1464 |
1465 if (nr == 0 && nc == 0) | |
1466 return; | |
1467 | |
1468 if (idx_i.is_colon ()) | |
1469 { | |
1470 if (idx_j.is_colon ()) | |
1471 { | |
1472 // A(:,:) -- We are deleting columns and rows, so the result | |
1473 // is [](0x0). | |
1474 | |
1475 resize_no_fill (0, 0); | |
1476 return; | |
1477 } | |
1478 | |
1479 if (idx_j.is_colon_equiv (nc, 1)) | |
1480 { | |
1481 // A(:,j) -- We are deleting columns by enumerating them, | |
1482 // If we enumerate all of them, we should have zero columns | |
1483 // with the same number of rows that we started with. | |
1484 | |
1485 resize_no_fill (nr, 0); | |
1486 return; | |
1487 } | |
1488 } | |
1489 | |
1490 if (idx_j.is_colon () && idx_i.is_colon_equiv (nr, 1)) | |
1491 { | |
1492 // A(i,:) -- We are deleting rows by enumerating them. If we | |
1493 // enumerate all of them, we should have zero rows with the | |
1494 // same number of columns that we started with. | |
1495 | |
1496 resize_no_fill (0, nc); | |
1497 return; | |
1498 } | |
1499 | |
1500 if (idx_i.is_colon_equiv (nr, 1)) | |
1501 { | |
1502 if (idx_j.is_colon_equiv (nc, 1)) | |
1503 resize_no_fill (0, 0); | |
1504 else | |
1505 { | |
1506 idx_j.sort (true); | |
1507 | |
5275 | 1508 octave_idx_type num_to_delete = idx_j.length (nc); |
4517 | 1509 |
1510 if (num_to_delete != 0) | |
1511 { | |
1512 if (nr == 1 && num_to_delete == nc) | |
1513 resize_no_fill (0, 0); | |
1514 else | |
1515 { | |
5275 | 1516 octave_idx_type new_nc = nc; |
1517 | |
1518 octave_idx_type iidx = 0; | |
1519 | |
1520 for (octave_idx_type j = 0; j < nc; j++) | |
4517 | 1521 if (j == idx_j.elem (iidx)) |
1522 { | |
1523 iidx++; | |
1524 new_nc--; | |
1525 | |
1526 if (iidx == num_to_delete) | |
1527 break; | |
1528 } | |
1529 | |
1530 if (new_nc > 0) | |
1531 { | |
1532 T *new_data = new T [nr * new_nc]; | |
1533 | |
5275 | 1534 octave_idx_type jj = 0; |
4517 | 1535 iidx = 0; |
5275 | 1536 for (octave_idx_type j = 0; j < nc; j++) |
4517 | 1537 { |
1538 if (iidx < num_to_delete && j == idx_j.elem (iidx)) | |
1539 iidx++; | |
1540 else | |
1541 { | |
5275 | 1542 for (octave_idx_type i = 0; i < nr; i++) |
6884 | 1543 new_data[nr*jj+i] = xelem (i, j); |
4517 | 1544 jj++; |
1545 } | |
1546 } | |
1547 | |
1548 if (--(Array<T>::rep)->count <= 0) | |
1549 delete Array<T>::rep; | |
1550 | |
1551 Array<T>::rep = new typename Array<T>::ArrayRep (new_data, nr * new_nc); | |
1552 | |
1553 dimensions.resize (2); | |
1554 dimensions(1) = new_nc; | |
1555 } | |
1556 else | |
1557 (*current_liboctave_error_handler) | |
1558 ("A(idx) = []: index out of range"); | |
1559 } | |
1560 } | |
1561 } | |
1562 } | |
1563 else if (idx_j.is_colon_equiv (nc, 1)) | |
1564 { | |
1565 if (idx_i.is_colon_equiv (nr, 1)) | |
1566 resize_no_fill (0, 0); | |
1567 else | |
1568 { | |
1569 idx_i.sort (true); | |
1570 | |
5275 | 1571 octave_idx_type num_to_delete = idx_i.length (nr); |
4517 | 1572 |
1573 if (num_to_delete != 0) | |
1574 { | |
1575 if (nc == 1 && num_to_delete == nr) | |
1576 resize_no_fill (0, 0); | |
1577 else | |
1578 { | |
5275 | 1579 octave_idx_type new_nr = nr; |
1580 | |
1581 octave_idx_type iidx = 0; | |
1582 | |
1583 for (octave_idx_type i = 0; i < nr; i++) | |
4517 | 1584 if (i == idx_i.elem (iidx)) |
1585 { | |
1586 iidx++; | |
1587 new_nr--; | |
1588 | |
1589 if (iidx == num_to_delete) | |
1590 break; | |
1591 } | |
1592 | |
1593 if (new_nr > 0) | |
1594 { | |
1595 T *new_data = new T [new_nr * nc]; | |
1596 | |
5275 | 1597 octave_idx_type ii = 0; |
4517 | 1598 iidx = 0; |
5275 | 1599 for (octave_idx_type i = 0; i < nr; i++) |
4517 | 1600 { |
1601 if (iidx < num_to_delete && i == idx_i.elem (iidx)) | |
1602 iidx++; | |
1603 else | |
1604 { | |
5275 | 1605 for (octave_idx_type j = 0; j < nc; j++) |
6884 | 1606 new_data[new_nr*j+ii] = xelem (i, j); |
4517 | 1607 ii++; |
1608 } | |
1609 } | |
1610 | |
1611 if (--(Array<T>::rep)->count <= 0) | |
1612 delete Array<T>::rep; | |
1613 | |
1614 Array<T>::rep = new typename Array<T>::ArrayRep (new_data, new_nr * nc); | |
1615 | |
1616 dimensions.resize (2); | |
1617 dimensions(0) = new_nr; | |
1618 } | |
1619 else | |
1620 (*current_liboctave_error_handler) | |
1621 ("A(idx) = []: index out of range"); | |
1622 } | |
1623 } | |
1624 } | |
1625 } | |
1626 } | |
1627 | |
1628 template <class T> | |
1629 void | |
1630 Array<T>::maybe_delete_elements (idx_vector&, idx_vector&, idx_vector&) | |
1631 { | |
1632 assert (0); | |
1633 } | |
1634 | |
1635 template <class T> | |
1636 void | |
4585 | 1637 Array<T>::maybe_delete_elements (Array<idx_vector>& ra_idx, const T& rfv) |
4517 | 1638 { |
5275 | 1639 octave_idx_type n_idx = ra_idx.length (); |
4517 | 1640 |
1641 dim_vector lhs_dims = dims (); | |
1642 | |
6388 | 1643 int n_lhs_dims = lhs_dims.length (); |
1644 | |
4821 | 1645 if (lhs_dims.all_zero ()) |
1646 return; | |
1647 | |
6384 | 1648 if (n_idx == 1 && ra_idx(0).is_colon ()) |
1649 { | |
1650 resize (dim_vector (0, 0), rfv); | |
1651 return; | |
1652 } | |
1653 | |
6388 | 1654 if (n_idx > n_lhs_dims) |
1655 { | |
1656 for (int i = n_idx; i < n_lhs_dims; i++) | |
1657 { | |
1658 // Ensure that extra indices are either colon or 1. | |
1659 | |
1660 if (! ra_idx(i).is_colon_equiv (1, 1)) | |
1661 { | |
1662 (*current_liboctave_error_handler) | |
1663 ("index exceeds array dimensions"); | |
1664 return; | |
1665 } | |
1666 } | |
1667 | |
1668 ra_idx.resize (n_lhs_dims); | |
1669 | |
1670 n_idx = n_lhs_dims; | |
1671 } | |
4757 | 1672 |
4740 | 1673 Array<int> idx_is_colon (n_idx, 0); |
1674 | |
1675 Array<int> idx_is_colon_equiv (n_idx, 0); | |
4517 | 1676 |
1677 // Initialization of colon arrays. | |
4757 | 1678 |
5275 | 1679 for (octave_idx_type i = 0; i < n_idx; i++) |
4517 | 1680 { |
4585 | 1681 idx_is_colon_equiv(i) = ra_idx(i).is_colon_equiv (lhs_dims(i), 1); |
1682 | |
1683 idx_is_colon(i) = ra_idx(i).is_colon (); | |
4517 | 1684 } |
1685 | |
4755 | 1686 bool idx_ok = true; |
1687 | |
1688 // Check for index out of bounds. | |
1689 | |
5275 | 1690 for (octave_idx_type i = 0 ; i < n_idx - 1; i++) |
4517 | 1691 { |
4755 | 1692 if (! (idx_is_colon(i) || idx_is_colon_equiv(i))) |
1693 { | |
1694 ra_idx(i).sort (true); | |
4757 | 1695 |
4755 | 1696 if (ra_idx(i).max () > lhs_dims(i)) |
1697 { | |
1698 (*current_liboctave_error_handler) | |
1699 ("index exceeds array dimensions"); | |
4757 | 1700 |
4755 | 1701 idx_ok = false; |
1702 break; | |
1703 } | |
1704 else if (ra_idx(i).min () < 0) // I believe this is checked elsewhere | |
1705 { | |
1706 (*current_liboctave_error_handler) | |
1707 ("index must be one or larger"); | |
1708 | |
1709 idx_ok = false; | |
1710 break; | |
1711 } | |
1712 } | |
4517 | 1713 } |
4757 | 1714 |
4755 | 1715 if (n_idx <= n_lhs_dims) |
4517 | 1716 { |
5275 | 1717 octave_idx_type last_idx = ra_idx(n_idx-1).max (); |
1718 | |
1719 octave_idx_type sum_el = lhs_dims(n_idx-1); | |
1720 | |
1721 for (octave_idx_type i = n_idx; i < n_lhs_dims; i++) | |
4755 | 1722 sum_el *= lhs_dims(i); |
1723 | |
1724 if (last_idx > sum_el - 1) | |
1725 { | |
1726 (*current_liboctave_error_handler) | |
1727 ("index exceeds array dimensions"); | |
1728 | |
1729 idx_ok = false; | |
1730 } | |
4757 | 1731 } |
4755 | 1732 |
1733 if (idx_ok) | |
1734 { | |
1735 if (n_idx > 1 | |
1736 && (all_ones (idx_is_colon) || all_ones (idx_is_colon_equiv))) | |
4517 | 1737 { |
4755 | 1738 // A(:,:,:) -- we are deleting elements in all dimensions, so |
1739 // the result is [](0x0x0). | |
1740 | |
1741 dim_vector zeros; | |
1742 zeros.resize (n_idx); | |
1743 | |
1744 for (int i = 0; i < n_idx; i++) | |
1745 zeros(i) = 0; | |
1746 | |
1747 resize (zeros, rfv); | |
4517 | 1748 } |
1749 | |
4755 | 1750 else if (n_idx > 1 |
1751 && num_ones (idx_is_colon) == n_idx - 1 | |
1752 && num_ones (idx_is_colon_equiv) == n_idx) | |
1753 { | |
1754 // A(:,:,j) -- we are deleting elements in one dimension by | |
1755 // enumerating them. | |
1756 // | |
1757 // If we enumerate all of the elements, we should have zero | |
1758 // elements in that dimension with the same number of elements | |
1759 // in the other dimensions that we started with. | |
1760 | |
1761 dim_vector temp_dims; | |
1762 temp_dims.resize (n_idx); | |
1763 | |
5275 | 1764 for (octave_idx_type i = 0; i < n_idx; i++) |
4755 | 1765 { |
1766 if (idx_is_colon (i)) | |
1767 temp_dims(i) = lhs_dims(i); | |
1768 else | |
1769 temp_dims(i) = 0; | |
1770 } | |
1771 | |
1772 resize (temp_dims); | |
1773 } | |
1774 else if (n_idx > 1 && num_ones (idx_is_colon) == n_idx - 1) | |
4741 | 1775 { |
4755 | 1776 // We have colons in all indices except for one. |
1777 // This index tells us which slice to delete | |
1778 | |
1779 if (n_idx < n_lhs_dims) | |
1780 { | |
1781 // Collapse dimensions beyond last index. | |
1782 | |
5781 | 1783 if (! (ra_idx(n_idx-1).is_colon ())) |
1784 (*current_liboctave_warning_with_id_handler) | |
1785 ("Octave:fortran-indexing", | |
1786 "fewer indices than dimensions for N-d array"); | |
4755 | 1787 |
5275 | 1788 for (octave_idx_type i = n_idx; i < n_lhs_dims; i++) |
4755 | 1789 lhs_dims(n_idx-1) *= lhs_dims(i); |
1790 | |
1791 lhs_dims.resize (n_idx); | |
1792 | |
1793 // Reshape *this. | |
1794 dimensions = lhs_dims; | |
1795 } | |
1796 | |
1797 int non_col = 0; | |
1798 | |
1799 // Find the non-colon column. | |
1800 | |
5275 | 1801 for (octave_idx_type i = 0; i < n_idx; i++) |
4755 | 1802 { |
1803 if (! idx_is_colon(i)) | |
1804 non_col = i; | |
1805 } | |
1806 | |
1807 // The length of the non-colon dimension. | |
1808 | |
5275 | 1809 octave_idx_type non_col_dim = lhs_dims (non_col); |
1810 | |
1811 octave_idx_type num_to_delete = ra_idx(non_col).length (lhs_dims (non_col)); | |
4755 | 1812 |
1813 if (num_to_delete > 0) | |
1814 { | |
1815 int temp = lhs_dims.num_ones (); | |
1816 | |
1817 if (non_col_dim == 1) | |
1818 temp--; | |
1819 | |
1820 if (temp == n_idx - 1 && num_to_delete == non_col_dim) | |
1821 { | |
1822 // We have A with (1x1x4), where A(1,:,1:4) | |
1823 // Delete all (0x0x0) | |
1824 | |
1825 dim_vector zero_dims (n_idx, 0); | |
1826 | |
1827 resize (zero_dims, rfv); | |
1828 } | |
1829 else | |
1830 { | |
1831 // New length of non-colon dimension | |
1832 // (calculated in the next for loop) | |
1833 | |
5275 | 1834 octave_idx_type new_dim = non_col_dim; |
1835 | |
1836 octave_idx_type iidx = 0; | |
1837 | |
1838 for (octave_idx_type j = 0; j < non_col_dim; j++) | |
4755 | 1839 if (j == ra_idx(non_col).elem (iidx)) |
1840 { | |
1841 iidx++; | |
1842 | |
1843 new_dim--; | |
1844 | |
1845 if (iidx == num_to_delete) | |
1846 break; | |
1847 } | |
1848 | |
1849 // Creating the new nd array after deletions. | |
1850 | |
1851 if (new_dim > 0) | |
1852 { | |
1853 // Calculate number of elements in new array. | |
1854 | |
5275 | 1855 octave_idx_type num_new_elem=1; |
4755 | 1856 |
1857 for (int i = 0; i < n_idx; i++) | |
1858 { | |
1859 if (i == non_col) | |
1860 num_new_elem *= new_dim; | |
1861 | |
1862 else | |
1863 num_new_elem *= lhs_dims(i); | |
1864 } | |
1865 | |
1866 T *new_data = new T [num_new_elem]; | |
1867 | |
5275 | 1868 Array<octave_idx_type> result_idx (n_lhs_dims, 0); |
4755 | 1869 |
1870 dim_vector new_lhs_dim = lhs_dims; | |
1871 | |
1872 new_lhs_dim(non_col) = new_dim; | |
1873 | |
5275 | 1874 octave_idx_type num_elem = 1; |
1875 | |
1876 octave_idx_type numidx = 0; | |
1877 | |
1878 octave_idx_type n = length (); | |
4755 | 1879 |
1880 for (int i = 0; i < n_lhs_dims; i++) | |
1881 if (i != non_col) | |
1882 num_elem *= lhs_dims(i); | |
1883 | |
1884 num_elem *= ra_idx(non_col).capacity (); | |
1885 | |
5275 | 1886 for (octave_idx_type i = 0; i < n; i++) |
4755 | 1887 { |
1888 if (numidx < num_elem | |
1889 && is_in (result_idx(non_col), ra_idx(non_col))) | |
1890 numidx++; | |
1891 | |
1892 else | |
1893 { | |
5275 | 1894 Array<octave_idx_type> temp_result_idx = result_idx; |
1895 | |
1896 octave_idx_type num_lgt = how_many_lgt (result_idx(non_col), | |
4755 | 1897 ra_idx(non_col)); |
1898 | |
1899 temp_result_idx(non_col) -= num_lgt; | |
1900 | |
5275 | 1901 octave_idx_type kidx |
4755 | 1902 = ::compute_index (temp_result_idx, new_lhs_dim); |
1903 | |
6884 | 1904 new_data[kidx] = xelem (result_idx); |
4755 | 1905 } |
1906 | |
1907 increment_index (result_idx, lhs_dims); | |
1908 } | |
1909 | |
1910 if (--rep->count <= 0) | |
1911 delete rep; | |
1912 | |
1913 rep = new typename Array<T>::ArrayRep (new_data, | |
1914 num_new_elem); | |
1915 | |
1916 dimensions = new_lhs_dim; | |
1917 } | |
1918 } | |
1919 } | |
4517 | 1920 } |
4755 | 1921 else if (n_idx == 1) |
4517 | 1922 { |
4821 | 1923 // This handle cases where we only have one index (not |
1924 // colon). The index denotes which elements we should | |
1925 // delete in the array which can be of any dimension. We | |
1926 // return a column vector, except for the case where we are | |
1927 // operating on a row vector. The elements are numerated | |
1928 // column by column. | |
4755 | 1929 // |
1930 // A(3,3,3)=2; | |
1931 // A(3:5) = []; A(6)=[] | |
4757 | 1932 |
5275 | 1933 octave_idx_type lhs_numel = numel (); |
4757 | 1934 |
4821 | 1935 idx_vector idx_vec = ra_idx(0); |
1936 | |
5781 | 1937 idx_vec.freeze (lhs_numel, 0, true); |
4821 | 1938 |
1939 idx_vec.sort (true); | |
1940 | |
5275 | 1941 octave_idx_type num_to_delete = idx_vec.length (lhs_numel); |
4821 | 1942 |
1943 if (num_to_delete > 0) | |
4517 | 1944 { |
5275 | 1945 octave_idx_type new_numel = lhs_numel - num_to_delete; |
4821 | 1946 |
1947 T *new_data = new T[new_numel]; | |
1948 | |
5275 | 1949 Array<octave_idx_type> lhs_ra_idx (ndims (), 0); |
1950 | |
1951 octave_idx_type ii = 0; | |
1952 octave_idx_type iidx = 0; | |
1953 | |
1954 for (octave_idx_type i = 0; i < lhs_numel; i++) | |
4755 | 1955 { |
4821 | 1956 if (iidx < num_to_delete && i == idx_vec.elem (iidx)) |
1957 { | |
1958 iidx++; | |
1959 } | |
1960 else | |
1961 { | |
6884 | 1962 new_data[ii++] = xelem (lhs_ra_idx); |
4821 | 1963 } |
1964 | |
1965 increment_index (lhs_ra_idx, lhs_dims); | |
1966 } | |
1967 | |
1968 if (--(Array<T>::rep)->count <= 0) | |
1969 delete Array<T>::rep; | |
1970 | |
1971 Array<T>::rep = new typename Array<T>::ArrayRep (new_data, new_numel); | |
1972 | |
1973 dimensions.resize (2); | |
1974 | |
1975 if (lhs_dims.length () == 2 && lhs_dims(1) == 1) | |
1976 { | |
1977 dimensions(0) = new_numel; | |
1978 dimensions(1) = 1; | |
4755 | 1979 } |
1980 else | |
1981 { | |
4821 | 1982 dimensions(0) = 1; |
1983 dimensions(1) = new_numel; | |
4755 | 1984 } |
4517 | 1985 } |
1986 } | |
4755 | 1987 else if (num_ones (idx_is_colon) < n_idx) |
1988 { | |
1989 (*current_liboctave_error_handler) | |
1990 ("a null assignment can have only one non-colon index"); | |
1991 } | |
4517 | 1992 } |
1993 } | |
1994 | |
1995 template <class T> | |
1996 Array<T> | |
6881 | 1997 Array<T>::value (void) const |
4517 | 1998 { |
1999 Array<T> retval; | |
2000 | |
2001 int n_idx = index_count (); | |
2002 | |
2003 if (n_idx == 2) | |
2004 { | |
2005 idx_vector *tmp = get_idx (); | |
2006 | |
2007 idx_vector idx_i = tmp[0]; | |
2008 idx_vector idx_j = tmp[1]; | |
2009 | |
2010 retval = index (idx_i, idx_j); | |
2011 } | |
2012 else if (n_idx == 1) | |
2013 { | |
2014 retval = index (idx[0]); | |
2015 } | |
2016 else | |
2017 (*current_liboctave_error_handler) | |
2018 ("Array<T>::value: invalid number of indices specified"); | |
2019 | |
2020 clear_index (); | |
2021 | |
2022 return retval; | |
2023 } | |
2024 | |
2025 template <class T> | |
2026 Array<T> | |
2027 Array<T>::index (idx_vector& idx_arg, int resize_ok, const T& rfv) const | |
2028 { | |
2029 Array<T> retval; | |
2030 | |
5081 | 2031 dim_vector dv = idx_arg.orig_dimensions (); |
2032 | |
2033 if (dv.length () > 2 || ndims () > 2) | |
2034 retval = indexN (idx_arg, resize_ok, rfv); | |
2035 else | |
4517 | 2036 { |
5081 | 2037 switch (ndims ()) |
2038 { | |
2039 case 1: | |
2040 retval = index1 (idx_arg, resize_ok, rfv); | |
2041 break; | |
2042 | |
2043 case 2: | |
2044 retval = index2 (idx_arg, resize_ok, rfv); | |
2045 break; | |
2046 | |
2047 default: | |
2048 (*current_liboctave_error_handler) | |
2049 ("invalid array (internal error)"); | |
2050 break; | |
2051 } | |
4517 | 2052 } |
2053 | |
2054 return retval; | |
2055 } | |
2056 | |
2057 template <class T> | |
2058 Array<T> | |
2059 Array<T>::index1 (idx_vector& idx_arg, int resize_ok, const T& rfv) const | |
2060 { | |
2061 Array<T> retval; | |
2062 | |
5275 | 2063 octave_idx_type len = length (); |
2064 | |
2065 octave_idx_type n = idx_arg.freeze (len, "vector", resize_ok); | |
4517 | 2066 |
2067 if (idx_arg) | |
2068 { | |
2069 if (idx_arg.is_colon_equiv (len)) | |
2070 { | |
2071 retval = *this; | |
2072 } | |
2073 else if (n == 0) | |
2074 { | |
2075 retval.resize_no_fill (0); | |
2076 } | |
2077 else | |
2078 { | |
2079 retval.resize_no_fill (n); | |
2080 | |
5275 | 2081 for (octave_idx_type i = 0; i < n; i++) |
4517 | 2082 { |
5275 | 2083 octave_idx_type ii = idx_arg.elem (i); |
4517 | 2084 if (ii >= len) |
2085 retval.elem (i) = rfv; | |
2086 else | |
2087 retval.elem (i) = elem (ii); | |
2088 } | |
2089 } | |
2090 } | |
2091 | |
2092 // idx_vector::freeze() printed an error message for us. | |
2093 | |
2094 return retval; | |
2095 } | |
2096 | |
2097 template <class T> | |
2098 Array<T> | |
2099 Array<T>::index2 (idx_vector& idx_arg, int resize_ok, const T& rfv) const | |
2100 { | |
2101 Array<T> retval; | |
2102 | |
4548 | 2103 assert (ndims () == 2); |
2104 | |
5275 | 2105 octave_idx_type nr = dim1 (); |
2106 octave_idx_type nc = dim2 (); | |
2107 | |
2108 octave_idx_type orig_len = nr * nc; | |
4517 | 2109 |
4832 | 2110 dim_vector idx_orig_dims = idx_arg.orig_dimensions (); |
2111 | |
5275 | 2112 octave_idx_type idx_orig_rows = idx_arg.orig_rows (); |
2113 octave_idx_type idx_orig_columns = idx_arg.orig_columns (); | |
4517 | 2114 |
2115 if (idx_arg.is_colon ()) | |
2116 { | |
2117 // Fast magic colon processing. | |
2118 | |
5275 | 2119 octave_idx_type result_nr = nr * nc; |
2120 octave_idx_type result_nc = 1; | |
4517 | 2121 |
2122 retval = Array<T> (*this, dim_vector (result_nr, result_nc)); | |
2123 } | |
2124 else if (nr == 1 && nc == 1) | |
2125 { | |
2126 Array<T> tmp = Array<T>::index1 (idx_arg, resize_ok); | |
2127 | |
5275 | 2128 octave_idx_type len = tmp.length (); |
4828 | 2129 |
7573
755bf7ecc29b
eliminate one_zero stuff from idx_vector
John W. Eaton <jwe@octave.org>
parents:
7480
diff
changeset
|
2130 if (len >= idx_orig_dims.numel ()) |
4832 | 2131 retval = Array<T> (tmp, idx_orig_dims); |
4517 | 2132 } |
2133 else if (nr == 1 || nc == 1) | |
2134 { | |
2135 // If indexing a vector with a matrix, return value has same | |
2136 // shape as the index. Otherwise, it has same orientation as | |
2137 // indexed object. | |
2138 | |
4828 | 2139 Array<T> tmp = Array<T>::index1 (idx_arg, resize_ok); |
4517 | 2140 |
5275 | 2141 octave_idx_type len = tmp.length (); |
4517 | 2142 |
7573
755bf7ecc29b
eliminate one_zero stuff from idx_vector
John W. Eaton <jwe@octave.org>
parents:
7480
diff
changeset
|
2143 if (idx_orig_rows == 1 || idx_orig_columns == 1) |
4517 | 2144 { |
4827 | 2145 if (nr == 1) |
2146 retval = Array<T> (tmp, dim_vector (1, len)); | |
4517 | 2147 else |
4827 | 2148 retval = Array<T> (tmp, dim_vector (len, 1)); |
4517 | 2149 } |
4876 | 2150 else if (len >= idx_orig_dims.numel ()) |
4832 | 2151 retval = Array<T> (tmp, idx_orig_dims); |
4517 | 2152 } |
2153 else | |
2154 { | |
7573
755bf7ecc29b
eliminate one_zero stuff from idx_vector
John W. Eaton <jwe@octave.org>
parents:
7480
diff
changeset
|
2155 (*current_liboctave_warning_with_id_handler) |
755bf7ecc29b
eliminate one_zero stuff from idx_vector
John W. Eaton <jwe@octave.org>
parents:
7480
diff
changeset
|
2156 ("Octave:fortran-indexing", "single index used for matrix"); |
4517 | 2157 |
2158 // This code is only for indexing matrices. The vector | |
2159 // cases are handled above. | |
2160 | |
2161 idx_arg.freeze (nr * nc, "matrix", resize_ok); | |
2162 | |
2163 if (idx_arg) | |
2164 { | |
5275 | 2165 octave_idx_type result_nr = idx_orig_rows; |
2166 octave_idx_type result_nc = idx_orig_columns; | |
4517 | 2167 |
2168 retval.resize_no_fill (result_nr, result_nc); | |
2169 | |
5275 | 2170 octave_idx_type k = 0; |
2171 for (octave_idx_type j = 0; j < result_nc; j++) | |
4517 | 2172 { |
5275 | 2173 for (octave_idx_type i = 0; i < result_nr; i++) |
4517 | 2174 { |
5275 | 2175 octave_idx_type ii = idx_arg.elem (k++); |
4517 | 2176 if (ii >= orig_len) |
2177 retval.elem (i, j) = rfv; | |
2178 else | |
2179 { | |
5275 | 2180 octave_idx_type fr = ii % nr; |
2181 octave_idx_type fc = (ii - fr) / nr; | |
4517 | 2182 retval.elem (i, j) = elem (fr, fc); |
2183 } | |
2184 } | |
2185 } | |
2186 } | |
2187 // idx_vector::freeze() printed an error message for us. | |
2188 } | |
2189 | |
2190 return retval; | |
2191 } | |
2192 | |
2193 template <class T> | |
2194 Array<T> | |
4530 | 2195 Array<T>::indexN (idx_vector& ra_idx, int resize_ok, const T& rfv) const |
2196 { | |
2197 Array<T> retval; | |
2198 | |
5519 | 2199 dim_vector dv = dims (); |
2200 | |
2201 int n_dims = dv.length (); | |
2202 | |
2203 octave_idx_type orig_len = dv.numel (); | |
4530 | 2204 |
4757 | 2205 dim_vector idx_orig_dims = ra_idx.orig_dimensions (); |
4530 | 2206 |
2207 if (ra_idx.is_colon ()) | |
2208 { | |
4651 | 2209 // Fast magic colon processing. |
2210 | |
2211 retval = Array<T> (*this, dim_vector (orig_len, 1)); | |
4530 | 2212 } |
4651 | 2213 else |
4530 | 2214 { |
5519 | 2215 bool vec_equiv = vector_equivalent (dv); |
2216 | |
7573
755bf7ecc29b
eliminate one_zero stuff from idx_vector
John W. Eaton <jwe@octave.org>
parents:
7480
diff
changeset
|
2217 if (! (vec_equiv || ra_idx.is_colon ())) |
5781 | 2218 (*current_liboctave_warning_with_id_handler) |
2219 ("Octave:fortran-indexing", "single index used for N-d array"); | |
4530 | 2220 |
5519 | 2221 octave_idx_type frozen_len |
2222 = ra_idx.freeze (orig_len, "nd-array", resize_ok); | |
4530 | 2223 |
2224 if (ra_idx) | |
4757 | 2225 { |
5519 | 2226 dim_vector result_dims; |
2227 | |
7321 | 2228 if (vec_equiv && ! orig_len == 1) |
5519 | 2229 { |
2230 result_dims = dv; | |
2231 | |
2232 for (int i = 0; i < n_dims; i++) | |
2233 { | |
2234 if (result_dims(i) != 1) | |
2235 { | |
2236 // All but this dim should be one. | |
2237 result_dims(i) = frozen_len; | |
2238 break; | |
2239 } | |
2240 } | |
2241 } | |
2242 else | |
2243 result_dims = idx_orig_dims; | |
4530 | 2244 |
4673 | 2245 result_dims.chop_trailing_singletons (); |
2246 | |
4530 | 2247 retval.resize (result_dims); |
2248 | |
5275 | 2249 octave_idx_type n = result_dims.numel (); |
4530 | 2250 |
5275 | 2251 octave_idx_type k = 0; |
2252 | |
2253 for (octave_idx_type i = 0; i < n; i++) | |
4530 | 2254 { |
5275 | 2255 octave_idx_type ii = ra_idx.elem (k++); |
4530 | 2256 |
2257 if (ii >= orig_len) | |
5535 | 2258 retval.elem (i) = rfv; |
4530 | 2259 else |
5535 | 2260 retval.elem (i) = elem (ii); |
4530 | 2261 } |
2262 } | |
2263 } | |
2264 | |
2265 return retval; | |
2266 } | |
2267 | |
2268 template <class T> | |
2269 Array<T> | |
4517 | 2270 Array<T>::index (idx_vector& idx_i, idx_vector& idx_j, int resize_ok, |
2271 const T& rfv) const | |
2272 { | |
2273 Array<T> retval; | |
2274 | |
7189 | 2275 if (ndims () != 2) |
2276 { | |
2277 Array<idx_vector> ra_idx (2); | |
2278 ra_idx(0) = idx_i; | |
2279 ra_idx(1) = idx_j; | |
2280 return index (ra_idx, resize_ok, rfv); | |
2281 } | |
4548 | 2282 |
5275 | 2283 octave_idx_type nr = dim1 (); |
2284 octave_idx_type nc = dim2 (); | |
2285 | |
2286 octave_idx_type n = idx_i.freeze (nr, "row", resize_ok); | |
2287 octave_idx_type m = idx_j.freeze (nc, "column", resize_ok); | |
4517 | 2288 |
2289 if (idx_i && idx_j) | |
2290 { | |
2291 if (idx_i.orig_empty () || idx_j.orig_empty () || n == 0 || m == 0) | |
2292 { | |
2293 retval.resize_no_fill (n, m); | |
2294 } | |
2295 else if (idx_i.is_colon_equiv (nr) && idx_j.is_colon_equiv (nc)) | |
2296 { | |
2297 retval = *this; | |
2298 } | |
2299 else | |
2300 { | |
2301 retval.resize_no_fill (n, m); | |
2302 | |
5275 | 2303 for (octave_idx_type j = 0; j < m; j++) |
4517 | 2304 { |
5275 | 2305 octave_idx_type jj = idx_j.elem (j); |
2306 for (octave_idx_type i = 0; i < n; i++) | |
4517 | 2307 { |
5275 | 2308 octave_idx_type ii = idx_i.elem (i); |
4517 | 2309 if (ii >= nr || jj >= nc) |
2310 retval.elem (i, j) = rfv; | |
2311 else | |
2312 retval.elem (i, j) = elem (ii, jj); | |
2313 } | |
2314 } | |
2315 } | |
2316 } | |
2317 | |
2318 // idx_vector::freeze() printed an error message for us. | |
2319 | |
2320 return retval; | |
2321 } | |
2322 | |
2323 template <class T> | |
2324 Array<T> | |
5992 | 2325 Array<T>::index (Array<idx_vector>& ra_idx, int resize_ok, const T& rfv) const |
4517 | 2326 { |
4530 | 2327 // This function handles all calls with more than one idx. |
2328 // For (3x3x3), the call can be A(2,5), A(2,:,:), A(3,2,3) etc. | |
2329 | |
4517 | 2330 Array<T> retval; |
2331 | |
2332 int n_dims = dimensions.length (); | |
2333 | |
4737 | 2334 // Remove trailing singletons in ra_idx, but leave at least ndims |
2335 // elements. | |
2336 | |
5275 | 2337 octave_idx_type ra_idx_len = ra_idx.length (); |
4737 | 2338 |
4887 | 2339 bool trim_trailing_singletons = true; |
5275 | 2340 for (octave_idx_type j = ra_idx_len; j > n_dims; j--) |
4737 | 2341 { |
4887 | 2342 idx_vector iidx = ra_idx (ra_idx_len-1); |
2343 if (iidx.capacity () == 1 && trim_trailing_singletons) | |
4737 | 2344 ra_idx_len--; |
2345 else | |
4887 | 2346 trim_trailing_singletons = false; |
2347 | |
5992 | 2348 if (! resize_ok) |
2349 { | |
2350 for (octave_idx_type i = 0; i < iidx.capacity (); i++) | |
2351 if (iidx (i) != 0) | |
2352 { | |
2353 (*current_liboctave_error_handler) | |
2354 ("index exceeds N-d array dimensions"); | |
2355 | |
2356 return retval; | |
2357 } | |
2358 } | |
4737 | 2359 } |
2360 | |
2361 ra_idx.resize (ra_idx_len); | |
2362 | |
4887 | 2363 dim_vector new_dims = dims (); |
2364 dim_vector frozen_lengths; | |
2365 | |
7597
6b2a99e44ff2
shortened empty indexing fix
David Bateman <dbateman@free.fr>
parents:
7573
diff
changeset
|
2366 if (!ra_idx (ra_idx_len - 1).orig_empty () && ra_idx_len < n_dims) |
4887 | 2367 frozen_lengths = short_freeze (ra_idx, dimensions, resize_ok); |
2368 else | |
4517 | 2369 { |
4887 | 2370 new_dims.resize (ra_idx_len, 1); |
2371 frozen_lengths = freeze (ra_idx, new_dims, resize_ok); | |
4530 | 2372 } |
2373 | |
4887 | 2374 if (all_ok (ra_idx)) |
4530 | 2375 { |
4887 | 2376 if (any_orig_empty (ra_idx) || frozen_lengths.any_zero ()) |
2377 { | |
2378 frozen_lengths.chop_trailing_singletons (); | |
2379 | |
2380 retval.resize (frozen_lengths); | |
2381 } | |
2382 else if (frozen_lengths.length () == n_dims | |
2383 && all_colon_equiv (ra_idx, dimensions)) | |
2384 { | |
2385 retval = *this; | |
2386 } | |
2387 else | |
4517 | 2388 { |
4887 | 2389 dim_vector frozen_lengths_for_resize = frozen_lengths; |
2390 | |
2391 frozen_lengths_for_resize.chop_trailing_singletons (); | |
2392 | |
2393 retval.resize (frozen_lengths_for_resize); | |
2394 | |
5275 | 2395 octave_idx_type n = retval.length (); |
2396 | |
2397 Array<octave_idx_type> result_idx (ra_idx.length (), 0); | |
2398 | |
2399 Array<octave_idx_type> elt_idx; | |
2400 | |
2401 for (octave_idx_type i = 0; i < n; i++) | |
4530 | 2402 { |
4887 | 2403 elt_idx = get_elt_idx (ra_idx, result_idx); |
2404 | |
5275 | 2405 octave_idx_type numelem_elt = get_scalar_idx (elt_idx, new_dims); |
4887 | 2406 |
5992 | 2407 if (numelem_elt >= length () || numelem_elt < 0) |
2408 retval.elem (i) = rfv; | |
4887 | 2409 else |
2410 retval.elem (i) = elem (numelem_elt); | |
2411 | |
2412 increment_index (result_idx, frozen_lengths); | |
2413 | |
4517 | 2414 } |
2415 } | |
2416 } | |
2417 | |
2418 return retval; | |
2419 } | |
2420 | |
7433 | 2421 template <class T> |
2422 bool | |
2423 ascending_compare (T a, T b) | |
2424 { | |
2425 return (a < b); | |
2426 } | |
2427 | |
2428 template <class T> | |
2429 bool | |
2430 descending_compare (T a, T b) | |
2431 { | |
2432 return (a > b); | |
2433 } | |
2434 | |
2435 template <class T> | |
2436 bool | |
2437 ascending_compare (vec_index<T> *a, vec_index<T> *b) | |
2438 { | |
2439 return (a->vec < b->vec); | |
2440 } | |
2441 | |
2442 template <class T> | |
2443 bool | |
2444 descending_compare (vec_index<T> *a, vec_index<T> *b) | |
2445 { | |
2446 return (a->vec > b->vec); | |
2447 } | |
2448 | |
2449 template <class T> | |
2450 Array<T> | |
2451 Array<T>::sort (octave_idx_type dim, sortmode mode) const | |
2452 { | |
2453 Array<T> m = *this; | |
2454 | |
2455 dim_vector dv = m.dims (); | |
2456 | |
2457 if (m.length () < 1) | |
2458 return m; | |
2459 | |
2460 octave_idx_type ns = dv(dim); | |
2461 octave_idx_type iter = dv.numel () / ns; | |
2462 octave_idx_type stride = 1; | |
2463 for (int i = 0; i < dim; i++) | |
2464 stride *= dv(i); | |
2465 | |
2466 T *v = m.fortran_vec (); | |
2467 octave_sort<T> lsort; | |
2468 | |
2469 if (mode == ASCENDING) | |
2470 lsort.set_compare (ascending_compare); | |
2471 else if (mode == DESCENDING) | |
2472 lsort.set_compare (descending_compare); | |
7463
2467639bd8c0
eliminate UNDEFINED sort mode
John W. Eaton <jwe@octave.org>
parents:
7443
diff
changeset
|
2473 else |
2467639bd8c0
eliminate UNDEFINED sort mode
John W. Eaton <jwe@octave.org>
parents:
7443
diff
changeset
|
2474 abort (); |
7433 | 2475 |
2476 if (stride == 1) | |
2477 { | |
2478 for (octave_idx_type j = 0; j < iter; j++) | |
2479 { | |
2480 lsort.sort (v, ns); | |
2481 v += ns; | |
2482 } | |
2483 } | |
2484 else | |
2485 { | |
2486 // Don't use OCTAVE_LOCAL_BUFFER here as it doesn't work with bool | |
2487 // on some compilers. | |
2488 Array<T> vi (ns); | |
2489 T *pvi = vi.fortran_vec (); | |
2490 | |
2491 for (octave_idx_type j = 0; j < iter; j++) | |
2492 { | |
2493 octave_idx_type offset = j; | |
2494 octave_idx_type offset2 = 0; | |
2495 while (offset >= stride) | |
2496 { | |
2497 offset -= stride; | |
2498 offset2++; | |
2499 } | |
2500 offset += offset2 * stride * ns; | |
2501 | |
2502 for (octave_idx_type i = 0; i < ns; i++) | |
2503 pvi[i] = v[i*stride + offset]; | |
2504 | |
2505 lsort.sort (pvi, ns); | |
2506 | |
2507 for (octave_idx_type i = 0; i < ns; i++) | |
2508 v[i*stride + offset] = pvi[i]; | |
2509 } | |
2510 } | |
2511 | |
2512 return m; | |
2513 } | |
2514 | |
2515 template <class T> | |
2516 Array<T> | |
2517 Array<T>::sort (Array<octave_idx_type> &sidx, octave_idx_type dim, | |
2518 sortmode mode) const | |
2519 { | |
2520 Array<T> m = *this; | |
2521 | |
2522 dim_vector dv = m.dims (); | |
2523 | |
2524 if (m.length () < 1) | |
2525 { | |
2526 sidx = Array<octave_idx_type> (dv); | |
2527 return m; | |
2528 } | |
2529 | |
2530 octave_idx_type ns = dv(dim); | |
2531 octave_idx_type iter = dv.numel () / ns; | |
2532 octave_idx_type stride = 1; | |
2533 for (int i = 0; i < dim; i++) | |
2534 stride *= dv(i); | |
2535 | |
2536 T *v = m.fortran_vec (); | |
2537 octave_sort<vec_index<T> *> indexed_sort; | |
2538 | |
2539 if (mode == ASCENDING) | |
2540 indexed_sort.set_compare (ascending_compare); | |
2541 else if (mode == DESCENDING) | |
2542 indexed_sort.set_compare (descending_compare); | |
7463
2467639bd8c0
eliminate UNDEFINED sort mode
John W. Eaton <jwe@octave.org>
parents:
7443
diff
changeset
|
2543 else |
2467639bd8c0
eliminate UNDEFINED sort mode
John W. Eaton <jwe@octave.org>
parents:
7443
diff
changeset
|
2544 abort (); |
7433 | 2545 |
2546 OCTAVE_LOCAL_BUFFER (vec_index<T> *, vi, ns); | |
2547 OCTAVE_LOCAL_BUFFER (vec_index<T>, vix, ns); | |
2548 | |
2549 for (octave_idx_type i = 0; i < ns; i++) | |
2550 vi[i] = &vix[i]; | |
2551 | |
2552 sidx = Array<octave_idx_type> (dv); | |
2553 | |
2554 if (stride == 1) | |
2555 { | |
2556 for (octave_idx_type j = 0; j < iter; j++) | |
2557 { | |
2558 octave_idx_type offset = j * ns; | |
2559 | |
2560 for (octave_idx_type i = 0; i < ns; i++) | |
2561 { | |
2562 vi[i]->vec = v[i]; | |
2563 vi[i]->indx = i; | |
2564 } | |
2565 | |
2566 indexed_sort.sort (vi, ns); | |
2567 | |
2568 for (octave_idx_type i = 0; i < ns; i++) | |
2569 { | |
2570 v[i] = vi[i]->vec; | |
2571 sidx(i + offset) = vi[i]->indx; | |
2572 } | |
2573 v += ns; | |
2574 } | |
2575 } | |
2576 else | |
2577 { | |
2578 for (octave_idx_type j = 0; j < iter; j++) | |
2579 { | |
2580 octave_idx_type offset = j; | |
2581 octave_idx_type offset2 = 0; | |
2582 while (offset >= stride) | |
2583 { | |
2584 offset -= stride; | |
2585 offset2++; | |
2586 } | |
2587 offset += offset2 * stride * ns; | |
2588 | |
2589 for (octave_idx_type i = 0; i < ns; i++) | |
2590 { | |
2591 vi[i]->vec = v[i*stride + offset]; | |
2592 vi[i]->indx = i; | |
2593 } | |
2594 | |
2595 indexed_sort.sort (vi, ns); | |
2596 | |
2597 for (octave_idx_type i = 0; i < ns; i++) | |
2598 { | |
2599 v[i*stride+offset] = vi[i]->vec; | |
2600 sidx(i*stride+offset) = vi[i]->indx; | |
2601 } | |
2602 } | |
2603 } | |
2604 | |
2605 return m; | |
2606 } | |
2607 | |
7443 | 2608 #if defined (HAVE_IEEE754_DATA_FORMAT) |
2609 | |
2610 template <> | |
7480 | 2611 bool ascending_compare (double, double); |
7443 | 2612 template <> |
7480 | 2613 bool ascending_compare (vec_index<double>*, vec_index<double>*); |
7443 | 2614 template <> |
7480 | 2615 bool descending_compare (double, double); |
7443 | 2616 template <> |
7480 | 2617 bool descending_compare (vec_index<double>*, vec_index<double>*); |
7443 | 2618 |
2619 template <> | |
2620 Array<double> Array<double>::sort (octave_idx_type dim, sortmode mode) const; | |
2621 template <> | |
2622 Array<double> Array<double>::sort (Array<octave_idx_type> &sidx, | |
2623 octave_idx_type dim, sortmode mode) const; | |
2624 | |
2625 #endif | |
2626 | |
5775 | 2627 // FIXME -- this is a mess. |
4517 | 2628 |
2629 template <class LT, class RT> | |
2630 int | |
2631 assign (Array<LT>& lhs, const Array<RT>& rhs, const LT& rfv) | |
2632 { | |
6388 | 2633 int n_idx = lhs.index_count (); |
2634 | |
2635 // kluge... | |
2636 if (lhs.ndims () == 0) | |
2637 lhs.resize_no_fill (0, 0); | |
2638 | |
2639 return (lhs.ndims () == 2 | |
2640 && (n_idx == 1 | |
2641 || (n_idx < 3 | |
2642 && rhs.ndims () == 2 | |
2643 && rhs.rows () == 0 && rhs.columns () == 0))) | |
2644 ? assign2 (lhs, rhs, rfv) : assignN (lhs, rhs, rfv); | |
4517 | 2645 } |
2646 | |
2647 template <class LT, class RT> | |
2648 int | |
2649 assign1 (Array<LT>& lhs, const Array<RT>& rhs, const LT& rfv) | |
2650 { | |
2651 int retval = 1; | |
2652 | |
2653 idx_vector *tmp = lhs.get_idx (); | |
2654 | |
2655 idx_vector lhs_idx = tmp[0]; | |
2656 | |
5275 | 2657 octave_idx_type lhs_len = lhs.length (); |
2658 octave_idx_type rhs_len = rhs.length (); | |
2659 | |
5781 | 2660 octave_idx_type n = lhs_idx.freeze (lhs_len, "vector", true); |
4517 | 2661 |
2662 if (n != 0) | |
2663 { | |
6389 | 2664 dim_vector lhs_dims = lhs.dims (); |
2665 | |
6922 | 2666 if (lhs_len != 0 |
2667 || lhs_dims.all_zero () | |
2668 || (lhs_dims.length () == 2 && lhs_dims(0) < 2)) | |
4517 | 2669 { |
6392 | 2670 if (rhs_len == n || rhs_len == 1) |
2671 { | |
6884 | 2672 lhs.make_unique (); |
2673 | |
6392 | 2674 octave_idx_type max_idx = lhs_idx.max () + 1; |
2675 if (max_idx > lhs_len) | |
2676 lhs.resize_and_fill (max_idx, rfv); | |
2677 } | |
2678 | |
2679 if (rhs_len == n) | |
2680 { | |
6884 | 2681 lhs.make_unique (); |
2682 | |
2683 if (lhs_idx.is_colon ()) | |
6392 | 2684 { |
6884 | 2685 for (octave_idx_type i = 0; i < n; i++) |
2686 lhs.xelem (i) = rhs.elem (i); | |
2687 } | |
2688 else | |
2689 { | |
2690 for (octave_idx_type i = 0; i < n; i++) | |
2691 { | |
2692 octave_idx_type ii = lhs_idx.elem (i); | |
2693 lhs.xelem (ii) = rhs.elem (i); | |
2694 } | |
6392 | 2695 } |
2696 } | |
2697 else if (rhs_len == 1) | |
2698 { | |
6884 | 2699 lhs.make_unique (); |
2700 | |
6392 | 2701 RT scalar = rhs.elem (0); |
2702 | |
6884 | 2703 if (lhs_idx.is_colon ()) |
6392 | 2704 { |
6884 | 2705 for (octave_idx_type i = 0; i < n; i++) |
2706 lhs.xelem (i) = scalar; | |
2707 } | |
2708 else | |
2709 { | |
2710 for (octave_idx_type i = 0; i < n; i++) | |
2711 { | |
2712 octave_idx_type ii = lhs_idx.elem (i); | |
2713 lhs.xelem (ii) = scalar; | |
2714 } | |
6392 | 2715 } |
2716 } | |
2717 else | |
2718 { | |
2719 (*current_liboctave_error_handler) | |
2720 ("A(I) = X: X must be a scalar or a vector with same length as I"); | |
2721 | |
2722 retval = 0; | |
2723 } | |
4517 | 2724 } |
6922 | 2725 else |
2726 { | |
2727 (*current_liboctave_error_handler) | |
2728 ("A(I) = X: unable to resize A"); | |
2729 | |
2730 retval = 0; | |
2731 } | |
4517 | 2732 } |
2733 else if (lhs_idx.is_colon ()) | |
2734 { | |
6384 | 2735 dim_vector lhs_dims = lhs.dims (); |
2736 | |
2737 if (lhs_dims.all_zero ()) | |
4517 | 2738 { |
6884 | 2739 lhs.make_unique (); |
2740 | |
4517 | 2741 lhs.resize_no_fill (rhs_len); |
2742 | |
5275 | 2743 for (octave_idx_type i = 0; i < rhs_len; i++) |
6884 | 2744 lhs.xelem (i) = rhs.elem (i); |
4517 | 2745 } |
6553 | 2746 else if (rhs_len != lhs_len) |
4517 | 2747 (*current_liboctave_error_handler) |
2748 ("A(:) = X: A must be the same size as X"); | |
2749 } | |
2750 else if (! (rhs_len == 1 || rhs_len == 0)) | |
2751 { | |
2752 (*current_liboctave_error_handler) | |
2753 ("A([]) = X: X must also be an empty matrix or a scalar"); | |
2754 | |
2755 retval = 0; | |
2756 } | |
2757 | |
2758 lhs.clear_index (); | |
2759 | |
2760 return retval; | |
2761 } | |
2762 | |
2763 #define MAYBE_RESIZE_LHS \ | |
2764 do \ | |
2765 { \ | |
5275 | 2766 octave_idx_type max_row_idx = idx_i_is_colon ? rhs_nr : idx_i.max () + 1; \ |
2767 octave_idx_type max_col_idx = idx_j_is_colon ? rhs_nc : idx_j.max () + 1; \ | |
4517 | 2768 \ |
5275 | 2769 octave_idx_type new_nr = max_row_idx > lhs_nr ? max_row_idx : lhs_nr; \ |
2770 octave_idx_type new_nc = max_col_idx > lhs_nc ? max_col_idx : lhs_nc; \ | |
4517 | 2771 \ |
2772 lhs.resize_and_fill (new_nr, new_nc, rfv); \ | |
2773 } \ | |
2774 while (0) | |
2775 | |
2776 template <class LT, class RT> | |
2777 int | |
2778 assign2 (Array<LT>& lhs, const Array<RT>& rhs, const LT& rfv) | |
2779 { | |
2780 int retval = 1; | |
2781 | |
2782 int n_idx = lhs.index_count (); | |
2783 | |
5275 | 2784 octave_idx_type lhs_nr = lhs.rows (); |
2785 octave_idx_type lhs_nc = lhs.cols (); | |
4517 | 2786 |
5047 | 2787 Array<RT> xrhs = rhs; |
2788 | |
5275 | 2789 octave_idx_type rhs_nr = xrhs.rows (); |
2790 octave_idx_type rhs_nc = xrhs.cols (); | |
5047 | 2791 |
2792 if (xrhs.ndims () > 2) | |
4707 | 2793 { |
5047 | 2794 xrhs = xrhs.squeeze (); |
2795 | |
2796 dim_vector dv_tmp = xrhs.dims (); | |
4709 | 2797 |
4708 | 2798 switch (dv_tmp.length ()) |
4707 | 2799 { |
4708 | 2800 case 1: |
5775 | 2801 // FIXME -- this case should be unnecessary, because |
5047 | 2802 // squeeze should always return an object with 2 dimensions. |
4708 | 2803 if (rhs_nr == 1) |
2804 rhs_nc = dv_tmp.elem (0); | |
2805 break; | |
4709 | 2806 |
4708 | 2807 case 2: |
4707 | 2808 rhs_nr = dv_tmp.elem (0); |
2809 rhs_nc = dv_tmp.elem (1); | |
4708 | 2810 break; |
2811 | |
2812 default: | |
2813 (*current_liboctave_error_handler) | |
2814 ("Array<T>::assign2: Dimension mismatch"); | |
4709 | 2815 return 0; |
4707 | 2816 } |
2817 } | |
4517 | 2818 |
6384 | 2819 bool rhs_is_scalar = rhs_nr == 1 && rhs_nc == 1; |
2820 | |
4517 | 2821 idx_vector *tmp = lhs.get_idx (); |
2822 | |
2823 idx_vector idx_i; | |
2824 idx_vector idx_j; | |
2825 | |
2826 if (n_idx > 1) | |
2827 idx_j = tmp[1]; | |
2828 | |
2829 if (n_idx > 0) | |
2830 idx_i = tmp[0]; | |
2831 | |
2832 if (n_idx == 2) | |
2833 { | |
5781 | 2834 octave_idx_type n = idx_i.freeze (lhs_nr, "row", true); |
2835 octave_idx_type m = idx_j.freeze (lhs_nc, "column", true); | |
4517 | 2836 |
2837 int idx_i_is_colon = idx_i.is_colon (); | |
2838 int idx_j_is_colon = idx_j.is_colon (); | |
2839 | |
6384 | 2840 if (lhs_nr == 0 && lhs_nc == 0) |
2841 { | |
2842 if (idx_i_is_colon) | |
2843 n = rhs_nr; | |
2844 | |
2845 if (idx_j_is_colon) | |
2846 m = rhs_nc; | |
2847 } | |
4517 | 2848 |
2849 if (idx_i && idx_j) | |
2850 { | |
2851 if (rhs_nr == 0 && rhs_nc == 0) | |
2852 { | |
2853 lhs.maybe_delete_elements (idx_i, idx_j); | |
2854 } | |
2855 else | |
2856 { | |
6384 | 2857 if (rhs_is_scalar && n >= 0 && m >= 0) |
4517 | 2858 { |
4534 | 2859 // No need to do anything if either of the indices |
2860 // are empty. | |
2861 | |
2862 if (n > 0 && m > 0) | |
4517 | 2863 { |
6884 | 2864 lhs.make_unique (); |
2865 | |
4534 | 2866 MAYBE_RESIZE_LHS; |
2867 | |
5047 | 2868 RT scalar = xrhs.elem (0, 0); |
4534 | 2869 |
5275 | 2870 for (octave_idx_type j = 0; j < m; j++) |
4517 | 2871 { |
5275 | 2872 octave_idx_type jj = idx_j.elem (j); |
2873 for (octave_idx_type i = 0; i < n; i++) | |
4534 | 2874 { |
5275 | 2875 octave_idx_type ii = idx_i.elem (i); |
6884 | 2876 lhs.xelem (ii, jj) = scalar; |
4534 | 2877 } |
4517 | 2878 } |
2879 } | |
2880 } | |
6072 | 2881 else if ((n == 1 || m == 1) |
2882 && (rhs_nr == 1 || rhs_nc == 1) | |
2883 && n * m == rhs_nr * rhs_nc) | |
2884 { | |
6884 | 2885 lhs.make_unique (); |
2886 | |
6384 | 2887 MAYBE_RESIZE_LHS; |
2888 | |
6072 | 2889 if (n > 0 && m > 0) |
2890 { | |
2891 octave_idx_type k = 0; | |
2892 | |
2893 for (octave_idx_type j = 0; j < m; j++) | |
2894 { | |
2895 octave_idx_type jj = idx_j.elem (j); | |
2896 for (octave_idx_type i = 0; i < n; i++) | |
2897 { | |
2898 octave_idx_type ii = idx_i.elem (i); | |
6884 | 2899 lhs.xelem (ii, jj) = xrhs.elem (k++); |
6072 | 2900 } |
2901 } | |
2902 } | |
2903 } | |
4517 | 2904 else if (n == rhs_nr && m == rhs_nc) |
2905 { | |
6884 | 2906 lhs.make_unique (); |
2907 | |
6384 | 2908 MAYBE_RESIZE_LHS; |
2909 | |
4517 | 2910 if (n > 0 && m > 0) |
2911 { | |
5275 | 2912 for (octave_idx_type j = 0; j < m; j++) |
4517 | 2913 { |
5275 | 2914 octave_idx_type jj = idx_j.elem (j); |
2915 for (octave_idx_type i = 0; i < n; i++) | |
4517 | 2916 { |
5275 | 2917 octave_idx_type ii = idx_i.elem (i); |
6884 | 2918 lhs.xelem (ii, jj) = xrhs.elem (i, j); |
4517 | 2919 } |
2920 } | |
2921 } | |
2922 } | |
2923 else if (n == 0 && m == 0) | |
2924 { | |
6384 | 2925 if (! (rhs_is_scalar || (rhs_nr == 0 || rhs_nc == 0))) |
4517 | 2926 { |
2927 (*current_liboctave_error_handler) | |
2928 ("A([], []) = X: X must be an empty matrix or a scalar"); | |
2929 | |
2930 retval = 0; | |
2931 } | |
2932 } | |
2933 else | |
2934 { | |
2935 (*current_liboctave_error_handler) | |
2936 ("A(I, J) = X: X must be a scalar or the number of elements in I must"); | |
2937 (*current_liboctave_error_handler) | |
2938 ("match the number of rows in X and the number of elements in J must"); | |
2939 (*current_liboctave_error_handler) | |
2940 ("match the number of columns in X"); | |
2941 | |
2942 retval = 0; | |
2943 } | |
2944 } | |
2945 } | |
2946 // idx_vector::freeze() printed an error message for us. | |
2947 } | |
2948 else if (n_idx == 1) | |
2949 { | |
2950 int lhs_is_empty = lhs_nr == 0 || lhs_nc == 0; | |
2951 | |
2952 if (lhs_is_empty || (lhs_nr == 1 && lhs_nc == 1)) | |
2953 { | |
5275 | 2954 octave_idx_type lhs_len = lhs.length (); |
2955 | |
6384 | 2956 idx_i.freeze (lhs_len, 0, true); |
4517 | 2957 |
2958 if (idx_i) | |
2959 { | |
2960 if (rhs_nr == 0 && rhs_nc == 0) | |
2961 { | |
6384 | 2962 lhs.maybe_delete_elements (idx_i); |
4517 | 2963 } |
2964 else | |
2965 { | |
5781 | 2966 if (lhs_is_empty |
2967 && idx_i.is_colon () | |
2968 && ! (rhs_nr == 1 || rhs_nc == 1)) | |
4517 | 2969 { |
5781 | 2970 (*current_liboctave_warning_with_id_handler) |
2971 ("Octave:fortran-indexing", | |
2972 "A(:) = X: X is not a vector or scalar"); | |
2973 } | |
2974 else | |
2975 { | |
2976 octave_idx_type idx_nr = idx_i.orig_rows (); | |
2977 octave_idx_type idx_nc = idx_i.orig_columns (); | |
2978 | |
2979 if (! (rhs_nr == idx_nr && rhs_nc == idx_nc)) | |
2980 (*current_liboctave_warning_with_id_handler) | |
2981 ("Octave:fortran-indexing", | |
2982 "A(I) = X: X does not have same shape as I"); | |
4517 | 2983 } |
2984 | |
5047 | 2985 if (assign1 (lhs, xrhs, rfv)) |
4517 | 2986 { |
5275 | 2987 octave_idx_type len = lhs.length (); |
4517 | 2988 |
2989 if (len > 0) | |
2990 { | |
2991 // The following behavior is much simplified | |
2992 // over previous versions of Octave. It | |
2993 // seems to be compatible with Matlab. | |
2994 | |
2995 lhs.dimensions = dim_vector (1, lhs.length ()); | |
2996 } | |
2997 } | |
2998 else | |
2999 retval = 0; | |
3000 } | |
3001 } | |
3002 // idx_vector::freeze() printed an error message for us. | |
3003 } | |
3004 else if (lhs_nr == 1) | |
3005 { | |
5781 | 3006 idx_i.freeze (lhs_nc, "vector", true); |
4517 | 3007 |
3008 if (idx_i) | |
3009 { | |
3010 if (rhs_nr == 0 && rhs_nc == 0) | |
3011 lhs.maybe_delete_elements (idx_i); | |
3012 else | |
3013 { | |
5047 | 3014 if (assign1 (lhs, xrhs, rfv)) |
4517 | 3015 lhs.dimensions = dim_vector (1, lhs.length ()); |
3016 else | |
3017 retval = 0; | |
3018 } | |
3019 } | |
3020 // idx_vector::freeze() printed an error message for us. | |
3021 } | |
3022 else if (lhs_nc == 1) | |
3023 { | |
5781 | 3024 idx_i.freeze (lhs_nr, "vector", true); |
4517 | 3025 |
3026 if (idx_i) | |
3027 { | |
3028 if (rhs_nr == 0 && rhs_nc == 0) | |
3029 lhs.maybe_delete_elements (idx_i); | |
3030 else | |
3031 { | |
5047 | 3032 if (assign1 (lhs, xrhs, rfv)) |
4517 | 3033 lhs.dimensions = dim_vector (lhs.length (), 1); |
3034 else | |
3035 retval = 0; | |
3036 } | |
3037 } | |
3038 // idx_vector::freeze() printed an error message for us. | |
3039 } | |
3040 else | |
3041 { | |
7573
755bf7ecc29b
eliminate one_zero stuff from idx_vector
John W. Eaton <jwe@octave.org>
parents:
7480
diff
changeset
|
3042 if (! idx_i.is_colon ()) |
5781 | 3043 (*current_liboctave_warning_with_id_handler) |
3044 ("Octave:fortran-indexing", "single index used for matrix"); | |
4517 | 3045 |
5275 | 3046 octave_idx_type len = idx_i.freeze (lhs_nr * lhs_nc, "matrix"); |
4517 | 3047 |
3048 if (idx_i) | |
3049 { | |
4756 | 3050 if (rhs_nr == 0 && rhs_nc == 0) |
3051 lhs.maybe_delete_elements (idx_i); | |
3052 else if (len == 0) | |
4517 | 3053 { |
6384 | 3054 if (! (rhs_is_scalar || (rhs_nr == 0 || rhs_nc == 0))) |
4517 | 3055 (*current_liboctave_error_handler) |
3056 ("A([]) = X: X must be an empty matrix or scalar"); | |
3057 } | |
3058 else if (len == rhs_nr * rhs_nc) | |
3059 { | |
6884 | 3060 lhs.make_unique (); |
3061 | |
3062 if (idx_i.is_colon ()) | |
4517 | 3063 { |
6884 | 3064 for (octave_idx_type i = 0; i < len; i++) |
3065 lhs.xelem (i) = xrhs.elem (i); | |
3066 } | |
3067 else | |
3068 { | |
3069 for (octave_idx_type i = 0; i < len; i++) | |
3070 { | |
3071 octave_idx_type ii = idx_i.elem (i); | |
3072 lhs.xelem (ii) = xrhs.elem (i); | |
3073 } | |
4517 | 3074 } |
3075 } | |
6384 | 3076 else if (rhs_is_scalar) |
4517 | 3077 { |
6884 | 3078 lhs.make_unique (); |
3079 | |
4517 | 3080 RT scalar = rhs.elem (0, 0); |
3081 | |
6884 | 3082 if (idx_i.is_colon ()) |
4517 | 3083 { |
6884 | 3084 for (octave_idx_type i = 0; i < len; i++) |
3085 lhs.xelem (i) = scalar; | |
3086 } | |
3087 else | |
3088 { | |
3089 for (octave_idx_type i = 0; i < len; i++) | |
3090 { | |
3091 octave_idx_type ii = idx_i.elem (i); | |
3092 lhs.xelem (ii) = scalar; | |
3093 } | |
4517 | 3094 } |
3095 } | |
3096 else | |
3097 { | |
3098 (*current_liboctave_error_handler) | |
3099 ("A(I) = X: X must be a scalar or a matrix with the same size as I"); | |
3100 | |
3101 retval = 0; | |
3102 } | |
3103 } | |
3104 // idx_vector::freeze() printed an error message for us. | |
3105 } | |
3106 } | |
3107 else | |
3108 { | |
3109 (*current_liboctave_error_handler) | |
3110 ("invalid number of indices for matrix expression"); | |
3111 | |
3112 retval = 0; | |
3113 } | |
3114 | |
3115 lhs.clear_index (); | |
3116 | |
3117 return retval; | |
3118 } | |
3119 | |
3120 template <class LT, class RT> | |
3121 int | |
3122 assignN (Array<LT>& lhs, const Array<RT>& rhs, const LT& rfv) | |
3123 { | |
3124 int retval = 1; | |
3125 | |
4746 | 3126 dim_vector rhs_dims = rhs.dims (); |
3127 | |
5275 | 3128 octave_idx_type rhs_dims_len = rhs_dims.length (); |
4746 | 3129 |
3130 bool rhs_is_scalar = is_scalar (rhs_dims); | |
3131 | |
4517 | 3132 int n_idx = lhs.index_count (); |
3133 | |
4745 | 3134 idx_vector *idx_vex = lhs.get_idx (); |
3135 | |
3136 Array<idx_vector> idx = conv_to_array (idx_vex, n_idx); | |
4517 | 3137 |
4743 | 3138 if (rhs_dims_len == 2 && rhs_dims(0) == 0 && rhs_dims(1) == 0) |
4517 | 3139 { |
3140 lhs.maybe_delete_elements (idx, rfv); | |
3141 } | |
5285 | 3142 else if (n_idx == 0) |
3143 { | |
3144 (*current_liboctave_error_handler) | |
3145 ("invalid number of indices for matrix expression"); | |
3146 | |
3147 retval = 0; | |
3148 } | |
4657 | 3149 else if (n_idx == 1) |
4517 | 3150 { |
4657 | 3151 idx_vector iidx = idx(0); |
6884 | 3152 int iidx_is_colon = iidx.is_colon (); |
3153 | |
7573
755bf7ecc29b
eliminate one_zero stuff from idx_vector
John W. Eaton <jwe@octave.org>
parents:
7480
diff
changeset
|
3154 if (! iidx_is_colon) |
5781 | 3155 (*current_liboctave_warning_with_id_handler) |
3156 ("Octave:fortran-indexing", "single index used for N-d array"); | |
4657 | 3157 |
5275 | 3158 octave_idx_type lhs_len = lhs.length (); |
3159 | |
3160 octave_idx_type len = iidx.freeze (lhs_len, "N-d arrray"); | |
4657 | 3161 |
3162 if (iidx) | |
4533 | 3163 { |
4657 | 3164 if (len == 0) |
4656 | 3165 { |
5039 | 3166 if (! (rhs_dims.all_ones () || rhs_dims.any_zero ())) |
4743 | 3167 { |
3168 (*current_liboctave_error_handler) | |
3169 ("A([]) = X: X must be an empty matrix or scalar"); | |
3170 | |
3171 retval = 0; | |
3172 } | |
4657 | 3173 } |
3174 else if (len == rhs.length ()) | |
3175 { | |
6884 | 3176 lhs.make_unique (); |
3177 | |
3178 if (iidx_is_colon) | |
4656 | 3179 { |
6884 | 3180 for (octave_idx_type i = 0; i < len; i++) |
3181 lhs.xelem (i) = rhs.elem (i); | |
3182 } | |
3183 else | |
3184 { | |
3185 for (octave_idx_type i = 0; i < len; i++) | |
3186 { | |
3187 octave_idx_type ii = iidx.elem (i); | |
3188 | |
3189 lhs.xelem (ii) = rhs.elem (i); | |
3190 } | |
4656 | 3191 } |
3192 } | |
4716 | 3193 else if (rhs_is_scalar) |
4657 | 3194 { |
3195 RT scalar = rhs.elem (0); | |
3196 | |
6884 | 3197 lhs.make_unique (); |
3198 | |
3199 if (iidx_is_colon) | |
4657 | 3200 { |
6884 | 3201 for (octave_idx_type i = 0; i < len; i++) |
3202 lhs.xelem (i) = scalar; | |
3203 } | |
3204 else | |
3205 { | |
3206 for (octave_idx_type i = 0; i < len; i++) | |
3207 { | |
3208 octave_idx_type ii = iidx.elem (i); | |
3209 | |
3210 lhs.xelem (ii) = scalar; | |
3211 } | |
4657 | 3212 } |
3213 } | |
3214 else | |
3215 { | |
3216 (*current_liboctave_error_handler) | |
4702 | 3217 ("A(I) = X: X must be a scalar or a matrix with the same size as I"); |
3218 | |
4657 | 3219 retval = 0; |
3220 } | |
3221 | |
4656 | 3222 // idx_vector::freeze() printed an error message for us. |
4533 | 3223 } |
4702 | 3224 } |
4743 | 3225 else |
4702 | 3226 { |
4746 | 3227 // Maybe expand to more dimensions. |
3228 | |
3229 dim_vector lhs_dims = lhs.dims (); | |
3230 | |
5275 | 3231 octave_idx_type lhs_dims_len = lhs_dims.length (); |
4746 | 3232 |
3233 dim_vector final_lhs_dims = lhs_dims; | |
3234 | |
3235 dim_vector frozen_len; | |
3236 | |
5275 | 3237 octave_idx_type orig_lhs_dims_len = lhs_dims_len; |
4747 | 3238 |
3239 bool orig_empty = lhs_dims.all_zero (); | |
3240 | |
3241 if (n_idx < lhs_dims_len) | |
4517 | 3242 { |
5052 | 3243 // Collapse dimensions beyond last index. Note that we |
3244 // delay resizing LHS until we know that the assignment will | |
3245 // succeed. | |
4747 | 3246 |
5781 | 3247 if (! (idx(n_idx-1).is_colon ())) |
3248 (*current_liboctave_warning_with_id_handler) | |
3249 ("Octave:fortran-indexing", | |
3250 "fewer indices than dimensions for N-d array"); | |
4747 | 3251 |
3252 for (int i = n_idx; i < lhs_dims_len; i++) | |
3253 lhs_dims(n_idx-1) *= lhs_dims(i); | |
3254 | |
3255 lhs_dims.resize (n_idx); | |
3256 | |
3257 lhs_dims_len = lhs_dims.length (); | |
3258 } | |
3259 | |
3260 // Resize. | |
3261 | |
3262 dim_vector new_dims; | |
3263 new_dims.resize (n_idx); | |
3264 | |
5264 | 3265 if (orig_empty) |
4747 | 3266 { |
5264 | 3267 int k = 0; |
3268 for (int i = 0; i < n_idx; i++) | |
4746 | 3269 { |
4747 | 3270 // If index is a colon, resizing to RHS dimensions is |
3271 // allowed because we started out empty. | |
4746 | 3272 |
5264 | 3273 if (idx(i).is_colon ()) |
3274 { | |
3275 if (k < rhs_dims.length ()) | |
3276 new_dims(i) = rhs_dims(k++); | |
3277 else | |
5379 | 3278 new_dims(i) = 1; |
3279 } | |
3280 else | |
3281 { | |
3282 octave_idx_type nelem = idx(i).capacity (); | |
3283 | |
3284 if (nelem >= 1 | |
6388 | 3285 && ((k < rhs_dims.length () && nelem == rhs_dims(k)) |
3286 || rhs_is_scalar)) | |
5379 | 3287 k++; |
6388 | 3288 else if (! (nelem == 1 || rhs_is_scalar)) |
5264 | 3289 { |
3290 (*current_liboctave_error_handler) | |
5379 | 3291 ("A(IDX-LIST) = RHS: mismatched index and RHS dimension"); |
5264 | 3292 return retval; |
3293 } | |
5379 | 3294 |
6388 | 3295 new_dims(i) = idx(i).orig_empty () ? 0 : idx(i).max () + 1; |
5264 | 3296 } |
4746 | 3297 } |
5264 | 3298 } |
3299 else | |
3300 { | |
3301 for (int i = 0; i < n_idx; i++) | |
4746 | 3302 { |
4747 | 3303 // We didn't start out with all zero dimensions, so if |
3304 // index is a colon, it refers to the current LHS | |
3305 // dimension. Otherwise, it is OK to enlarge to a | |
5264 | 3306 // dimension given by the largest index, but if that |
4898 | 3307 // index is a colon the new dimension is singleton. |
4749 | 3308 |
3309 if (i < lhs_dims_len | |
6481 | 3310 && (idx(i).is_colon () |
3311 || idx(i).orig_empty () | |
3312 || idx(i).max () < lhs_dims(i))) | |
4749 | 3313 new_dims(i) = lhs_dims(i); |
3314 else if (! idx(i).is_colon ()) | |
3315 new_dims(i) = idx(i).max () + 1; | |
3316 else | |
4898 | 3317 new_dims(i) = 1; |
4745 | 3318 } |
4747 | 3319 } |
3320 | |
4749 | 3321 if (retval != 0) |
4747 | 3322 { |
4749 | 3323 if (! orig_empty |
3324 && n_idx < orig_lhs_dims_len | |
3325 && new_dims(n_idx-1) != lhs_dims(n_idx-1)) | |
4702 | 3326 { |
4749 | 3327 // We reshaped and the last dimension changed. This has to |
3328 // be an error, because we don't know how to undo that | |
3329 // later... | |
3330 | |
3331 (*current_liboctave_error_handler) | |
3332 ("array index %d (= %d) for assignment requires invalid resizing operation", | |
3333 n_idx, new_dims(n_idx-1)); | |
3334 | |
3335 retval = 0; | |
4743 | 3336 } |
3337 else | |
3338 { | |
5052 | 3339 // Determine final dimensions for LHS and reset the |
3340 // current size of the LHS. Note that we delay actually | |
3341 // resizing LHS until we know that the assignment will | |
3342 // succeed. | |
3343 | |
4749 | 3344 if (n_idx < orig_lhs_dims_len) |
4743 | 3345 { |
4749 | 3346 for (int i = 0; i < n_idx-1; i++) |
3347 final_lhs_dims(i) = new_dims(i); | |
4747 | 3348 } |
3349 else | |
4749 | 3350 final_lhs_dims = new_dims; |
3351 | |
5837 | 3352 lhs_dims_len = new_dims.length (); |
3353 | |
3354 frozen_len = freeze (idx, new_dims, true); | |
4749 | 3355 |
6141 | 3356 for (int i = 0; i < idx.length (); i++) |
3357 { | |
3358 if (! idx(i)) | |
3359 { | |
3360 retval = 0; | |
3361 lhs.clear_index (); | |
3362 return retval; | |
3363 } | |
3364 } | |
3365 | |
4749 | 3366 if (rhs_is_scalar) |
4747 | 3367 { |
6884 | 3368 lhs.make_unique (); |
3369 | |
5837 | 3370 if (n_idx < orig_lhs_dims_len) |
3371 lhs = lhs.reshape (lhs_dims); | |
3372 | |
5052 | 3373 lhs.resize_and_fill (new_dims, rfv); |
3374 | |
4747 | 3375 if (! final_lhs_dims.any_zero ()) |
3376 { | |
4749 | 3377 RT scalar = rhs.elem (0); |
3378 | |
6388 | 3379 if (n_idx == 1) |
3380 { | |
3381 idx_vector iidx = idx(0); | |
3382 | |
3383 octave_idx_type len = frozen_len(0); | |
3384 | |
6884 | 3385 if (iidx.is_colon ()) |
3386 { | |
3387 for (octave_idx_type i = 0; i < len; i++) | |
3388 lhs.xelem (i) = scalar; | |
3389 } | |
3390 else | |
6388 | 3391 { |
6884 | 3392 for (octave_idx_type i = 0; i < len; i++) |
3393 { | |
3394 octave_idx_type ii = iidx.elem (i); | |
3395 | |
3396 lhs.xelem (ii) = scalar; | |
3397 } | |
6388 | 3398 } |
3399 } | |
3400 else if (lhs_dims_len == 2 && n_idx == 2) | |
4747 | 3401 { |
6388 | 3402 idx_vector idx_i = idx(0); |
3403 idx_vector idx_j = idx(1); | |
3404 | |
3405 octave_idx_type i_len = frozen_len(0); | |
3406 octave_idx_type j_len = frozen_len(1); | |
3407 | |
6884 | 3408 if (idx_i.is_colon()) |
6388 | 3409 { |
6884 | 3410 for (octave_idx_type j = 0; j < j_len; j++) |
6388 | 3411 { |
6884 | 3412 octave_idx_type off = new_dims (0) * |
3413 idx_j.elem (j); | |
3414 for (octave_idx_type i = 0; i < i_len; i++) | |
3415 lhs.xelem (i + off) = scalar; | |
3416 } | |
3417 } | |
3418 else | |
3419 { | |
3420 for (octave_idx_type j = 0; j < j_len; j++) | |
3421 { | |
3422 octave_idx_type off = new_dims (0) * | |
3423 idx_j.elem (j); | |
3424 for (octave_idx_type i = 0; i < i_len; i++) | |
3425 { | |
3426 octave_idx_type ii = idx_i.elem (i); | |
3427 lhs.xelem (ii + off) = scalar; | |
3428 } | |
6388 | 3429 } |
3430 } | |
3431 } | |
3432 else | |
3433 { | |
3434 octave_idx_type n = Array<LT>::get_size (frozen_len); | |
3435 | |
3436 Array<octave_idx_type> result_idx (lhs_dims_len, 0); | |
3437 | |
3438 for (octave_idx_type i = 0; i < n; i++) | |
3439 { | |
3440 Array<octave_idx_type> elt_idx = get_elt_idx (idx, result_idx); | |
3441 | |
6884 | 3442 lhs.xelem (elt_idx) = scalar; |
6388 | 3443 |
3444 increment_index (result_idx, frozen_len); | |
3445 } | |
4747 | 3446 } |
3447 } | |
4743 | 3448 } |
4749 | 3449 else |
3450 { | |
3451 // RHS is matrix or higher dimension. | |
3452 | |
5275 | 3453 octave_idx_type n = Array<LT>::get_size (frozen_len); |
5264 | 3454 |
3455 if (n != rhs.numel ()) | |
4749 | 3456 { |
3457 (*current_liboctave_error_handler) | |
3458 ("A(IDX-LIST) = X: X must be a scalar or size of X must equal number of elements indexed by IDX-LIST"); | |
3459 | |
3460 retval = 0; | |
3461 } | |
3462 else | |
3463 { | |
6884 | 3464 lhs.make_unique (); |
3465 | |
5837 | 3466 if (n_idx < orig_lhs_dims_len) |
3467 lhs = lhs.reshape (lhs_dims); | |
3468 | |
5052 | 3469 lhs.resize_and_fill (new_dims, rfv); |
3470 | |
4749 | 3471 if (! final_lhs_dims.any_zero ()) |
3472 { | |
6388 | 3473 if (n_idx == 1) |
3474 { | |
3475 idx_vector iidx = idx(0); | |
3476 | |
3477 octave_idx_type len = frozen_len(0); | |
3478 | |
6884 | 3479 if (iidx.is_colon ()) |
3480 { | |
3481 for (octave_idx_type i = 0; i < len; i++) | |
3482 lhs.xelem (i) = rhs.elem (i); | |
3483 } | |
3484 else | |
6388 | 3485 { |
6884 | 3486 for (octave_idx_type i = 0; i < len; i++) |
3487 { | |
3488 octave_idx_type ii = iidx.elem (i); | |
3489 | |
3490 lhs.xelem (ii) = rhs.elem (i); | |
3491 } | |
6388 | 3492 } |
3493 } | |
3494 else if (lhs_dims_len == 2 && n_idx == 2) | |
4749 | 3495 { |
6388 | 3496 idx_vector idx_i = idx(0); |
3497 idx_vector idx_j = idx(1); | |
3498 | |
3499 octave_idx_type i_len = frozen_len(0); | |
3500 octave_idx_type j_len = frozen_len(1); | |
3501 octave_idx_type k = 0; | |
3502 | |
6884 | 3503 if (idx_i.is_colon()) |
6388 | 3504 { |
6884 | 3505 for (octave_idx_type j = 0; j < j_len; j++) |
6388 | 3506 { |
6884 | 3507 octave_idx_type off = new_dims (0) * |
3508 idx_j.elem (j); | |
3509 for (octave_idx_type i = 0; | |
3510 i < i_len; i++) | |
3511 lhs.xelem (i + off) = rhs.elem (k++); | |
6388 | 3512 } |
3513 } | |
6884 | 3514 else |
3515 { | |
3516 for (octave_idx_type j = 0; j < j_len; j++) | |
3517 { | |
3518 octave_idx_type off = new_dims (0) * | |
3519 idx_j.elem (j); | |
3520 for (octave_idx_type i = 0; i < i_len; i++) | |
3521 { | |
3522 octave_idx_type ii = idx_i.elem (i); | |
3523 lhs.xelem (ii + off) = rhs.elem (k++); | |
3524 } | |
3525 } | |
3526 } | |
3527 | |
6388 | 3528 } |
3529 else | |
3530 { | |
3531 n = Array<LT>::get_size (frozen_len); | |
3532 | |
3533 Array<octave_idx_type> result_idx (lhs_dims_len, 0); | |
3534 | |
3535 for (octave_idx_type i = 0; i < n; i++) | |
3536 { | |
3537 Array<octave_idx_type> elt_idx = get_elt_idx (idx, result_idx); | |
3538 | |
6884 | 3539 lhs.xelem (elt_idx) = rhs.elem (i); |
6388 | 3540 |
3541 increment_index (result_idx, frozen_len); | |
3542 } | |
4749 | 3543 } |
3544 } | |
3545 } | |
3546 } | |
4743 | 3547 } |
4517 | 3548 } |
4745 | 3549 |
5632 | 3550 lhs.clear_index (); |
3551 | |
5052 | 3552 if (retval != 0) |
5516 | 3553 lhs = lhs.reshape (final_lhs_dims); |
4517 | 3554 } |
3555 | |
5052 | 3556 if (retval != 0) |
3557 lhs.chop_trailing_singletons (); | |
4757 | 3558 |
4517 | 3559 lhs.clear_index (); |
3560 | |
3561 return retval; | |
3562 } | |
3563 | |
3564 template <class T> | |
3565 void | |
3933 | 3566 Array<T>::print_info (std::ostream& os, const std::string& prefix) const |
3567 { | |
3568 os << prefix << "rep address: " << rep << "\n" | |
3569 << prefix << "rep->len: " << rep->len << "\n" | |
3570 << prefix << "rep->data: " << static_cast<void *> (rep->data) << "\n" | |
3571 << prefix << "rep->count: " << rep->count << "\n"; | |
4513 | 3572 |
3573 // 2D info: | |
3574 // | |
4657 | 3575 // << pefix << "rows: " << rows () << "\n" |
4513 | 3576 // << prefix << "cols: " << cols () << "\n"; |
3933 | 3577 } |
3578 | |
237 | 3579 /* |
3580 ;;; Local Variables: *** | |
3581 ;;; mode: C++ *** | |
3582 ;;; End: *** | |
3583 */ |