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