1993
|
1 // Template array classes |
237
|
2 /* |
|
3 |
2847
|
4 Copyright (C) 1996, 1997 John W. Eaton |
237
|
5 |
|
6 This file is part of Octave. |
|
7 |
|
8 Octave is free software; you can redistribute it and/or modify it |
|
9 under the terms of the GNU General Public License as published by the |
|
10 Free Software Foundation; either version 2, or (at your option) any |
|
11 later version. |
|
12 |
|
13 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
16 for more details. |
|
17 |
|
18 You should have received a copy of the GNU General Public License |
|
19 along with Octave; see the file COPYING. If not, write to the Free |
1315
|
20 Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
237
|
21 |
|
22 */ |
|
23 |
4192
|
24 #if defined (__GNUG__) && defined (USE_PRAGMA_INTERFACE_IMPLEMENTATION) |
1296
|
25 #pragma implementation |
|
26 #endif |
|
27 |
237
|
28 #ifdef HAVE_CONFIG_H |
1192
|
29 #include <config.h> |
237
|
30 #endif |
|
31 |
1367
|
32 #include <cassert> |
4518
|
33 #include <climits> |
449
|
34 |
3503
|
35 #include <iostream> |
1560
|
36 |
237
|
37 #include "Array.h" |
4517
|
38 #include "Array-flags.h" |
4588
|
39 #include "Array-util.h" |
4517
|
40 #include "Range.h" |
1560
|
41 #include "idx-vector.h" |
|
42 #include "lo-error.h" |
4661
|
43 #include "lo-sstream.h" |
1560
|
44 |
1360
|
45 // One dimensional array class. Handles the reference counting for |
|
46 // all the derived classes. |
237
|
47 |
|
48 template <class T> |
1619
|
49 Array<T>::~Array (void) |
|
50 { |
|
51 if (--rep->count <= 0) |
|
52 delete rep; |
|
53 |
|
54 delete [] idx; |
4513
|
55 } |
|
56 |
4532
|
57 template <class T> |
|
58 Array<T> |
|
59 Array<T>::squeeze (void) const |
|
60 { |
|
61 Array<T> retval = *this; |
|
62 |
|
63 bool dims_changed = false; |
|
64 |
|
65 dim_vector new_dimensions = dimensions; |
|
66 |
|
67 int k = 0; |
|
68 |
|
69 for (int i = 0; i < ndims (); i++) |
|
70 { |
|
71 if (dimensions(i) == 1) |
|
72 dims_changed = true; |
|
73 else |
|
74 new_dimensions(k++) = dimensions(i); |
|
75 } |
|
76 |
|
77 if (dims_changed) |
|
78 { |
|
79 if (k == 0) |
|
80 new_dimensions = dim_vector (1); |
|
81 else |
|
82 new_dimensions.resize (k); |
|
83 |
|
84 retval.make_unique (); |
|
85 |
|
86 retval.dimensions = new_dimensions; |
|
87 } |
|
88 |
|
89 return retval; |
|
90 } |
|
91 |
4513
|
92 // A guess (should be quite conservative). |
|
93 #define MALLOC_OVERHEAD 1024 |
|
94 |
|
95 template <class T> |
|
96 int |
|
97 Array<T>::get_size (int r, int c) |
|
98 { |
|
99 // XXX KLUGE XXX |
|
100 |
|
101 // If an allocation of an array with r * c elements of type T |
|
102 // would cause an overflow in the allocator when computing the |
|
103 // size of the allocation, then return a value which, although |
|
104 // not equivalent to the actual request, should be too large for |
|
105 // most current hardware, but not so large to cause the |
|
106 // allocator to barf on computing retval * sizeof (T). |
|
107 |
|
108 static int nl; |
|
109 static double dl |
|
110 = frexp (static_cast<double> |
|
111 (INT_MAX - MALLOC_OVERHEAD) / sizeof (T), &nl); |
|
112 |
|
113 // This value should be an integer. If we return this value and |
|
114 // things work the way we expect, we should be paying a visit to |
|
115 // new_handler in no time flat. |
|
116 static int max_items = static_cast<int> (ldexp (dl, nl)); |
|
117 |
|
118 int nr, nc; |
|
119 double dr = frexp (static_cast<double> (r), &nr); |
|
120 double dc = frexp (static_cast<double> (c), &nc); |
|
121 |
|
122 int nt = nr + nc; |
|
123 double dt = dr * dc; |
|
124 |
4532
|
125 if (dt < 0.5) |
4513
|
126 { |
|
127 nt--; |
|
128 dt *= 2; |
|
129 } |
|
130 |
|
131 return (nt < nl || (nt == nl && dt < dl)) ? r * c : max_items; |
237
|
132 } |
|
133 |
|
134 template <class T> |
4513
|
135 int |
|
136 Array<T>::get_size (int r, int c, int p) |
237
|
137 { |
4513
|
138 // XXX KLUGE XXX |
|
139 |
|
140 // If an allocation of an array with r * c * p elements of type T |
|
141 // would cause an overflow in the allocator when computing the |
|
142 // size of the allocation, then return a value which, although |
|
143 // not equivalent to the actual request, should be too large for |
|
144 // most current hardware, but not so large to cause the |
|
145 // allocator to barf on computing retval * sizeof (T). |
|
146 |
|
147 static int nl; |
|
148 static double dl |
|
149 = frexp (static_cast<double> |
|
150 (INT_MAX - MALLOC_OVERHEAD) / sizeof (T), &nl); |
|
151 |
|
152 // This value should be an integer. If we return this value and |
|
153 // things work the way we expect, we should be paying a visit to |
|
154 // new_handler in no time flat. |
|
155 static int max_items = static_cast<int> (ldexp (dl, nl)); |
|
156 |
|
157 int nr, nc, np; |
|
158 double dr = frexp (static_cast<double> (r), &nr); |
|
159 double dc = frexp (static_cast<double> (c), &nc); |
|
160 double dp = frexp (static_cast<double> (p), &np); |
|
161 |
|
162 int nt = nr + nc + np; |
|
163 double dt = dr * dc * dp; |
|
164 |
4532
|
165 if (dt < 0.5) |
659
|
166 { |
4513
|
167 nt--; |
|
168 dt *= 2; |
237
|
169 |
4532
|
170 if (dt < 0.5) |
|
171 { |
|
172 nt--; |
|
173 dt *= 2; |
|
174 } |
659
|
175 } |
1619
|
176 |
4513
|
177 return (nt < nl || (nt == nl && dt < dl)) ? r * c * p : max_items; |
237
|
178 } |
|
179 |
|
180 template <class T> |
4513
|
181 int |
|
182 Array<T>::get_size (const dim_vector& ra_idx) |
237
|
183 { |
4513
|
184 // XXX KLUGE XXX |
|
185 |
|
186 // If an allocation of an array with r * c elements of type T |
|
187 // would cause an overflow in the allocator when computing the |
|
188 // size of the allocation, then return a value which, although |
|
189 // not equivalent to the actual request, should be too large for |
|
190 // most current hardware, but not so large to cause the |
|
191 // allocator to barf on computing retval * sizeof (T). |
|
192 |
|
193 static int nl; |
|
194 static double dl |
|
195 = frexp (static_cast<double> |
|
196 (INT_MAX - MALLOC_OVERHEAD) / sizeof (T), &nl); |
|
197 |
|
198 // This value should be an integer. If we return this value and |
|
199 // things work the way we expect, we should be paying a visit to |
|
200 // new_handler in no time flat. |
|
201 |
|
202 static int max_items = static_cast<int> (ldexp (dl, nl)); |
|
203 |
|
204 int retval = max_items; |
|
205 |
|
206 int n = ra_idx.length (); |
|
207 |
|
208 int nt = 0; |
|
209 double dt = 1; |
|
210 |
|
211 for (int i = 0; i < n; i++) |
237
|
212 { |
4513
|
213 int nra_idx; |
|
214 double dra_idx = frexp (static_cast<double> (ra_idx(i)), &nra_idx); |
|
215 |
|
216 nt += nra_idx; |
|
217 dt *= dra_idx; |
4532
|
218 |
|
219 if (dt < 0.5) |
|
220 { |
|
221 nt--; |
|
222 dt *= 2; |
|
223 } |
237
|
224 } |
|
225 |
4513
|
226 if (nt < nl || (nt == nl && dt < dl)) |
|
227 { |
|
228 retval = 1; |
|
229 |
|
230 for (int i = 0; i < n; i++) |
|
231 retval *= ra_idx(i); |
|
232 } |
|
233 |
|
234 return retval; |
237
|
235 } |
|
236 |
4513
|
237 #undef MALLOC_OVERHEAD |
|
238 |
237
|
239 template <class T> |
4513
|
240 int |
|
241 Array<T>::compute_index (const Array<int>& ra_idx) const |
237
|
242 { |
4513
|
243 int retval = -1; |
|
244 |
|
245 int n = dimensions.length (); |
|
246 |
|
247 if (n > 0 && n == ra_idx.length ()) |
237
|
248 { |
4513
|
249 retval = ra_idx(--n); |
237
|
250 |
4513
|
251 while (--n >= 0) |
|
252 { |
|
253 retval *= dimensions(n); |
|
254 retval += ra_idx(n); |
|
255 } |
|
256 } |
|
257 else |
|
258 (*current_liboctave_error_handler) |
|
259 ("Array<T>::compute_index: invalid ra_idxing operation"); |
237
|
260 |
4513
|
261 return retval; |
237
|
262 } |
|
263 |
2049
|
264 template <class T> |
|
265 T |
2109
|
266 Array<T>::range_error (const char *fcn, int n) const |
2049
|
267 { |
2109
|
268 (*current_liboctave_error_handler) ("%s (%d): range error", fcn, n); |
2049
|
269 return T (); |
|
270 } |
|
271 |
|
272 template <class T> |
|
273 T& |
2109
|
274 Array<T>::range_error (const char *fcn, int n) |
2049
|
275 { |
2109
|
276 (*current_liboctave_error_handler) ("%s (%d): range error", fcn, n); |
2049
|
277 static T foo; |
|
278 return foo; |
|
279 } |
|
280 |
3933
|
281 template <class T> |
4513
|
282 T |
|
283 Array<T>::range_error (const char *fcn, int i, int j) const |
|
284 { |
|
285 (*current_liboctave_error_handler) |
|
286 ("%s (%d, %d): range error", fcn, i, j); |
|
287 return T (); |
|
288 } |
|
289 |
|
290 template <class T> |
|
291 T& |
|
292 Array<T>::range_error (const char *fcn, int i, int j) |
|
293 { |
|
294 (*current_liboctave_error_handler) |
|
295 ("%s (%d, %d): range error", fcn, i, j); |
|
296 static T foo; |
|
297 return foo; |
|
298 } |
|
299 |
|
300 template <class T> |
|
301 T |
|
302 Array<T>::range_error (const char *fcn, int i, int j, int k) const |
|
303 { |
|
304 (*current_liboctave_error_handler) |
|
305 ("%s (%d, %d, %d): range error", fcn, i, j, k); |
|
306 return T (); |
|
307 } |
|
308 |
|
309 template <class T> |
|
310 T& |
|
311 Array<T>::range_error (const char *fcn, int i, int j, int k) |
|
312 { |
|
313 (*current_liboctave_error_handler) |
|
314 ("%s (%d, %d, %d): range error", fcn, i, j, k); |
|
315 static T foo; |
|
316 return foo; |
|
317 } |
|
318 |
|
319 template <class T> |
|
320 T |
|
321 Array<T>::range_error (const char *fcn, const Array<int>& ra_idx) const |
|
322 { |
4661
|
323 OSSTREAM buf; |
|
324 |
|
325 buf << fcn << " ("; |
|
326 |
|
327 int n = ra_idx.length (); |
|
328 |
|
329 if (n > 0) |
|
330 buf << ra_idx(0); |
|
331 |
|
332 for (int i = 1; i < n; i++) |
|
333 buf << ", " << ra_idx(i); |
|
334 |
|
335 buf << "): range error"; |
|
336 |
|
337 buf << OSSTREAM_ENDS; |
|
338 |
|
339 (*current_liboctave_error_handler) (OSSTREAM_C_STR (buf)); |
|
340 |
|
341 OSSTREAM_FREEZE (buf); |
4513
|
342 |
|
343 return T (); |
|
344 } |
|
345 |
|
346 template <class T> |
|
347 T& |
|
348 Array<T>::range_error (const char *fcn, const Array<int>& ra_idx) |
|
349 { |
4661
|
350 OSSTREAM buf; |
|
351 |
|
352 buf << fcn << " ("; |
|
353 |
|
354 int n = ra_idx.length (); |
|
355 |
|
356 if (n > 0) |
|
357 buf << ra_idx(0); |
|
358 |
|
359 for (int i = 1; i < n; i++) |
|
360 buf << ", " << ra_idx(i); |
|
361 |
|
362 buf << "): range error"; |
|
363 |
|
364 buf << OSSTREAM_ENDS; |
|
365 |
|
366 (*current_liboctave_error_handler) (OSSTREAM_C_STR (buf)); |
|
367 |
|
368 OSSTREAM_FREEZE (buf); |
4513
|
369 |
|
370 static T foo; |
|
371 return foo; |
|
372 } |
|
373 |
|
374 template <class T> |
4567
|
375 Array<T> |
|
376 Array<T>::reshape (const dim_vector& new_dims) const |
|
377 { |
|
378 Array<T> retval; |
|
379 |
|
380 if (dimensions != new_dims) |
|
381 { |
|
382 if (dimensions.numel () == new_dims.numel ()) |
|
383 retval = Array<T> (*this, new_dims); |
|
384 else |
|
385 (*current_liboctave_error_handler) ("reshape: size mismatch"); |
|
386 } |
|
387 |
|
388 return retval; |
|
389 } |
|
390 |
|
391 template <class T> |
4593
|
392 Array<T> |
|
393 Array<T>::permute (const Array<int>& perm_vec, bool inv) const |
|
394 { |
|
395 Array<T> retval; |
|
396 |
|
397 dim_vector dv = dims (); |
|
398 dim_vector dv_new; |
|
399 |
|
400 int nd = dv.length (); |
|
401 |
|
402 dv_new.resize (nd); |
|
403 |
|
404 // Need this array to check for identical elements in permutation array. |
|
405 Array<bool> checked (nd, false); |
|
406 |
|
407 // Find dimension vector of permuted array. |
|
408 for (int i = 0; i < nd; i++) |
|
409 { |
|
410 int perm_el = perm_vec.elem (i); |
|
411 |
|
412 if (perm_el > dv.length () || perm_el < 1) |
|
413 { |
|
414 (*current_liboctave_error_handler) |
|
415 ("permutation vector contains an invalid element"); |
|
416 |
|
417 return retval; |
|
418 } |
|
419 |
|
420 if (checked.elem(perm_el - 1)) |
|
421 { |
|
422 (*current_liboctave_error_handler) |
|
423 ("PERM cannot contain identical elements"); |
|
424 |
|
425 return retval; |
|
426 } |
|
427 else |
|
428 checked.elem(perm_el - 1) = true; |
|
429 |
|
430 dv_new (i) = dv (perm_el - 1); |
|
431 } |
|
432 |
|
433 retval.resize (dv_new); |
|
434 |
|
435 // Index array to the original array. |
|
436 Array<int> old_idx (nd, 0); |
|
437 |
|
438 // Number of elements in Array (should be the same for |
|
439 // both the permuted array and original array). |
|
440 int n = retval.length (); |
|
441 |
|
442 // Permute array. |
|
443 for (int i = 0; i < n; i++) |
|
444 { |
|
445 // Get the idx of permuted array. |
|
446 Array<int> new_idx = calc_permutated_idx (old_idx, perm_vec, inv); |
|
447 |
|
448 retval.elem (new_idx) = elem (old_idx); |
|
449 |
|
450 increment_index (old_idx, dv); |
|
451 } |
|
452 |
|
453 return retval; |
|
454 } |
|
455 |
|
456 template <class T> |
4513
|
457 void |
|
458 Array<T>::resize_no_fill (int n) |
|
459 { |
|
460 if (n < 0) |
|
461 { |
|
462 (*current_liboctave_error_handler) |
|
463 ("can't resize to negative dimension"); |
|
464 return; |
|
465 } |
|
466 |
|
467 if (n == length ()) |
|
468 return; |
|
469 |
|
470 typename Array<T>::ArrayRep *old_rep = rep; |
|
471 const T *old_data = data (); |
|
472 int old_len = length (); |
|
473 |
|
474 rep = new typename Array<T>::ArrayRep (n); |
|
475 |
|
476 dimensions = dim_vector (n); |
|
477 |
4747
|
478 if (n > 0 && old_data && old_len > 0) |
4513
|
479 { |
|
480 int min_len = old_len < n ? old_len : n; |
|
481 |
|
482 for (int i = 0; i < min_len; i++) |
|
483 xelem (i) = old_data[i]; |
|
484 } |
|
485 |
|
486 if (--old_rep->count <= 0) |
|
487 delete old_rep; |
|
488 } |
|
489 |
|
490 template <class T> |
|
491 void |
4587
|
492 Array<T>::resize_no_fill (const dim_vector& dv) |
4513
|
493 { |
4587
|
494 int n = dv.length (); |
4513
|
495 |
|
496 for (int i = 0; i < n; i++) |
|
497 { |
4587
|
498 if (dv(i) < 0) |
4513
|
499 { |
|
500 (*current_liboctave_error_handler) |
|
501 ("can't resize to negative dimension"); |
|
502 return; |
|
503 } |
|
504 } |
|
505 |
4548
|
506 bool same_size = true; |
|
507 |
|
508 if (dimensions.length () != n) |
|
509 { |
|
510 same_size = false; |
|
511 } |
|
512 else |
4513
|
513 { |
4548
|
514 for (int i = 0; i < n; i++) |
4513
|
515 { |
4587
|
516 if (dv(i) != dimensions(i)) |
4548
|
517 { |
|
518 same_size = false; |
|
519 break; |
|
520 } |
4513
|
521 } |
|
522 } |
|
523 |
4548
|
524 if (same_size) |
4513
|
525 return; |
|
526 |
|
527 int old_len = length (); |
|
528 |
|
529 typename Array<T>::ArrayRep *old_rep = rep; |
|
530 const T *old_data = data (); |
|
531 |
4747
|
532 int ts = get_size (dv); |
|
533 |
|
534 rep = new typename Array<T>::ArrayRep (ts); |
4587
|
535 |
|
536 dimensions = dv; |
4513
|
537 |
4747
|
538 if (ts > 0) |
4513
|
539 { |
4747
|
540 Array<int> ra_idx (dimensions.length (), 0); |
|
541 |
|
542 for (int i = 0; i < old_len; i++) |
|
543 { |
|
544 if (index_in_bounds (ra_idx, dimensions)) |
|
545 xelem (ra_idx) = old_data[i]; |
|
546 |
|
547 increment_index (ra_idx, dimensions); |
|
548 } |
4513
|
549 } |
|
550 |
|
551 if (--old_rep->count <= 0) |
|
552 delete old_rep; |
|
553 } |
|
554 |
|
555 template <class T> |
|
556 void |
|
557 Array<T>::resize_no_fill (int r, int c) |
|
558 { |
|
559 if (r < 0 || c < 0) |
|
560 { |
|
561 (*current_liboctave_error_handler) |
|
562 ("can't resize to negative dimension"); |
|
563 return; |
|
564 } |
|
565 |
4548
|
566 int n = ndims (); |
|
567 |
|
568 if (n == 0) |
|
569 dimensions = dim_vector (0, 0); |
|
570 |
|
571 assert (ndims () == 2); |
|
572 |
4513
|
573 if (r == dim1 () && c == dim2 ()) |
|
574 return; |
|
575 |
|
576 typename Array<T>::ArrayRep *old_rep = Array<T>::rep; |
|
577 const T *old_data = data (); |
|
578 |
|
579 int old_d1 = dim1 (); |
|
580 int old_d2 = dim2 (); |
|
581 int old_len = length (); |
|
582 |
4747
|
583 int ts = get_size (r, c); |
|
584 |
|
585 rep = new typename Array<T>::ArrayRep (ts); |
4513
|
586 |
|
587 dimensions = dim_vector (r, c); |
|
588 |
4747
|
589 if (ts > 0 && old_data && old_len > 0) |
4513
|
590 { |
|
591 int min_r = old_d1 < r ? old_d1 : r; |
|
592 int min_c = old_d2 < c ? old_d2 : c; |
|
593 |
|
594 for (int j = 0; j < min_c; j++) |
|
595 for (int i = 0; i < min_r; i++) |
|
596 xelem (i, j) = old_data[old_d1*j+i]; |
|
597 } |
|
598 |
|
599 if (--old_rep->count <= 0) |
|
600 delete old_rep; |
|
601 } |
|
602 |
|
603 template <class T> |
|
604 void |
|
605 Array<T>::resize_no_fill (int r, int c, int p) |
|
606 { |
|
607 if (r < 0 || c < 0 || p < 0) |
|
608 { |
|
609 (*current_liboctave_error_handler) |
|
610 ("can't resize to negative dimension"); |
|
611 return; |
|
612 } |
|
613 |
4548
|
614 int n = ndims (); |
|
615 |
|
616 if (n == 0) |
|
617 dimensions = dim_vector (0, 0, 0); |
|
618 |
|
619 assert (ndims () == 3); |
|
620 |
4513
|
621 if (r == dim1 () && c == dim2 () && p == dim3 ()) |
|
622 return; |
|
623 |
|
624 typename Array<T>::ArrayRep *old_rep = rep; |
|
625 const T *old_data = data (); |
|
626 |
|
627 int old_d1 = dim1 (); |
|
628 int old_d2 = dim2 (); |
|
629 int old_d3 = dim3 (); |
|
630 int old_len = length (); |
|
631 |
|
632 int ts = get_size (get_size (r, c), p); |
|
633 |
|
634 rep = new typename Array<T>::ArrayRep (ts); |
|
635 |
|
636 dimensions = dim_vector (r, c, p); |
|
637 |
4747
|
638 if (ts > 0 && old_data && old_len > 0) |
4513
|
639 { |
|
640 int min_r = old_d1 < r ? old_d1 : r; |
|
641 int min_c = old_d2 < c ? old_d2 : c; |
|
642 int min_p = old_d3 < p ? old_d3 : p; |
|
643 |
|
644 for (int k = 0; k < min_p; k++) |
|
645 for (int j = 0; j < min_c; j++) |
|
646 for (int i = 0; i < min_r; i++) |
|
647 xelem (i, j, k) = old_data[old_d1*(old_d2*k+j)+i]; |
|
648 } |
|
649 |
|
650 if (--old_rep->count <= 0) |
|
651 delete old_rep; |
|
652 } |
|
653 |
|
654 template <class T> |
|
655 void |
|
656 Array<T>::resize_and_fill (int n, const T& val) |
|
657 { |
|
658 if (n < 0) |
|
659 { |
|
660 (*current_liboctave_error_handler) |
|
661 ("can't resize to negative dimension"); |
|
662 return; |
|
663 } |
|
664 |
|
665 if (n == length ()) |
|
666 return; |
|
667 |
|
668 typename Array<T>::ArrayRep *old_rep = rep; |
|
669 const T *old_data = data (); |
|
670 int old_len = length (); |
|
671 |
|
672 rep = new typename Array<T>::ArrayRep (n); |
|
673 |
|
674 dimensions = dim_vector (n); |
|
675 |
4747
|
676 if (n > 0) |
4513
|
677 { |
4747
|
678 int min_len = old_len < n ? old_len : n; |
|
679 |
|
680 if (old_data && old_len > 0) |
|
681 { |
|
682 for (int i = 0; i < min_len; i++) |
|
683 xelem (i) = old_data[i]; |
|
684 } |
|
685 |
|
686 for (int i = old_len; i < n; i++) |
|
687 xelem (i) = val; |
4513
|
688 } |
|
689 |
|
690 if (--old_rep->count <= 0) |
|
691 delete old_rep; |
|
692 } |
|
693 |
|
694 template <class T> |
|
695 void |
|
696 Array<T>::resize_and_fill (int r, int c, const T& val) |
|
697 { |
|
698 if (r < 0 || c < 0) |
|
699 { |
|
700 (*current_liboctave_error_handler) |
|
701 ("can't resize to negative dimension"); |
|
702 return; |
|
703 } |
|
704 |
4548
|
705 if (ndims () == 0) |
|
706 dimensions = dim_vector (0, 0); |
|
707 |
|
708 assert (ndims () == 2); |
|
709 |
4513
|
710 if (r == dim1 () && c == dim2 ()) |
|
711 return; |
|
712 |
|
713 typename Array<T>::ArrayRep *old_rep = Array<T>::rep; |
|
714 const T *old_data = data (); |
|
715 |
|
716 int old_d1 = dim1 (); |
|
717 int old_d2 = dim2 (); |
|
718 int old_len = length (); |
|
719 |
4747
|
720 int ts = get_size (r, c); |
|
721 |
|
722 rep = new typename Array<T>::ArrayRep (ts); |
4513
|
723 |
|
724 dimensions = dim_vector (r, c); |
|
725 |
4747
|
726 if (ts > 0) |
4513
|
727 { |
4747
|
728 int min_r = old_d1 < r ? old_d1 : r; |
|
729 int min_c = old_d2 < c ? old_d2 : c; |
|
730 |
|
731 if (old_data && old_len > 0) |
|
732 { |
|
733 for (int j = 0; j < min_c; j++) |
|
734 for (int i = 0; i < min_r; i++) |
|
735 xelem (i, j) = old_data[old_d1*j+i]; |
|
736 } |
|
737 |
4513
|
738 for (int j = 0; j < min_c; j++) |
4747
|
739 for (int i = min_r; i < r; i++) |
|
740 xelem (i, j) = val; |
|
741 |
|
742 for (int j = min_c; j < c; j++) |
|
743 for (int i = 0; i < r; i++) |
|
744 xelem (i, j) = val; |
4513
|
745 } |
|
746 |
|
747 if (--old_rep->count <= 0) |
|
748 delete old_rep; |
|
749 } |
|
750 |
|
751 template <class T> |
|
752 void |
|
753 Array<T>::resize_and_fill (int r, int c, int p, const T& val) |
|
754 { |
|
755 if (r < 0 || c < 0 || p < 0) |
|
756 { |
|
757 (*current_liboctave_error_handler) |
|
758 ("can't resize to negative dimension"); |
|
759 return; |
|
760 } |
|
761 |
4548
|
762 if (ndims () == 0) |
|
763 dimensions = dim_vector (0, 0, 0); |
|
764 |
|
765 assert (ndims () == 3); |
|
766 |
4513
|
767 if (r == dim1 () && c == dim2 () && p == dim3 ()) |
|
768 return; |
|
769 |
|
770 typename Array<T>::ArrayRep *old_rep = rep; |
|
771 const T *old_data = data (); |
|
772 |
|
773 int old_d1 = dim1 (); |
|
774 int old_d2 = dim2 (); |
|
775 int old_d3 = dim3 (); |
|
776 |
|
777 int old_len = length (); |
|
778 |
|
779 int ts = get_size (get_size (r, c), p); |
|
780 |
|
781 rep = new typename Array<T>::ArrayRep (ts); |
|
782 |
|
783 dimensions = dim_vector (r, c, p); |
|
784 |
4747
|
785 if (ts > 0) |
|
786 { |
|
787 int min_r = old_d1 < r ? old_d1 : r; |
|
788 int min_c = old_d2 < c ? old_d2 : c; |
|
789 int min_p = old_d3 < p ? old_d3 : p; |
|
790 |
|
791 if (old_data && old_len > 0) |
|
792 for (int k = 0; k < min_p; k++) |
|
793 for (int j = 0; j < min_c; j++) |
|
794 for (int i = 0; i < min_r; i++) |
|
795 xelem (i, j, k) = old_data[old_d1*(old_d2*k+j)+i]; |
|
796 |
|
797 // XXX FIXME XXX -- if the copy constructor is expensive, this |
|
798 // may win. Otherwise, it may make more sense to just copy the |
|
799 // value everywhere when making the new ArrayRep. |
|
800 |
|
801 for (int k = 0; k < min_p; k++) |
|
802 for (int j = min_c; j < c; j++) |
|
803 for (int i = 0; i < min_r; i++) |
|
804 xelem (i, j, k) = val; |
|
805 |
|
806 for (int k = 0; k < min_p; k++) |
|
807 for (int j = 0; j < c; j++) |
|
808 for (int i = min_r; i < r; i++) |
|
809 xelem (i, j, k) = val; |
|
810 |
|
811 for (int k = min_p; k < p; k++) |
|
812 for (int j = 0; j < c; j++) |
|
813 for (int i = 0; i < r; i++) |
|
814 xelem (i, j, k) = val; |
|
815 } |
4513
|
816 |
|
817 if (--old_rep->count <= 0) |
|
818 delete old_rep; |
|
819 } |
|
820 |
|
821 template <class T> |
|
822 void |
4587
|
823 Array<T>::resize_and_fill (const dim_vector& dv, const T& val) |
4513
|
824 { |
4587
|
825 int n = dv.length (); |
4513
|
826 |
|
827 for (int i = 0; i < n; i++) |
|
828 { |
4587
|
829 if (dv(i) < 0) |
4513
|
830 { |
|
831 (*current_liboctave_error_handler) |
|
832 ("can't resize to negative dimension"); |
|
833 return; |
|
834 } |
|
835 } |
|
836 |
4553
|
837 bool same_size = true; |
|
838 |
|
839 if (dimensions.length () != n) |
|
840 { |
|
841 same_size = false; |
|
842 } |
|
843 else |
4513
|
844 { |
4553
|
845 for (int i = 0; i < n; i++) |
4513
|
846 { |
4587
|
847 if (dv(i) != dimensions(i)) |
4553
|
848 { |
|
849 same_size = false; |
|
850 break; |
|
851 } |
4513
|
852 } |
|
853 } |
|
854 |
4553
|
855 if (same_size) |
4513
|
856 return; |
|
857 |
|
858 typename Array<T>::ArrayRep *old_rep = rep; |
|
859 const T *old_data = data (); |
|
860 |
|
861 int old_len = length (); |
|
862 |
4587
|
863 int len = get_size (dv); |
4709
|
864 |
4513
|
865 rep = new typename Array<T>::ArrayRep (len); |
|
866 |
4707
|
867 dim_vector dv_old = dimensions; |
4709
|
868 |
4708
|
869 if (n > dv_old.length ()) |
|
870 { |
|
871 dv_old.resize (n); |
4709
|
872 |
4708
|
873 for (int i = dv_old.length (); i < n; i++) |
|
874 dv_old.elem (i) = 1; |
|
875 } |
|
876 |
4587
|
877 dimensions = dv; |
4513
|
878 |
4747
|
879 if (len > 0) |
4513
|
880 { |
4747
|
881 Array<int> ra_idx (dimensions.length (), 0); |
|
882 |
|
883 // XXX FIXME XXX -- it is much simpler to fill the whole array |
|
884 // first, but probably slower for large arrays, or if the assignment |
|
885 // operator for the type T is expensive. OTOH, the logic for |
|
886 // deciding whether an element needs the copied value or the filled |
|
887 // value might be more expensive. |
|
888 |
|
889 for (int i = 0; i < len; i++) |
|
890 rep->elem (i) = val; |
|
891 |
|
892 for (int i = 0; i < old_len; i++) |
|
893 { |
|
894 if (index_in_bounds (ra_idx, dv_old)) |
|
895 xelem (ra_idx) = old_data[get_scalar_idx (ra_idx, dv_old)]; |
|
896 |
|
897 increment_index (ra_idx, dv_old); |
|
898 } |
4513
|
899 } |
|
900 |
|
901 if (--old_rep->count <= 0) |
|
902 delete old_rep; |
|
903 } |
|
904 |
|
905 template <class T> |
|
906 Array<T>& |
|
907 Array<T>::insert (const Array<T>& a, int r, int c) |
|
908 { |
|
909 int a_rows = a.rows (); |
|
910 int a_cols = a.cols (); |
|
911 |
|
912 if (r < 0 || r + a_rows > rows () || c < 0 || c + a_cols > cols ()) |
|
913 { |
|
914 (*current_liboctave_error_handler) ("range error for insert"); |
|
915 return *this; |
|
916 } |
|
917 |
|
918 for (int j = 0; j < a_cols; j++) |
|
919 for (int i = 0; i < a_rows; i++) |
|
920 elem (r+i, c+j) = a.elem (i, j); |
|
921 |
|
922 return *this; |
|
923 } |
|
924 |
|
925 template <class T> |
|
926 Array<T>& |
|
927 Array<T>::insert (const Array<T>& a, const Array<int>& ra_idx) |
|
928 { |
|
929 int n = ra_idx.length (); |
|
930 |
|
931 if (n == dimensions.length ()) |
|
932 { |
|
933 dim_vector a_dims = a.dims (); |
|
934 |
|
935 for (int i = 0; i < n; i++) |
|
936 { |
|
937 if (ra_idx(i) < 0 || ra_idx(i) + a_dims(i) > dimensions(i)) |
|
938 { |
|
939 (*current_liboctave_error_handler) |
|
940 ("Array<T>::insert: range error for insert"); |
|
941 return *this; |
|
942 } |
|
943 } |
|
944 |
|
945 #if 0 |
|
946 // XXX FIXME XXX -- need to copy elements |
|
947 |
|
948 for (int j = 0; j < a_cols; j++) |
|
949 for (int i = 0; i < a_rows; i++) |
|
950 elem (r+i, c+j) = a.elem (i, j); |
|
951 #endif |
|
952 |
|
953 } |
|
954 else |
|
955 (*current_liboctave_error_handler) |
|
956 ("Array<T>::insert: invalid indexing operation"); |
|
957 |
|
958 return *this; |
|
959 } |
|
960 |
|
961 template <class T> |
|
962 Array<T> |
|
963 Array<T>::transpose (void) const |
|
964 { |
4548
|
965 assert (ndims () == 2); |
|
966 |
4513
|
967 int nr = dim1 (); |
|
968 int nc = dim2 (); |
|
969 |
|
970 if (nr > 1 && nc > 1) |
|
971 { |
|
972 Array<T> result (dim_vector (nc, nr)); |
|
973 |
|
974 for (int j = 0; j < nc; j++) |
|
975 for (int i = 0; i < nr; i++) |
|
976 result.xelem (j, i) = xelem (i, j); |
|
977 |
|
978 return result; |
|
979 } |
|
980 else |
|
981 { |
|
982 // Fast transpose for vectors and empty matrices |
|
983 return Array<T> (*this, dim_vector (nc, nr)); |
|
984 } |
|
985 } |
|
986 |
|
987 template <class T> |
|
988 T * |
|
989 Array<T>::fortran_vec (void) |
|
990 { |
|
991 if (rep->count > 1) |
|
992 { |
|
993 --rep->count; |
|
994 rep = new typename Array<T>::ArrayRep (*rep); |
|
995 } |
|
996 return rep->data; |
|
997 } |
|
998 |
|
999 template <class T> |
3933
|
1000 void |
4517
|
1001 Array<T>::maybe_delete_dims (void) |
|
1002 { |
4587
|
1003 int nd = dimensions.length (); |
4517
|
1004 |
|
1005 dim_vector new_dims (1, 1); |
|
1006 |
|
1007 bool delete_dims = true; |
|
1008 |
4587
|
1009 for (int i = nd - 1; i >= 0; i--) |
4517
|
1010 { |
|
1011 if (delete_dims) |
|
1012 { |
|
1013 if (dimensions(i) != 1) |
|
1014 { |
|
1015 delete_dims = false; |
|
1016 |
|
1017 new_dims = dim_vector (i + 1, dimensions(i)); |
|
1018 } |
|
1019 } |
|
1020 else |
|
1021 new_dims(i) = dimensions(i); |
|
1022 } |
4530
|
1023 |
4587
|
1024 if (nd != new_dims.length ()) |
4517
|
1025 dimensions = new_dims; |
|
1026 } |
|
1027 |
|
1028 template <class T> |
|
1029 void |
|
1030 Array<T>::clear_index (void) |
|
1031 { |
|
1032 delete [] idx; |
|
1033 idx = 0; |
|
1034 idx_count = 0; |
|
1035 } |
|
1036 |
|
1037 template <class T> |
|
1038 void |
|
1039 Array<T>::set_index (const idx_vector& idx_arg) |
|
1040 { |
|
1041 int nd = ndims (); |
|
1042 |
|
1043 if (! idx && nd > 0) |
|
1044 idx = new idx_vector [nd]; |
|
1045 |
|
1046 if (idx_count < nd) |
|
1047 { |
|
1048 idx[idx_count++] = idx_arg; |
|
1049 } |
|
1050 else |
|
1051 { |
|
1052 idx_vector *new_idx = new idx_vector [idx_count+1]; |
|
1053 |
|
1054 for (int i = 0; i < idx_count; i++) |
|
1055 new_idx[i] = idx[i]; |
|
1056 |
|
1057 new_idx[idx_count++] = idx_arg; |
|
1058 |
|
1059 delete [] idx; |
|
1060 |
|
1061 idx = new_idx; |
|
1062 } |
|
1063 } |
|
1064 |
|
1065 template <class T> |
|
1066 void |
|
1067 Array<T>::maybe_delete_elements (idx_vector& idx_arg) |
|
1068 { |
|
1069 switch (ndims ()) |
|
1070 { |
|
1071 case 1: |
|
1072 maybe_delete_elements_1 (idx_arg); |
|
1073 break; |
|
1074 |
|
1075 case 2: |
|
1076 maybe_delete_elements_2 (idx_arg); |
|
1077 break; |
|
1078 |
|
1079 default: |
|
1080 (*current_liboctave_error_handler) |
|
1081 ("Array<T>::maybe_delete_elements: invalid operation"); |
|
1082 break; |
|
1083 } |
|
1084 } |
|
1085 |
|
1086 template <class T> |
|
1087 void |
|
1088 Array<T>::maybe_delete_elements_1 (idx_vector& idx_arg) |
|
1089 { |
|
1090 int len = length (); |
|
1091 |
|
1092 if (len == 0) |
|
1093 return; |
|
1094 |
|
1095 if (idx_arg.is_colon_equiv (len, 1)) |
|
1096 resize_no_fill (0); |
|
1097 else |
|
1098 { |
|
1099 int num_to_delete = idx_arg.length (len); |
|
1100 |
|
1101 if (num_to_delete != 0) |
|
1102 { |
|
1103 int new_len = len; |
|
1104 |
|
1105 int iidx = 0; |
|
1106 |
|
1107 for (int i = 0; i < len; i++) |
|
1108 if (i == idx_arg.elem (iidx)) |
|
1109 { |
|
1110 iidx++; |
|
1111 new_len--; |
|
1112 |
|
1113 if (iidx == num_to_delete) |
|
1114 break; |
|
1115 } |
|
1116 |
|
1117 if (new_len > 0) |
|
1118 { |
|
1119 T *new_data = new T [new_len]; |
|
1120 |
|
1121 int ii = 0; |
|
1122 iidx = 0; |
|
1123 for (int i = 0; i < len; i++) |
|
1124 { |
|
1125 if (iidx < num_to_delete && i == idx_arg.elem (iidx)) |
|
1126 iidx++; |
|
1127 else |
|
1128 { |
|
1129 new_data[ii] = elem (i); |
|
1130 ii++; |
|
1131 } |
|
1132 } |
|
1133 |
|
1134 if (--rep->count <= 0) |
|
1135 delete rep; |
|
1136 |
|
1137 rep = new typename Array<T>::ArrayRep (new_data, new_len); |
|
1138 |
|
1139 dimensions.resize (1); |
|
1140 dimensions(0) = new_len; |
|
1141 } |
|
1142 else |
|
1143 (*current_liboctave_error_handler) |
|
1144 ("A(idx) = []: index out of range"); |
|
1145 } |
|
1146 } |
|
1147 } |
|
1148 |
|
1149 template <class T> |
|
1150 void |
|
1151 Array<T>::maybe_delete_elements_2 (idx_vector& idx_arg) |
|
1152 { |
4548
|
1153 assert (ndims () == 2); |
|
1154 |
4517
|
1155 int nr = dim1 (); |
|
1156 int nc = dim2 (); |
|
1157 |
|
1158 if (nr == 0 && nc == 0) |
|
1159 return; |
|
1160 |
|
1161 int n; |
|
1162 if (nr == 1) |
|
1163 n = nc; |
|
1164 else if (nc == 1) |
|
1165 n = nr; |
|
1166 else |
|
1167 { |
|
1168 (*current_liboctave_error_handler) |
|
1169 ("A(idx) = []: expecting A to be row or column vector or scalar"); |
|
1170 |
|
1171 return; |
|
1172 } |
|
1173 |
|
1174 if (idx_arg.is_colon_equiv (n, 1)) |
|
1175 { |
|
1176 // Either A(:) = [] or A(idx) = [] with idx enumerating all |
|
1177 // elements, so we delete all elements and return [](0x0). To |
|
1178 // preserve the orientation of the vector, you have to use |
|
1179 // A(idx,:) = [] (delete rows) or A(:,idx) (delete columns). |
|
1180 |
|
1181 resize_no_fill (0, 0); |
|
1182 return; |
|
1183 } |
|
1184 |
|
1185 idx_arg.sort (true); |
|
1186 |
|
1187 int num_to_delete = idx_arg.length (n); |
|
1188 |
|
1189 if (num_to_delete != 0) |
|
1190 { |
|
1191 int new_n = n; |
|
1192 |
|
1193 int iidx = 0; |
|
1194 |
|
1195 for (int i = 0; i < n; i++) |
|
1196 if (i == idx_arg.elem (iidx)) |
|
1197 { |
|
1198 iidx++; |
|
1199 new_n--; |
|
1200 |
|
1201 if (iidx == num_to_delete) |
|
1202 break; |
|
1203 } |
|
1204 |
|
1205 if (new_n > 0) |
|
1206 { |
|
1207 T *new_data = new T [new_n]; |
|
1208 |
|
1209 int ii = 0; |
|
1210 iidx = 0; |
|
1211 for (int i = 0; i < n; i++) |
|
1212 { |
|
1213 if (iidx < num_to_delete && i == idx_arg.elem (iidx)) |
|
1214 iidx++; |
|
1215 else |
|
1216 { |
|
1217 if (nr == 1) |
|
1218 new_data[ii] = elem (0, i); |
|
1219 else |
|
1220 new_data[ii] = elem (i, 0); |
|
1221 |
|
1222 ii++; |
|
1223 } |
|
1224 } |
|
1225 |
|
1226 if (--(Array<T>::rep)->count <= 0) |
|
1227 delete Array<T>::rep; |
|
1228 |
|
1229 Array<T>::rep = new typename Array<T>::ArrayRep (new_data, new_n); |
|
1230 |
|
1231 dimensions.resize (2); |
|
1232 |
|
1233 if (nr == 1) |
|
1234 { |
|
1235 dimensions(0) = 1; |
|
1236 dimensions(1) = new_n; |
|
1237 } |
|
1238 else |
|
1239 { |
|
1240 dimensions(0) = new_n; |
|
1241 dimensions(1) = 1; |
|
1242 } |
|
1243 } |
|
1244 else |
|
1245 (*current_liboctave_error_handler) |
|
1246 ("A(idx) = []: index out of range"); |
|
1247 } |
|
1248 } |
|
1249 |
|
1250 template <class T> |
|
1251 void |
|
1252 Array<T>::maybe_delete_elements (idx_vector& idx_i, idx_vector& idx_j) |
|
1253 { |
4548
|
1254 assert (ndims () == 2); |
|
1255 |
4517
|
1256 int nr = dim1 (); |
|
1257 int nc = dim2 (); |
|
1258 |
|
1259 if (nr == 0 && nc == 0) |
|
1260 return; |
|
1261 |
|
1262 if (idx_i.is_colon ()) |
|
1263 { |
|
1264 if (idx_j.is_colon ()) |
|
1265 { |
|
1266 // A(:,:) -- We are deleting columns and rows, so the result |
|
1267 // is [](0x0). |
|
1268 |
|
1269 resize_no_fill (0, 0); |
|
1270 return; |
|
1271 } |
|
1272 |
|
1273 if (idx_j.is_colon_equiv (nc, 1)) |
|
1274 { |
|
1275 // A(:,j) -- We are deleting columns by enumerating them, |
|
1276 // If we enumerate all of them, we should have zero columns |
|
1277 // with the same number of rows that we started with. |
|
1278 |
|
1279 resize_no_fill (nr, 0); |
|
1280 return; |
|
1281 } |
|
1282 } |
|
1283 |
|
1284 if (idx_j.is_colon () && idx_i.is_colon_equiv (nr, 1)) |
|
1285 { |
|
1286 // A(i,:) -- We are deleting rows by enumerating them. If we |
|
1287 // enumerate all of them, we should have zero rows with the |
|
1288 // same number of columns that we started with. |
|
1289 |
|
1290 resize_no_fill (0, nc); |
|
1291 return; |
|
1292 } |
|
1293 |
|
1294 if (idx_i.is_colon_equiv (nr, 1)) |
|
1295 { |
|
1296 if (idx_j.is_colon_equiv (nc, 1)) |
|
1297 resize_no_fill (0, 0); |
|
1298 else |
|
1299 { |
|
1300 idx_j.sort (true); |
|
1301 |
|
1302 int num_to_delete = idx_j.length (nc); |
|
1303 |
|
1304 if (num_to_delete != 0) |
|
1305 { |
|
1306 if (nr == 1 && num_to_delete == nc) |
|
1307 resize_no_fill (0, 0); |
|
1308 else |
|
1309 { |
|
1310 int new_nc = nc; |
|
1311 |
|
1312 int iidx = 0; |
|
1313 |
|
1314 for (int j = 0; j < nc; j++) |
|
1315 if (j == idx_j.elem (iidx)) |
|
1316 { |
|
1317 iidx++; |
|
1318 new_nc--; |
|
1319 |
|
1320 if (iidx == num_to_delete) |
|
1321 break; |
|
1322 } |
|
1323 |
|
1324 if (new_nc > 0) |
|
1325 { |
|
1326 T *new_data = new T [nr * new_nc]; |
|
1327 |
|
1328 int jj = 0; |
|
1329 iidx = 0; |
|
1330 for (int j = 0; j < nc; j++) |
|
1331 { |
|
1332 if (iidx < num_to_delete && j == idx_j.elem (iidx)) |
|
1333 iidx++; |
|
1334 else |
|
1335 { |
|
1336 for (int i = 0; i < nr; i++) |
|
1337 new_data[nr*jj+i] = elem (i, j); |
|
1338 jj++; |
|
1339 } |
|
1340 } |
|
1341 |
|
1342 if (--(Array<T>::rep)->count <= 0) |
|
1343 delete Array<T>::rep; |
|
1344 |
|
1345 Array<T>::rep = new typename Array<T>::ArrayRep (new_data, nr * new_nc); |
|
1346 |
|
1347 dimensions.resize (2); |
|
1348 dimensions(1) = new_nc; |
|
1349 } |
|
1350 else |
|
1351 (*current_liboctave_error_handler) |
|
1352 ("A(idx) = []: index out of range"); |
|
1353 } |
|
1354 } |
|
1355 } |
|
1356 } |
|
1357 else if (idx_j.is_colon_equiv (nc, 1)) |
|
1358 { |
|
1359 if (idx_i.is_colon_equiv (nr, 1)) |
|
1360 resize_no_fill (0, 0); |
|
1361 else |
|
1362 { |
|
1363 idx_i.sort (true); |
|
1364 |
|
1365 int num_to_delete = idx_i.length (nr); |
|
1366 |
|
1367 if (num_to_delete != 0) |
|
1368 { |
|
1369 if (nc == 1 && num_to_delete == nr) |
|
1370 resize_no_fill (0, 0); |
|
1371 else |
|
1372 { |
|
1373 int new_nr = nr; |
|
1374 |
|
1375 int iidx = 0; |
|
1376 |
|
1377 for (int i = 0; i < nr; i++) |
|
1378 if (i == idx_i.elem (iidx)) |
|
1379 { |
|
1380 iidx++; |
|
1381 new_nr--; |
|
1382 |
|
1383 if (iidx == num_to_delete) |
|
1384 break; |
|
1385 } |
|
1386 |
|
1387 if (new_nr > 0) |
|
1388 { |
|
1389 T *new_data = new T [new_nr * nc]; |
|
1390 |
|
1391 int ii = 0; |
|
1392 iidx = 0; |
|
1393 for (int i = 0; i < nr; i++) |
|
1394 { |
|
1395 if (iidx < num_to_delete && i == idx_i.elem (iidx)) |
|
1396 iidx++; |
|
1397 else |
|
1398 { |
|
1399 for (int j = 0; j < nc; j++) |
|
1400 new_data[new_nr*j+ii] = elem (i, j); |
|
1401 ii++; |
|
1402 } |
|
1403 } |
|
1404 |
|
1405 if (--(Array<T>::rep)->count <= 0) |
|
1406 delete Array<T>::rep; |
|
1407 |
|
1408 Array<T>::rep = new typename Array<T>::ArrayRep (new_data, new_nr * nc); |
|
1409 |
|
1410 dimensions.resize (2); |
|
1411 dimensions(0) = new_nr; |
|
1412 } |
|
1413 else |
|
1414 (*current_liboctave_error_handler) |
|
1415 ("A(idx) = []: index out of range"); |
|
1416 } |
|
1417 } |
|
1418 } |
|
1419 } |
|
1420 } |
|
1421 |
|
1422 template <class T> |
|
1423 void |
|
1424 Array<T>::maybe_delete_elements (idx_vector&, idx_vector&, idx_vector&) |
|
1425 { |
|
1426 assert (0); |
|
1427 } |
|
1428 |
|
1429 template <class T> |
|
1430 void |
4585
|
1431 Array<T>::maybe_delete_elements (Array<idx_vector>& ra_idx, const T& rfv) |
4517
|
1432 { |
4585
|
1433 int n_idx = ra_idx.length (); |
4517
|
1434 |
|
1435 dim_vector lhs_dims = dims (); |
|
1436 |
4740
|
1437 Array<int> idx_is_colon (n_idx, 0); |
|
1438 |
|
1439 Array<int> idx_is_colon_equiv (n_idx, 0); |
4517
|
1440 |
|
1441 // Initialization of colon arrays. |
|
1442 |
|
1443 for (int i = 0; i < n_idx; i++) |
|
1444 { |
4585
|
1445 idx_is_colon_equiv(i) = ra_idx(i).is_colon_equiv (lhs_dims(i), 1); |
|
1446 |
|
1447 idx_is_colon(i) = ra_idx(i).is_colon (); |
4517
|
1448 } |
|
1449 |
|
1450 if (all_ones (idx_is_colon) || all_ones (idx_is_colon_equiv)) |
|
1451 { |
|
1452 // A(:,:,:) -- we are deleting elements in all dimensions, so |
|
1453 // the result is [](0x0x0). |
|
1454 |
|
1455 dim_vector zeros; |
|
1456 zeros.resize (n_idx); |
|
1457 |
|
1458 for (int i = 0; i < n_idx; i++) |
|
1459 zeros(i) = 0; |
|
1460 |
|
1461 resize (zeros, rfv); |
|
1462 } |
|
1463 |
|
1464 else if (num_ones (idx_is_colon) == n_idx - 1 |
|
1465 && num_ones (idx_is_colon_equiv) == n_idx) |
|
1466 { |
|
1467 // A(:,:,j) -- we are deleting elements in one dimension by |
|
1468 // enumerating them. |
|
1469 // |
|
1470 // If we enumerate all of the elements, we should have zero |
|
1471 // elements in that dimension with the same number of elements |
|
1472 // in the other dimensions that we started with. |
|
1473 |
|
1474 dim_vector temp_dims; |
|
1475 temp_dims.resize (n_idx); |
|
1476 |
|
1477 for (int i = 0; i < n_idx; i++) |
|
1478 { |
|
1479 if (idx_is_colon (i)) |
|
1480 temp_dims (i) = lhs_dims (i); |
|
1481 else |
|
1482 temp_dims (i) = 0; |
|
1483 } |
|
1484 |
|
1485 resize (temp_dims); |
|
1486 } |
|
1487 else if (num_ones (idx_is_colon) == n_idx - 1) |
|
1488 { |
|
1489 // We have colons in all indices except for one. |
|
1490 // This index tells us which slice to delete |
|
1491 |
4741
|
1492 if (n_idx < lhs_dims.length ()) |
|
1493 { |
4746
|
1494 // Collapse dimensions beyond last index. |
|
1495 |
|
1496 if (liboctave_wfi_flag && ! (ra_idx(n_idx-1).is_colon ())) |
|
1497 (*current_liboctave_warning_handler) |
|
1498 ("fewer indices than dimensions for N-d array"); |
|
1499 |
4741
|
1500 for (int i = n_idx; i < lhs_dims.length (); i++) |
|
1501 lhs_dims(n_idx-1) *= lhs_dims(i); |
|
1502 |
|
1503 lhs_dims.resize (n_idx); |
|
1504 |
|
1505 // Reshape *this. |
|
1506 dimensions = lhs_dims; |
|
1507 } |
|
1508 |
4517
|
1509 int non_col = 0; |
|
1510 |
|
1511 // Find the non-colon column. |
|
1512 |
|
1513 for (int i = 0; i < n_idx; i++) |
|
1514 { |
|
1515 if (! idx_is_colon (i)) |
|
1516 non_col = i; |
|
1517 } |
|
1518 |
|
1519 // The length of the non-colon dimension. |
|
1520 |
|
1521 int non_col_dim = lhs_dims (non_col); |
|
1522 |
4585
|
1523 ra_idx(non_col).sort (true); |
|
1524 |
|
1525 int num_to_delete = ra_idx(non_col).length (lhs_dims (non_col)); |
4517
|
1526 |
|
1527 if (num_to_delete > 0) |
|
1528 { |
4635
|
1529 int temp = lhs_dims.num_ones (); |
4517
|
1530 |
|
1531 if (non_col_dim == 1) |
|
1532 temp--; |
|
1533 |
|
1534 if (temp == n_idx - 1 && num_to_delete == non_col_dim) |
|
1535 { |
|
1536 // We have A with (1x1x4), where A(1,:,1:4) |
|
1537 // Delete all (0x0x0) |
|
1538 |
|
1539 dim_vector zero_dims (n_idx, 0); |
|
1540 |
|
1541 resize (zero_dims, rfv); |
|
1542 } |
|
1543 else |
|
1544 { |
|
1545 // New length of non-colon dimension |
|
1546 // (calculated in the next for loop) |
|
1547 |
|
1548 int new_dim = non_col_dim; |
|
1549 |
|
1550 int iidx = 0; |
|
1551 |
|
1552 for (int j = 0; j < non_col_dim; j++) |
4585
|
1553 if (j == ra_idx(non_col).elem (iidx)) |
4517
|
1554 { |
|
1555 iidx++; |
|
1556 |
|
1557 new_dim--; |
|
1558 |
|
1559 if (iidx == num_to_delete) |
|
1560 break; |
|
1561 } |
|
1562 |
|
1563 // Creating the new nd array after deletions. |
|
1564 |
|
1565 if (new_dim > 0) |
|
1566 { |
|
1567 // Calculate number of elements in new array. |
|
1568 |
|
1569 int num_new_elem=1; |
|
1570 |
|
1571 for (int i = 0; i < n_idx; i++) |
|
1572 { |
|
1573 if (i == non_col) |
|
1574 num_new_elem *= new_dim; |
|
1575 |
|
1576 else |
|
1577 num_new_elem *= lhs_dims(i); |
|
1578 } |
|
1579 |
|
1580 T *new_data = new T [num_new_elem]; |
4530
|
1581 |
4517
|
1582 Array<int> result_idx (lhs_dims.length (), 0); |
|
1583 |
|
1584 dim_vector new_lhs_dim = lhs_dims; |
|
1585 |
|
1586 new_lhs_dim(non_col) = new_dim; |
|
1587 |
|
1588 int num_elem = 1; |
|
1589 |
|
1590 int numidx = 0; |
|
1591 |
|
1592 int n = length (); |
|
1593 |
|
1594 for (int i =0; i < lhs_dims.length (); i++) |
|
1595 if (i != non_col) |
|
1596 num_elem *= lhs_dims (i); |
|
1597 |
4585
|
1598 num_elem *= ra_idx(non_col).capacity (); |
4517
|
1599 |
|
1600 for (int i = 0; i < n; i++) |
|
1601 { |
|
1602 if (numidx < num_elem |
4585
|
1603 && is_in (result_idx(non_col), ra_idx(non_col))) |
4517
|
1604 numidx++; |
|
1605 |
|
1606 else |
|
1607 { |
|
1608 Array<int> temp_result_idx = result_idx; |
|
1609 |
4585
|
1610 int num_lgt = how_many_lgt (result_idx(non_col), |
|
1611 ra_idx(non_col)); |
4517
|
1612 |
|
1613 temp_result_idx(non_col) -= num_lgt; |
|
1614 |
|
1615 int kidx |
|
1616 = ::compute_index (temp_result_idx, new_lhs_dim); |
|
1617 |
|
1618 new_data[kidx] = elem (result_idx); |
|
1619 } |
|
1620 |
|
1621 increment_index (result_idx, lhs_dims); |
|
1622 } |
|
1623 |
|
1624 if (--rep->count <= 0) |
|
1625 delete rep; |
|
1626 |
|
1627 rep = new typename Array<T>::ArrayRep (new_data, |
|
1628 num_new_elem); |
|
1629 |
|
1630 dimensions = new_lhs_dim; |
|
1631 } |
|
1632 } |
|
1633 } |
|
1634 } |
4530
|
1635 else if (num_ones (idx_is_colon) < n_idx) |
4517
|
1636 { |
|
1637 (*current_liboctave_error_handler) |
4530
|
1638 ("A null assignment can have only one non-colon index"); |
4517
|
1639 } |
|
1640 } |
|
1641 |
|
1642 template <class T> |
|
1643 Array<T> |
|
1644 Array<T>::value (void) |
|
1645 { |
|
1646 Array<T> retval; |
|
1647 |
|
1648 int n_idx = index_count (); |
|
1649 |
|
1650 if (n_idx == 2) |
|
1651 { |
|
1652 idx_vector *tmp = get_idx (); |
|
1653 |
|
1654 idx_vector idx_i = tmp[0]; |
|
1655 idx_vector idx_j = tmp[1]; |
|
1656 |
|
1657 retval = index (idx_i, idx_j); |
|
1658 } |
|
1659 else if (n_idx == 1) |
|
1660 { |
|
1661 retval = index (idx[0]); |
|
1662 } |
|
1663 else |
|
1664 (*current_liboctave_error_handler) |
|
1665 ("Array<T>::value: invalid number of indices specified"); |
|
1666 |
|
1667 clear_index (); |
|
1668 |
|
1669 return retval; |
|
1670 } |
|
1671 |
|
1672 template <class T> |
|
1673 Array<T> |
|
1674 Array<T>::index (idx_vector& idx_arg, int resize_ok, const T& rfv) const |
|
1675 { |
|
1676 Array<T> retval; |
|
1677 |
|
1678 switch (ndims ()) |
|
1679 { |
|
1680 case 1: |
|
1681 retval = index1 (idx_arg, resize_ok, rfv); |
|
1682 break; |
|
1683 |
|
1684 case 2: |
|
1685 retval = index2 (idx_arg, resize_ok, rfv); |
|
1686 break; |
|
1687 |
|
1688 default: |
4530
|
1689 retval = indexN (idx_arg, resize_ok, rfv); |
4517
|
1690 break; |
|
1691 } |
|
1692 |
|
1693 return retval; |
|
1694 } |
|
1695 |
|
1696 template <class T> |
|
1697 Array<T> |
|
1698 Array<T>::index1 (idx_vector& idx_arg, int resize_ok, const T& rfv) const |
|
1699 { |
|
1700 Array<T> retval; |
|
1701 |
|
1702 int len = length (); |
|
1703 |
|
1704 int n = idx_arg.freeze (len, "vector", resize_ok); |
|
1705 |
|
1706 if (idx_arg) |
|
1707 { |
|
1708 if (idx_arg.is_colon_equiv (len)) |
|
1709 { |
|
1710 retval = *this; |
|
1711 } |
|
1712 else if (n == 0) |
|
1713 { |
|
1714 retval.resize_no_fill (0); |
|
1715 } |
|
1716 else if (len == 1 && n > 1 |
|
1717 && idx_arg.one_zero_only () |
|
1718 && idx_arg.ones_count () == n) |
|
1719 { |
4548
|
1720 retval.resize_and_fill (n, elem (0)); |
4517
|
1721 } |
|
1722 else |
|
1723 { |
|
1724 retval.resize_no_fill (n); |
|
1725 |
|
1726 for (int i = 0; i < n; i++) |
|
1727 { |
|
1728 int ii = idx_arg.elem (i); |
|
1729 if (ii >= len) |
|
1730 retval.elem (i) = rfv; |
|
1731 else |
|
1732 retval.elem (i) = elem (ii); |
|
1733 } |
|
1734 } |
|
1735 } |
|
1736 |
|
1737 // idx_vector::freeze() printed an error message for us. |
|
1738 |
|
1739 return retval; |
|
1740 } |
|
1741 |
|
1742 template <class T> |
|
1743 Array<T> |
|
1744 Array<T>::index2 (idx_vector& idx_arg, int resize_ok, const T& rfv) const |
|
1745 { |
|
1746 Array<T> retval; |
|
1747 |
4548
|
1748 assert (ndims () == 2); |
|
1749 |
4517
|
1750 int nr = dim1 (); |
|
1751 int nc = dim2 (); |
|
1752 |
|
1753 int orig_len = nr * nc; |
|
1754 |
|
1755 int idx_orig_rows = idx_arg.orig_rows (); |
|
1756 int idx_orig_columns = idx_arg.orig_columns (); |
|
1757 |
|
1758 if (idx_arg.is_colon ()) |
|
1759 { |
|
1760 // Fast magic colon processing. |
|
1761 |
|
1762 int result_nr = nr * nc; |
|
1763 int result_nc = 1; |
|
1764 |
|
1765 retval = Array<T> (*this, dim_vector (result_nr, result_nc)); |
|
1766 } |
|
1767 else if (nr == 1 && nc == 1) |
|
1768 { |
|
1769 Array<T> tmp = Array<T>::index1 (idx_arg, resize_ok); |
|
1770 |
|
1771 if (tmp.length () != 0) |
|
1772 retval = Array<T> (tmp, dim_vector (idx_orig_rows, idx_orig_columns)); |
|
1773 else |
|
1774 retval = Array<T> (tmp, dim_vector (0, 0)); |
|
1775 } |
|
1776 else if (nr == 1 || nc == 1) |
|
1777 { |
|
1778 // If indexing a vector with a matrix, return value has same |
|
1779 // shape as the index. Otherwise, it has same orientation as |
|
1780 // indexed object. |
|
1781 |
|
1782 Array<T> tmp = index1 (idx_arg, resize_ok); |
|
1783 |
|
1784 int len = tmp.length (); |
|
1785 |
|
1786 if (len == 0) |
|
1787 { |
|
1788 if (idx_orig_rows == 0 || idx_orig_columns == 0) |
|
1789 retval = Array<T> (dim_vector (idx_orig_rows, idx_orig_columns)); |
|
1790 else if (nr == 1) |
|
1791 retval = Array<T> (dim_vector (1, 0)); |
|
1792 else |
|
1793 retval = Array<T> (dim_vector (0, 1)); |
|
1794 } |
|
1795 else |
|
1796 { |
4592
|
1797 if (idx_arg.one_zero_only () |
|
1798 || idx_orig_rows == 1 || idx_orig_columns == 1) |
4517
|
1799 { |
|
1800 if (nr == 1) |
|
1801 retval = Array<T> (tmp, dim_vector (1, len)); |
|
1802 else |
|
1803 retval = Array<T> (tmp, dim_vector (len, 1)); |
|
1804 } |
|
1805 else |
|
1806 retval = Array<T> (tmp, dim_vector (idx_orig_rows, idx_orig_columns)); |
|
1807 } |
|
1808 } |
|
1809 else |
|
1810 { |
|
1811 if (liboctave_wfi_flag |
|
1812 && ! (idx_arg.one_zero_only () |
|
1813 && idx_orig_rows == nr |
|
1814 && idx_orig_columns == nc)) |
|
1815 (*current_liboctave_warning_handler) ("single index used for matrix"); |
|
1816 |
|
1817 // This code is only for indexing matrices. The vector |
|
1818 // cases are handled above. |
|
1819 |
|
1820 idx_arg.freeze (nr * nc, "matrix", resize_ok); |
|
1821 |
|
1822 if (idx_arg) |
|
1823 { |
|
1824 int result_nr = idx_orig_rows; |
|
1825 int result_nc = idx_orig_columns; |
|
1826 |
|
1827 if (idx_arg.one_zero_only ()) |
|
1828 { |
|
1829 result_nr = idx_arg.ones_count (); |
|
1830 result_nc = (result_nr > 0 ? 1 : 0); |
|
1831 } |
|
1832 |
|
1833 retval.resize_no_fill (result_nr, result_nc); |
|
1834 |
|
1835 int k = 0; |
|
1836 for (int j = 0; j < result_nc; j++) |
|
1837 { |
|
1838 for (int i = 0; i < result_nr; i++) |
|
1839 { |
|
1840 int ii = idx_arg.elem (k++); |
|
1841 if (ii >= orig_len) |
|
1842 retval.elem (i, j) = rfv; |
|
1843 else |
|
1844 { |
|
1845 int fr = ii % nr; |
|
1846 int fc = (ii - fr) / nr; |
|
1847 retval.elem (i, j) = elem (fr, fc); |
|
1848 } |
|
1849 } |
|
1850 } |
|
1851 } |
|
1852 // idx_vector::freeze() printed an error message for us. |
|
1853 } |
|
1854 |
|
1855 return retval; |
|
1856 } |
|
1857 |
|
1858 template <class T> |
|
1859 Array<T> |
4530
|
1860 Array<T>::indexN (idx_vector& ra_idx, int resize_ok, const T& rfv) const |
|
1861 { |
|
1862 Array<T> retval; |
|
1863 |
4747
|
1864 int n_dims = dims().length (); |
|
1865 |
|
1866 int orig_len = dims().numel (); |
4530
|
1867 |
4653
|
1868 dim_vector idx_orig_dims = ra_idx.orig_dimensions (); |
4530
|
1869 |
|
1870 if (ra_idx.is_colon ()) |
|
1871 { |
4651
|
1872 // Fast magic colon processing. |
|
1873 |
|
1874 retval = Array<T> (*this, dim_vector (orig_len, 1)); |
4530
|
1875 } |
|
1876 else if (length () == 1) |
|
1877 { |
|
1878 // Only one element in array. |
|
1879 |
|
1880 Array<T> tmp = Array<T>::index (ra_idx, resize_ok); |
|
1881 |
|
1882 if (tmp.length () != 0) |
|
1883 retval = Array<T> (tmp, idx_orig_dims); |
|
1884 else |
4548
|
1885 retval = Array<T> (tmp, dim_vector (0)); |
4530
|
1886 } |
|
1887 else if (vector_equivalent (dims ())) |
|
1888 { |
|
1889 // We're getting elements from a vector equivalent i.e. (1x4x1). |
|
1890 |
|
1891 Array<T> tmp = Array<T>::index (ra_idx, resize_ok); |
|
1892 |
|
1893 int len = tmp.length (); |
|
1894 |
|
1895 if (len == 0) |
|
1896 { |
4747
|
1897 if (idx_orig_dims.any_zero ()) |
4530
|
1898 retval = Array<T> (idx_orig_dims); |
|
1899 else |
|
1900 { |
|
1901 dim_vector new_dims; |
4673
|
1902 |
4530
|
1903 new_dims.resize (n_dims); |
|
1904 |
|
1905 for (int i = 0; i < n_dims; i++) |
|
1906 { |
|
1907 if ((dims ())(i) == 1) |
|
1908 new_dims(i) = 1; |
|
1909 } |
|
1910 |
4673
|
1911 new_dims.chop_trailing_singletons (); |
|
1912 |
4530
|
1913 retval = Array<T> (new_dims); |
|
1914 } |
|
1915 } |
|
1916 else |
|
1917 { |
4746
|
1918 if (vector_equivalent (idx_orig_dims)) |
4530
|
1919 { |
|
1920 // Array<int> index (n_dims, len); |
|
1921 dim_vector new_dims; |
|
1922 |
|
1923 new_dims.resize (n_dims); |
|
1924 |
|
1925 for (int i = 0; i < n_dims; i++) |
|
1926 { |
|
1927 if ((dims ())(i) == 1) |
|
1928 new_dims(i) = 1; |
|
1929 } |
|
1930 |
4673
|
1931 new_dims.chop_trailing_singletons (); |
|
1932 |
4530
|
1933 retval = Array<T> (tmp, new_dims); |
|
1934 } |
|
1935 else |
|
1936 retval = Array<T> (tmp, idx_orig_dims); |
|
1937 |
|
1938 (*current_liboctave_error_handler) |
|
1939 ("I do not know what to do here yet!"); |
|
1940 } |
|
1941 } |
4651
|
1942 else |
4530
|
1943 { |
4651
|
1944 if (liboctave_wfi_flag |
|
1945 && ! (ra_idx.is_colon () |
4747
|
1946 || (ra_idx.one_zero_only () && idx_orig_dims == dims ()))) |
4651
|
1947 (*current_liboctave_warning_handler) |
|
1948 ("single index used for N-d array"); |
4530
|
1949 |
|
1950 ra_idx.freeze (orig_len, "nd-array", resize_ok); |
|
1951 |
|
1952 if (ra_idx) |
|
1953 { |
|
1954 dim_vector result_dims (idx_orig_dims); |
|
1955 |
|
1956 if (ra_idx.one_zero_only ()) |
|
1957 { |
4651
|
1958 result_dims.resize (2); |
|
1959 int ntot = ra_idx.ones_count (); |
|
1960 result_dims(0) = ntot; |
|
1961 result_dims(1) = (ntot > 0 ? 1 : 0); |
4530
|
1962 } |
|
1963 |
4673
|
1964 result_dims.chop_trailing_singletons (); |
|
1965 |
4530
|
1966 retval.resize (result_dims); |
|
1967 |
4747
|
1968 int n = result_dims.numel (); |
4530
|
1969 |
|
1970 int r_dims = result_dims.length (); |
|
1971 |
4587
|
1972 Array<int> iidx (r_dims, 0); |
4530
|
1973 |
|
1974 int k = 0; |
|
1975 |
|
1976 for (int i = 0; i < n; i++) |
|
1977 { |
|
1978 int ii = ra_idx.elem (k++); |
|
1979 |
|
1980 if (ii >= orig_len) |
4587
|
1981 retval.elem (iidx) = rfv; |
4530
|
1982 else |
|
1983 { |
|
1984 Array<int> temp = get_ra_idx (ii, dims ()); |
|
1985 |
4587
|
1986 retval.elem (iidx) = elem (temp); |
4530
|
1987 } |
|
1988 if (i != n - 1) |
4587
|
1989 increment_index (iidx, result_dims); |
4530
|
1990 } |
|
1991 } |
|
1992 } |
|
1993 |
|
1994 return retval; |
|
1995 } |
|
1996 |
|
1997 template <class T> |
|
1998 Array<T> |
4517
|
1999 Array<T>::index (idx_vector& idx_i, idx_vector& idx_j, int resize_ok, |
|
2000 const T& rfv) const |
|
2001 { |
|
2002 Array<T> retval; |
|
2003 |
4548
|
2004 assert (ndims () == 2); |
|
2005 |
4517
|
2006 int nr = dim1 (); |
|
2007 int nc = dim2 (); |
|
2008 |
|
2009 int n = idx_i.freeze (nr, "row", resize_ok); |
|
2010 int m = idx_j.freeze (nc, "column", resize_ok); |
|
2011 |
|
2012 if (idx_i && idx_j) |
|
2013 { |
|
2014 if (idx_i.orig_empty () || idx_j.orig_empty () || n == 0 || m == 0) |
|
2015 { |
|
2016 retval.resize_no_fill (n, m); |
|
2017 } |
|
2018 else if (idx_i.is_colon_equiv (nr) && idx_j.is_colon_equiv (nc)) |
|
2019 { |
|
2020 retval = *this; |
|
2021 } |
|
2022 else |
|
2023 { |
|
2024 retval.resize_no_fill (n, m); |
|
2025 |
|
2026 for (int j = 0; j < m; j++) |
|
2027 { |
|
2028 int jj = idx_j.elem (j); |
|
2029 for (int i = 0; i < n; i++) |
|
2030 { |
|
2031 int ii = idx_i.elem (i); |
|
2032 if (ii >= nr || jj >= nc) |
|
2033 retval.elem (i, j) = rfv; |
|
2034 else |
|
2035 retval.elem (i, j) = elem (ii, jj); |
|
2036 } |
|
2037 } |
|
2038 } |
|
2039 } |
|
2040 |
|
2041 // idx_vector::freeze() printed an error message for us. |
|
2042 |
|
2043 return retval; |
|
2044 } |
|
2045 |
|
2046 template <class T> |
|
2047 Array<T> |
4661
|
2048 Array<T>::index (Array<idx_vector>& ra_idx, int resize_ok, const T&) const |
4517
|
2049 { |
4530
|
2050 // This function handles all calls with more than one idx. |
|
2051 // For (3x3x3), the call can be A(2,5), A(2,:,:), A(3,2,3) etc. |
|
2052 |
4517
|
2053 Array<T> retval; |
|
2054 |
|
2055 int n_dims = dimensions.length (); |
|
2056 |
4737
|
2057 // Remove trailing singletons in ra_idx, but leave at least ndims |
|
2058 // elements. |
|
2059 |
|
2060 int ra_idx_len = ra_idx.length (); |
|
2061 |
|
2062 while (ra_idx_len > n_dims) |
|
2063 { |
|
2064 if (ra_idx(ra_idx_len-1) == 1) |
|
2065 ra_idx_len--; |
|
2066 else |
|
2067 break; |
|
2068 } |
|
2069 |
|
2070 ra_idx.resize (ra_idx_len); |
|
2071 |
4530
|
2072 if (n_dims < ra_idx.length ()) |
4517
|
2073 { |
4530
|
2074 (*current_liboctave_error_handler) |
|
2075 ("index exceeds N-d array dimensions"); |
|
2076 |
|
2077 return retval; |
|
2078 } |
|
2079 |
|
2080 dim_vector frozen_lengths = short_freeze (ra_idx, dimensions, resize_ok); |
|
2081 |
|
2082 if (frozen_lengths.length () <= n_dims) |
|
2083 { |
|
2084 if (all_ok (ra_idx)) |
4517
|
2085 { |
4747
|
2086 if (any_orig_empty (ra_idx) || frozen_lengths.any_zero ()) |
4530
|
2087 { |
4673
|
2088 frozen_lengths.chop_trailing_singletons (); |
|
2089 |
4530
|
2090 retval.resize (frozen_lengths); |
|
2091 } |
4738
|
2092 else if (frozen_lengths.length () == n_dims |
|
2093 && all_colon_equiv (ra_idx, dimensions)) |
4530
|
2094 { |
|
2095 retval = *this; |
|
2096 } |
|
2097 else |
|
2098 { |
4673
|
2099 dim_vector frozen_lengths_for_resize = frozen_lengths; |
|
2100 |
|
2101 frozen_lengths_for_resize.chop_trailing_singletons (); |
|
2102 |
|
2103 retval.resize (frozen_lengths_for_resize); |
|
2104 |
|
2105 int n = retval.length (); |
4530
|
2106 |
|
2107 Array<int> result_idx (ra_idx.length (), 0); |
|
2108 |
|
2109 dim_vector this_dims = dims (); |
4588
|
2110 |
|
2111 Array<int> elt_idx; |
|
2112 |
4530
|
2113 for (int i = 0; i < n; i++) |
4517
|
2114 { |
4588
|
2115 elt_idx = get_elt_idx (ra_idx, result_idx); |
4702
|
2116 |
4530
|
2117 int numelem_elt = get_scalar_idx (elt_idx, this_dims); |
|
2118 |
4673
|
2119 if (numelem_elt > length () || numelem_elt < 0) |
4530
|
2120 (*current_liboctave_error_handler) |
4673
|
2121 ("invalid N-d array index"); |
4530
|
2122 else |
4673
|
2123 retval.elem (i) = elem (numelem_elt); |
4702
|
2124 |
4530
|
2125 increment_index (result_idx, frozen_lengths); |
4702
|
2126 |
4517
|
2127 } |
|
2128 } |
|
2129 } |
|
2130 } |
|
2131 else |
|
2132 (*current_liboctave_error_handler) |
|
2133 ("invalid number of dimensions for N-dimensional array index"); |
|
2134 |
|
2135 return retval; |
|
2136 } |
|
2137 |
|
2138 // XXX FIXME XXX -- this is a mess. |
|
2139 |
|
2140 template <class LT, class RT> |
|
2141 int |
|
2142 assign (Array<LT>& lhs, const Array<RT>& rhs, const LT& rfv) |
|
2143 { |
|
2144 int retval = 0; |
|
2145 |
|
2146 switch (lhs.ndims ()) |
|
2147 { |
|
2148 case 0: |
|
2149 { |
|
2150 if (lhs.index_count () < 3) |
|
2151 { |
|
2152 // kluge... |
|
2153 lhs.resize_no_fill (0, 0); |
|
2154 retval = assign2 (lhs, rhs, rfv); |
|
2155 } |
|
2156 else |
|
2157 retval = assignN (lhs, rhs, rfv); |
|
2158 } |
|
2159 break; |
|
2160 |
|
2161 case 1: |
|
2162 { |
|
2163 if (lhs.index_count () > 1) |
|
2164 retval = assignN (lhs, rhs, rfv); |
|
2165 else |
|
2166 retval = assign1 (lhs, rhs, rfv); |
|
2167 } |
|
2168 break; |
|
2169 |
|
2170 case 2: |
|
2171 { |
|
2172 if (lhs.index_count () > 2) |
|
2173 retval = assignN (lhs, rhs, rfv); |
|
2174 else |
|
2175 retval = assign2 (lhs, rhs, rfv); |
|
2176 } |
|
2177 break; |
|
2178 |
|
2179 default: |
|
2180 retval = assignN (lhs, rhs, rfv); |
|
2181 break; |
|
2182 } |
|
2183 |
|
2184 return retval; |
|
2185 } |
|
2186 |
|
2187 template <class LT, class RT> |
|
2188 int |
|
2189 assign1 (Array<LT>& lhs, const Array<RT>& rhs, const LT& rfv) |
|
2190 { |
|
2191 int retval = 1; |
|
2192 |
|
2193 idx_vector *tmp = lhs.get_idx (); |
|
2194 |
|
2195 idx_vector lhs_idx = tmp[0]; |
|
2196 |
|
2197 int lhs_len = lhs.length (); |
|
2198 int rhs_len = rhs.length (); |
|
2199 |
|
2200 int n = lhs_idx.freeze (lhs_len, "vector", true, liboctave_wrore_flag); |
|
2201 |
|
2202 if (n != 0) |
|
2203 { |
|
2204 if (rhs_len == n || rhs_len == 1) |
|
2205 { |
|
2206 int max_idx = lhs_idx.max () + 1; |
|
2207 if (max_idx > lhs_len) |
4548
|
2208 lhs.resize_and_fill (max_idx, rfv); |
4517
|
2209 } |
|
2210 |
|
2211 if (rhs_len == n) |
|
2212 { |
|
2213 for (int i = 0; i < n; i++) |
|
2214 { |
|
2215 int ii = lhs_idx.elem (i); |
|
2216 lhs.elem (ii) = rhs.elem (i); |
|
2217 } |
|
2218 } |
|
2219 else if (rhs_len == 1) |
|
2220 { |
|
2221 RT scalar = rhs.elem (0); |
|
2222 |
|
2223 for (int i = 0; i < n; i++) |
|
2224 { |
|
2225 int ii = lhs_idx.elem (i); |
|
2226 lhs.elem (ii) = scalar; |
|
2227 } |
|
2228 } |
|
2229 else |
|
2230 { |
|
2231 (*current_liboctave_error_handler) |
|
2232 ("A(I) = X: X must be a scalar or a vector with same length as I"); |
|
2233 |
|
2234 retval = 0; |
|
2235 } |
|
2236 } |
|
2237 else if (lhs_idx.is_colon ()) |
|
2238 { |
|
2239 if (lhs_len == 0) |
|
2240 { |
|
2241 lhs.resize_no_fill (rhs_len); |
|
2242 |
|
2243 for (int i = 0; i < rhs_len; i++) |
|
2244 lhs.elem (i) = rhs.elem (i); |
|
2245 } |
|
2246 else |
|
2247 (*current_liboctave_error_handler) |
|
2248 ("A(:) = X: A must be the same size as X"); |
|
2249 } |
|
2250 else if (! (rhs_len == 1 || rhs_len == 0)) |
|
2251 { |
|
2252 (*current_liboctave_error_handler) |
|
2253 ("A([]) = X: X must also be an empty matrix or a scalar"); |
|
2254 |
|
2255 retval = 0; |
|
2256 } |
|
2257 |
|
2258 lhs.clear_index (); |
|
2259 |
|
2260 return retval; |
|
2261 } |
|
2262 |
|
2263 #define MAYBE_RESIZE_LHS \ |
|
2264 do \ |
|
2265 { \ |
|
2266 int max_row_idx = idx_i_is_colon ? rhs_nr : idx_i.max () + 1; \ |
|
2267 int max_col_idx = idx_j_is_colon ? rhs_nc : idx_j.max () + 1; \ |
|
2268 \ |
|
2269 int new_nr = max_row_idx > lhs_nr ? max_row_idx : lhs_nr; \ |
|
2270 int new_nc = max_col_idx > lhs_nc ? max_col_idx : lhs_nc; \ |
|
2271 \ |
|
2272 lhs.resize_and_fill (new_nr, new_nc, rfv); \ |
|
2273 } \ |
|
2274 while (0) |
|
2275 |
|
2276 template <class LT, class RT> |
|
2277 int |
|
2278 assign2 (Array<LT>& lhs, const Array<RT>& rhs, const LT& rfv) |
|
2279 { |
|
2280 int retval = 1; |
|
2281 |
|
2282 int n_idx = lhs.index_count (); |
|
2283 |
|
2284 int lhs_nr = lhs.rows (); |
|
2285 int lhs_nc = lhs.cols (); |
|
2286 |
|
2287 int rhs_nr = rhs.rows (); |
|
2288 int rhs_nc = rhs.cols (); |
4707
|
2289 |
|
2290 if (rhs.length () > 2) |
|
2291 { |
|
2292 dim_vector dv_tmp = rhs.squeeze().dims (); |
4709
|
2293 |
4708
|
2294 switch (dv_tmp.length ()) |
4707
|
2295 { |
4708
|
2296 case 1: |
|
2297 if (rhs_nr == 1) |
|
2298 rhs_nc = dv_tmp.elem (0); |
|
2299 break; |
4709
|
2300 |
4708
|
2301 case 2: |
4707
|
2302 rhs_nr = dv_tmp.elem (0); |
|
2303 rhs_nc = dv_tmp.elem (1); |
4708
|
2304 break; |
|
2305 |
|
2306 default: |
|
2307 (*current_liboctave_error_handler) |
|
2308 ("Array<T>::assign2: Dimension mismatch"); |
4709
|
2309 return 0; |
4707
|
2310 } |
|
2311 } |
4517
|
2312 |
|
2313 idx_vector *tmp = lhs.get_idx (); |
|
2314 |
|
2315 idx_vector idx_i; |
|
2316 idx_vector idx_j; |
|
2317 |
|
2318 if (n_idx > 1) |
|
2319 idx_j = tmp[1]; |
|
2320 |
|
2321 if (n_idx > 0) |
|
2322 idx_i = tmp[0]; |
|
2323 |
|
2324 if (n_idx == 2) |
|
2325 { |
|
2326 int n = idx_i.freeze (lhs_nr, "row", true, liboctave_wrore_flag); |
|
2327 |
|
2328 int m = idx_j.freeze (lhs_nc, "column", true, liboctave_wrore_flag); |
|
2329 |
|
2330 int idx_i_is_colon = idx_i.is_colon (); |
|
2331 int idx_j_is_colon = idx_j.is_colon (); |
|
2332 |
|
2333 if (idx_i_is_colon) |
|
2334 n = lhs_nr > 0 ? lhs_nr : rhs_nr; |
|
2335 |
|
2336 if (idx_j_is_colon) |
|
2337 m = lhs_nc > 0 ? lhs_nc : rhs_nc; |
|
2338 |
|
2339 if (idx_i && idx_j) |
|
2340 { |
|
2341 if (rhs_nr == 0 && rhs_nc == 0) |
|
2342 { |
|
2343 lhs.maybe_delete_elements (idx_i, idx_j); |
|
2344 } |
|
2345 else |
|
2346 { |
4534
|
2347 if (rhs_nr == 1 && rhs_nc == 1 && n >= 0 && m >= 0) |
4517
|
2348 { |
4534
|
2349 // No need to do anything if either of the indices |
|
2350 // are empty. |
|
2351 |
|
2352 if (n > 0 && m > 0) |
4517
|
2353 { |
4534
|
2354 MAYBE_RESIZE_LHS; |
|
2355 |
|
2356 RT scalar = rhs.elem (0, 0); |
|
2357 |
|
2358 for (int j = 0; j < m; j++) |
4517
|
2359 { |
4534
|
2360 int jj = idx_j.elem (j); |
|
2361 for (int i = 0; i < n; i++) |
|
2362 { |
|
2363 int ii = idx_i.elem (i); |
|
2364 lhs.elem (ii, jj) = scalar; |
|
2365 } |
4517
|
2366 } |
|
2367 } |
|
2368 } |
|
2369 else if (n == rhs_nr && m == rhs_nc) |
|
2370 { |
|
2371 if (n > 0 && m > 0) |
|
2372 { |
|
2373 MAYBE_RESIZE_LHS; |
|
2374 |
|
2375 for (int j = 0; j < m; j++) |
|
2376 { |
|
2377 int jj = idx_j.elem (j); |
|
2378 for (int i = 0; i < n; i++) |
|
2379 { |
|
2380 int ii = idx_i.elem (i); |
|
2381 lhs.elem (ii, jj) = rhs.elem (i, j); |
|
2382 } |
|
2383 } |
|
2384 } |
|
2385 } |
|
2386 else if (n == 0 && m == 0) |
|
2387 { |
|
2388 if (! ((rhs_nr == 1 && rhs_nc == 1) |
|
2389 || (rhs_nr == 0 && rhs_nc == 0))) |
|
2390 { |
|
2391 (*current_liboctave_error_handler) |
|
2392 ("A([], []) = X: X must be an empty matrix or a scalar"); |
|
2393 |
|
2394 retval = 0; |
|
2395 } |
|
2396 } |
|
2397 else |
|
2398 { |
|
2399 (*current_liboctave_error_handler) |
|
2400 ("A(I, J) = X: X must be a scalar or the number of elements in I must"); |
|
2401 (*current_liboctave_error_handler) |
|
2402 ("match the number of rows in X and the number of elements in J must"); |
|
2403 (*current_liboctave_error_handler) |
|
2404 ("match the number of columns in X"); |
|
2405 |
|
2406 retval = 0; |
|
2407 } |
|
2408 } |
|
2409 } |
|
2410 // idx_vector::freeze() printed an error message for us. |
|
2411 } |
|
2412 else if (n_idx == 1) |
|
2413 { |
|
2414 int lhs_is_empty = lhs_nr == 0 || lhs_nc == 0; |
|
2415 |
|
2416 if (lhs_is_empty || (lhs_nr == 1 && lhs_nc == 1)) |
|
2417 { |
|
2418 int lhs_len = lhs.length (); |
|
2419 |
|
2420 int n = idx_i.freeze (lhs_len, 0, true, liboctave_wrore_flag); |
|
2421 |
|
2422 if (idx_i) |
|
2423 { |
|
2424 if (rhs_nr == 0 && rhs_nc == 0) |
|
2425 { |
|
2426 if (n != 0 && (lhs_nr != 0 || lhs_nc != 0)) |
|
2427 lhs.maybe_delete_elements (idx_i); |
|
2428 } |
|
2429 else |
|
2430 { |
|
2431 if (liboctave_wfi_flag) |
|
2432 { |
|
2433 if (lhs_is_empty |
|
2434 && idx_i.is_colon () |
|
2435 && ! (rhs_nr == 1 || rhs_nc == 1)) |
|
2436 { |
|
2437 (*current_liboctave_warning_handler) |
|
2438 ("A(:) = X: X is not a vector or scalar"); |
|
2439 } |
|
2440 else |
|
2441 { |
|
2442 int idx_nr = idx_i.orig_rows (); |
|
2443 int idx_nc = idx_i.orig_columns (); |
|
2444 |
|
2445 if (! (rhs_nr == idx_nr && rhs_nc == idx_nc)) |
|
2446 (*current_liboctave_warning_handler) |
|
2447 ("A(I) = X: X does not have same shape as I"); |
|
2448 } |
|
2449 } |
|
2450 |
|
2451 if (assign1 ((Array<LT>&) lhs, (Array<RT>&) rhs, rfv)) |
|
2452 { |
|
2453 int len = lhs.length (); |
|
2454 |
|
2455 if (len > 0) |
|
2456 { |
|
2457 // The following behavior is much simplified |
|
2458 // over previous versions of Octave. It |
|
2459 // seems to be compatible with Matlab. |
|
2460 |
|
2461 lhs.dimensions = dim_vector (1, lhs.length ()); |
|
2462 } |
|
2463 else |
|
2464 lhs.dimensions = dim_vector (0, 0); |
|
2465 } |
|
2466 else |
|
2467 retval = 0; |
|
2468 } |
|
2469 } |
|
2470 // idx_vector::freeze() printed an error message for us. |
|
2471 } |
|
2472 else if (lhs_nr == 1) |
|
2473 { |
|
2474 idx_i.freeze (lhs_nc, "vector", true, liboctave_wrore_flag); |
|
2475 |
|
2476 if (idx_i) |
|
2477 { |
|
2478 if (rhs_nr == 0 && rhs_nc == 0) |
|
2479 lhs.maybe_delete_elements (idx_i); |
|
2480 else |
|
2481 { |
|
2482 if (assign1 ((Array<LT>&) lhs, (Array<RT>&) rhs, rfv)) |
|
2483 lhs.dimensions = dim_vector (1, lhs.length ()); |
|
2484 else |
|
2485 retval = 0; |
|
2486 } |
|
2487 } |
|
2488 // idx_vector::freeze() printed an error message for us. |
|
2489 } |
|
2490 else if (lhs_nc == 1) |
|
2491 { |
|
2492 idx_i.freeze (lhs_nr, "vector", true, liboctave_wrore_flag); |
|
2493 |
|
2494 if (idx_i) |
|
2495 { |
|
2496 if (rhs_nr == 0 && rhs_nc == 0) |
|
2497 lhs.maybe_delete_elements (idx_i); |
|
2498 else |
|
2499 { |
|
2500 if (assign1 ((Array<LT>&) lhs, (Array<RT>&) rhs, rfv)) |
|
2501 lhs.dimensions = dim_vector (lhs.length (), 1); |
|
2502 else |
|
2503 retval = 0; |
|
2504 } |
|
2505 } |
|
2506 // idx_vector::freeze() printed an error message for us. |
|
2507 } |
|
2508 else |
|
2509 { |
|
2510 if (liboctave_wfi_flag |
|
2511 && ! (idx_i.is_colon () |
|
2512 || (idx_i.one_zero_only () |
|
2513 && idx_i.orig_rows () == lhs_nr |
|
2514 && idx_i.orig_columns () == lhs_nc))) |
|
2515 (*current_liboctave_warning_handler) |
|
2516 ("single index used for matrix"); |
|
2517 |
|
2518 int len = idx_i.freeze (lhs_nr * lhs_nc, "matrix"); |
|
2519 |
|
2520 if (idx_i) |
|
2521 { |
|
2522 if (len == 0) |
|
2523 { |
|
2524 if (! ((rhs_nr == 1 && rhs_nc == 1) |
|
2525 || (rhs_nr == 0 && rhs_nc == 0))) |
|
2526 (*current_liboctave_error_handler) |
|
2527 ("A([]) = X: X must be an empty matrix or scalar"); |
|
2528 } |
|
2529 else if (len == rhs_nr * rhs_nc) |
|
2530 { |
|
2531 int k = 0; |
|
2532 for (int j = 0; j < rhs_nc; j++) |
|
2533 { |
|
2534 for (int i = 0; i < rhs_nr; i++) |
|
2535 { |
|
2536 int ii = idx_i.elem (k++); |
|
2537 int fr = ii % lhs_nr; |
|
2538 int fc = (ii - fr) / lhs_nr; |
|
2539 lhs.elem (fr, fc) = rhs.elem (i, j); |
|
2540 } |
|
2541 } |
|
2542 } |
4716
|
2543 else if (rhs_nr == 1 && rhs_nc == 1) |
4517
|
2544 { |
|
2545 RT scalar = rhs.elem (0, 0); |
|
2546 |
|
2547 for (int i = 0; i < len; i++) |
|
2548 { |
|
2549 int ii = idx_i.elem (i); |
4716
|
2550 lhs.elem (ii) = scalar; |
4517
|
2551 } |
|
2552 } |
|
2553 else |
|
2554 { |
|
2555 (*current_liboctave_error_handler) |
|
2556 ("A(I) = X: X must be a scalar or a matrix with the same size as I"); |
|
2557 |
|
2558 retval = 0; |
|
2559 } |
|
2560 } |
|
2561 // idx_vector::freeze() printed an error message for us. |
|
2562 } |
|
2563 } |
|
2564 else |
|
2565 { |
|
2566 (*current_liboctave_error_handler) |
|
2567 ("invalid number of indices for matrix expression"); |
|
2568 |
|
2569 retval = 0; |
|
2570 } |
|
2571 |
|
2572 lhs.clear_index (); |
|
2573 |
|
2574 return retval; |
|
2575 } |
|
2576 |
|
2577 template <class LT, class RT> |
|
2578 int |
|
2579 assignN (Array<LT>& lhs, const Array<RT>& rhs, const LT& rfv) |
|
2580 { |
|
2581 int retval = 1; |
|
2582 |
4746
|
2583 dim_vector rhs_dims = rhs.dims (); |
|
2584 |
|
2585 int rhs_dims_len = rhs_dims.length (); |
|
2586 |
|
2587 bool rhs_is_scalar = is_scalar (rhs_dims); |
|
2588 |
4517
|
2589 int n_idx = lhs.index_count (); |
|
2590 |
4745
|
2591 idx_vector *idx_vex = lhs.get_idx (); |
|
2592 |
|
2593 Array<idx_vector> idx = conv_to_array (idx_vex, n_idx); |
4517
|
2594 |
4743
|
2595 if (rhs_dims_len == 2 && rhs_dims(0) == 0 && rhs_dims(1) == 0) |
4517
|
2596 { |
|
2597 lhs.maybe_delete_elements (idx, rfv); |
|
2598 } |
4657
|
2599 else if (n_idx == 1) |
4517
|
2600 { |
4657
|
2601 idx_vector iidx = idx(0); |
|
2602 |
|
2603 if (liboctave_wfi_flag |
|
2604 && ! (iidx.is_colon () |
|
2605 || (iidx.one_zero_only () |
|
2606 && iidx.orig_dimensions () == lhs.dims ()))) |
|
2607 (*current_liboctave_warning_handler) |
4746
|
2608 ("single index used for N-d array"); |
4657
|
2609 |
|
2610 int lhs_len = lhs.length (); |
|
2611 |
4746
|
2612 int len = iidx.freeze (lhs_len, "N-d arrray"); |
4657
|
2613 |
|
2614 if (iidx) |
4533
|
2615 { |
4657
|
2616 if (len == 0) |
4656
|
2617 { |
4657
|
2618 if (! (rhs_dims.all_ones () || rhs_dims.all_zero ())) |
4743
|
2619 { |
|
2620 (*current_liboctave_error_handler) |
|
2621 ("A([]) = X: X must be an empty matrix or scalar"); |
|
2622 |
|
2623 retval = 0; |
|
2624 } |
4657
|
2625 } |
|
2626 else if (len == rhs.length ()) |
|
2627 { |
|
2628 for (int i = 0; i < len; i++) |
4656
|
2629 { |
4657
|
2630 int ii = iidx.elem (i); |
|
2631 |
|
2632 lhs.elem (ii) = rhs.elem (i); |
4656
|
2633 } |
|
2634 } |
4716
|
2635 else if (rhs_is_scalar) |
4657
|
2636 { |
|
2637 RT scalar = rhs.elem (0); |
|
2638 |
|
2639 for (int i = 0; i < len; i++) |
|
2640 { |
|
2641 int ii = iidx.elem (i); |
|
2642 |
|
2643 lhs.elem (ii) = scalar; |
|
2644 } |
|
2645 } |
|
2646 else |
|
2647 { |
|
2648 (*current_liboctave_error_handler) |
4702
|
2649 ("A(I) = X: X must be a scalar or a matrix with the same size as I"); |
|
2650 |
4657
|
2651 retval = 0; |
|
2652 } |
|
2653 |
4656
|
2654 // idx_vector::freeze() printed an error message for us. |
4533
|
2655 } |
4702
|
2656 } |
4743
|
2657 else |
4702
|
2658 { |
4746
|
2659 // Maybe expand to more dimensions. |
|
2660 |
|
2661 dim_vector lhs_dims = lhs.dims (); |
|
2662 |
|
2663 int lhs_dims_len = lhs_dims.length (); |
|
2664 |
|
2665 dim_vector final_lhs_dims = lhs_dims; |
|
2666 |
|
2667 dim_vector frozen_len; |
|
2668 |
4747
|
2669 int orig_lhs_dims_len = lhs_dims_len; |
|
2670 |
|
2671 bool orig_empty = lhs_dims.all_zero (); |
|
2672 |
|
2673 if (n_idx < lhs_dims_len) |
4517
|
2674 { |
4747
|
2675 // Collapse dimensions beyond last index. |
|
2676 |
|
2677 if (liboctave_wfi_flag && ! (idx(n_idx-1).is_colon ())) |
|
2678 (*current_liboctave_warning_handler) |
|
2679 ("fewer indices than dimensions for N-d array"); |
|
2680 |
|
2681 for (int i = n_idx; i < lhs_dims_len; i++) |
|
2682 lhs_dims(n_idx-1) *= lhs_dims(i); |
|
2683 |
|
2684 lhs_dims.resize (n_idx); |
|
2685 |
|
2686 lhs.resize (lhs_dims); |
|
2687 |
|
2688 lhs_dims = lhs.dims (); |
|
2689 |
|
2690 lhs_dims_len = lhs_dims.length (); |
|
2691 } |
|
2692 |
|
2693 // Resize. |
|
2694 |
|
2695 dim_vector new_dims; |
|
2696 new_dims.resize (n_idx); |
|
2697 |
|
2698 for (int i = 0; i < n_idx; i++) |
|
2699 { |
|
2700 if (orig_empty) |
4746
|
2701 { |
4747
|
2702 // If index is a colon, resizing to RHS dimensions is |
|
2703 // allowed because we started out empty. |
4746
|
2704 |
|
2705 new_dims(i) |
4747
|
2706 = (i < rhs_dims.length () && idx(i).is_colon ()) |
|
2707 ? rhs_dims(i) : idx(i).max () + 1; |
4746
|
2708 } |
4747
|
2709 else |
4746
|
2710 { |
4747
|
2711 // We didn't start out with all zero dimensions, so if |
|
2712 // index is a colon, it refers to the current LHS |
|
2713 // dimension. Otherwise, it is OK to enlarge to a |
|
2714 // dimension given by the largest index. |
|
2715 |
|
2716 new_dims(i) |
|
2717 = (idx(i).is_colon () || idx(i).max () < lhs_dims(i)) |
|
2718 ? lhs_dims(i) : idx(i).max () + 1; |
4745
|
2719 } |
4747
|
2720 } |
|
2721 |
|
2722 if (! orig_empty |
|
2723 && n_idx < orig_lhs_dims_len |
|
2724 && new_dims(n_idx-1) != lhs_dims(n_idx-1)) |
|
2725 { |
|
2726 // We reshaped and the last dimension changed. This has to |
|
2727 // be an error, because we don't know how to undo that |
|
2728 // later... |
|
2729 |
|
2730 (*current_liboctave_error_handler) |
|
2731 ("array index %d (= %d) for assignment requires invalid resizing operation", |
|
2732 n_idx, new_dims(n_idx-1)); |
|
2733 |
|
2734 retval = 0; |
|
2735 } |
|
2736 else |
|
2737 { |
|
2738 if (n_idx < orig_lhs_dims_len) |
4702
|
2739 { |
4746
|
2740 for (int i = 0; i < n_idx-1; i++) |
|
2741 final_lhs_dims(i) = new_dims(i); |
4702
|
2742 } |
4747
|
2743 else |
|
2744 final_lhs_dims = new_dims; |
4746
|
2745 |
|
2746 lhs.resize_and_fill (new_dims, rfv); |
|
2747 lhs_dims = lhs.dims (); |
|
2748 lhs_dims_len = lhs_dims.length (); |
4747
|
2749 |
|
2750 frozen_len = freeze (idx, lhs_dims, true); |
|
2751 |
|
2752 if (rhs_is_scalar) |
4635
|
2753 { |
4747
|
2754 if (! final_lhs_dims.any_zero ()) |
|
2755 { |
|
2756 int n = Array<LT>::get_size (frozen_len); |
|
2757 |
|
2758 Array<int> result_idx (lhs_dims_len, 0); |
|
2759 |
|
2760 RT scalar = rhs.elem (0); |
|
2761 |
|
2762 for (int i = 0; i < n; i++) |
|
2763 { |
|
2764 Array<int> elt_idx = get_elt_idx (idx, result_idx); |
|
2765 |
|
2766 lhs.elem (elt_idx) = scalar; |
|
2767 |
|
2768 increment_index (result_idx, frozen_len); |
|
2769 } |
|
2770 } |
4743
|
2771 } |
|
2772 else |
|
2773 { |
4747
|
2774 // RHS is matrix or higher dimension. |
|
2775 |
|
2776 // Check that non-singleton RHS dimensions conform to |
|
2777 // non-singleton LHS index dimensions. |
|
2778 |
|
2779 dim_vector t_rhs_dims = rhs_dims.squeeze (); |
|
2780 dim_vector t_frozen_len = frozen_len.squeeze (); |
|
2781 |
|
2782 // If after sqeezing out singleton dimensions, RHS is |
|
2783 // vector and LHS is vector, force them to have the same |
|
2784 // orientation so that operations like |
|
2785 // |
|
2786 // a = zeros (3, 3, 3); |
|
2787 // a(1:3,1,1) = [1,2,3]; |
|
2788 // |
|
2789 // will work. |
|
2790 |
|
2791 if (t_rhs_dims.length () == 2 && t_frozen_len.length () == 2 |
|
2792 && ((t_rhs_dims.elem(1) == 1 |
|
2793 && t_frozen_len.elem(0) == 1) |
|
2794 || (t_rhs_dims.elem(0) == 1 |
|
2795 && t_frozen_len.elem(1) == 1))) |
4743
|
2796 { |
4747
|
2797 int t0 = t_rhs_dims.elem(0); |
|
2798 t_rhs_dims.elem(0) = t_rhs_dims.elem(1); |
|
2799 t_rhs_dims.elem(1) = t0; |
|
2800 } |
|
2801 |
|
2802 if (t_rhs_dims != t_frozen_len) |
|
2803 { |
|
2804 (*current_liboctave_error_handler) |
|
2805 ("A(IDX-LIST) = X: X must be a scalar or size of X must equal number of elements indexed by IDX-LIST"); |
|
2806 |
|
2807 retval = 0; |
|
2808 } |
|
2809 else |
|
2810 { |
|
2811 if (! final_lhs_dims.any_zero ()) |
|
2812 { |
|
2813 int n = Array<LT>::get_size (frozen_len); |
|
2814 |
|
2815 Array<int> result_idx (lhs_dims_len, 0); |
|
2816 |
|
2817 for (int i = 0; i < n; i++) |
|
2818 { |
|
2819 Array<int> elt_idx = get_elt_idx (idx, result_idx); |
|
2820 |
|
2821 lhs.elem (elt_idx) = rhs.elem (i); |
|
2822 |
|
2823 increment_index (result_idx, frozen_len); |
|
2824 } |
|
2825 } |
4743
|
2826 } |
|
2827 } |
4517
|
2828 } |
4745
|
2829 |
4746
|
2830 lhs.resize (final_lhs_dims); |
4517
|
2831 } |
|
2832 |
4703
|
2833 lhs.chop_trailing_singletons (); |
|
2834 |
4517
|
2835 lhs.clear_index (); |
|
2836 |
|
2837 return retval; |
|
2838 } |
|
2839 |
|
2840 template <class T> |
|
2841 void |
3933
|
2842 Array<T>::print_info (std::ostream& os, const std::string& prefix) const |
|
2843 { |
|
2844 os << prefix << "rep address: " << rep << "\n" |
|
2845 << prefix << "rep->len: " << rep->len << "\n" |
|
2846 << prefix << "rep->data: " << static_cast<void *> (rep->data) << "\n" |
|
2847 << prefix << "rep->count: " << rep->count << "\n"; |
4513
|
2848 |
|
2849 // 2D info: |
|
2850 // |
4657
|
2851 // << pefix << "rows: " << rows () << "\n" |
4513
|
2852 // << prefix << "cols: " << cols () << "\n"; |
3933
|
2853 } |
|
2854 |
237
|
2855 /* |
|
2856 ;;; Local Variables: *** |
|
2857 ;;; mode: C++ *** |
|
2858 ;;; End: *** |
|
2859 */ |