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