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