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 |
4755
|
1437 int n_lhs_dims = lhs_dims.length (); |
|
1438 |
4740
|
1439 Array<int> idx_is_colon (n_idx, 0); |
|
1440 |
|
1441 Array<int> idx_is_colon_equiv (n_idx, 0); |
4517
|
1442 |
|
1443 // Initialization of colon arrays. |
4755
|
1444 |
4517
|
1445 for (int i = 0; i < n_idx; i++) |
|
1446 { |
4585
|
1447 idx_is_colon_equiv(i) = ra_idx(i).is_colon_equiv (lhs_dims(i), 1); |
|
1448 |
|
1449 idx_is_colon(i) = ra_idx(i).is_colon (); |
4517
|
1450 } |
|
1451 |
4755
|
1452 bool idx_ok = true; |
|
1453 |
|
1454 // Check for index out of bounds. |
|
1455 |
|
1456 for (int i = 0 ; i < n_idx - 1; i++) |
4517
|
1457 { |
4755
|
1458 if (! (idx_is_colon(i) || idx_is_colon_equiv(i))) |
|
1459 { |
|
1460 ra_idx(i).sort (true); |
|
1461 |
|
1462 if (ra_idx(i).max () > lhs_dims(i)) |
|
1463 { |
|
1464 (*current_liboctave_error_handler) |
|
1465 ("index exceeds array dimensions"); |
|
1466 |
|
1467 idx_ok = false; |
|
1468 break; |
|
1469 } |
|
1470 else if (ra_idx(i).min () < 0) // I believe this is checked elsewhere |
|
1471 { |
|
1472 (*current_liboctave_error_handler) |
|
1473 ("index must be one or larger"); |
|
1474 |
|
1475 idx_ok = false; |
|
1476 break; |
|
1477 } |
|
1478 } |
4517
|
1479 } |
4755
|
1480 |
|
1481 if (n_idx <= n_lhs_dims) |
4517
|
1482 { |
4755
|
1483 int last_idx = ra_idx(n_idx-1).max (); |
|
1484 |
|
1485 int sum_el = lhs_dims(n_idx-1); |
|
1486 |
|
1487 for (int i = n_idx; i < n_lhs_dims; i++) |
|
1488 sum_el *= lhs_dims(i); |
|
1489 |
|
1490 if (last_idx > sum_el - 1) |
|
1491 { |
|
1492 (*current_liboctave_error_handler) |
|
1493 ("index exceeds array dimensions"); |
|
1494 |
|
1495 idx_ok = false; |
|
1496 } |
|
1497 } |
|
1498 |
|
1499 if (idx_ok) |
|
1500 { |
|
1501 if (n_idx > 1 |
|
1502 && (all_ones (idx_is_colon) || all_ones (idx_is_colon_equiv))) |
4517
|
1503 { |
4755
|
1504 // A(:,:,:) -- we are deleting elements in all dimensions, so |
|
1505 // the result is [](0x0x0). |
|
1506 |
|
1507 dim_vector zeros; |
|
1508 zeros.resize (n_idx); |
|
1509 |
|
1510 for (int i = 0; i < n_idx; i++) |
|
1511 zeros(i) = 0; |
|
1512 |
|
1513 resize (zeros, rfv); |
4517
|
1514 } |
|
1515 |
4755
|
1516 else if (n_idx > 1 |
|
1517 && num_ones (idx_is_colon) == n_idx - 1 |
|
1518 && num_ones (idx_is_colon_equiv) == n_idx) |
|
1519 { |
|
1520 // A(:,:,j) -- we are deleting elements in one dimension by |
|
1521 // enumerating them. |
|
1522 // |
|
1523 // If we enumerate all of the elements, we should have zero |
|
1524 // elements in that dimension with the same number of elements |
|
1525 // in the other dimensions that we started with. |
|
1526 |
|
1527 dim_vector temp_dims; |
|
1528 temp_dims.resize (n_idx); |
|
1529 |
|
1530 for (int i = 0; i < n_idx; i++) |
|
1531 { |
|
1532 if (idx_is_colon (i)) |
|
1533 temp_dims(i) = lhs_dims(i); |
|
1534 else |
|
1535 temp_dims(i) = 0; |
|
1536 } |
|
1537 |
|
1538 resize (temp_dims); |
|
1539 } |
|
1540 else if (n_idx > 1 && num_ones (idx_is_colon) == n_idx - 1) |
4741
|
1541 { |
4755
|
1542 // We have colons in all indices except for one. |
|
1543 // This index tells us which slice to delete |
|
1544 |
|
1545 if (n_idx < n_lhs_dims) |
|
1546 { |
|
1547 // Collapse dimensions beyond last index. |
|
1548 |
|
1549 if (liboctave_wfi_flag && ! (ra_idx(n_idx-1).is_colon ())) |
|
1550 (*current_liboctave_warning_handler) |
|
1551 ("fewer indices than dimensions for N-d array"); |
|
1552 |
|
1553 for (int i = n_idx; i < n_lhs_dims; i++) |
|
1554 lhs_dims(n_idx-1) *= lhs_dims(i); |
|
1555 |
|
1556 lhs_dims.resize (n_idx); |
|
1557 |
|
1558 // Reshape *this. |
|
1559 dimensions = lhs_dims; |
|
1560 } |
|
1561 |
|
1562 int non_col = 0; |
|
1563 |
|
1564 // Find the non-colon column. |
|
1565 |
|
1566 for (int i = 0; i < n_idx; i++) |
|
1567 { |
|
1568 if (! idx_is_colon(i)) |
|
1569 non_col = i; |
|
1570 } |
|
1571 |
|
1572 // The length of the non-colon dimension. |
|
1573 |
|
1574 int non_col_dim = lhs_dims (non_col); |
|
1575 |
|
1576 int num_to_delete = ra_idx(non_col).length (lhs_dims (non_col)); |
|
1577 |
|
1578 if (num_to_delete > 0) |
|
1579 { |
|
1580 int temp = lhs_dims.num_ones (); |
|
1581 |
|
1582 if (non_col_dim == 1) |
|
1583 temp--; |
|
1584 |
|
1585 if (temp == n_idx - 1 && num_to_delete == non_col_dim) |
|
1586 { |
|
1587 // We have A with (1x1x4), where A(1,:,1:4) |
|
1588 // Delete all (0x0x0) |
|
1589 |
|
1590 dim_vector zero_dims (n_idx, 0); |
|
1591 |
|
1592 resize (zero_dims, rfv); |
|
1593 } |
|
1594 else |
|
1595 { |
|
1596 // New length of non-colon dimension |
|
1597 // (calculated in the next for loop) |
|
1598 |
|
1599 int new_dim = non_col_dim; |
|
1600 |
|
1601 int iidx = 0; |
|
1602 |
|
1603 for (int j = 0; j < non_col_dim; j++) |
|
1604 if (j == ra_idx(non_col).elem (iidx)) |
|
1605 { |
|
1606 iidx++; |
|
1607 |
|
1608 new_dim--; |
|
1609 |
|
1610 if (iidx == num_to_delete) |
|
1611 break; |
|
1612 } |
|
1613 |
|
1614 // Creating the new nd array after deletions. |
|
1615 |
|
1616 if (new_dim > 0) |
|
1617 { |
|
1618 // Calculate number of elements in new array. |
|
1619 |
|
1620 int num_new_elem=1; |
|
1621 |
|
1622 for (int i = 0; i < n_idx; i++) |
|
1623 { |
|
1624 if (i == non_col) |
|
1625 num_new_elem *= new_dim; |
|
1626 |
|
1627 else |
|
1628 num_new_elem *= lhs_dims(i); |
|
1629 } |
|
1630 |
|
1631 T *new_data = new T [num_new_elem]; |
|
1632 |
|
1633 Array<int> result_idx (n_lhs_dims, 0); |
|
1634 |
|
1635 dim_vector new_lhs_dim = lhs_dims; |
|
1636 |
|
1637 new_lhs_dim(non_col) = new_dim; |
|
1638 |
|
1639 int num_elem = 1; |
|
1640 |
|
1641 int numidx = 0; |
|
1642 |
|
1643 int n = length (); |
|
1644 |
|
1645 for (int i = 0; i < n_lhs_dims; i++) |
|
1646 if (i != non_col) |
|
1647 num_elem *= lhs_dims(i); |
|
1648 |
|
1649 num_elem *= ra_idx(non_col).capacity (); |
|
1650 |
|
1651 for (int i = 0; i < n; i++) |
|
1652 { |
|
1653 if (numidx < num_elem |
|
1654 && is_in (result_idx(non_col), ra_idx(non_col))) |
|
1655 numidx++; |
|
1656 |
|
1657 else |
|
1658 { |
|
1659 Array<int> temp_result_idx = result_idx; |
|
1660 |
|
1661 int num_lgt = how_many_lgt (result_idx(non_col), |
|
1662 ra_idx(non_col)); |
|
1663 |
|
1664 temp_result_idx(non_col) -= num_lgt; |
|
1665 |
|
1666 int kidx |
|
1667 = ::compute_index (temp_result_idx, new_lhs_dim); |
|
1668 |
|
1669 new_data[kidx] = elem (result_idx); |
|
1670 } |
|
1671 |
|
1672 increment_index (result_idx, lhs_dims); |
|
1673 } |
|
1674 |
|
1675 if (--rep->count <= 0) |
|
1676 delete rep; |
|
1677 |
|
1678 rep = new typename Array<T>::ArrayRep (new_data, |
|
1679 num_new_elem); |
|
1680 |
|
1681 dimensions = new_lhs_dim; |
|
1682 } |
|
1683 } |
|
1684 } |
4517
|
1685 } |
4755
|
1686 else if (n_idx == 1) |
4517
|
1687 { |
4755
|
1688 // This handle cases where we only have one index (not colon). |
|
1689 // The index denotes which elements we should delete in the array |
|
1690 // which can be of any dimension. We return a column vector, except |
|
1691 // for the case where we are operating on a row column. The elements |
|
1692 // are numerated columns by column. |
|
1693 // |
|
1694 // A(3,3,3)=2; |
|
1695 // A(3:5) = []; A(6)=[] |
|
1696 // |
|
1697 idx_vector idx_vec = ra_idx(0); |
|
1698 |
|
1699 int num_to_delete = idx_vec.capacity (); |
|
1700 |
|
1701 int lhs_numel = numel (); |
|
1702 |
|
1703 int new_numel = lhs_numel - num_to_delete; |
|
1704 |
|
1705 T *new_data = new T[new_numel]; |
|
1706 |
|
1707 Array<int> lhs_ra_idx (ndims (), 0); |
|
1708 |
|
1709 int ii = 0; |
|
1710 int iidx = 0; |
|
1711 |
|
1712 for (int i = 0; i < lhs_numel; i++) |
4517
|
1713 { |
4755
|
1714 if (iidx < num_to_delete && i == idx_vec.elem (iidx)) |
|
1715 { |
|
1716 iidx++; |
|
1717 } |
|
1718 else |
|
1719 { |
|
1720 new_data[ii++] = elem (lhs_ra_idx); |
|
1721 } |
|
1722 |
|
1723 increment_index (lhs_ra_idx, lhs_dims); |
|
1724 } |
|
1725 |
|
1726 if (--(Array<T>::rep)->count <= 0) |
|
1727 delete Array<T>::rep; |
|
1728 |
|
1729 Array<T>::rep = new typename Array<T>::ArrayRep (new_data, new_numel); |
|
1730 |
|
1731 dimensions.resize (2); |
|
1732 |
|
1733 if (lhs_dims.length () == 2 && lhs_dims(1) == 1) |
|
1734 { |
|
1735 dimensions(0) = new_numel; |
|
1736 dimensions(1) = 1; |
4517
|
1737 } |
|
1738 else |
|
1739 { |
4755
|
1740 dimensions(0) = 1; |
|
1741 dimensions(1) = new_numel; |
4517
|
1742 } |
|
1743 } |
4755
|
1744 else if (num_ones (idx_is_colon) < n_idx) |
|
1745 { |
|
1746 (*current_liboctave_error_handler) |
|
1747 ("a null assignment can have only one non-colon index"); |
|
1748 } |
4517
|
1749 } |
|
1750 } |
|
1751 |
|
1752 template <class T> |
|
1753 Array<T> |
|
1754 Array<T>::value (void) |
|
1755 { |
|
1756 Array<T> retval; |
|
1757 |
|
1758 int n_idx = index_count (); |
|
1759 |
|
1760 if (n_idx == 2) |
|
1761 { |
|
1762 idx_vector *tmp = get_idx (); |
|
1763 |
|
1764 idx_vector idx_i = tmp[0]; |
|
1765 idx_vector idx_j = tmp[1]; |
|
1766 |
|
1767 retval = index (idx_i, idx_j); |
|
1768 } |
|
1769 else if (n_idx == 1) |
|
1770 { |
|
1771 retval = index (idx[0]); |
|
1772 } |
|
1773 else |
|
1774 (*current_liboctave_error_handler) |
|
1775 ("Array<T>::value: invalid number of indices specified"); |
|
1776 |
|
1777 clear_index (); |
|
1778 |
|
1779 return retval; |
|
1780 } |
|
1781 |
|
1782 template <class T> |
|
1783 Array<T> |
|
1784 Array<T>::index (idx_vector& idx_arg, int resize_ok, const T& rfv) const |
|
1785 { |
|
1786 Array<T> retval; |
|
1787 |
|
1788 switch (ndims ()) |
|
1789 { |
|
1790 case 1: |
|
1791 retval = index1 (idx_arg, resize_ok, rfv); |
|
1792 break; |
|
1793 |
|
1794 case 2: |
|
1795 retval = index2 (idx_arg, resize_ok, rfv); |
|
1796 break; |
|
1797 |
|
1798 default: |
4530
|
1799 retval = indexN (idx_arg, resize_ok, rfv); |
4517
|
1800 break; |
|
1801 } |
|
1802 |
|
1803 return retval; |
|
1804 } |
|
1805 |
|
1806 template <class T> |
|
1807 Array<T> |
|
1808 Array<T>::index1 (idx_vector& idx_arg, int resize_ok, const T& rfv) const |
|
1809 { |
|
1810 Array<T> retval; |
|
1811 |
|
1812 int len = length (); |
|
1813 |
|
1814 int n = idx_arg.freeze (len, "vector", resize_ok); |
|
1815 |
|
1816 if (idx_arg) |
|
1817 { |
|
1818 if (idx_arg.is_colon_equiv (len)) |
|
1819 { |
|
1820 retval = *this; |
|
1821 } |
|
1822 else if (n == 0) |
|
1823 { |
|
1824 retval.resize_no_fill (0); |
|
1825 } |
|
1826 else if (len == 1 && n > 1 |
|
1827 && idx_arg.one_zero_only () |
|
1828 && idx_arg.ones_count () == n) |
|
1829 { |
4548
|
1830 retval.resize_and_fill (n, elem (0)); |
4517
|
1831 } |
|
1832 else |
|
1833 { |
|
1834 retval.resize_no_fill (n); |
|
1835 |
|
1836 for (int i = 0; i < n; i++) |
|
1837 { |
|
1838 int ii = idx_arg.elem (i); |
|
1839 if (ii >= len) |
|
1840 retval.elem (i) = rfv; |
|
1841 else |
|
1842 retval.elem (i) = elem (ii); |
|
1843 } |
|
1844 } |
|
1845 } |
|
1846 |
|
1847 // idx_vector::freeze() printed an error message for us. |
|
1848 |
|
1849 return retval; |
|
1850 } |
|
1851 |
|
1852 template <class T> |
|
1853 Array<T> |
|
1854 Array<T>::index2 (idx_vector& idx_arg, int resize_ok, const T& rfv) const |
|
1855 { |
|
1856 Array<T> retval; |
|
1857 |
4548
|
1858 assert (ndims () == 2); |
|
1859 |
4517
|
1860 int nr = dim1 (); |
|
1861 int nc = dim2 (); |
|
1862 |
|
1863 int orig_len = nr * nc; |
|
1864 |
|
1865 int idx_orig_rows = idx_arg.orig_rows (); |
|
1866 int idx_orig_columns = idx_arg.orig_columns (); |
|
1867 |
|
1868 if (idx_arg.is_colon ()) |
|
1869 { |
|
1870 // Fast magic colon processing. |
|
1871 |
|
1872 int result_nr = nr * nc; |
|
1873 int result_nc = 1; |
|
1874 |
|
1875 retval = Array<T> (*this, dim_vector (result_nr, result_nc)); |
|
1876 } |
|
1877 else if (nr == 1 && nc == 1) |
|
1878 { |
|
1879 Array<T> tmp = Array<T>::index1 (idx_arg, resize_ok); |
|
1880 |
|
1881 if (tmp.length () != 0) |
|
1882 retval = Array<T> (tmp, dim_vector (idx_orig_rows, idx_orig_columns)); |
|
1883 else |
|
1884 retval = Array<T> (tmp, dim_vector (0, 0)); |
|
1885 } |
|
1886 else if (nr == 1 || nc == 1) |
|
1887 { |
|
1888 // If indexing a vector with a matrix, return value has same |
|
1889 // shape as the index. Otherwise, it has same orientation as |
|
1890 // indexed object. |
|
1891 |
|
1892 Array<T> tmp = index1 (idx_arg, resize_ok); |
|
1893 |
|
1894 int len = tmp.length (); |
|
1895 |
|
1896 if (len == 0) |
|
1897 { |
|
1898 if (idx_orig_rows == 0 || idx_orig_columns == 0) |
|
1899 retval = Array<T> (dim_vector (idx_orig_rows, idx_orig_columns)); |
|
1900 else if (nr == 1) |
|
1901 retval = Array<T> (dim_vector (1, 0)); |
|
1902 else |
|
1903 retval = Array<T> (dim_vector (0, 1)); |
|
1904 } |
|
1905 else |
|
1906 { |
4592
|
1907 if (idx_arg.one_zero_only () |
|
1908 || idx_orig_rows == 1 || idx_orig_columns == 1) |
4517
|
1909 { |
|
1910 if (nr == 1) |
|
1911 retval = Array<T> (tmp, dim_vector (1, len)); |
|
1912 else |
|
1913 retval = Array<T> (tmp, dim_vector (len, 1)); |
|
1914 } |
|
1915 else |
|
1916 retval = Array<T> (tmp, dim_vector (idx_orig_rows, idx_orig_columns)); |
|
1917 } |
|
1918 } |
|
1919 else |
|
1920 { |
|
1921 if (liboctave_wfi_flag |
|
1922 && ! (idx_arg.one_zero_only () |
|
1923 && idx_orig_rows == nr |
|
1924 && idx_orig_columns == nc)) |
|
1925 (*current_liboctave_warning_handler) ("single index used for matrix"); |
|
1926 |
|
1927 // This code is only for indexing matrices. The vector |
|
1928 // cases are handled above. |
|
1929 |
|
1930 idx_arg.freeze (nr * nc, "matrix", resize_ok); |
|
1931 |
|
1932 if (idx_arg) |
|
1933 { |
|
1934 int result_nr = idx_orig_rows; |
|
1935 int result_nc = idx_orig_columns; |
|
1936 |
|
1937 if (idx_arg.one_zero_only ()) |
|
1938 { |
|
1939 result_nr = idx_arg.ones_count (); |
|
1940 result_nc = (result_nr > 0 ? 1 : 0); |
|
1941 } |
|
1942 |
|
1943 retval.resize_no_fill (result_nr, result_nc); |
|
1944 |
|
1945 int k = 0; |
|
1946 for (int j = 0; j < result_nc; j++) |
|
1947 { |
|
1948 for (int i = 0; i < result_nr; i++) |
|
1949 { |
|
1950 int ii = idx_arg.elem (k++); |
|
1951 if (ii >= orig_len) |
|
1952 retval.elem (i, j) = rfv; |
|
1953 else |
|
1954 { |
|
1955 int fr = ii % nr; |
|
1956 int fc = (ii - fr) / nr; |
|
1957 retval.elem (i, j) = elem (fr, fc); |
|
1958 } |
|
1959 } |
|
1960 } |
|
1961 } |
|
1962 // idx_vector::freeze() printed an error message for us. |
|
1963 } |
|
1964 |
|
1965 return retval; |
|
1966 } |
|
1967 |
|
1968 template <class T> |
|
1969 Array<T> |
4530
|
1970 Array<T>::indexN (idx_vector& ra_idx, int resize_ok, const T& rfv) const |
|
1971 { |
|
1972 Array<T> retval; |
|
1973 |
4747
|
1974 int n_dims = dims().length (); |
|
1975 |
|
1976 int orig_len = dims().numel (); |
4530
|
1977 |
4653
|
1978 dim_vector idx_orig_dims = ra_idx.orig_dimensions (); |
4530
|
1979 |
|
1980 if (ra_idx.is_colon ()) |
|
1981 { |
4651
|
1982 // Fast magic colon processing. |
|
1983 |
|
1984 retval = Array<T> (*this, dim_vector (orig_len, 1)); |
4530
|
1985 } |
|
1986 else if (length () == 1) |
|
1987 { |
|
1988 // Only one element in array. |
|
1989 |
|
1990 Array<T> tmp = Array<T>::index (ra_idx, resize_ok); |
|
1991 |
|
1992 if (tmp.length () != 0) |
|
1993 retval = Array<T> (tmp, idx_orig_dims); |
|
1994 else |
4755
|
1995 retval = Array<T> (tmp, dim_vector (0, 0)); |
4530
|
1996 } |
|
1997 else if (vector_equivalent (dims ())) |
|
1998 { |
|
1999 // We're getting elements from a vector equivalent i.e. (1x4x1). |
|
2000 |
|
2001 Array<T> tmp = Array<T>::index (ra_idx, resize_ok); |
|
2002 |
|
2003 int len = tmp.length (); |
|
2004 |
|
2005 if (len == 0) |
|
2006 { |
4747
|
2007 if (idx_orig_dims.any_zero ()) |
4530
|
2008 retval = Array<T> (idx_orig_dims); |
|
2009 else |
|
2010 { |
|
2011 dim_vector new_dims; |
4673
|
2012 |
4530
|
2013 new_dims.resize (n_dims); |
|
2014 |
|
2015 for (int i = 0; i < n_dims; i++) |
|
2016 { |
|
2017 if ((dims ())(i) == 1) |
|
2018 new_dims(i) = 1; |
|
2019 } |
|
2020 |
4673
|
2021 new_dims.chop_trailing_singletons (); |
|
2022 |
4530
|
2023 retval = Array<T> (new_dims); |
|
2024 } |
|
2025 } |
|
2026 else |
|
2027 { |
4746
|
2028 if (vector_equivalent (idx_orig_dims)) |
4530
|
2029 { |
|
2030 // Array<int> index (n_dims, len); |
|
2031 dim_vector new_dims; |
|
2032 |
|
2033 new_dims.resize (n_dims); |
|
2034 |
|
2035 for (int i = 0; i < n_dims; i++) |
|
2036 { |
|
2037 if ((dims ())(i) == 1) |
|
2038 new_dims(i) = 1; |
|
2039 } |
|
2040 |
4673
|
2041 new_dims.chop_trailing_singletons (); |
|
2042 |
4530
|
2043 retval = Array<T> (tmp, new_dims); |
|
2044 } |
|
2045 else |
|
2046 retval = Array<T> (tmp, idx_orig_dims); |
|
2047 |
|
2048 (*current_liboctave_error_handler) |
|
2049 ("I do not know what to do here yet!"); |
|
2050 } |
|
2051 } |
4651
|
2052 else |
4530
|
2053 { |
4651
|
2054 if (liboctave_wfi_flag |
|
2055 && ! (ra_idx.is_colon () |
4747
|
2056 || (ra_idx.one_zero_only () && idx_orig_dims == dims ()))) |
4651
|
2057 (*current_liboctave_warning_handler) |
|
2058 ("single index used for N-d array"); |
4530
|
2059 |
|
2060 ra_idx.freeze (orig_len, "nd-array", resize_ok); |
|
2061 |
|
2062 if (ra_idx) |
|
2063 { |
|
2064 dim_vector result_dims (idx_orig_dims); |
|
2065 |
|
2066 if (ra_idx.one_zero_only ()) |
|
2067 { |
4651
|
2068 result_dims.resize (2); |
|
2069 int ntot = ra_idx.ones_count (); |
|
2070 result_dims(0) = ntot; |
|
2071 result_dims(1) = (ntot > 0 ? 1 : 0); |
4530
|
2072 } |
|
2073 |
4673
|
2074 result_dims.chop_trailing_singletons (); |
|
2075 |
4530
|
2076 retval.resize (result_dims); |
|
2077 |
4747
|
2078 int n = result_dims.numel (); |
4530
|
2079 |
|
2080 int r_dims = result_dims.length (); |
|
2081 |
4587
|
2082 Array<int> iidx (r_dims, 0); |
4530
|
2083 |
|
2084 int k = 0; |
|
2085 |
|
2086 for (int i = 0; i < n; i++) |
|
2087 { |
|
2088 int ii = ra_idx.elem (k++); |
|
2089 |
|
2090 if (ii >= orig_len) |
4587
|
2091 retval.elem (iidx) = rfv; |
4530
|
2092 else |
|
2093 { |
|
2094 Array<int> temp = get_ra_idx (ii, dims ()); |
|
2095 |
4587
|
2096 retval.elem (iidx) = elem (temp); |
4530
|
2097 } |
|
2098 if (i != n - 1) |
4587
|
2099 increment_index (iidx, result_dims); |
4530
|
2100 } |
|
2101 } |
|
2102 } |
|
2103 |
|
2104 return retval; |
|
2105 } |
|
2106 |
|
2107 template <class T> |
|
2108 Array<T> |
4517
|
2109 Array<T>::index (idx_vector& idx_i, idx_vector& idx_j, int resize_ok, |
|
2110 const T& rfv) const |
|
2111 { |
|
2112 Array<T> retval; |
|
2113 |
4548
|
2114 assert (ndims () == 2); |
|
2115 |
4517
|
2116 int nr = dim1 (); |
|
2117 int nc = dim2 (); |
|
2118 |
|
2119 int n = idx_i.freeze (nr, "row", resize_ok); |
|
2120 int m = idx_j.freeze (nc, "column", resize_ok); |
|
2121 |
|
2122 if (idx_i && idx_j) |
|
2123 { |
|
2124 if (idx_i.orig_empty () || idx_j.orig_empty () || n == 0 || m == 0) |
|
2125 { |
|
2126 retval.resize_no_fill (n, m); |
|
2127 } |
|
2128 else if (idx_i.is_colon_equiv (nr) && idx_j.is_colon_equiv (nc)) |
|
2129 { |
|
2130 retval = *this; |
|
2131 } |
|
2132 else |
|
2133 { |
|
2134 retval.resize_no_fill (n, m); |
|
2135 |
|
2136 for (int j = 0; j < m; j++) |
|
2137 { |
|
2138 int jj = idx_j.elem (j); |
|
2139 for (int i = 0; i < n; i++) |
|
2140 { |
|
2141 int ii = idx_i.elem (i); |
|
2142 if (ii >= nr || jj >= nc) |
|
2143 retval.elem (i, j) = rfv; |
|
2144 else |
|
2145 retval.elem (i, j) = elem (ii, jj); |
|
2146 } |
|
2147 } |
|
2148 } |
|
2149 } |
|
2150 |
|
2151 // idx_vector::freeze() printed an error message for us. |
|
2152 |
|
2153 return retval; |
|
2154 } |
|
2155 |
|
2156 template <class T> |
|
2157 Array<T> |
4661
|
2158 Array<T>::index (Array<idx_vector>& ra_idx, int resize_ok, const T&) const |
4517
|
2159 { |
4530
|
2160 // This function handles all calls with more than one idx. |
|
2161 // For (3x3x3), the call can be A(2,5), A(2,:,:), A(3,2,3) etc. |
|
2162 |
4517
|
2163 Array<T> retval; |
|
2164 |
|
2165 int n_dims = dimensions.length (); |
|
2166 |
4737
|
2167 // Remove trailing singletons in ra_idx, but leave at least ndims |
|
2168 // elements. |
|
2169 |
|
2170 int ra_idx_len = ra_idx.length (); |
|
2171 |
|
2172 while (ra_idx_len > n_dims) |
|
2173 { |
|
2174 if (ra_idx(ra_idx_len-1) == 1) |
|
2175 ra_idx_len--; |
|
2176 else |
|
2177 break; |
|
2178 } |
|
2179 |
|
2180 ra_idx.resize (ra_idx_len); |
|
2181 |
4530
|
2182 if (n_dims < ra_idx.length ()) |
4517
|
2183 { |
4530
|
2184 (*current_liboctave_error_handler) |
|
2185 ("index exceeds N-d array dimensions"); |
|
2186 |
|
2187 return retval; |
|
2188 } |
|
2189 |
|
2190 dim_vector frozen_lengths = short_freeze (ra_idx, dimensions, resize_ok); |
|
2191 |
|
2192 if (frozen_lengths.length () <= n_dims) |
|
2193 { |
|
2194 if (all_ok (ra_idx)) |
4517
|
2195 { |
4747
|
2196 if (any_orig_empty (ra_idx) || frozen_lengths.any_zero ()) |
4530
|
2197 { |
4673
|
2198 frozen_lengths.chop_trailing_singletons (); |
|
2199 |
4530
|
2200 retval.resize (frozen_lengths); |
|
2201 } |
4738
|
2202 else if (frozen_lengths.length () == n_dims |
|
2203 && all_colon_equiv (ra_idx, dimensions)) |
4530
|
2204 { |
|
2205 retval = *this; |
|
2206 } |
|
2207 else |
|
2208 { |
4673
|
2209 dim_vector frozen_lengths_for_resize = frozen_lengths; |
|
2210 |
|
2211 frozen_lengths_for_resize.chop_trailing_singletons (); |
|
2212 |
|
2213 retval.resize (frozen_lengths_for_resize); |
|
2214 |
|
2215 int n = retval.length (); |
4530
|
2216 |
|
2217 Array<int> result_idx (ra_idx.length (), 0); |
|
2218 |
|
2219 dim_vector this_dims = dims (); |
4588
|
2220 |
|
2221 Array<int> elt_idx; |
|
2222 |
4530
|
2223 for (int i = 0; i < n; i++) |
4517
|
2224 { |
4588
|
2225 elt_idx = get_elt_idx (ra_idx, result_idx); |
4702
|
2226 |
4530
|
2227 int numelem_elt = get_scalar_idx (elt_idx, this_dims); |
|
2228 |
4673
|
2229 if (numelem_elt > length () || numelem_elt < 0) |
4530
|
2230 (*current_liboctave_error_handler) |
4673
|
2231 ("invalid N-d array index"); |
4530
|
2232 else |
4673
|
2233 retval.elem (i) = elem (numelem_elt); |
4702
|
2234 |
4530
|
2235 increment_index (result_idx, frozen_lengths); |
4702
|
2236 |
4517
|
2237 } |
|
2238 } |
|
2239 } |
|
2240 } |
|
2241 else |
|
2242 (*current_liboctave_error_handler) |
|
2243 ("invalid number of dimensions for N-dimensional array index"); |
|
2244 |
|
2245 return retval; |
|
2246 } |
|
2247 |
|
2248 // XXX FIXME XXX -- this is a mess. |
|
2249 |
|
2250 template <class LT, class RT> |
|
2251 int |
|
2252 assign (Array<LT>& lhs, const Array<RT>& rhs, const LT& rfv) |
|
2253 { |
|
2254 int retval = 0; |
|
2255 |
|
2256 switch (lhs.ndims ()) |
|
2257 { |
|
2258 case 0: |
|
2259 { |
|
2260 if (lhs.index_count () < 3) |
|
2261 { |
|
2262 // kluge... |
|
2263 lhs.resize_no_fill (0, 0); |
|
2264 retval = assign2 (lhs, rhs, rfv); |
|
2265 } |
|
2266 else |
|
2267 retval = assignN (lhs, rhs, rfv); |
|
2268 } |
|
2269 break; |
|
2270 |
|
2271 case 1: |
|
2272 { |
|
2273 if (lhs.index_count () > 1) |
|
2274 retval = assignN (lhs, rhs, rfv); |
|
2275 else |
|
2276 retval = assign1 (lhs, rhs, rfv); |
|
2277 } |
|
2278 break; |
|
2279 |
|
2280 case 2: |
|
2281 { |
|
2282 if (lhs.index_count () > 2) |
|
2283 retval = assignN (lhs, rhs, rfv); |
|
2284 else |
|
2285 retval = assign2 (lhs, rhs, rfv); |
|
2286 } |
|
2287 break; |
|
2288 |
|
2289 default: |
|
2290 retval = assignN (lhs, rhs, rfv); |
|
2291 break; |
|
2292 } |
|
2293 |
|
2294 return retval; |
|
2295 } |
|
2296 |
|
2297 template <class LT, class RT> |
|
2298 int |
|
2299 assign1 (Array<LT>& lhs, const Array<RT>& rhs, const LT& rfv) |
|
2300 { |
|
2301 int retval = 1; |
|
2302 |
|
2303 idx_vector *tmp = lhs.get_idx (); |
|
2304 |
|
2305 idx_vector lhs_idx = tmp[0]; |
|
2306 |
|
2307 int lhs_len = lhs.length (); |
|
2308 int rhs_len = rhs.length (); |
|
2309 |
|
2310 int n = lhs_idx.freeze (lhs_len, "vector", true, liboctave_wrore_flag); |
|
2311 |
|
2312 if (n != 0) |
|
2313 { |
|
2314 if (rhs_len == n || rhs_len == 1) |
|
2315 { |
|
2316 int max_idx = lhs_idx.max () + 1; |
|
2317 if (max_idx > lhs_len) |
4548
|
2318 lhs.resize_and_fill (max_idx, rfv); |
4517
|
2319 } |
|
2320 |
|
2321 if (rhs_len == n) |
|
2322 { |
|
2323 for (int i = 0; i < n; i++) |
|
2324 { |
|
2325 int ii = lhs_idx.elem (i); |
|
2326 lhs.elem (ii) = rhs.elem (i); |
|
2327 } |
|
2328 } |
|
2329 else if (rhs_len == 1) |
|
2330 { |
|
2331 RT scalar = rhs.elem (0); |
|
2332 |
|
2333 for (int i = 0; i < n; i++) |
|
2334 { |
|
2335 int ii = lhs_idx.elem (i); |
|
2336 lhs.elem (ii) = scalar; |
|
2337 } |
|
2338 } |
|
2339 else |
|
2340 { |
|
2341 (*current_liboctave_error_handler) |
|
2342 ("A(I) = X: X must be a scalar or a vector with same length as I"); |
|
2343 |
|
2344 retval = 0; |
|
2345 } |
|
2346 } |
|
2347 else if (lhs_idx.is_colon ()) |
|
2348 { |
|
2349 if (lhs_len == 0) |
|
2350 { |
|
2351 lhs.resize_no_fill (rhs_len); |
|
2352 |
|
2353 for (int i = 0; i < rhs_len; i++) |
|
2354 lhs.elem (i) = rhs.elem (i); |
|
2355 } |
|
2356 else |
|
2357 (*current_liboctave_error_handler) |
|
2358 ("A(:) = X: A must be the same size as X"); |
|
2359 } |
|
2360 else if (! (rhs_len == 1 || rhs_len == 0)) |
|
2361 { |
|
2362 (*current_liboctave_error_handler) |
|
2363 ("A([]) = X: X must also be an empty matrix or a scalar"); |
|
2364 |
|
2365 retval = 0; |
|
2366 } |
|
2367 |
|
2368 lhs.clear_index (); |
|
2369 |
|
2370 return retval; |
|
2371 } |
|
2372 |
|
2373 #define MAYBE_RESIZE_LHS \ |
|
2374 do \ |
|
2375 { \ |
|
2376 int max_row_idx = idx_i_is_colon ? rhs_nr : idx_i.max () + 1; \ |
|
2377 int max_col_idx = idx_j_is_colon ? rhs_nc : idx_j.max () + 1; \ |
|
2378 \ |
|
2379 int new_nr = max_row_idx > lhs_nr ? max_row_idx : lhs_nr; \ |
|
2380 int new_nc = max_col_idx > lhs_nc ? max_col_idx : lhs_nc; \ |
|
2381 \ |
|
2382 lhs.resize_and_fill (new_nr, new_nc, rfv); \ |
|
2383 } \ |
|
2384 while (0) |
|
2385 |
|
2386 template <class LT, class RT> |
|
2387 int |
|
2388 assign2 (Array<LT>& lhs, const Array<RT>& rhs, const LT& rfv) |
|
2389 { |
|
2390 int retval = 1; |
|
2391 |
|
2392 int n_idx = lhs.index_count (); |
|
2393 |
|
2394 int lhs_nr = lhs.rows (); |
|
2395 int lhs_nc = lhs.cols (); |
|
2396 |
|
2397 int rhs_nr = rhs.rows (); |
|
2398 int rhs_nc = rhs.cols (); |
4707
|
2399 |
|
2400 if (rhs.length () > 2) |
|
2401 { |
|
2402 dim_vector dv_tmp = rhs.squeeze().dims (); |
4709
|
2403 |
4708
|
2404 switch (dv_tmp.length ()) |
4707
|
2405 { |
4708
|
2406 case 1: |
|
2407 if (rhs_nr == 1) |
|
2408 rhs_nc = dv_tmp.elem (0); |
|
2409 break; |
4709
|
2410 |
4708
|
2411 case 2: |
4707
|
2412 rhs_nr = dv_tmp.elem (0); |
|
2413 rhs_nc = dv_tmp.elem (1); |
4708
|
2414 break; |
|
2415 |
|
2416 default: |
|
2417 (*current_liboctave_error_handler) |
|
2418 ("Array<T>::assign2: Dimension mismatch"); |
4709
|
2419 return 0; |
4707
|
2420 } |
|
2421 } |
4517
|
2422 |
|
2423 idx_vector *tmp = lhs.get_idx (); |
|
2424 |
|
2425 idx_vector idx_i; |
|
2426 idx_vector idx_j; |
|
2427 |
|
2428 if (n_idx > 1) |
|
2429 idx_j = tmp[1]; |
|
2430 |
|
2431 if (n_idx > 0) |
|
2432 idx_i = tmp[0]; |
|
2433 |
|
2434 if (n_idx == 2) |
|
2435 { |
|
2436 int n = idx_i.freeze (lhs_nr, "row", true, liboctave_wrore_flag); |
|
2437 |
|
2438 int m = idx_j.freeze (lhs_nc, "column", true, liboctave_wrore_flag); |
|
2439 |
|
2440 int idx_i_is_colon = idx_i.is_colon (); |
|
2441 int idx_j_is_colon = idx_j.is_colon (); |
|
2442 |
|
2443 if (idx_i_is_colon) |
|
2444 n = lhs_nr > 0 ? lhs_nr : rhs_nr; |
|
2445 |
|
2446 if (idx_j_is_colon) |
|
2447 m = lhs_nc > 0 ? lhs_nc : rhs_nc; |
|
2448 |
|
2449 if (idx_i && idx_j) |
|
2450 { |
|
2451 if (rhs_nr == 0 && rhs_nc == 0) |
|
2452 { |
|
2453 lhs.maybe_delete_elements (idx_i, idx_j); |
|
2454 } |
|
2455 else |
|
2456 { |
4534
|
2457 if (rhs_nr == 1 && rhs_nc == 1 && n >= 0 && m >= 0) |
4517
|
2458 { |
4534
|
2459 // No need to do anything if either of the indices |
|
2460 // are empty. |
|
2461 |
|
2462 if (n > 0 && m > 0) |
4517
|
2463 { |
4534
|
2464 MAYBE_RESIZE_LHS; |
|
2465 |
|
2466 RT scalar = rhs.elem (0, 0); |
|
2467 |
|
2468 for (int j = 0; j < m; j++) |
4517
|
2469 { |
4534
|
2470 int jj = idx_j.elem (j); |
|
2471 for (int i = 0; i < n; i++) |
|
2472 { |
|
2473 int ii = idx_i.elem (i); |
|
2474 lhs.elem (ii, jj) = scalar; |
|
2475 } |
4517
|
2476 } |
|
2477 } |
|
2478 } |
|
2479 else if (n == rhs_nr && m == rhs_nc) |
|
2480 { |
|
2481 if (n > 0 && m > 0) |
|
2482 { |
|
2483 MAYBE_RESIZE_LHS; |
|
2484 |
|
2485 for (int j = 0; j < m; j++) |
|
2486 { |
|
2487 int jj = idx_j.elem (j); |
|
2488 for (int i = 0; i < n; i++) |
|
2489 { |
|
2490 int ii = idx_i.elem (i); |
|
2491 lhs.elem (ii, jj) = rhs.elem (i, j); |
|
2492 } |
|
2493 } |
|
2494 } |
|
2495 } |
|
2496 else if (n == 0 && m == 0) |
|
2497 { |
|
2498 if (! ((rhs_nr == 1 && rhs_nc == 1) |
|
2499 || (rhs_nr == 0 && rhs_nc == 0))) |
|
2500 { |
|
2501 (*current_liboctave_error_handler) |
|
2502 ("A([], []) = X: X must be an empty matrix or a scalar"); |
|
2503 |
|
2504 retval = 0; |
|
2505 } |
|
2506 } |
|
2507 else |
|
2508 { |
|
2509 (*current_liboctave_error_handler) |
|
2510 ("A(I, J) = X: X must be a scalar or the number of elements in I must"); |
|
2511 (*current_liboctave_error_handler) |
|
2512 ("match the number of rows in X and the number of elements in J must"); |
|
2513 (*current_liboctave_error_handler) |
|
2514 ("match the number of columns in X"); |
|
2515 |
|
2516 retval = 0; |
|
2517 } |
|
2518 } |
|
2519 } |
|
2520 // idx_vector::freeze() printed an error message for us. |
|
2521 } |
|
2522 else if (n_idx == 1) |
|
2523 { |
|
2524 int lhs_is_empty = lhs_nr == 0 || lhs_nc == 0; |
|
2525 |
|
2526 if (lhs_is_empty || (lhs_nr == 1 && lhs_nc == 1)) |
|
2527 { |
|
2528 int lhs_len = lhs.length (); |
|
2529 |
|
2530 int n = idx_i.freeze (lhs_len, 0, true, liboctave_wrore_flag); |
|
2531 |
|
2532 if (idx_i) |
|
2533 { |
|
2534 if (rhs_nr == 0 && rhs_nc == 0) |
|
2535 { |
|
2536 if (n != 0 && (lhs_nr != 0 || lhs_nc != 0)) |
|
2537 lhs.maybe_delete_elements (idx_i); |
|
2538 } |
|
2539 else |
|
2540 { |
|
2541 if (liboctave_wfi_flag) |
|
2542 { |
|
2543 if (lhs_is_empty |
|
2544 && idx_i.is_colon () |
|
2545 && ! (rhs_nr == 1 || rhs_nc == 1)) |
|
2546 { |
|
2547 (*current_liboctave_warning_handler) |
|
2548 ("A(:) = X: X is not a vector or scalar"); |
|
2549 } |
|
2550 else |
|
2551 { |
|
2552 int idx_nr = idx_i.orig_rows (); |
|
2553 int idx_nc = idx_i.orig_columns (); |
|
2554 |
|
2555 if (! (rhs_nr == idx_nr && rhs_nc == idx_nc)) |
|
2556 (*current_liboctave_warning_handler) |
|
2557 ("A(I) = X: X does not have same shape as I"); |
|
2558 } |
|
2559 } |
|
2560 |
|
2561 if (assign1 ((Array<LT>&) lhs, (Array<RT>&) rhs, rfv)) |
|
2562 { |
|
2563 int len = lhs.length (); |
|
2564 |
|
2565 if (len > 0) |
|
2566 { |
|
2567 // The following behavior is much simplified |
|
2568 // over previous versions of Octave. It |
|
2569 // seems to be compatible with Matlab. |
|
2570 |
|
2571 lhs.dimensions = dim_vector (1, lhs.length ()); |
|
2572 } |
|
2573 else |
|
2574 lhs.dimensions = dim_vector (0, 0); |
|
2575 } |
|
2576 else |
|
2577 retval = 0; |
|
2578 } |
|
2579 } |
|
2580 // idx_vector::freeze() printed an error message for us. |
|
2581 } |
|
2582 else if (lhs_nr == 1) |
|
2583 { |
|
2584 idx_i.freeze (lhs_nc, "vector", true, liboctave_wrore_flag); |
|
2585 |
|
2586 if (idx_i) |
|
2587 { |
|
2588 if (rhs_nr == 0 && rhs_nc == 0) |
|
2589 lhs.maybe_delete_elements (idx_i); |
|
2590 else |
|
2591 { |
|
2592 if (assign1 ((Array<LT>&) lhs, (Array<RT>&) rhs, rfv)) |
|
2593 lhs.dimensions = dim_vector (1, lhs.length ()); |
|
2594 else |
|
2595 retval = 0; |
|
2596 } |
|
2597 } |
|
2598 // idx_vector::freeze() printed an error message for us. |
|
2599 } |
|
2600 else if (lhs_nc == 1) |
|
2601 { |
|
2602 idx_i.freeze (lhs_nr, "vector", true, liboctave_wrore_flag); |
|
2603 |
|
2604 if (idx_i) |
|
2605 { |
|
2606 if (rhs_nr == 0 && rhs_nc == 0) |
|
2607 lhs.maybe_delete_elements (idx_i); |
|
2608 else |
|
2609 { |
|
2610 if (assign1 ((Array<LT>&) lhs, (Array<RT>&) rhs, rfv)) |
|
2611 lhs.dimensions = dim_vector (lhs.length (), 1); |
|
2612 else |
|
2613 retval = 0; |
|
2614 } |
|
2615 } |
|
2616 // idx_vector::freeze() printed an error message for us. |
|
2617 } |
|
2618 else |
|
2619 { |
|
2620 if (liboctave_wfi_flag |
|
2621 && ! (idx_i.is_colon () |
|
2622 || (idx_i.one_zero_only () |
|
2623 && idx_i.orig_rows () == lhs_nr |
|
2624 && idx_i.orig_columns () == lhs_nc))) |
|
2625 (*current_liboctave_warning_handler) |
|
2626 ("single index used for matrix"); |
|
2627 |
|
2628 int len = idx_i.freeze (lhs_nr * lhs_nc, "matrix"); |
|
2629 |
|
2630 if (idx_i) |
|
2631 { |
|
2632 if (len == 0) |
|
2633 { |
|
2634 if (! ((rhs_nr == 1 && rhs_nc == 1) |
|
2635 || (rhs_nr == 0 && rhs_nc == 0))) |
|
2636 (*current_liboctave_error_handler) |
|
2637 ("A([]) = X: X must be an empty matrix or scalar"); |
|
2638 } |
|
2639 else if (len == rhs_nr * rhs_nc) |
|
2640 { |
|
2641 int k = 0; |
|
2642 for (int j = 0; j < rhs_nc; j++) |
|
2643 { |
|
2644 for (int i = 0; i < rhs_nr; i++) |
|
2645 { |
|
2646 int ii = idx_i.elem (k++); |
|
2647 int fr = ii % lhs_nr; |
|
2648 int fc = (ii - fr) / lhs_nr; |
|
2649 lhs.elem (fr, fc) = rhs.elem (i, j); |
|
2650 } |
|
2651 } |
|
2652 } |
4716
|
2653 else if (rhs_nr == 1 && rhs_nc == 1) |
4517
|
2654 { |
|
2655 RT scalar = rhs.elem (0, 0); |
|
2656 |
|
2657 for (int i = 0; i < len; i++) |
|
2658 { |
|
2659 int ii = idx_i.elem (i); |
4716
|
2660 lhs.elem (ii) = scalar; |
4517
|
2661 } |
|
2662 } |
|
2663 else |
|
2664 { |
|
2665 (*current_liboctave_error_handler) |
|
2666 ("A(I) = X: X must be a scalar or a matrix with the same size as I"); |
|
2667 |
|
2668 retval = 0; |
|
2669 } |
|
2670 } |
|
2671 // idx_vector::freeze() printed an error message for us. |
|
2672 } |
|
2673 } |
|
2674 else |
|
2675 { |
|
2676 (*current_liboctave_error_handler) |
|
2677 ("invalid number of indices for matrix expression"); |
|
2678 |
|
2679 retval = 0; |
|
2680 } |
|
2681 |
|
2682 lhs.clear_index (); |
|
2683 |
|
2684 return retval; |
|
2685 } |
|
2686 |
|
2687 template <class LT, class RT> |
|
2688 int |
|
2689 assignN (Array<LT>& lhs, const Array<RT>& rhs, const LT& rfv) |
|
2690 { |
|
2691 int retval = 1; |
|
2692 |
4746
|
2693 dim_vector rhs_dims = rhs.dims (); |
|
2694 |
|
2695 int rhs_dims_len = rhs_dims.length (); |
|
2696 |
|
2697 bool rhs_is_scalar = is_scalar (rhs_dims); |
|
2698 |
4517
|
2699 int n_idx = lhs.index_count (); |
|
2700 |
4745
|
2701 idx_vector *idx_vex = lhs.get_idx (); |
|
2702 |
|
2703 Array<idx_vector> idx = conv_to_array (idx_vex, n_idx); |
4517
|
2704 |
4743
|
2705 if (rhs_dims_len == 2 && rhs_dims(0) == 0 && rhs_dims(1) == 0) |
4517
|
2706 { |
|
2707 lhs.maybe_delete_elements (idx, rfv); |
|
2708 } |
4657
|
2709 else if (n_idx == 1) |
4517
|
2710 { |
4657
|
2711 idx_vector iidx = idx(0); |
|
2712 |
|
2713 if (liboctave_wfi_flag |
|
2714 && ! (iidx.is_colon () |
|
2715 || (iidx.one_zero_only () |
|
2716 && iidx.orig_dimensions () == lhs.dims ()))) |
|
2717 (*current_liboctave_warning_handler) |
4746
|
2718 ("single index used for N-d array"); |
4657
|
2719 |
|
2720 int lhs_len = lhs.length (); |
|
2721 |
4746
|
2722 int len = iidx.freeze (lhs_len, "N-d arrray"); |
4657
|
2723 |
|
2724 if (iidx) |
4533
|
2725 { |
4657
|
2726 if (len == 0) |
4656
|
2727 { |
4657
|
2728 if (! (rhs_dims.all_ones () || rhs_dims.all_zero ())) |
4743
|
2729 { |
|
2730 (*current_liboctave_error_handler) |
|
2731 ("A([]) = X: X must be an empty matrix or scalar"); |
|
2732 |
|
2733 retval = 0; |
|
2734 } |
4657
|
2735 } |
|
2736 else if (len == rhs.length ()) |
|
2737 { |
|
2738 for (int i = 0; i < len; i++) |
4656
|
2739 { |
4657
|
2740 int ii = iidx.elem (i); |
|
2741 |
|
2742 lhs.elem (ii) = rhs.elem (i); |
4656
|
2743 } |
|
2744 } |
4716
|
2745 else if (rhs_is_scalar) |
4657
|
2746 { |
|
2747 RT scalar = rhs.elem (0); |
|
2748 |
|
2749 for (int i = 0; i < len; i++) |
|
2750 { |
|
2751 int ii = iidx.elem (i); |
|
2752 |
|
2753 lhs.elem (ii) = scalar; |
|
2754 } |
|
2755 } |
|
2756 else |
|
2757 { |
|
2758 (*current_liboctave_error_handler) |
4702
|
2759 ("A(I) = X: X must be a scalar or a matrix with the same size as I"); |
|
2760 |
4657
|
2761 retval = 0; |
|
2762 } |
|
2763 |
4656
|
2764 // idx_vector::freeze() printed an error message for us. |
4533
|
2765 } |
4702
|
2766 } |
4743
|
2767 else |
4702
|
2768 { |
4746
|
2769 // Maybe expand to more dimensions. |
|
2770 |
|
2771 dim_vector lhs_dims = lhs.dims (); |
|
2772 |
|
2773 int lhs_dims_len = lhs_dims.length (); |
|
2774 |
|
2775 dim_vector final_lhs_dims = lhs_dims; |
|
2776 |
|
2777 dim_vector frozen_len; |
|
2778 |
4747
|
2779 int orig_lhs_dims_len = lhs_dims_len; |
|
2780 |
|
2781 bool orig_empty = lhs_dims.all_zero (); |
|
2782 |
|
2783 if (n_idx < lhs_dims_len) |
4517
|
2784 { |
4747
|
2785 // Collapse dimensions beyond last index. |
|
2786 |
|
2787 if (liboctave_wfi_flag && ! (idx(n_idx-1).is_colon ())) |
|
2788 (*current_liboctave_warning_handler) |
|
2789 ("fewer indices than dimensions for N-d array"); |
|
2790 |
|
2791 for (int i = n_idx; i < lhs_dims_len; i++) |
|
2792 lhs_dims(n_idx-1) *= lhs_dims(i); |
|
2793 |
|
2794 lhs_dims.resize (n_idx); |
|
2795 |
|
2796 lhs.resize (lhs_dims); |
|
2797 |
|
2798 lhs_dims = lhs.dims (); |
|
2799 |
|
2800 lhs_dims_len = lhs_dims.length (); |
|
2801 } |
|
2802 |
|
2803 // Resize. |
|
2804 |
|
2805 dim_vector new_dims; |
|
2806 new_dims.resize (n_idx); |
|
2807 |
|
2808 for (int i = 0; i < n_idx; i++) |
|
2809 { |
|
2810 if (orig_empty) |
4746
|
2811 { |
4747
|
2812 // If index is a colon, resizing to RHS dimensions is |
|
2813 // allowed because we started out empty. |
4746
|
2814 |
|
2815 new_dims(i) |
4747
|
2816 = (i < rhs_dims.length () && idx(i).is_colon ()) |
|
2817 ? rhs_dims(i) : idx(i).max () + 1; |
4746
|
2818 } |
4747
|
2819 else |
4746
|
2820 { |
4747
|
2821 // We didn't start out with all zero dimensions, so if |
|
2822 // index is a colon, it refers to the current LHS |
|
2823 // dimension. Otherwise, it is OK to enlarge to a |
4749
|
2824 // dimension given by the largest index (but that index |
|
2825 // needs to be a number, not a colon). |
|
2826 |
|
2827 if (i < lhs_dims_len |
|
2828 && (idx(i).is_colon () || idx(i).max () < lhs_dims(i))) |
|
2829 new_dims(i) = lhs_dims(i); |
|
2830 else if (! idx(i).is_colon ()) |
|
2831 new_dims(i) = idx(i).max () + 1; |
|
2832 else |
|
2833 { |
|
2834 // XXX FIXME XXX -- can we provide a more |
|
2835 // informative message here? |
|
2836 |
|
2837 (*current_liboctave_error_handler) |
|
2838 ("invalid array index for assignment"); |
|
2839 |
|
2840 retval = 0; |
|
2841 |
|
2842 break; |
|
2843 } |
4745
|
2844 } |
4747
|
2845 } |
|
2846 |
4749
|
2847 if (retval != 0) |
4747
|
2848 { |
4749
|
2849 if (! orig_empty |
|
2850 && n_idx < orig_lhs_dims_len |
|
2851 && new_dims(n_idx-1) != lhs_dims(n_idx-1)) |
4702
|
2852 { |
4749
|
2853 // We reshaped and the last dimension changed. This has to |
|
2854 // be an error, because we don't know how to undo that |
|
2855 // later... |
|
2856 |
|
2857 (*current_liboctave_error_handler) |
|
2858 ("array index %d (= %d) for assignment requires invalid resizing operation", |
|
2859 n_idx, new_dims(n_idx-1)); |
|
2860 |
|
2861 retval = 0; |
4743
|
2862 } |
|
2863 else |
|
2864 { |
4749
|
2865 if (n_idx < orig_lhs_dims_len) |
4743
|
2866 { |
4749
|
2867 for (int i = 0; i < n_idx-1; i++) |
|
2868 final_lhs_dims(i) = new_dims(i); |
4747
|
2869 } |
|
2870 else |
4749
|
2871 final_lhs_dims = new_dims; |
|
2872 |
|
2873 lhs.resize_and_fill (new_dims, rfv); |
|
2874 lhs_dims = lhs.dims (); |
|
2875 lhs_dims_len = lhs_dims.length (); |
|
2876 |
|
2877 frozen_len = freeze (idx, lhs_dims, true); |
|
2878 |
|
2879 if (rhs_is_scalar) |
4747
|
2880 { |
|
2881 if (! final_lhs_dims.any_zero ()) |
|
2882 { |
|
2883 int n = Array<LT>::get_size (frozen_len); |
|
2884 |
|
2885 Array<int> result_idx (lhs_dims_len, 0); |
|
2886 |
4749
|
2887 RT scalar = rhs.elem (0); |
|
2888 |
4747
|
2889 for (int i = 0; i < n; i++) |
|
2890 { |
|
2891 Array<int> elt_idx = get_elt_idx (idx, result_idx); |
|
2892 |
4749
|
2893 lhs.elem (elt_idx) = scalar; |
4747
|
2894 |
|
2895 increment_index (result_idx, frozen_len); |
|
2896 } |
|
2897 } |
4743
|
2898 } |
4749
|
2899 else |
|
2900 { |
|
2901 // RHS is matrix or higher dimension. |
|
2902 |
|
2903 // Check that non-singleton RHS dimensions conform to |
|
2904 // non-singleton LHS index dimensions. |
|
2905 |
|
2906 dim_vector t_rhs_dims = rhs_dims.squeeze (); |
|
2907 dim_vector t_frozen_len = frozen_len.squeeze (); |
|
2908 |
|
2909 // If after sqeezing out singleton dimensions, RHS is |
|
2910 // vector and LHS is vector, force them to have the same |
|
2911 // orientation so that operations like |
|
2912 // |
|
2913 // a = zeros (3, 3, 3); |
|
2914 // a(1:3,1,1) = [1,2,3]; |
|
2915 // |
|
2916 // will work. |
|
2917 |
|
2918 if (t_rhs_dims.length () == 2 && t_frozen_len.length () == 2 |
|
2919 && ((t_rhs_dims.elem(1) == 1 |
|
2920 && t_frozen_len.elem(0) == 1) |
|
2921 || (t_rhs_dims.elem(0) == 1 |
|
2922 && t_frozen_len.elem(1) == 1))) |
|
2923 { |
|
2924 int t0 = t_rhs_dims.elem(0); |
|
2925 t_rhs_dims.elem(0) = t_rhs_dims.elem(1); |
|
2926 t_rhs_dims.elem(1) = t0; |
|
2927 } |
|
2928 |
|
2929 if (t_rhs_dims != t_frozen_len) |
|
2930 { |
|
2931 (*current_liboctave_error_handler) |
|
2932 ("A(IDX-LIST) = X: X must be a scalar or size of X must equal number of elements indexed by IDX-LIST"); |
|
2933 |
|
2934 retval = 0; |
|
2935 } |
|
2936 else |
|
2937 { |
|
2938 if (! final_lhs_dims.any_zero ()) |
|
2939 { |
|
2940 int n = Array<LT>::get_size (frozen_len); |
|
2941 |
|
2942 Array<int> result_idx (lhs_dims_len, 0); |
|
2943 |
|
2944 for (int i = 0; i < n; i++) |
|
2945 { |
|
2946 Array<int> elt_idx = get_elt_idx (idx, result_idx); |
|
2947 |
|
2948 lhs.elem (elt_idx) = rhs.elem (i); |
|
2949 |
|
2950 increment_index (result_idx, frozen_len); |
|
2951 } |
|
2952 } |
|
2953 } |
|
2954 } |
4743
|
2955 } |
4517
|
2956 } |
4745
|
2957 |
4746
|
2958 lhs.resize (final_lhs_dims); |
4517
|
2959 } |
|
2960 |
4703
|
2961 lhs.chop_trailing_singletons (); |
|
2962 |
4517
|
2963 lhs.clear_index (); |
|
2964 |
|
2965 return retval; |
|
2966 } |
|
2967 |
|
2968 template <class T> |
|
2969 void |
3933
|
2970 Array<T>::print_info (std::ostream& os, const std::string& prefix) const |
|
2971 { |
|
2972 os << prefix << "rep address: " << rep << "\n" |
|
2973 << prefix << "rep->len: " << rep->len << "\n" |
|
2974 << prefix << "rep->data: " << static_cast<void *> (rep->data) << "\n" |
|
2975 << prefix << "rep->count: " << rep->count << "\n"; |
4513
|
2976 |
|
2977 // 2D info: |
|
2978 // |
4657
|
2979 // << pefix << "rows: " << rows () << "\n" |
4513
|
2980 // << prefix << "cols: " << cols () << "\n"; |
3933
|
2981 } |
|
2982 |
237
|
2983 /* |
|
2984 ;;; Local Variables: *** |
|
2985 ;;; mode: C++ *** |
|
2986 ;;; End: *** |
|
2987 */ |