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