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