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