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