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 |
4653
|
1795 dim_vector idx_orig_dims = ra_idx.orig_dimensions (); |
4530
|
1796 |
|
1797 if (ra_idx.is_colon ()) |
|
1798 { |
4651
|
1799 // Fast magic colon processing. |
|
1800 |
|
1801 retval = Array<T> (*this, dim_vector (orig_len, 1)); |
4530
|
1802 } |
|
1803 else if (length () == 1) |
|
1804 { |
|
1805 // Only one element in array. |
|
1806 |
|
1807 Array<T> tmp = Array<T>::index (ra_idx, resize_ok); |
|
1808 |
|
1809 if (tmp.length () != 0) |
|
1810 retval = Array<T> (tmp, idx_orig_dims); |
|
1811 else |
4548
|
1812 retval = Array<T> (tmp, dim_vector (0)); |
4530
|
1813 } |
|
1814 else if (vector_equivalent (dims ())) |
|
1815 { |
|
1816 // We're getting elements from a vector equivalent i.e. (1x4x1). |
|
1817 |
|
1818 Array<T> tmp = Array<T>::index (ra_idx, resize_ok); |
|
1819 |
|
1820 int len = tmp.length (); |
|
1821 |
|
1822 if (len == 0) |
|
1823 { |
|
1824 if (any_zero_len (idx_orig_dims)) |
|
1825 retval = Array<T> (idx_orig_dims); |
|
1826 else |
|
1827 { |
|
1828 dim_vector new_dims; |
|
1829 new_dims.resize (n_dims); |
|
1830 |
|
1831 for (int i = 0; i < n_dims; i++) |
|
1832 { |
|
1833 if ((dims ())(i) == 1) |
|
1834 new_dims(i) = 1; |
|
1835 } |
|
1836 |
|
1837 retval = Array<T> (new_dims); |
|
1838 } |
|
1839 } |
|
1840 else |
|
1841 { |
|
1842 if (vector_equivalent(idx_orig_dims)) |
|
1843 { |
|
1844 // Array<int> index (n_dims, len); |
|
1845 dim_vector new_dims; |
|
1846 |
|
1847 new_dims.resize (n_dims); |
|
1848 |
|
1849 for (int i = 0; i < n_dims; i++) |
|
1850 { |
|
1851 if ((dims ())(i) == 1) |
|
1852 new_dims(i) = 1; |
|
1853 } |
|
1854 |
|
1855 retval = Array<T> (tmp, new_dims); |
|
1856 } |
|
1857 else |
|
1858 retval = Array<T> (tmp, idx_orig_dims); |
|
1859 |
|
1860 (*current_liboctave_error_handler) |
|
1861 ("I do not know what to do here yet!"); |
|
1862 } |
|
1863 } |
4651
|
1864 else |
4530
|
1865 { |
4651
|
1866 if (liboctave_wfi_flag |
|
1867 && ! (ra_idx.is_colon () |
|
1868 || (ra_idx.one_zero_only () |
|
1869 && equal_arrays (idx_orig_dims, dims ())))) |
|
1870 (*current_liboctave_warning_handler) |
|
1871 ("single index used for N-d array"); |
4530
|
1872 |
|
1873 ra_idx.freeze (orig_len, "nd-array", resize_ok); |
|
1874 |
|
1875 if (ra_idx) |
|
1876 { |
|
1877 dim_vector result_dims (idx_orig_dims); |
|
1878 |
|
1879 if (ra_idx.one_zero_only ()) |
|
1880 { |
4651
|
1881 result_dims.resize (2); |
|
1882 int ntot = ra_idx.ones_count (); |
|
1883 result_dims(0) = ntot; |
|
1884 result_dims(1) = (ntot > 0 ? 1 : 0); |
4530
|
1885 } |
|
1886 |
|
1887 retval.resize (result_dims); |
|
1888 |
|
1889 int n = number_of_elements (result_dims); |
|
1890 |
|
1891 int r_dims = result_dims.length (); |
|
1892 |
4587
|
1893 Array<int> iidx (r_dims, 0); |
4530
|
1894 |
|
1895 int k = 0; |
|
1896 |
|
1897 for (int i = 0; i < n; i++) |
|
1898 { |
|
1899 int ii = ra_idx.elem (k++); |
|
1900 |
|
1901 if (ii >= orig_len) |
4587
|
1902 retval.elem (iidx) = rfv; |
4530
|
1903 else |
|
1904 { |
|
1905 Array<int> temp = get_ra_idx (ii, dims ()); |
|
1906 |
4587
|
1907 retval.elem (iidx) = elem (temp); |
4530
|
1908 } |
|
1909 if (i != n - 1) |
4587
|
1910 increment_index (iidx, result_dims); |
4530
|
1911 } |
|
1912 } |
|
1913 } |
|
1914 |
|
1915 return retval; |
|
1916 } |
|
1917 |
|
1918 template <class T> |
|
1919 Array<T> |
4517
|
1920 Array<T>::index (idx_vector& idx_i, idx_vector& idx_j, int resize_ok, |
|
1921 const T& rfv) const |
|
1922 { |
|
1923 Array<T> retval; |
|
1924 |
4548
|
1925 assert (ndims () == 2); |
|
1926 |
4517
|
1927 int nr = dim1 (); |
|
1928 int nc = dim2 (); |
|
1929 |
|
1930 int n = idx_i.freeze (nr, "row", resize_ok); |
|
1931 int m = idx_j.freeze (nc, "column", resize_ok); |
|
1932 |
|
1933 if (idx_i && idx_j) |
|
1934 { |
|
1935 if (idx_i.orig_empty () || idx_j.orig_empty () || n == 0 || m == 0) |
|
1936 { |
|
1937 retval.resize_no_fill (n, m); |
|
1938 } |
|
1939 else if (idx_i.is_colon_equiv (nr) && idx_j.is_colon_equiv (nc)) |
|
1940 { |
|
1941 retval = *this; |
|
1942 } |
|
1943 else |
|
1944 { |
|
1945 retval.resize_no_fill (n, m); |
|
1946 |
|
1947 for (int j = 0; j < m; j++) |
|
1948 { |
|
1949 int jj = idx_j.elem (j); |
|
1950 for (int i = 0; i < n; i++) |
|
1951 { |
|
1952 int ii = idx_i.elem (i); |
|
1953 if (ii >= nr || jj >= nc) |
|
1954 retval.elem (i, j) = rfv; |
|
1955 else |
|
1956 retval.elem (i, j) = elem (ii, jj); |
|
1957 } |
|
1958 } |
|
1959 } |
|
1960 } |
|
1961 |
|
1962 // idx_vector::freeze() printed an error message for us. |
|
1963 |
|
1964 return retval; |
|
1965 } |
|
1966 |
|
1967 template <class T> |
|
1968 Array<T> |
|
1969 Array<T>::index (Array<idx_vector>& ra_idx, int resize_ok, const T& rfv) const |
|
1970 { |
4530
|
1971 // This function handles all calls with more than one idx. |
|
1972 // For (3x3x3), the call can be A(2,5), A(2,:,:), A(3,2,3) etc. |
|
1973 |
4517
|
1974 Array<T> retval; |
|
1975 |
|
1976 int n_dims = dimensions.length (); |
|
1977 |
4530
|
1978 if (n_dims < ra_idx.length ()) |
4517
|
1979 { |
4530
|
1980 (*current_liboctave_error_handler) |
|
1981 ("index exceeds N-d array dimensions"); |
|
1982 |
|
1983 return retval; |
|
1984 } |
|
1985 |
|
1986 dim_vector frozen_lengths = short_freeze (ra_idx, dimensions, resize_ok); |
|
1987 |
|
1988 if (frozen_lengths.length () <= n_dims) |
|
1989 { |
|
1990 if (all_ok (ra_idx)) |
4517
|
1991 { |
4530
|
1992 if (any_orig_empty (ra_idx)) |
|
1993 { |
|
1994 retval.resize (frozen_lengths); |
|
1995 } |
|
1996 else if (any_zero_len (frozen_lengths)) |
4517
|
1997 { |
4548
|
1998 retval.resize (get_zero_len_size (frozen_lengths, dimensions)); |
4530
|
1999 } |
|
2000 else if (all_colon_equiv (ra_idx, dimensions) |
|
2001 && frozen_lengths.length () == n_dims) |
|
2002 { |
|
2003 retval = *this; |
|
2004 } |
|
2005 else |
|
2006 { |
|
2007 retval.resize (frozen_lengths); |
|
2008 |
|
2009 int n = number_of_elements (frozen_lengths); |
|
2010 |
|
2011 Array<int> result_idx (ra_idx.length (), 0); |
|
2012 |
|
2013 dim_vector this_dims = dims (); |
4588
|
2014 |
|
2015 Array<int> elt_idx; |
|
2016 |
4530
|
2017 for (int i = 0; i < n; i++) |
4517
|
2018 { |
4588
|
2019 elt_idx = get_elt_idx (ra_idx, result_idx); |
4530
|
2020 |
|
2021 int numelem_result = |
|
2022 get_scalar_idx (result_idx, frozen_lengths); |
|
2023 |
|
2024 int numelem_elt = get_scalar_idx (elt_idx, this_dims); |
|
2025 |
|
2026 if (numelem_result > length () || numelem_result < 0 |
|
2027 || numelem_elt > length () || numelem_elt < 0) |
|
2028 (*current_liboctave_error_handler) |
|
2029 ("attempt to grow array along ambiguous dimension"); |
|
2030 else |
4533
|
2031 retval.checkelem (numelem_result) = checkelem (numelem_elt); |
4530
|
2032 |
|
2033 increment_index (result_idx, frozen_lengths); |
|
2034 |
4517
|
2035 } |
|
2036 } |
|
2037 } |
|
2038 } |
|
2039 else |
|
2040 (*current_liboctave_error_handler) |
|
2041 ("invalid number of dimensions for N-dimensional array index"); |
|
2042 |
|
2043 return retval; |
|
2044 } |
|
2045 |
|
2046 // XXX FIXME XXX -- this is a mess. |
|
2047 |
|
2048 template <class LT, class RT> |
|
2049 int |
|
2050 assign (Array<LT>& lhs, const Array<RT>& rhs, const LT& rfv) |
|
2051 { |
|
2052 int retval = 0; |
|
2053 |
|
2054 switch (lhs.ndims ()) |
|
2055 { |
|
2056 case 0: |
|
2057 { |
|
2058 if (lhs.index_count () < 3) |
|
2059 { |
|
2060 // kluge... |
|
2061 lhs.resize_no_fill (0, 0); |
|
2062 retval = assign2 (lhs, rhs, rfv); |
|
2063 } |
|
2064 else |
|
2065 retval = assignN (lhs, rhs, rfv); |
|
2066 } |
|
2067 break; |
|
2068 |
|
2069 case 1: |
|
2070 { |
|
2071 if (lhs.index_count () > 1) |
|
2072 retval = assignN (lhs, rhs, rfv); |
|
2073 else |
|
2074 retval = assign1 (lhs, rhs, rfv); |
|
2075 } |
|
2076 break; |
|
2077 |
|
2078 case 2: |
|
2079 { |
|
2080 if (lhs.index_count () > 2) |
|
2081 retval = assignN (lhs, rhs, rfv); |
|
2082 else |
|
2083 retval = assign2 (lhs, rhs, rfv); |
|
2084 } |
|
2085 break; |
|
2086 |
|
2087 default: |
|
2088 retval = assignN (lhs, rhs, rfv); |
|
2089 break; |
|
2090 } |
|
2091 |
|
2092 return retval; |
|
2093 } |
|
2094 |
|
2095 template <class LT, class RT> |
|
2096 int |
|
2097 assign1 (Array<LT>& lhs, const Array<RT>& rhs, const LT& rfv) |
|
2098 { |
|
2099 int retval = 1; |
|
2100 |
|
2101 idx_vector *tmp = lhs.get_idx (); |
|
2102 |
|
2103 idx_vector lhs_idx = tmp[0]; |
|
2104 |
|
2105 int lhs_len = lhs.length (); |
|
2106 int rhs_len = rhs.length (); |
|
2107 |
|
2108 int n = lhs_idx.freeze (lhs_len, "vector", true, liboctave_wrore_flag); |
|
2109 |
|
2110 if (n != 0) |
|
2111 { |
|
2112 if (rhs_len == n || rhs_len == 1) |
|
2113 { |
|
2114 int max_idx = lhs_idx.max () + 1; |
|
2115 if (max_idx > lhs_len) |
4548
|
2116 lhs.resize_and_fill (max_idx, rfv); |
4517
|
2117 } |
|
2118 |
|
2119 if (rhs_len == n) |
|
2120 { |
|
2121 for (int i = 0; i < n; i++) |
|
2122 { |
|
2123 int ii = lhs_idx.elem (i); |
|
2124 lhs.elem (ii) = rhs.elem (i); |
|
2125 } |
|
2126 } |
|
2127 else if (rhs_len == 1) |
|
2128 { |
|
2129 RT scalar = rhs.elem (0); |
|
2130 |
|
2131 for (int i = 0; i < n; i++) |
|
2132 { |
|
2133 int ii = lhs_idx.elem (i); |
|
2134 lhs.elem (ii) = scalar; |
|
2135 } |
|
2136 } |
|
2137 else |
|
2138 { |
|
2139 (*current_liboctave_error_handler) |
|
2140 ("A(I) = X: X must be a scalar or a vector with same length as I"); |
|
2141 |
|
2142 retval = 0; |
|
2143 } |
|
2144 } |
|
2145 else if (lhs_idx.is_colon ()) |
|
2146 { |
|
2147 if (lhs_len == 0) |
|
2148 { |
|
2149 lhs.resize_no_fill (rhs_len); |
|
2150 |
|
2151 for (int i = 0; i < rhs_len; i++) |
|
2152 lhs.elem (i) = rhs.elem (i); |
|
2153 } |
|
2154 else |
|
2155 (*current_liboctave_error_handler) |
|
2156 ("A(:) = X: A must be the same size as X"); |
|
2157 } |
|
2158 else if (! (rhs_len == 1 || rhs_len == 0)) |
|
2159 { |
|
2160 (*current_liboctave_error_handler) |
|
2161 ("A([]) = X: X must also be an empty matrix or a scalar"); |
|
2162 |
|
2163 retval = 0; |
|
2164 } |
|
2165 |
|
2166 lhs.clear_index (); |
|
2167 |
|
2168 return retval; |
|
2169 } |
|
2170 |
|
2171 #define MAYBE_RESIZE_LHS \ |
|
2172 do \ |
|
2173 { \ |
|
2174 int max_row_idx = idx_i_is_colon ? rhs_nr : idx_i.max () + 1; \ |
|
2175 int max_col_idx = idx_j_is_colon ? rhs_nc : idx_j.max () + 1; \ |
|
2176 \ |
|
2177 int new_nr = max_row_idx > lhs_nr ? max_row_idx : lhs_nr; \ |
|
2178 int new_nc = max_col_idx > lhs_nc ? max_col_idx : lhs_nc; \ |
|
2179 \ |
|
2180 lhs.resize_and_fill (new_nr, new_nc, rfv); \ |
|
2181 } \ |
|
2182 while (0) |
|
2183 |
|
2184 template <class LT, class RT> |
|
2185 int |
|
2186 assign2 (Array<LT>& lhs, const Array<RT>& rhs, const LT& rfv) |
|
2187 { |
|
2188 int retval = 1; |
|
2189 |
|
2190 int n_idx = lhs.index_count (); |
|
2191 |
|
2192 int lhs_nr = lhs.rows (); |
|
2193 int lhs_nc = lhs.cols (); |
|
2194 |
|
2195 int rhs_nr = rhs.rows (); |
|
2196 int rhs_nc = rhs.cols (); |
|
2197 |
|
2198 idx_vector *tmp = lhs.get_idx (); |
|
2199 |
|
2200 idx_vector idx_i; |
|
2201 idx_vector idx_j; |
|
2202 |
|
2203 if (n_idx > 1) |
|
2204 idx_j = tmp[1]; |
|
2205 |
|
2206 if (n_idx > 0) |
|
2207 idx_i = tmp[0]; |
|
2208 |
|
2209 if (n_idx == 2) |
|
2210 { |
|
2211 int n = idx_i.freeze (lhs_nr, "row", true, liboctave_wrore_flag); |
|
2212 |
|
2213 int m = idx_j.freeze (lhs_nc, "column", true, liboctave_wrore_flag); |
|
2214 |
|
2215 int idx_i_is_colon = idx_i.is_colon (); |
|
2216 int idx_j_is_colon = idx_j.is_colon (); |
|
2217 |
|
2218 if (idx_i_is_colon) |
|
2219 n = lhs_nr > 0 ? lhs_nr : rhs_nr; |
|
2220 |
|
2221 if (idx_j_is_colon) |
|
2222 m = lhs_nc > 0 ? lhs_nc : rhs_nc; |
|
2223 |
|
2224 if (idx_i && idx_j) |
|
2225 { |
|
2226 if (rhs_nr == 0 && rhs_nc == 0) |
|
2227 { |
|
2228 lhs.maybe_delete_elements (idx_i, idx_j); |
|
2229 } |
|
2230 else |
|
2231 { |
4534
|
2232 if (rhs_nr == 1 && rhs_nc == 1 && n >= 0 && m >= 0) |
4517
|
2233 { |
4534
|
2234 // No need to do anything if either of the indices |
|
2235 // are empty. |
|
2236 |
|
2237 if (n > 0 && m > 0) |
4517
|
2238 { |
4534
|
2239 MAYBE_RESIZE_LHS; |
|
2240 |
|
2241 RT scalar = rhs.elem (0, 0); |
|
2242 |
|
2243 for (int j = 0; j < m; j++) |
4517
|
2244 { |
4534
|
2245 int jj = idx_j.elem (j); |
|
2246 for (int i = 0; i < n; i++) |
|
2247 { |
|
2248 int ii = idx_i.elem (i); |
|
2249 lhs.elem (ii, jj) = scalar; |
|
2250 } |
4517
|
2251 } |
|
2252 } |
|
2253 } |
|
2254 else if (n == rhs_nr && m == rhs_nc) |
|
2255 { |
|
2256 if (n > 0 && m > 0) |
|
2257 { |
|
2258 MAYBE_RESIZE_LHS; |
|
2259 |
|
2260 for (int j = 0; j < m; j++) |
|
2261 { |
|
2262 int jj = idx_j.elem (j); |
|
2263 for (int i = 0; i < n; i++) |
|
2264 { |
|
2265 int ii = idx_i.elem (i); |
|
2266 lhs.elem (ii, jj) = rhs.elem (i, j); |
|
2267 } |
|
2268 } |
|
2269 } |
|
2270 } |
|
2271 else if (n == 0 && m == 0) |
|
2272 { |
|
2273 if (! ((rhs_nr == 1 && rhs_nc == 1) |
|
2274 || (rhs_nr == 0 && rhs_nc == 0))) |
|
2275 { |
|
2276 (*current_liboctave_error_handler) |
|
2277 ("A([], []) = X: X must be an empty matrix or a scalar"); |
|
2278 |
|
2279 retval = 0; |
|
2280 } |
|
2281 } |
|
2282 else |
|
2283 { |
|
2284 (*current_liboctave_error_handler) |
|
2285 ("A(I, J) = X: X must be a scalar or the number of elements in I must"); |
|
2286 (*current_liboctave_error_handler) |
|
2287 ("match the number of rows in X and the number of elements in J must"); |
|
2288 (*current_liboctave_error_handler) |
|
2289 ("match the number of columns in X"); |
|
2290 |
|
2291 retval = 0; |
|
2292 } |
|
2293 } |
|
2294 } |
|
2295 // idx_vector::freeze() printed an error message for us. |
|
2296 } |
|
2297 else if (n_idx == 1) |
|
2298 { |
|
2299 int lhs_is_empty = lhs_nr == 0 || lhs_nc == 0; |
|
2300 |
|
2301 if (lhs_is_empty || (lhs_nr == 1 && lhs_nc == 1)) |
|
2302 { |
|
2303 int lhs_len = lhs.length (); |
|
2304 |
|
2305 int n = idx_i.freeze (lhs_len, 0, true, liboctave_wrore_flag); |
|
2306 |
|
2307 if (idx_i) |
|
2308 { |
|
2309 if (rhs_nr == 0 && rhs_nc == 0) |
|
2310 { |
|
2311 if (n != 0 && (lhs_nr != 0 || lhs_nc != 0)) |
|
2312 lhs.maybe_delete_elements (idx_i); |
|
2313 } |
|
2314 else |
|
2315 { |
|
2316 if (liboctave_wfi_flag) |
|
2317 { |
|
2318 if (lhs_is_empty |
|
2319 && idx_i.is_colon () |
|
2320 && ! (rhs_nr == 1 || rhs_nc == 1)) |
|
2321 { |
|
2322 (*current_liboctave_warning_handler) |
|
2323 ("A(:) = X: X is not a vector or scalar"); |
|
2324 } |
|
2325 else |
|
2326 { |
|
2327 int idx_nr = idx_i.orig_rows (); |
|
2328 int idx_nc = idx_i.orig_columns (); |
|
2329 |
|
2330 if (! (rhs_nr == idx_nr && rhs_nc == idx_nc)) |
|
2331 (*current_liboctave_warning_handler) |
|
2332 ("A(I) = X: X does not have same shape as I"); |
|
2333 } |
|
2334 } |
|
2335 |
|
2336 if (assign1 ((Array<LT>&) lhs, (Array<RT>&) rhs, rfv)) |
|
2337 { |
|
2338 int len = lhs.length (); |
|
2339 |
|
2340 if (len > 0) |
|
2341 { |
|
2342 // The following behavior is much simplified |
|
2343 // over previous versions of Octave. It |
|
2344 // seems to be compatible with Matlab. |
|
2345 |
|
2346 lhs.dimensions = dim_vector (1, lhs.length ()); |
|
2347 } |
|
2348 else |
|
2349 lhs.dimensions = dim_vector (0, 0); |
|
2350 } |
|
2351 else |
|
2352 retval = 0; |
|
2353 } |
|
2354 } |
|
2355 // idx_vector::freeze() printed an error message for us. |
|
2356 } |
|
2357 else if (lhs_nr == 1) |
|
2358 { |
|
2359 idx_i.freeze (lhs_nc, "vector", true, liboctave_wrore_flag); |
|
2360 |
|
2361 if (idx_i) |
|
2362 { |
|
2363 if (rhs_nr == 0 && rhs_nc == 0) |
|
2364 lhs.maybe_delete_elements (idx_i); |
|
2365 else |
|
2366 { |
|
2367 if (assign1 ((Array<LT>&) lhs, (Array<RT>&) rhs, rfv)) |
|
2368 lhs.dimensions = dim_vector (1, lhs.length ()); |
|
2369 else |
|
2370 retval = 0; |
|
2371 } |
|
2372 } |
|
2373 // idx_vector::freeze() printed an error message for us. |
|
2374 } |
|
2375 else if (lhs_nc == 1) |
|
2376 { |
|
2377 idx_i.freeze (lhs_nr, "vector", true, liboctave_wrore_flag); |
|
2378 |
|
2379 if (idx_i) |
|
2380 { |
|
2381 if (rhs_nr == 0 && rhs_nc == 0) |
|
2382 lhs.maybe_delete_elements (idx_i); |
|
2383 else |
|
2384 { |
|
2385 if (assign1 ((Array<LT>&) lhs, (Array<RT>&) rhs, rfv)) |
|
2386 lhs.dimensions = dim_vector (lhs.length (), 1); |
|
2387 else |
|
2388 retval = 0; |
|
2389 } |
|
2390 } |
|
2391 // idx_vector::freeze() printed an error message for us. |
|
2392 } |
|
2393 else |
|
2394 { |
|
2395 if (liboctave_wfi_flag |
|
2396 && ! (idx_i.is_colon () |
|
2397 || (idx_i.one_zero_only () |
|
2398 && idx_i.orig_rows () == lhs_nr |
|
2399 && idx_i.orig_columns () == lhs_nc))) |
|
2400 (*current_liboctave_warning_handler) |
|
2401 ("single index used for matrix"); |
|
2402 |
|
2403 int len = idx_i.freeze (lhs_nr * lhs_nc, "matrix"); |
|
2404 |
|
2405 if (idx_i) |
|
2406 { |
|
2407 if (len == 0) |
|
2408 { |
|
2409 if (! ((rhs_nr == 1 && rhs_nc == 1) |
|
2410 || (rhs_nr == 0 && rhs_nc == 0))) |
|
2411 (*current_liboctave_error_handler) |
|
2412 ("A([]) = X: X must be an empty matrix or scalar"); |
|
2413 } |
|
2414 else if (len == rhs_nr * rhs_nc) |
|
2415 { |
|
2416 int k = 0; |
|
2417 for (int j = 0; j < rhs_nc; j++) |
|
2418 { |
|
2419 for (int i = 0; i < rhs_nr; i++) |
|
2420 { |
|
2421 int ii = idx_i.elem (k++); |
|
2422 int fr = ii % lhs_nr; |
|
2423 int fc = (ii - fr) / lhs_nr; |
|
2424 lhs.elem (fr, fc) = rhs.elem (i, j); |
|
2425 } |
|
2426 } |
|
2427 } |
|
2428 else if (rhs_nr == 1 && rhs_nc == 1 && len <= lhs_nr * lhs_nc) |
|
2429 { |
|
2430 RT scalar = rhs.elem (0, 0); |
|
2431 |
|
2432 for (int i = 0; i < len; i++) |
|
2433 { |
|
2434 int ii = idx_i.elem (i); |
|
2435 int fr = ii % lhs_nr; |
|
2436 int fc = (ii - fr) / lhs_nr; |
|
2437 lhs.elem (fr, fc) = scalar; |
|
2438 } |
|
2439 } |
|
2440 else |
|
2441 { |
|
2442 (*current_liboctave_error_handler) |
|
2443 ("A(I) = X: X must be a scalar or a matrix with the same size as I"); |
|
2444 |
|
2445 retval = 0; |
|
2446 } |
|
2447 } |
|
2448 // idx_vector::freeze() printed an error message for us. |
|
2449 } |
|
2450 } |
|
2451 else |
|
2452 { |
|
2453 (*current_liboctave_error_handler) |
|
2454 ("invalid number of indices for matrix expression"); |
|
2455 |
|
2456 retval = 0; |
|
2457 } |
|
2458 |
|
2459 lhs.clear_index (); |
|
2460 |
|
2461 return retval; |
|
2462 } |
|
2463 |
|
2464 #define MAYBE_RESIZE_ND_DIMS \ |
|
2465 do \ |
|
2466 { \ |
|
2467 if (n_idx >= lhs_dims.length () && ! rhs_is_empty) \ |
|
2468 { \ |
|
2469 Array<int> max_idx (n_idx); \ |
4548
|
2470 dim_vector new_dims; \ |
|
2471 new_dims.resize (n_idx); \ |
4517
|
2472 \ |
|
2473 for (int i = 0; i < n_idx; i++) \ |
|
2474 { \ |
|
2475 if (lhs_dims.length () == 0 || i >= lhs_dims.length ()) \ |
4548
|
2476 new_dims(i) = idx(i).max () + 1; \ |
4517
|
2477 else \ |
|
2478 { \ |
|
2479 if (i < rhs_dims.length ()) \ |
|
2480 max_idx(i) = idx(i).is_colon () ? rhs_dims(i) : idx(i).max () + 1; \ |
|
2481 else \ |
|
2482 max_idx(i) = idx(i).max () + 1; \ |
|
2483 \ |
4548
|
2484 new_dims(i) = max_idx(i) > lhs_dims(i) ? max_idx(i) : lhs_dims(i); \ |
4517
|
2485 } \ |
|
2486 } \ |
|
2487 \ |
4548
|
2488 lhs.resize_and_fill (new_dims, rfv); \ |
4517
|
2489 lhs_dims = lhs.dims (); \ |
|
2490 } \ |
|
2491 } \ |
|
2492 while (0) |
|
2493 |
|
2494 template <class LT, class RT> |
|
2495 int |
|
2496 assignN (Array<LT>& lhs, const Array<RT>& rhs, const LT& rfv) |
|
2497 { |
|
2498 int retval = 1; |
|
2499 |
|
2500 int n_idx = lhs.index_count (); |
|
2501 |
|
2502 dim_vector lhs_dims = lhs.dims (); |
|
2503 dim_vector rhs_dims = rhs.dims (); |
|
2504 |
|
2505 idx_vector *tmp = lhs.get_idx (); |
|
2506 |
|
2507 Array<idx_vector> idx = conv_to_array (tmp, n_idx); |
|
2508 |
|
2509 // This needs to be defined before MAYBE_RESIZE_ND_DIMS. |
|
2510 |
|
2511 bool rhs_is_empty = rhs_dims.length () == 0 ? true : any_zero_len (rhs_dims); |
|
2512 |
|
2513 // Maybe expand to more dimensions. |
|
2514 |
|
2515 MAYBE_RESIZE_ND_DIMS; |
|
2516 |
|
2517 Array<int> idx_is_colon (n_idx, 0); |
|
2518 Array<int> idx_is_colon_equiv (n_idx, 0); |
|
2519 |
|
2520 for (int i = 0; i < n_idx; i++) |
|
2521 { |
|
2522 idx_is_colon_equiv(i) = idx(i).is_colon_equiv (lhs_dims(i), 1); |
|
2523 |
|
2524 idx_is_colon(i) = idx(i).is_colon (); |
|
2525 } |
|
2526 |
|
2527 int resize_ok = 1; |
|
2528 |
|
2529 dim_vector frozen_len; |
|
2530 |
|
2531 if (n_idx == lhs_dims.length ()) |
|
2532 frozen_len = freeze (idx, lhs_dims, resize_ok); |
|
2533 |
|
2534 bool rhs_is_scalar = is_scalar (rhs_dims); |
|
2535 |
|
2536 bool idx_is_empty = any_zero_len (frozen_len); |
|
2537 |
|
2538 if (rhs_is_empty) |
|
2539 { |
|
2540 lhs.maybe_delete_elements (idx, rfv); |
|
2541 } |
|
2542 else if (rhs_is_scalar) |
|
2543 { |
|
2544 if (n_idx == 0) |
|
2545 (*current_liboctave_error_handler) |
4530
|
2546 ("number of indices is zero"); |
4533
|
2547 else if (n_idx == 1) |
|
2548 { |
4656
|
2549 idx_vector iidx = idx(0); |
|
2550 |
|
2551 if (liboctave_wfi_flag |
|
2552 && ! (iidx.is_colon () |
|
2553 || (iidx.one_zero_only () |
|
2554 && iidx.orig_dimensions () == lhs.dims ()))) |
|
2555 (*current_liboctave_warning_handler) |
|
2556 ("single index used for n-d array"); |
|
2557 |
|
2558 int lhs_len = lhs.length (); |
|
2559 |
|
2560 int len = iidx.freeze (lhs_len, "n-d arrray"); |
|
2561 |
|
2562 if (iidx) |
|
2563 { |
|
2564 if (len == 0) |
|
2565 { |
|
2566 if (! (rhs_dims.all_ones () || rhs_dims.all_zero ())) |
|
2567 (*current_liboctave_error_handler) |
|
2568 ("A([]) = X: X must be an empty matrix or scalar"); |
|
2569 } |
|
2570 else if (len <= lhs_len) |
|
2571 { |
|
2572 RT scalar = rhs.elem (0); |
|
2573 |
|
2574 for (int i = 0; i < len; i++) |
|
2575 { |
|
2576 int ii = iidx.elem (i); |
|
2577 |
|
2578 lhs.elem (ii) = scalar; |
|
2579 } |
|
2580 } |
|
2581 else |
|
2582 { |
|
2583 (*current_liboctave_error_handler) |
|
2584 ("A(I) = X: X must be a scalar or a matrix with the same size as I"); |
|
2585 |
|
2586 retval = 0; |
|
2587 } |
|
2588 } |
|
2589 // idx_vector::freeze() printed an error message for us. |
4533
|
2590 } |
4517
|
2591 else if (n_idx < lhs_dims.length ()) |
|
2592 { |
|
2593 // Number of indices is less than dimensions. |
|
2594 |
|
2595 if (any_ones (idx_is_colon)|| any_ones (idx_is_colon_equiv)) |
|
2596 { |
|
2597 (*current_liboctave_error_handler) |
4530
|
2598 ("number of indices is less than number of dimensions, one or more indices are colons"); |
4517
|
2599 } |
|
2600 else |
|
2601 { |
|
2602 // Fewer indices than dimensions, no colons. |
|
2603 |
|
2604 bool resize = false; |
|
2605 |
|
2606 // Subtract one since the last idx do not tell us |
|
2607 // anything about dimensionality. |
|
2608 |
|
2609 for (int i = 0; i < idx.length () - 1; i++) |
|
2610 { |
|
2611 // Subtract one since idx counts from 0 while dims |
|
2612 // count from 1. |
|
2613 |
|
2614 if (idx(i).elem (0) + 1 > lhs_dims(i)) |
|
2615 resize = true; |
|
2616 } |
|
2617 |
|
2618 if (resize) |
|
2619 { |
|
2620 dim_vector new_dims; |
|
2621 new_dims.resize (lhs_dims.length ()); |
|
2622 |
|
2623 for (int i = 0; i < lhs_dims.length (); i++) |
|
2624 { |
|
2625 if (i < idx.length () - 1 |
|
2626 && idx(i).elem (0) + 1 > lhs_dims(i)) |
|
2627 new_dims(i) = idx(i).elem (0)+1; |
|
2628 else |
|
2629 new_dims(i) = lhs_dims(i); |
|
2630 } |
|
2631 |
|
2632 lhs.resize (new_dims, rfv); |
|
2633 |
|
2634 lhs_dims = lhs.dims (); |
|
2635 } |
|
2636 |
|
2637 Array<int> one_arg_temp (1, 0); |
|
2638 |
|
2639 RT scalar = rhs.elem (one_arg_temp); |
|
2640 |
|
2641 Array<int> int_arr = conv_to_int_array (idx); |
|
2642 |
|
2643 int numelem = get_scalar_idx (int_arr, lhs_dims); |
|
2644 |
|
2645 if (numelem > lhs.length () || numelem < 0) |
|
2646 (*current_liboctave_error_handler) |
4530
|
2647 ("attempt to grow array along ambiguous dimension"); |
4517
|
2648 else |
4533
|
2649 lhs.checkelem (numelem) = scalar; |
4517
|
2650 } |
|
2651 } |
|
2652 else |
|
2653 { |
|
2654 // Scalar to matrix assignment with as many indices as lhs |
|
2655 // dimensions. |
|
2656 |
|
2657 int n = Array<LT>::get_size (frozen_len); |
|
2658 |
|
2659 Array<int> result_idx (lhs_dims.length (), 0); |
|
2660 |
|
2661 Array<int> elt_idx; |
|
2662 |
|
2663 RT scalar = rhs.elem (0); |
|
2664 |
|
2665 for (int i = 0; i < n; i++) |
|
2666 { |
|
2667 elt_idx = get_elt_idx (idx, result_idx); |
|
2668 |
|
2669 dim_vector lhs_inc; |
|
2670 lhs_inc.resize (lhs_dims.length ()); |
|
2671 |
4587
|
2672 for (int j = 0; j < lhs_dims.length (); j++) |
|
2673 lhs_inc(j) = lhs_dims(j) + 1; |
4517
|
2674 |
|
2675 if (index_in_bounds(elt_idx, lhs_inc)) |
|
2676 lhs.checkelem (elt_idx) = scalar; |
|
2677 else |
|
2678 lhs.checkelem (elt_idx) = rfv; |
|
2679 |
|
2680 increment_index (result_idx, frozen_len); |
|
2681 } |
|
2682 } |
|
2683 } |
4533
|
2684 else if (rhs_dims.length () > 1) |
4517
|
2685 { |
|
2686 // RHS is matrix or higher dimension. |
|
2687 |
4635
|
2688 bool dim_ok = true; |
|
2689 |
|
2690 int jj = 0; |
|
2691 |
|
2692 // Check that RHS dimensions are the same length as the |
|
2693 // corresponding LHS dimensions. |
|
2694 |
|
2695 int rhs_dims_len = rhs_dims.length (); |
|
2696 |
|
2697 for (int j = 0; j < idx_is_colon.length (); j++) |
4517
|
2698 { |
4635
|
2699 if (idx_is_colon(j)) |
|
2700 { |
|
2701 if (jj > rhs_dims_len || rhs_dims(jj) < lhs_dims(j)) |
|
2702 { |
|
2703 dim_ok = false; |
|
2704 |
|
2705 break; |
|
2706 } |
|
2707 |
|
2708 jj++; |
|
2709 } |
4517
|
2710 } |
4635
|
2711 |
|
2712 if (! dim_ok) |
|
2713 (*current_liboctave_error_handler) |
|
2714 ("subscripted assignment dimension mismatch"); |
4517
|
2715 else |
|
2716 { |
4635
|
2717 dim_vector new_dims; |
|
2718 new_dims.resize (n_idx); |
|
2719 |
|
2720 bool resize = false; |
|
2721 |
|
2722 int ii = 0; |
|
2723 |
|
2724 // Update idx vectors. |
|
2725 |
|
2726 for (int i = 0; i < n_idx; i++) |
4517
|
2727 { |
4635
|
2728 if (idx(i).is_colon ()) |
4517
|
2729 { |
4635
|
2730 // Add appropriate idx_vector to idx(i) since |
|
2731 // index with : contains no indexes. |
|
2732 |
|
2733 frozen_len(i) |
|
2734 = lhs_dims(i) > rhs_dims(ii) ? lhs_dims(i) : rhs_dims(ii); |
|
2735 |
|
2736 new_dims(i) |
|
2737 = lhs_dims(i) > rhs_dims(ii) ? lhs_dims(i) : rhs_dims(ii); |
|
2738 |
|
2739 ii++; |
|
2740 |
|
2741 Range idxrange (1, frozen_len(i), 1); |
|
2742 |
|
2743 idx_vector idxv (idxrange); |
|
2744 |
|
2745 idx(i) = idxv; |
4517
|
2746 } |
4635
|
2747 else |
4517
|
2748 { |
4635
|
2749 new_dims(i) = lhs_dims(i) > idx(i).max () + 1 ? lhs_dims(i) : idx(i).max () + 1; |
|
2750 |
|
2751 if (frozen_len(i) > 1) |
|
2752 ii++; |
4517
|
2753 } |
4635
|
2754 if (new_dims(i) != lhs_dims(i)) |
|
2755 resize = true; |
|
2756 } |
|
2757 |
|
2758 // Resize LHS if dimensions have changed. |
|
2759 |
|
2760 if (resize) |
|
2761 { |
|
2762 lhs.resize (new_dims, rfv); |
|
2763 |
|
2764 lhs_dims = lhs.dims (); |
|
2765 } |
|
2766 |
|
2767 // Number of elements which need to be set. |
|
2768 |
|
2769 int n = Array<LT>::get_size (frozen_len); |
|
2770 |
|
2771 Array<int> result_idx (lhs_dims.length (), 0); |
|
2772 Array<int> elt_idx; |
|
2773 |
|
2774 Array<int> result_rhs_idx (rhs_dims.length (), 0); |
|
2775 |
|
2776 dim_vector frozen_rhs; |
|
2777 frozen_rhs.resize (rhs_dims.length ()); |
|
2778 |
|
2779 for (int i = 0; i < rhs_dims.length (); i++) |
|
2780 frozen_rhs(i) = rhs_dims(i); |
|
2781 |
|
2782 dim_vector lhs_inc; |
|
2783 lhs_inc.resize (lhs_dims.length ()); |
|
2784 |
|
2785 for (int i = 0; i < lhs_dims.length (); i++) |
|
2786 lhs_inc(i) = lhs_dims(i) + 1; |
|
2787 |
|
2788 for (int i = 0; i < n; i++) |
|
2789 { |
|
2790 elt_idx = get_elt_idx (idx, result_idx); |
|
2791 |
|
2792 if (index_in_bounds (elt_idx, lhs_inc)) |
4517
|
2793 { |
4656
|
2794 int s = compute_index (result_rhs_idx, rhs_dims); |
4635
|
2795 |
|
2796 lhs.checkelem (elt_idx) = rhs.elem (s); |
|
2797 |
|
2798 increment_index (result_rhs_idx, frozen_rhs); |
4517
|
2799 } |
4635
|
2800 else |
|
2801 lhs.checkelem (elt_idx) = rfv; |
|
2802 |
|
2803 increment_index (result_idx, frozen_len); |
4517
|
2804 } |
|
2805 } |
|
2806 } |
|
2807 else if (idx_is_empty) |
|
2808 { |
|
2809 // Assignment to matrix with at least one empty index. |
|
2810 |
|
2811 if (! rhs_is_empty || ! rhs_is_scalar) |
|
2812 { |
|
2813 (*current_liboctave_error_handler) |
|
2814 ("A([], []) = X: X must be an empty matrix or a scalar"); |
|
2815 |
|
2816 retval = 0; |
|
2817 } |
|
2818 } |
|
2819 else if (lhs_dims.length () != rhs_dims.length ()) |
|
2820 { |
|
2821 (*current_liboctave_error_handler) |
|
2822 ("A(I) = X: X must be a scalar or a matrix with the same size as I"); |
|
2823 retval = 0; |
|
2824 } |
|
2825 |
|
2826 lhs.clear_index (); |
|
2827 |
|
2828 return retval; |
|
2829 } |
|
2830 |
|
2831 template <class T> |
|
2832 void |
3933
|
2833 Array<T>::print_info (std::ostream& os, const std::string& prefix) const |
|
2834 { |
|
2835 os << prefix << "rep address: " << rep << "\n" |
|
2836 << prefix << "rep->len: " << rep->len << "\n" |
|
2837 << prefix << "rep->data: " << static_cast<void *> (rep->data) << "\n" |
|
2838 << prefix << "rep->count: " << rep->count << "\n"; |
4513
|
2839 |
|
2840 // 2D info: |
|
2841 // |
|
2842 // << prefix << "rows: " << rows () << "\n" |
|
2843 // << prefix << "cols: " << cols () << "\n"; |
3933
|
2844 } |
|
2845 |
237
|
2846 /* |
|
2847 ;;; Local Variables: *** |
|
2848 ;;; mode: C++ *** |
|
2849 ;;; End: *** |
|
2850 */ |