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