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