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