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