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