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