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