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