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