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> |
5765
|
33 #include <sstream> |
5607
|
34 #include <vector> |
1560
|
35 |
237
|
36 #include "Array.h" |
4588
|
37 #include "Array-util.h" |
4517
|
38 #include "Range.h" |
1560
|
39 #include "idx-vector.h" |
|
40 #include "lo-error.h" |
|
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 |
5775
|
112 // FIXME -- it would be better if we did not have to do |
5047
|
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 { |
5775
|
131 // KLUGE |
4513
|
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 { |
5775
|
171 // KLUGE |
4513
|
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 { |
5775
|
217 // KLUGE |
4513
|
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 { |
5765
|
356 std::ostringstream buf; |
4661
|
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 |
5765
|
370 std::string buf_str = buf.str (); |
|
371 |
|
372 (*current_liboctave_error_handler) (buf_str.c_str ()); |
4513
|
373 |
|
374 return T (); |
|
375 } |
|
376 |
|
377 template <class T> |
|
378 T& |
|
379 Array<T>::range_error (const char *fcn, const Array<int>& ra_idx) |
|
380 { |
5765
|
381 std::ostringstream buf; |
4661
|
382 |
|
383 buf << fcn << " ("; |
|
384 |
5275
|
385 octave_idx_type n = ra_idx.length (); |
4661
|
386 |
|
387 if (n > 0) |
|
388 buf << ra_idx(0); |
|
389 |
5275
|
390 for (octave_idx_type i = 1; i < n; i++) |
4661
|
391 buf << ", " << ra_idx(i); |
|
392 |
|
393 buf << "): range error"; |
|
394 |
5765
|
395 std::string buf_str = buf.str (); |
|
396 |
|
397 (*current_liboctave_error_handler) (buf_str.c_str ()); |
4513
|
398 |
|
399 static T foo; |
|
400 return foo; |
|
401 } |
|
402 |
|
403 template <class T> |
4567
|
404 Array<T> |
|
405 Array<T>::reshape (const dim_vector& new_dims) const |
|
406 { |
|
407 Array<T> retval; |
|
408 |
|
409 if (dimensions != new_dims) |
|
410 { |
|
411 if (dimensions.numel () == new_dims.numel ()) |
|
412 retval = Array<T> (*this, new_dims); |
|
413 else |
|
414 (*current_liboctave_error_handler) ("reshape: size mismatch"); |
|
415 } |
4916
|
416 else |
|
417 retval = *this; |
4567
|
418 |
|
419 return retval; |
|
420 } |
|
421 |
5607
|
422 struct |
|
423 permute_vector |
|
424 { |
|
425 octave_idx_type pidx; |
|
426 octave_idx_type iidx; |
|
427 }; |
|
428 |
|
429 static int |
|
430 permute_vector_compare (const void *a, const void *b) |
|
431 { |
|
432 const permute_vector *pva = static_cast<const permute_vector *> (a); |
|
433 const permute_vector *pvb = static_cast<const permute_vector *> (b); |
|
434 |
|
435 return pva->pidx > pvb->pidx; |
|
436 } |
|
437 |
4567
|
438 template <class T> |
4593
|
439 Array<T> |
5607
|
440 Array<T>::permute (const Array<octave_idx_type>& perm_vec_arg, bool inv) const |
4593
|
441 { |
|
442 Array<T> retval; |
|
443 |
5607
|
444 Array<octave_idx_type> perm_vec = perm_vec_arg; |
|
445 |
4593
|
446 dim_vector dv = dims (); |
|
447 dim_vector dv_new; |
|
448 |
5148
|
449 int perm_vec_len = perm_vec.length (); |
|
450 |
|
451 if (perm_vec_len < dv.length ()) |
|
452 (*current_liboctave_error_handler) |
|
453 ("%s: invalid permutation vector", inv ? "ipermute" : "permute"); |
|
454 |
|
455 dv_new.resize (perm_vec_len); |
|
456 |
|
457 // Append singleton dimensions as needed. |
|
458 dv.resize (perm_vec_len, 1); |
|
459 |
4593
|
460 // Need this array to check for identical elements in permutation array. |
5148
|
461 Array<bool> checked (perm_vec_len, false); |
4593
|
462 |
|
463 // Find dimension vector of permuted array. |
5148
|
464 for (int i = 0; i < perm_vec_len; i++) |
4593
|
465 { |
5275
|
466 octave_idx_type perm_elt = perm_vec.elem (i); |
5148
|
467 |
|
468 if (perm_elt >= perm_vec_len || perm_elt < 0) |
4593
|
469 { |
|
470 (*current_liboctave_error_handler) |
5148
|
471 ("%s: permutation vector contains an invalid element", |
|
472 inv ? "ipermute" : "permute"); |
4593
|
473 |
|
474 return retval; |
|
475 } |
|
476 |
5148
|
477 if (checked.elem(perm_elt)) |
4593
|
478 { |
|
479 (*current_liboctave_error_handler) |
5148
|
480 ("%s: permutation vector cannot contain identical elements", |
|
481 inv ? "ipermute" : "permute"); |
4593
|
482 |
|
483 return retval; |
|
484 } |
|
485 else |
5148
|
486 checked.elem(perm_elt) = true; |
|
487 |
|
488 dv_new(i) = dv(perm_elt); |
4593
|
489 } |
|
490 |
5607
|
491 int nd = dv.length (); |
|
492 |
5775
|
493 // FIXME -- it would be nice to have a sort method in the |
5607
|
494 // Array class that also returns the sort indices. |
|
495 |
|
496 if (inv) |
|
497 { |
|
498 OCTAVE_LOCAL_BUFFER (permute_vector, pvec, nd); |
|
499 |
|
500 for (int i = 0; i < nd; i++) |
|
501 { |
|
502 pvec[i].pidx = perm_vec(i); |
|
503 pvec[i].iidx = i; |
|
504 } |
|
505 |
|
506 octave_qsort (pvec, static_cast<size_t> (nd), |
|
507 sizeof (permute_vector), permute_vector_compare); |
|
508 |
|
509 for (int i = 0; i < nd; i++) |
|
510 { |
|
511 perm_vec(i) = pvec[i].iidx; |
|
512 dv_new(i) = dv(perm_vec(i)); |
|
513 } |
|
514 } |
|
515 |
4593
|
516 retval.resize (dv_new); |
|
517 |
5940
|
518 if (numel () > 0) |
5607
|
519 { |
5940
|
520 Array<octave_idx_type> cp (nd+1, 1); |
|
521 for (octave_idx_type i = 1; i < nd+1; i++) |
|
522 cp(i) = cp(i-1) * dv(i-1); |
|
523 |
|
524 octave_idx_type incr = cp(perm_vec(0)); |
|
525 |
|
526 Array<octave_idx_type> base_delta (nd-1, 0); |
|
527 Array<octave_idx_type> base_delta_max (nd-1); |
|
528 Array<octave_idx_type> base_incr (nd-1); |
|
529 for (octave_idx_type i = 0; i < nd-1; i++) |
5607
|
530 { |
5940
|
531 base_delta_max(i) = dv_new(i+1); |
|
532 base_incr(i) = cp(perm_vec(i+1)); |
5607
|
533 } |
|
534 |
5940
|
535 octave_idx_type nr_new = dv_new(0); |
|
536 octave_idx_type nel_new = dv_new.numel (); |
|
537 octave_idx_type n = nel_new / nr_new; |
|
538 |
|
539 octave_idx_type k = 0; |
|
540 |
|
541 for (octave_idx_type i = 0; i < n; i++) |
5607
|
542 { |
5940
|
543 octave_idx_type iidx = 0; |
|
544 for (octave_idx_type kk = 0; kk < nd-1; kk++) |
|
545 iidx += base_delta(kk) * base_incr(kk); |
|
546 |
|
547 for (octave_idx_type j = 0; j < nr_new; j++) |
5607
|
548 { |
5940
|
549 OCTAVE_QUIT; |
|
550 |
|
551 retval(k++) = elem(iidx); |
|
552 iidx += incr; |
|
553 } |
|
554 |
|
555 base_delta(0)++; |
|
556 |
|
557 for (octave_idx_type kk = 0; kk < nd-2; kk++) |
|
558 { |
|
559 if (base_delta(kk) == base_delta_max(kk)) |
|
560 { |
|
561 base_delta(kk) = 0; |
|
562 base_delta(kk+1)++; |
|
563 } |
5607
|
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 |
5775
|
923 // FIXME -- if the copy constructor is expensive, this |
4747
|
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 |
5781
|
1797 if (! (ra_idx(n_idx-1).is_colon ())) |
|
1798 (*current_liboctave_warning_with_id_handler) |
|
1799 ("Octave:fortran-indexing", |
|
1800 "fewer indices than dimensions for N-d array"); |
4755
|
1801 |
5275
|
1802 for (octave_idx_type i = n_idx; i < n_lhs_dims; i++) |
4755
|
1803 lhs_dims(n_idx-1) *= lhs_dims(i); |
|
1804 |
|
1805 lhs_dims.resize (n_idx); |
|
1806 |
|
1807 // Reshape *this. |
|
1808 dimensions = lhs_dims; |
|
1809 } |
|
1810 |
|
1811 int non_col = 0; |
|
1812 |
|
1813 // Find the non-colon column. |
|
1814 |
5275
|
1815 for (octave_idx_type i = 0; i < n_idx; i++) |
4755
|
1816 { |
|
1817 if (! idx_is_colon(i)) |
|
1818 non_col = i; |
|
1819 } |
|
1820 |
|
1821 // The length of the non-colon dimension. |
|
1822 |
5275
|
1823 octave_idx_type non_col_dim = lhs_dims (non_col); |
|
1824 |
|
1825 octave_idx_type num_to_delete = ra_idx(non_col).length (lhs_dims (non_col)); |
4755
|
1826 |
|
1827 if (num_to_delete > 0) |
|
1828 { |
|
1829 int temp = lhs_dims.num_ones (); |
|
1830 |
|
1831 if (non_col_dim == 1) |
|
1832 temp--; |
|
1833 |
|
1834 if (temp == n_idx - 1 && num_to_delete == non_col_dim) |
|
1835 { |
|
1836 // We have A with (1x1x4), where A(1,:,1:4) |
|
1837 // Delete all (0x0x0) |
|
1838 |
|
1839 dim_vector zero_dims (n_idx, 0); |
|
1840 |
|
1841 resize (zero_dims, rfv); |
|
1842 } |
|
1843 else |
|
1844 { |
|
1845 // New length of non-colon dimension |
|
1846 // (calculated in the next for loop) |
|
1847 |
5275
|
1848 octave_idx_type new_dim = non_col_dim; |
|
1849 |
|
1850 octave_idx_type iidx = 0; |
|
1851 |
|
1852 for (octave_idx_type j = 0; j < non_col_dim; j++) |
4755
|
1853 if (j == ra_idx(non_col).elem (iidx)) |
|
1854 { |
|
1855 iidx++; |
|
1856 |
|
1857 new_dim--; |
|
1858 |
|
1859 if (iidx == num_to_delete) |
|
1860 break; |
|
1861 } |
|
1862 |
|
1863 // Creating the new nd array after deletions. |
|
1864 |
|
1865 if (new_dim > 0) |
|
1866 { |
|
1867 // Calculate number of elements in new array. |
|
1868 |
5275
|
1869 octave_idx_type num_new_elem=1; |
4755
|
1870 |
|
1871 for (int i = 0; i < n_idx; i++) |
|
1872 { |
|
1873 if (i == non_col) |
|
1874 num_new_elem *= new_dim; |
|
1875 |
|
1876 else |
|
1877 num_new_elem *= lhs_dims(i); |
|
1878 } |
|
1879 |
|
1880 T *new_data = new T [num_new_elem]; |
|
1881 |
5275
|
1882 Array<octave_idx_type> result_idx (n_lhs_dims, 0); |
4755
|
1883 |
|
1884 dim_vector new_lhs_dim = lhs_dims; |
|
1885 |
|
1886 new_lhs_dim(non_col) = new_dim; |
|
1887 |
5275
|
1888 octave_idx_type num_elem = 1; |
|
1889 |
|
1890 octave_idx_type numidx = 0; |
|
1891 |
|
1892 octave_idx_type n = length (); |
4755
|
1893 |
|
1894 for (int i = 0; i < n_lhs_dims; i++) |
|
1895 if (i != non_col) |
|
1896 num_elem *= lhs_dims(i); |
|
1897 |
|
1898 num_elem *= ra_idx(non_col).capacity (); |
|
1899 |
5275
|
1900 for (octave_idx_type i = 0; i < n; i++) |
4755
|
1901 { |
|
1902 if (numidx < num_elem |
|
1903 && is_in (result_idx(non_col), ra_idx(non_col))) |
|
1904 numidx++; |
|
1905 |
|
1906 else |
|
1907 { |
5275
|
1908 Array<octave_idx_type> temp_result_idx = result_idx; |
|
1909 |
|
1910 octave_idx_type num_lgt = how_many_lgt (result_idx(non_col), |
4755
|
1911 ra_idx(non_col)); |
|
1912 |
|
1913 temp_result_idx(non_col) -= num_lgt; |
|
1914 |
5275
|
1915 octave_idx_type kidx |
4755
|
1916 = ::compute_index (temp_result_idx, new_lhs_dim); |
|
1917 |
|
1918 new_data[kidx] = elem (result_idx); |
|
1919 } |
|
1920 |
|
1921 increment_index (result_idx, lhs_dims); |
|
1922 } |
|
1923 |
|
1924 if (--rep->count <= 0) |
|
1925 delete rep; |
|
1926 |
|
1927 rep = new typename Array<T>::ArrayRep (new_data, |
|
1928 num_new_elem); |
|
1929 |
|
1930 dimensions = new_lhs_dim; |
|
1931 } |
|
1932 } |
|
1933 } |
4517
|
1934 } |
4755
|
1935 else if (n_idx == 1) |
4517
|
1936 { |
4821
|
1937 // This handle cases where we only have one index (not |
|
1938 // colon). The index denotes which elements we should |
|
1939 // delete in the array which can be of any dimension. We |
|
1940 // return a column vector, except for the case where we are |
|
1941 // operating on a row vector. The elements are numerated |
|
1942 // column by column. |
4755
|
1943 // |
|
1944 // A(3,3,3)=2; |
|
1945 // A(3:5) = []; A(6)=[] |
4757
|
1946 |
5275
|
1947 octave_idx_type lhs_numel = numel (); |
4757
|
1948 |
4821
|
1949 idx_vector idx_vec = ra_idx(0); |
|
1950 |
5781
|
1951 idx_vec.freeze (lhs_numel, 0, true); |
4821
|
1952 |
|
1953 idx_vec.sort (true); |
|
1954 |
5275
|
1955 octave_idx_type num_to_delete = idx_vec.length (lhs_numel); |
4821
|
1956 |
|
1957 if (num_to_delete > 0) |
4517
|
1958 { |
5275
|
1959 octave_idx_type new_numel = lhs_numel - num_to_delete; |
4821
|
1960 |
|
1961 T *new_data = new T[new_numel]; |
|
1962 |
5275
|
1963 Array<octave_idx_type> lhs_ra_idx (ndims (), 0); |
|
1964 |
|
1965 octave_idx_type ii = 0; |
|
1966 octave_idx_type iidx = 0; |
|
1967 |
|
1968 for (octave_idx_type i = 0; i < lhs_numel; i++) |
4755
|
1969 { |
4821
|
1970 if (iidx < num_to_delete && i == idx_vec.elem (iidx)) |
|
1971 { |
|
1972 iidx++; |
|
1973 } |
|
1974 else |
|
1975 { |
|
1976 new_data[ii++] = elem (lhs_ra_idx); |
|
1977 } |
|
1978 |
|
1979 increment_index (lhs_ra_idx, lhs_dims); |
|
1980 } |
|
1981 |
|
1982 if (--(Array<T>::rep)->count <= 0) |
|
1983 delete Array<T>::rep; |
|
1984 |
|
1985 Array<T>::rep = new typename Array<T>::ArrayRep (new_data, new_numel); |
|
1986 |
|
1987 dimensions.resize (2); |
|
1988 |
|
1989 if (lhs_dims.length () == 2 && lhs_dims(1) == 1) |
|
1990 { |
|
1991 dimensions(0) = new_numel; |
|
1992 dimensions(1) = 1; |
4755
|
1993 } |
|
1994 else |
|
1995 { |
4821
|
1996 dimensions(0) = 1; |
|
1997 dimensions(1) = new_numel; |
4755
|
1998 } |
4517
|
1999 } |
|
2000 } |
4755
|
2001 else if (num_ones (idx_is_colon) < n_idx) |
|
2002 { |
|
2003 (*current_liboctave_error_handler) |
|
2004 ("a null assignment can have only one non-colon index"); |
|
2005 } |
4517
|
2006 } |
|
2007 } |
|
2008 |
|
2009 template <class T> |
|
2010 Array<T> |
|
2011 Array<T>::value (void) |
|
2012 { |
|
2013 Array<T> retval; |
|
2014 |
|
2015 int n_idx = index_count (); |
|
2016 |
|
2017 if (n_idx == 2) |
|
2018 { |
|
2019 idx_vector *tmp = get_idx (); |
|
2020 |
|
2021 idx_vector idx_i = tmp[0]; |
|
2022 idx_vector idx_j = tmp[1]; |
|
2023 |
|
2024 retval = index (idx_i, idx_j); |
|
2025 } |
|
2026 else if (n_idx == 1) |
|
2027 { |
|
2028 retval = index (idx[0]); |
|
2029 } |
|
2030 else |
|
2031 (*current_liboctave_error_handler) |
|
2032 ("Array<T>::value: invalid number of indices specified"); |
|
2033 |
|
2034 clear_index (); |
|
2035 |
|
2036 return retval; |
|
2037 } |
|
2038 |
|
2039 template <class T> |
|
2040 Array<T> |
|
2041 Array<T>::index (idx_vector& idx_arg, int resize_ok, const T& rfv) const |
|
2042 { |
|
2043 Array<T> retval; |
|
2044 |
5081
|
2045 dim_vector dv = idx_arg.orig_dimensions (); |
|
2046 |
|
2047 if (dv.length () > 2 || ndims () > 2) |
|
2048 retval = indexN (idx_arg, resize_ok, rfv); |
|
2049 else |
4517
|
2050 { |
5081
|
2051 switch (ndims ()) |
|
2052 { |
|
2053 case 1: |
|
2054 retval = index1 (idx_arg, resize_ok, rfv); |
|
2055 break; |
|
2056 |
|
2057 case 2: |
|
2058 retval = index2 (idx_arg, resize_ok, rfv); |
|
2059 break; |
|
2060 |
|
2061 default: |
|
2062 (*current_liboctave_error_handler) |
|
2063 ("invalid array (internal error)"); |
|
2064 break; |
|
2065 } |
4517
|
2066 } |
|
2067 |
|
2068 return retval; |
|
2069 } |
|
2070 |
|
2071 template <class T> |
|
2072 Array<T> |
|
2073 Array<T>::index1 (idx_vector& idx_arg, int resize_ok, const T& rfv) const |
|
2074 { |
|
2075 Array<T> retval; |
|
2076 |
5275
|
2077 octave_idx_type len = length (); |
|
2078 |
|
2079 octave_idx_type n = idx_arg.freeze (len, "vector", resize_ok); |
4517
|
2080 |
|
2081 if (idx_arg) |
|
2082 { |
|
2083 if (idx_arg.is_colon_equiv (len)) |
|
2084 { |
|
2085 retval = *this; |
|
2086 } |
|
2087 else if (n == 0) |
|
2088 { |
|
2089 retval.resize_no_fill (0); |
|
2090 } |
|
2091 else if (len == 1 && n > 1 |
|
2092 && idx_arg.one_zero_only () |
|
2093 && idx_arg.ones_count () == n) |
|
2094 { |
4548
|
2095 retval.resize_and_fill (n, elem (0)); |
4517
|
2096 } |
|
2097 else |
|
2098 { |
|
2099 retval.resize_no_fill (n); |
|
2100 |
5275
|
2101 for (octave_idx_type i = 0; i < n; i++) |
4517
|
2102 { |
5275
|
2103 octave_idx_type ii = idx_arg.elem (i); |
4517
|
2104 if (ii >= len) |
|
2105 retval.elem (i) = rfv; |
|
2106 else |
|
2107 retval.elem (i) = elem (ii); |
|
2108 } |
|
2109 } |
|
2110 } |
|
2111 |
|
2112 // idx_vector::freeze() printed an error message for us. |
|
2113 |
|
2114 return retval; |
|
2115 } |
|
2116 |
|
2117 template <class T> |
|
2118 Array<T> |
|
2119 Array<T>::index2 (idx_vector& idx_arg, int resize_ok, const T& rfv) const |
|
2120 { |
|
2121 Array<T> retval; |
|
2122 |
4548
|
2123 assert (ndims () == 2); |
|
2124 |
5275
|
2125 octave_idx_type nr = dim1 (); |
|
2126 octave_idx_type nc = dim2 (); |
|
2127 |
|
2128 octave_idx_type orig_len = nr * nc; |
4517
|
2129 |
4832
|
2130 dim_vector idx_orig_dims = idx_arg.orig_dimensions (); |
|
2131 |
5275
|
2132 octave_idx_type idx_orig_rows = idx_arg.orig_rows (); |
|
2133 octave_idx_type idx_orig_columns = idx_arg.orig_columns (); |
4517
|
2134 |
|
2135 if (idx_arg.is_colon ()) |
|
2136 { |
|
2137 // Fast magic colon processing. |
|
2138 |
5275
|
2139 octave_idx_type result_nr = nr * nc; |
|
2140 octave_idx_type result_nc = 1; |
4517
|
2141 |
|
2142 retval = Array<T> (*this, dim_vector (result_nr, result_nc)); |
|
2143 } |
|
2144 else if (nr == 1 && nc == 1) |
|
2145 { |
|
2146 Array<T> tmp = Array<T>::index1 (idx_arg, resize_ok); |
|
2147 |
5275
|
2148 octave_idx_type len = tmp.length (); |
4828
|
2149 |
|
2150 if (len == 0 && idx_arg.one_zero_only ()) |
|
2151 retval = Array<T> (tmp, dim_vector (0, 0)); |
4876
|
2152 else if (len >= idx_orig_dims.numel ()) |
4832
|
2153 retval = Array<T> (tmp, idx_orig_dims); |
4517
|
2154 } |
|
2155 else if (nr == 1 || nc == 1) |
|
2156 { |
|
2157 // If indexing a vector with a matrix, return value has same |
|
2158 // shape as the index. Otherwise, it has same orientation as |
|
2159 // indexed object. |
|
2160 |
4828
|
2161 Array<T> tmp = Array<T>::index1 (idx_arg, resize_ok); |
4517
|
2162 |
5275
|
2163 octave_idx_type len = tmp.length (); |
4517
|
2164 |
4827
|
2165 if ((len != 0 && idx_arg.one_zero_only ()) |
|
2166 || idx_orig_rows == 1 || idx_orig_columns == 1) |
4517
|
2167 { |
4827
|
2168 if (nr == 1) |
|
2169 retval = Array<T> (tmp, dim_vector (1, len)); |
4517
|
2170 else |
4827
|
2171 retval = Array<T> (tmp, dim_vector (len, 1)); |
4517
|
2172 } |
4876
|
2173 else if (len >= idx_orig_dims.numel ()) |
4832
|
2174 retval = Array<T> (tmp, idx_orig_dims); |
4517
|
2175 } |
|
2176 else |
|
2177 { |
5781
|
2178 if (! (idx_arg.one_zero_only () |
|
2179 && idx_orig_rows == nr |
|
2180 && idx_orig_columns == nc)) |
|
2181 (*current_liboctave_warning_with_id_handler) |
|
2182 ("Octave:fortran-indexing", "single index used for matrix"); |
4517
|
2183 |
|
2184 // This code is only for indexing matrices. The vector |
|
2185 // cases are handled above. |
|
2186 |
|
2187 idx_arg.freeze (nr * nc, "matrix", resize_ok); |
|
2188 |
|
2189 if (idx_arg) |
|
2190 { |
5275
|
2191 octave_idx_type result_nr = idx_orig_rows; |
|
2192 octave_idx_type result_nc = idx_orig_columns; |
4517
|
2193 |
|
2194 if (idx_arg.one_zero_only ()) |
|
2195 { |
|
2196 result_nr = idx_arg.ones_count (); |
|
2197 result_nc = (result_nr > 0 ? 1 : 0); |
|
2198 } |
|
2199 |
|
2200 retval.resize_no_fill (result_nr, result_nc); |
|
2201 |
5275
|
2202 octave_idx_type k = 0; |
|
2203 for (octave_idx_type j = 0; j < result_nc; j++) |
4517
|
2204 { |
5275
|
2205 for (octave_idx_type i = 0; i < result_nr; i++) |
4517
|
2206 { |
5275
|
2207 octave_idx_type ii = idx_arg.elem (k++); |
4517
|
2208 if (ii >= orig_len) |
|
2209 retval.elem (i, j) = rfv; |
|
2210 else |
|
2211 { |
5275
|
2212 octave_idx_type fr = ii % nr; |
|
2213 octave_idx_type fc = (ii - fr) / nr; |
4517
|
2214 retval.elem (i, j) = elem (fr, fc); |
|
2215 } |
|
2216 } |
|
2217 } |
|
2218 } |
|
2219 // idx_vector::freeze() printed an error message for us. |
|
2220 } |
|
2221 |
|
2222 return retval; |
|
2223 } |
|
2224 |
|
2225 template <class T> |
|
2226 Array<T> |
4530
|
2227 Array<T>::indexN (idx_vector& ra_idx, int resize_ok, const T& rfv) const |
|
2228 { |
|
2229 Array<T> retval; |
|
2230 |
5519
|
2231 dim_vector dv = dims (); |
|
2232 |
|
2233 int n_dims = dv.length (); |
|
2234 |
|
2235 octave_idx_type orig_len = dv.numel (); |
4530
|
2236 |
4757
|
2237 dim_vector idx_orig_dims = ra_idx.orig_dimensions (); |
4530
|
2238 |
|
2239 if (ra_idx.is_colon ()) |
|
2240 { |
4651
|
2241 // Fast magic colon processing. |
|
2242 |
|
2243 retval = Array<T> (*this, dim_vector (orig_len, 1)); |
4530
|
2244 } |
4651
|
2245 else |
4530
|
2246 { |
5519
|
2247 bool vec_equiv = vector_equivalent (dv); |
|
2248 |
|
2249 if (! vec_equiv |
4651
|
2250 && ! (ra_idx.is_colon () |
5519
|
2251 || (ra_idx.one_zero_only () && idx_orig_dims == dv))) |
5781
|
2252 (*current_liboctave_warning_with_id_handler) |
|
2253 ("Octave:fortran-indexing", "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> |
5992
|
2361 Array<T>::index (Array<idx_vector>& ra_idx, int resize_ok, const T& rfv) 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 |
5992
|
2384 if (! resize_ok) |
|
2385 { |
|
2386 for (octave_idx_type i = 0; i < iidx.capacity (); i++) |
|
2387 if (iidx (i) != 0) |
|
2388 { |
|
2389 (*current_liboctave_error_handler) |
|
2390 ("index exceeds N-d array dimensions"); |
|
2391 |
|
2392 return retval; |
|
2393 } |
|
2394 } |
4737
|
2395 } |
|
2396 |
|
2397 ra_idx.resize (ra_idx_len); |
|
2398 |
4887
|
2399 dim_vector new_dims = dims (); |
|
2400 dim_vector frozen_lengths; |
|
2401 |
|
2402 if (! any_orig_empty (ra_idx) && ra_idx_len < n_dims) |
|
2403 frozen_lengths = short_freeze (ra_idx, dimensions, resize_ok); |
|
2404 else |
4517
|
2405 { |
4887
|
2406 new_dims.resize (ra_idx_len, 1); |
|
2407 frozen_lengths = freeze (ra_idx, new_dims, resize_ok); |
4530
|
2408 } |
|
2409 |
4887
|
2410 if (all_ok (ra_idx)) |
4530
|
2411 { |
4887
|
2412 if (any_orig_empty (ra_idx) || frozen_lengths.any_zero ()) |
|
2413 { |
|
2414 frozen_lengths.chop_trailing_singletons (); |
|
2415 |
|
2416 retval.resize (frozen_lengths); |
|
2417 } |
|
2418 else if (frozen_lengths.length () == n_dims |
|
2419 && all_colon_equiv (ra_idx, dimensions)) |
|
2420 { |
|
2421 retval = *this; |
|
2422 } |
|
2423 else |
4517
|
2424 { |
4887
|
2425 dim_vector frozen_lengths_for_resize = frozen_lengths; |
|
2426 |
|
2427 frozen_lengths_for_resize.chop_trailing_singletons (); |
|
2428 |
|
2429 retval.resize (frozen_lengths_for_resize); |
|
2430 |
5275
|
2431 octave_idx_type n = retval.length (); |
|
2432 |
|
2433 Array<octave_idx_type> result_idx (ra_idx.length (), 0); |
|
2434 |
|
2435 Array<octave_idx_type> elt_idx; |
|
2436 |
|
2437 for (octave_idx_type i = 0; i < n; i++) |
4530
|
2438 { |
4887
|
2439 elt_idx = get_elt_idx (ra_idx, result_idx); |
|
2440 |
5275
|
2441 octave_idx_type numelem_elt = get_scalar_idx (elt_idx, new_dims); |
4887
|
2442 |
5992
|
2443 if (numelem_elt >= length () || numelem_elt < 0) |
|
2444 retval.elem (i) = rfv; |
4887
|
2445 else |
|
2446 retval.elem (i) = elem (numelem_elt); |
|
2447 |
|
2448 increment_index (result_idx, frozen_lengths); |
|
2449 |
4517
|
2450 } |
|
2451 } |
|
2452 } |
|
2453 |
|
2454 return retval; |
|
2455 } |
|
2456 |
5775
|
2457 // FIXME -- this is a mess. |
4517
|
2458 |
|
2459 template <class LT, class RT> |
|
2460 int |
|
2461 assign (Array<LT>& lhs, const Array<RT>& rhs, const LT& rfv) |
|
2462 { |
|
2463 int retval = 0; |
|
2464 |
|
2465 switch (lhs.ndims ()) |
|
2466 { |
|
2467 case 0: |
|
2468 { |
|
2469 if (lhs.index_count () < 3) |
|
2470 { |
|
2471 // kluge... |
|
2472 lhs.resize_no_fill (0, 0); |
|
2473 retval = assign2 (lhs, rhs, rfv); |
|
2474 } |
|
2475 else |
|
2476 retval = assignN (lhs, rhs, rfv); |
|
2477 } |
|
2478 break; |
|
2479 |
|
2480 case 1: |
|
2481 { |
|
2482 if (lhs.index_count () > 1) |
|
2483 retval = assignN (lhs, rhs, rfv); |
|
2484 else |
|
2485 retval = assign1 (lhs, rhs, rfv); |
|
2486 } |
|
2487 break; |
|
2488 |
|
2489 case 2: |
|
2490 { |
|
2491 if (lhs.index_count () > 2) |
|
2492 retval = assignN (lhs, rhs, rfv); |
|
2493 else |
|
2494 retval = assign2 (lhs, rhs, rfv); |
|
2495 } |
|
2496 break; |
|
2497 |
|
2498 default: |
|
2499 retval = assignN (lhs, rhs, rfv); |
|
2500 break; |
|
2501 } |
|
2502 |
|
2503 return retval; |
|
2504 } |
|
2505 |
|
2506 template <class LT, class RT> |
|
2507 int |
|
2508 assign1 (Array<LT>& lhs, const Array<RT>& rhs, const LT& rfv) |
|
2509 { |
|
2510 int retval = 1; |
|
2511 |
|
2512 idx_vector *tmp = lhs.get_idx (); |
|
2513 |
|
2514 idx_vector lhs_idx = tmp[0]; |
|
2515 |
5275
|
2516 octave_idx_type lhs_len = lhs.length (); |
|
2517 octave_idx_type rhs_len = rhs.length (); |
|
2518 |
5781
|
2519 octave_idx_type n = lhs_idx.freeze (lhs_len, "vector", true); |
4517
|
2520 |
|
2521 if (n != 0) |
|
2522 { |
|
2523 if (rhs_len == n || rhs_len == 1) |
|
2524 { |
5275
|
2525 octave_idx_type max_idx = lhs_idx.max () + 1; |
4517
|
2526 if (max_idx > lhs_len) |
4548
|
2527 lhs.resize_and_fill (max_idx, rfv); |
4517
|
2528 } |
|
2529 |
|
2530 if (rhs_len == n) |
|
2531 { |
5275
|
2532 for (octave_idx_type i = 0; i < n; i++) |
4517
|
2533 { |
5275
|
2534 octave_idx_type ii = lhs_idx.elem (i); |
4517
|
2535 lhs.elem (ii) = rhs.elem (i); |
|
2536 } |
|
2537 } |
|
2538 else if (rhs_len == 1) |
|
2539 { |
|
2540 RT scalar = rhs.elem (0); |
|
2541 |
5275
|
2542 for (octave_idx_type i = 0; i < n; i++) |
4517
|
2543 { |
5275
|
2544 octave_idx_type ii = lhs_idx.elem (i); |
4517
|
2545 lhs.elem (ii) = scalar; |
|
2546 } |
|
2547 } |
|
2548 else |
|
2549 { |
|
2550 (*current_liboctave_error_handler) |
|
2551 ("A(I) = X: X must be a scalar or a vector with same length as I"); |
|
2552 |
|
2553 retval = 0; |
|
2554 } |
|
2555 } |
|
2556 else if (lhs_idx.is_colon ()) |
|
2557 { |
|
2558 if (lhs_len == 0) |
|
2559 { |
|
2560 lhs.resize_no_fill (rhs_len); |
|
2561 |
5275
|
2562 for (octave_idx_type i = 0; i < rhs_len; i++) |
4517
|
2563 lhs.elem (i) = rhs.elem (i); |
|
2564 } |
|
2565 else |
|
2566 (*current_liboctave_error_handler) |
|
2567 ("A(:) = X: A must be the same size as X"); |
|
2568 } |
|
2569 else if (! (rhs_len == 1 || rhs_len == 0)) |
|
2570 { |
|
2571 (*current_liboctave_error_handler) |
|
2572 ("A([]) = X: X must also be an empty matrix or a scalar"); |
|
2573 |
|
2574 retval = 0; |
|
2575 } |
|
2576 |
|
2577 lhs.clear_index (); |
|
2578 |
|
2579 return retval; |
|
2580 } |
|
2581 |
|
2582 #define MAYBE_RESIZE_LHS \ |
|
2583 do \ |
|
2584 { \ |
5275
|
2585 octave_idx_type max_row_idx = idx_i_is_colon ? rhs_nr : idx_i.max () + 1; \ |
|
2586 octave_idx_type max_col_idx = idx_j_is_colon ? rhs_nc : idx_j.max () + 1; \ |
4517
|
2587 \ |
5275
|
2588 octave_idx_type new_nr = max_row_idx > lhs_nr ? max_row_idx : lhs_nr; \ |
|
2589 octave_idx_type new_nc = max_col_idx > lhs_nc ? max_col_idx : lhs_nc; \ |
4517
|
2590 \ |
|
2591 lhs.resize_and_fill (new_nr, new_nc, rfv); \ |
|
2592 } \ |
|
2593 while (0) |
|
2594 |
|
2595 template <class LT, class RT> |
|
2596 int |
|
2597 assign2 (Array<LT>& lhs, const Array<RT>& rhs, const LT& rfv) |
|
2598 { |
|
2599 int retval = 1; |
|
2600 |
|
2601 int n_idx = lhs.index_count (); |
|
2602 |
5275
|
2603 octave_idx_type lhs_nr = lhs.rows (); |
|
2604 octave_idx_type lhs_nc = lhs.cols (); |
4517
|
2605 |
5047
|
2606 Array<RT> xrhs = rhs; |
|
2607 |
5275
|
2608 octave_idx_type rhs_nr = xrhs.rows (); |
|
2609 octave_idx_type rhs_nc = xrhs.cols (); |
5047
|
2610 |
|
2611 if (xrhs.ndims () > 2) |
4707
|
2612 { |
5047
|
2613 xrhs = xrhs.squeeze (); |
|
2614 |
|
2615 dim_vector dv_tmp = xrhs.dims (); |
4709
|
2616 |
4708
|
2617 switch (dv_tmp.length ()) |
4707
|
2618 { |
4708
|
2619 case 1: |
5775
|
2620 // FIXME -- this case should be unnecessary, because |
5047
|
2621 // squeeze should always return an object with 2 dimensions. |
4708
|
2622 if (rhs_nr == 1) |
|
2623 rhs_nc = dv_tmp.elem (0); |
|
2624 break; |
4709
|
2625 |
4708
|
2626 case 2: |
4707
|
2627 rhs_nr = dv_tmp.elem (0); |
|
2628 rhs_nc = dv_tmp.elem (1); |
4708
|
2629 break; |
|
2630 |
|
2631 default: |
|
2632 (*current_liboctave_error_handler) |
|
2633 ("Array<T>::assign2: Dimension mismatch"); |
4709
|
2634 return 0; |
4707
|
2635 } |
|
2636 } |
4517
|
2637 |
|
2638 idx_vector *tmp = lhs.get_idx (); |
|
2639 |
|
2640 idx_vector idx_i; |
|
2641 idx_vector idx_j; |
|
2642 |
|
2643 if (n_idx > 1) |
|
2644 idx_j = tmp[1]; |
|
2645 |
|
2646 if (n_idx > 0) |
|
2647 idx_i = tmp[0]; |
|
2648 |
|
2649 if (n_idx == 2) |
|
2650 { |
5781
|
2651 octave_idx_type n = idx_i.freeze (lhs_nr, "row", true); |
|
2652 |
|
2653 octave_idx_type m = idx_j.freeze (lhs_nc, "column", true); |
4517
|
2654 |
|
2655 int idx_i_is_colon = idx_i.is_colon (); |
|
2656 int idx_j_is_colon = idx_j.is_colon (); |
|
2657 |
|
2658 if (idx_i_is_colon) |
|
2659 n = lhs_nr > 0 ? lhs_nr : rhs_nr; |
|
2660 |
|
2661 if (idx_j_is_colon) |
|
2662 m = lhs_nc > 0 ? lhs_nc : rhs_nc; |
|
2663 |
|
2664 if (idx_i && idx_j) |
|
2665 { |
|
2666 if (rhs_nr == 0 && rhs_nc == 0) |
|
2667 { |
|
2668 lhs.maybe_delete_elements (idx_i, idx_j); |
|
2669 } |
|
2670 else |
|
2671 { |
4534
|
2672 if (rhs_nr == 1 && rhs_nc == 1 && n >= 0 && m >= 0) |
4517
|
2673 { |
4534
|
2674 // No need to do anything if either of the indices |
|
2675 // are empty. |
|
2676 |
|
2677 if (n > 0 && m > 0) |
4517
|
2678 { |
4534
|
2679 MAYBE_RESIZE_LHS; |
|
2680 |
5047
|
2681 RT scalar = xrhs.elem (0, 0); |
4534
|
2682 |
5275
|
2683 for (octave_idx_type j = 0; j < m; j++) |
4517
|
2684 { |
5275
|
2685 octave_idx_type jj = idx_j.elem (j); |
|
2686 for (octave_idx_type i = 0; i < n; i++) |
4534
|
2687 { |
5275
|
2688 octave_idx_type ii = idx_i.elem (i); |
4534
|
2689 lhs.elem (ii, jj) = scalar; |
|
2690 } |
4517
|
2691 } |
|
2692 } |
|
2693 } |
6072
|
2694 else if ((n == 1 || m == 1) |
|
2695 && (rhs_nr == 1 || rhs_nc == 1) |
|
2696 && n * m == rhs_nr * rhs_nc) |
|
2697 { |
|
2698 if (n > 0 && m > 0) |
|
2699 { |
|
2700 MAYBE_RESIZE_LHS; |
|
2701 |
|
2702 octave_idx_type k = 0; |
|
2703 |
|
2704 for (octave_idx_type j = 0; j < m; j++) |
|
2705 { |
|
2706 octave_idx_type jj = idx_j.elem (j); |
|
2707 for (octave_idx_type i = 0; i < n; i++) |
|
2708 { |
|
2709 octave_idx_type ii = idx_i.elem (i); |
|
2710 lhs.elem (ii, jj) = xrhs.elem (k++); |
|
2711 } |
|
2712 } |
|
2713 } |
|
2714 } |
4517
|
2715 else if (n == rhs_nr && m == rhs_nc) |
|
2716 { |
|
2717 if (n > 0 && m > 0) |
|
2718 { |
|
2719 MAYBE_RESIZE_LHS; |
|
2720 |
5275
|
2721 for (octave_idx_type j = 0; j < m; j++) |
4517
|
2722 { |
5275
|
2723 octave_idx_type jj = idx_j.elem (j); |
|
2724 for (octave_idx_type i = 0; i < n; i++) |
4517
|
2725 { |
5275
|
2726 octave_idx_type ii = idx_i.elem (i); |
5047
|
2727 lhs.elem (ii, jj) = xrhs.elem (i, j); |
4517
|
2728 } |
|
2729 } |
|
2730 } |
|
2731 } |
|
2732 else if (n == 0 && m == 0) |
|
2733 { |
|
2734 if (! ((rhs_nr == 1 && rhs_nc == 1) |
5039
|
2735 || (rhs_nr == 0 || rhs_nc == 0))) |
4517
|
2736 { |
|
2737 (*current_liboctave_error_handler) |
|
2738 ("A([], []) = X: X must be an empty matrix or a scalar"); |
|
2739 |
|
2740 retval = 0; |
|
2741 } |
|
2742 } |
|
2743 else |
|
2744 { |
|
2745 (*current_liboctave_error_handler) |
|
2746 ("A(I, J) = X: X must be a scalar or the number of elements in I must"); |
|
2747 (*current_liboctave_error_handler) |
|
2748 ("match the number of rows in X and the number of elements in J must"); |
|
2749 (*current_liboctave_error_handler) |
|
2750 ("match the number of columns in X"); |
|
2751 |
|
2752 retval = 0; |
|
2753 } |
|
2754 } |
|
2755 } |
|
2756 // idx_vector::freeze() printed an error message for us. |
|
2757 } |
|
2758 else if (n_idx == 1) |
|
2759 { |
|
2760 int lhs_is_empty = lhs_nr == 0 || lhs_nc == 0; |
|
2761 |
|
2762 if (lhs_is_empty || (lhs_nr == 1 && lhs_nc == 1)) |
|
2763 { |
5275
|
2764 octave_idx_type lhs_len = lhs.length (); |
|
2765 |
5781
|
2766 octave_idx_type n = idx_i.freeze (lhs_len, 0, true); |
4517
|
2767 |
|
2768 if (idx_i) |
|
2769 { |
|
2770 if (rhs_nr == 0 && rhs_nc == 0) |
|
2771 { |
|
2772 if (n != 0 && (lhs_nr != 0 || lhs_nc != 0)) |
|
2773 lhs.maybe_delete_elements (idx_i); |
|
2774 } |
|
2775 else |
|
2776 { |
5781
|
2777 if (lhs_is_empty |
|
2778 && idx_i.is_colon () |
|
2779 && ! (rhs_nr == 1 || rhs_nc == 1)) |
4517
|
2780 { |
5781
|
2781 (*current_liboctave_warning_with_id_handler) |
|
2782 ("Octave:fortran-indexing", |
|
2783 "A(:) = X: X is not a vector or scalar"); |
|
2784 } |
|
2785 else |
|
2786 { |
|
2787 octave_idx_type idx_nr = idx_i.orig_rows (); |
|
2788 octave_idx_type idx_nc = idx_i.orig_columns (); |
|
2789 |
|
2790 if (! (rhs_nr == idx_nr && rhs_nc == idx_nc)) |
|
2791 (*current_liboctave_warning_with_id_handler) |
|
2792 ("Octave:fortran-indexing", |
|
2793 "A(I) = X: X does not have same shape as I"); |
4517
|
2794 } |
|
2795 |
5047
|
2796 if (assign1 (lhs, xrhs, rfv)) |
4517
|
2797 { |
5275
|
2798 octave_idx_type len = lhs.length (); |
4517
|
2799 |
|
2800 if (len > 0) |
|
2801 { |
|
2802 // The following behavior is much simplified |
|
2803 // over previous versions of Octave. It |
|
2804 // seems to be compatible with Matlab. |
|
2805 |
|
2806 lhs.dimensions = dim_vector (1, lhs.length ()); |
|
2807 } |
|
2808 else |
|
2809 lhs.dimensions = dim_vector (0, 0); |
|
2810 } |
|
2811 else |
|
2812 retval = 0; |
|
2813 } |
|
2814 } |
|
2815 // idx_vector::freeze() printed an error message for us. |
|
2816 } |
|
2817 else if (lhs_nr == 1) |
|
2818 { |
5781
|
2819 idx_i.freeze (lhs_nc, "vector", true); |
4517
|
2820 |
|
2821 if (idx_i) |
|
2822 { |
|
2823 if (rhs_nr == 0 && rhs_nc == 0) |
|
2824 lhs.maybe_delete_elements (idx_i); |
|
2825 else |
|
2826 { |
5047
|
2827 if (assign1 (lhs, xrhs, rfv)) |
4517
|
2828 lhs.dimensions = dim_vector (1, lhs.length ()); |
|
2829 else |
|
2830 retval = 0; |
|
2831 } |
|
2832 } |
|
2833 // idx_vector::freeze() printed an error message for us. |
|
2834 } |
|
2835 else if (lhs_nc == 1) |
|
2836 { |
5781
|
2837 idx_i.freeze (lhs_nr, "vector", true); |
4517
|
2838 |
|
2839 if (idx_i) |
|
2840 { |
|
2841 if (rhs_nr == 0 && rhs_nc == 0) |
|
2842 lhs.maybe_delete_elements (idx_i); |
|
2843 else |
|
2844 { |
5047
|
2845 if (assign1 (lhs, xrhs, rfv)) |
4517
|
2846 lhs.dimensions = dim_vector (lhs.length (), 1); |
|
2847 else |
|
2848 retval = 0; |
|
2849 } |
|
2850 } |
|
2851 // idx_vector::freeze() printed an error message for us. |
|
2852 } |
|
2853 else |
|
2854 { |
5781
|
2855 if (! (idx_i.is_colon () |
|
2856 || (idx_i.one_zero_only () |
|
2857 && idx_i.orig_rows () == lhs_nr |
|
2858 && idx_i.orig_columns () == lhs_nc))) |
|
2859 (*current_liboctave_warning_with_id_handler) |
|
2860 ("Octave:fortran-indexing", "single index used for matrix"); |
4517
|
2861 |
5275
|
2862 octave_idx_type len = idx_i.freeze (lhs_nr * lhs_nc, "matrix"); |
4517
|
2863 |
|
2864 if (idx_i) |
|
2865 { |
4756
|
2866 if (rhs_nr == 0 && rhs_nc == 0) |
|
2867 lhs.maybe_delete_elements (idx_i); |
|
2868 else if (len == 0) |
4517
|
2869 { |
|
2870 if (! ((rhs_nr == 1 && rhs_nc == 1) |
5039
|
2871 || (rhs_nr == 0 || rhs_nc == 0))) |
4517
|
2872 (*current_liboctave_error_handler) |
|
2873 ("A([]) = X: X must be an empty matrix or scalar"); |
|
2874 } |
|
2875 else if (len == rhs_nr * rhs_nc) |
|
2876 { |
5275
|
2877 octave_idx_type k = 0; |
|
2878 for (octave_idx_type j = 0; j < rhs_nc; j++) |
4517
|
2879 { |
5275
|
2880 for (octave_idx_type i = 0; i < rhs_nr; i++) |
4517
|
2881 { |
5275
|
2882 octave_idx_type ii = idx_i.elem (k++); |
|
2883 octave_idx_type fr = ii % lhs_nr; |
|
2884 octave_idx_type fc = (ii - fr) / lhs_nr; |
5047
|
2885 lhs.elem (fr, fc) = xrhs.elem (i, j); |
4517
|
2886 } |
|
2887 } |
|
2888 } |
4716
|
2889 else if (rhs_nr == 1 && rhs_nc == 1) |
4517
|
2890 { |
|
2891 RT scalar = rhs.elem (0, 0); |
|
2892 |
5275
|
2893 for (octave_idx_type i = 0; i < len; i++) |
4517
|
2894 { |
5275
|
2895 octave_idx_type ii = idx_i.elem (i); |
4716
|
2896 lhs.elem (ii) = scalar; |
4517
|
2897 } |
|
2898 } |
|
2899 else |
|
2900 { |
|
2901 (*current_liboctave_error_handler) |
|
2902 ("A(I) = X: X must be a scalar or a matrix with the same size as I"); |
|
2903 |
|
2904 retval = 0; |
|
2905 } |
|
2906 } |
|
2907 // idx_vector::freeze() printed an error message for us. |
|
2908 } |
|
2909 } |
|
2910 else |
|
2911 { |
|
2912 (*current_liboctave_error_handler) |
|
2913 ("invalid number of indices for matrix expression"); |
|
2914 |
|
2915 retval = 0; |
|
2916 } |
|
2917 |
|
2918 lhs.clear_index (); |
|
2919 |
|
2920 return retval; |
|
2921 } |
|
2922 |
|
2923 template <class LT, class RT> |
|
2924 int |
|
2925 assignN (Array<LT>& lhs, const Array<RT>& rhs, const LT& rfv) |
|
2926 { |
|
2927 int retval = 1; |
|
2928 |
4746
|
2929 dim_vector rhs_dims = rhs.dims (); |
|
2930 |
5275
|
2931 octave_idx_type rhs_dims_len = rhs_dims.length (); |
4746
|
2932 |
|
2933 bool rhs_is_scalar = is_scalar (rhs_dims); |
|
2934 |
4517
|
2935 int n_idx = lhs.index_count (); |
|
2936 |
4745
|
2937 idx_vector *idx_vex = lhs.get_idx (); |
|
2938 |
|
2939 Array<idx_vector> idx = conv_to_array (idx_vex, n_idx); |
4517
|
2940 |
4743
|
2941 if (rhs_dims_len == 2 && rhs_dims(0) == 0 && rhs_dims(1) == 0) |
4517
|
2942 { |
|
2943 lhs.maybe_delete_elements (idx, rfv); |
|
2944 } |
5285
|
2945 else if (n_idx == 0) |
|
2946 { |
|
2947 (*current_liboctave_error_handler) |
|
2948 ("invalid number of indices for matrix expression"); |
|
2949 |
|
2950 retval = 0; |
|
2951 } |
4657
|
2952 else if (n_idx == 1) |
4517
|
2953 { |
4657
|
2954 idx_vector iidx = idx(0); |
|
2955 |
5781
|
2956 if (! (iidx.is_colon () |
|
2957 || (iidx.one_zero_only () |
|
2958 && iidx.orig_dimensions () == lhs.dims ()))) |
|
2959 (*current_liboctave_warning_with_id_handler) |
|
2960 ("Octave:fortran-indexing", "single index used for N-d array"); |
4657
|
2961 |
5275
|
2962 octave_idx_type lhs_len = lhs.length (); |
|
2963 |
|
2964 octave_idx_type len = iidx.freeze (lhs_len, "N-d arrray"); |
4657
|
2965 |
|
2966 if (iidx) |
4533
|
2967 { |
4657
|
2968 if (len == 0) |
4656
|
2969 { |
5039
|
2970 if (! (rhs_dims.all_ones () || rhs_dims.any_zero ())) |
4743
|
2971 { |
|
2972 (*current_liboctave_error_handler) |
|
2973 ("A([]) = X: X must be an empty matrix or scalar"); |
|
2974 |
|
2975 retval = 0; |
|
2976 } |
4657
|
2977 } |
|
2978 else if (len == rhs.length ()) |
|
2979 { |
5275
|
2980 for (octave_idx_type i = 0; i < len; i++) |
4656
|
2981 { |
5275
|
2982 octave_idx_type ii = iidx.elem (i); |
4657
|
2983 |
|
2984 lhs.elem (ii) = rhs.elem (i); |
4656
|
2985 } |
|
2986 } |
4716
|
2987 else if (rhs_is_scalar) |
4657
|
2988 { |
|
2989 RT scalar = rhs.elem (0); |
|
2990 |
5275
|
2991 for (octave_idx_type i = 0; i < len; i++) |
4657
|
2992 { |
5275
|
2993 octave_idx_type ii = iidx.elem (i); |
4657
|
2994 |
|
2995 lhs.elem (ii) = scalar; |
|
2996 } |
|
2997 } |
|
2998 else |
|
2999 { |
|
3000 (*current_liboctave_error_handler) |
4702
|
3001 ("A(I) = X: X must be a scalar or a matrix with the same size as I"); |
|
3002 |
4657
|
3003 retval = 0; |
|
3004 } |
|
3005 |
4656
|
3006 // idx_vector::freeze() printed an error message for us. |
4533
|
3007 } |
4702
|
3008 } |
4743
|
3009 else |
4702
|
3010 { |
4746
|
3011 // Maybe expand to more dimensions. |
|
3012 |
|
3013 dim_vector lhs_dims = lhs.dims (); |
|
3014 |
5275
|
3015 octave_idx_type lhs_dims_len = lhs_dims.length (); |
4746
|
3016 |
|
3017 dim_vector final_lhs_dims = lhs_dims; |
|
3018 |
|
3019 dim_vector frozen_len; |
|
3020 |
5275
|
3021 octave_idx_type orig_lhs_dims_len = lhs_dims_len; |
4747
|
3022 |
|
3023 bool orig_empty = lhs_dims.all_zero (); |
|
3024 |
|
3025 if (n_idx < lhs_dims_len) |
4517
|
3026 { |
5052
|
3027 // Collapse dimensions beyond last index. Note that we |
|
3028 // delay resizing LHS until we know that the assignment will |
|
3029 // succeed. |
4747
|
3030 |
5781
|
3031 if (! (idx(n_idx-1).is_colon ())) |
|
3032 (*current_liboctave_warning_with_id_handler) |
|
3033 ("Octave:fortran-indexing", |
|
3034 "fewer indices than dimensions for N-d array"); |
4747
|
3035 |
|
3036 for (int i = n_idx; i < lhs_dims_len; i++) |
|
3037 lhs_dims(n_idx-1) *= lhs_dims(i); |
|
3038 |
|
3039 lhs_dims.resize (n_idx); |
|
3040 |
|
3041 lhs_dims_len = lhs_dims.length (); |
|
3042 } |
|
3043 |
|
3044 // Resize. |
|
3045 |
|
3046 dim_vector new_dims; |
|
3047 new_dims.resize (n_idx); |
|
3048 |
5264
|
3049 if (orig_empty) |
4747
|
3050 { |
5264
|
3051 int k = 0; |
|
3052 for (int i = 0; i < n_idx; i++) |
4746
|
3053 { |
4747
|
3054 // If index is a colon, resizing to RHS dimensions is |
|
3055 // allowed because we started out empty. |
4746
|
3056 |
5264
|
3057 if (idx(i).is_colon ()) |
|
3058 { |
|
3059 if (k < rhs_dims.length ()) |
|
3060 new_dims(i) = rhs_dims(k++); |
|
3061 else |
5379
|
3062 new_dims(i) = 1; |
|
3063 } |
|
3064 else |
|
3065 { |
|
3066 octave_idx_type nelem = idx(i).capacity (); |
|
3067 |
|
3068 if (nelem >= 1 |
|
3069 && k < rhs_dims.length () && nelem == rhs_dims(k)) |
|
3070 k++; |
|
3071 else if (nelem != 1) |
5264
|
3072 { |
|
3073 (*current_liboctave_error_handler) |
5379
|
3074 ("A(IDX-LIST) = RHS: mismatched index and RHS dimension"); |
5264
|
3075 return retval; |
|
3076 } |
5379
|
3077 |
|
3078 new_dims(i) = idx(i).max () + 1; |
5264
|
3079 } |
4746
|
3080 } |
5264
|
3081 } |
|
3082 else |
|
3083 { |
|
3084 for (int i = 0; i < n_idx; i++) |
4746
|
3085 { |
4747
|
3086 // We didn't start out with all zero dimensions, so if |
|
3087 // index is a colon, it refers to the current LHS |
|
3088 // dimension. Otherwise, it is OK to enlarge to a |
5264
|
3089 // dimension given by the largest index, but if that |
4898
|
3090 // index is a colon the new dimension is singleton. |
4749
|
3091 |
|
3092 if (i < lhs_dims_len |
|
3093 && (idx(i).is_colon () || idx(i).max () < lhs_dims(i))) |
|
3094 new_dims(i) = lhs_dims(i); |
|
3095 else if (! idx(i).is_colon ()) |
|
3096 new_dims(i) = idx(i).max () + 1; |
|
3097 else |
4898
|
3098 new_dims(i) = 1; |
4745
|
3099 } |
4747
|
3100 } |
|
3101 |
4749
|
3102 if (retval != 0) |
4747
|
3103 { |
4749
|
3104 if (! orig_empty |
|
3105 && n_idx < orig_lhs_dims_len |
|
3106 && new_dims(n_idx-1) != lhs_dims(n_idx-1)) |
4702
|
3107 { |
4749
|
3108 // We reshaped and the last dimension changed. This has to |
|
3109 // be an error, because we don't know how to undo that |
|
3110 // later... |
|
3111 |
|
3112 (*current_liboctave_error_handler) |
|
3113 ("array index %d (= %d) for assignment requires invalid resizing operation", |
|
3114 n_idx, new_dims(n_idx-1)); |
|
3115 |
|
3116 retval = 0; |
4743
|
3117 } |
|
3118 else |
|
3119 { |
5052
|
3120 // Determine final dimensions for LHS and reset the |
|
3121 // current size of the LHS. Note that we delay actually |
|
3122 // resizing LHS until we know that the assignment will |
|
3123 // succeed. |
|
3124 |
4749
|
3125 if (n_idx < orig_lhs_dims_len) |
4743
|
3126 { |
4749
|
3127 for (int i = 0; i < n_idx-1; i++) |
|
3128 final_lhs_dims(i) = new_dims(i); |
4747
|
3129 } |
|
3130 else |
4749
|
3131 final_lhs_dims = new_dims; |
|
3132 |
5837
|
3133 lhs_dims_len = new_dims.length (); |
|
3134 |
|
3135 frozen_len = freeze (idx, new_dims, true); |
4749
|
3136 |
|
3137 if (rhs_is_scalar) |
4747
|
3138 { |
5837
|
3139 if (n_idx < orig_lhs_dims_len) |
|
3140 lhs = lhs.reshape (lhs_dims); |
|
3141 |
5052
|
3142 lhs.resize_and_fill (new_dims, rfv); |
|
3143 |
4747
|
3144 if (! final_lhs_dims.any_zero ()) |
|
3145 { |
5275
|
3146 octave_idx_type n = Array<LT>::get_size (frozen_len); |
|
3147 |
|
3148 Array<octave_idx_type> result_idx (lhs_dims_len, 0); |
4747
|
3149 |
4749
|
3150 RT scalar = rhs.elem (0); |
|
3151 |
5275
|
3152 for (octave_idx_type i = 0; i < n; i++) |
4747
|
3153 { |
5275
|
3154 Array<octave_idx_type> elt_idx = get_elt_idx (idx, result_idx); |
4747
|
3155 |
4749
|
3156 lhs.elem (elt_idx) = scalar; |
4747
|
3157 |
|
3158 increment_index (result_idx, frozen_len); |
|
3159 } |
|
3160 } |
4743
|
3161 } |
4749
|
3162 else |
|
3163 { |
|
3164 // RHS is matrix or higher dimension. |
|
3165 |
5275
|
3166 octave_idx_type n = Array<LT>::get_size (frozen_len); |
5264
|
3167 |
|
3168 if (n != rhs.numel ()) |
4749
|
3169 { |
|
3170 (*current_liboctave_error_handler) |
|
3171 ("A(IDX-LIST) = X: X must be a scalar or size of X must equal number of elements indexed by IDX-LIST"); |
|
3172 |
|
3173 retval = 0; |
|
3174 } |
|
3175 else |
|
3176 { |
5837
|
3177 if (n_idx < orig_lhs_dims_len) |
|
3178 lhs = lhs.reshape (lhs_dims); |
|
3179 |
5052
|
3180 lhs.resize_and_fill (new_dims, rfv); |
|
3181 |
4749
|
3182 if (! final_lhs_dims.any_zero ()) |
|
3183 { |
5265
|
3184 n = Array<LT>::get_size (frozen_len); |
4749
|
3185 |
5275
|
3186 Array<octave_idx_type> result_idx (lhs_dims_len, 0); |
|
3187 |
|
3188 for (octave_idx_type i = 0; i < n; i++) |
4749
|
3189 { |
5275
|
3190 Array<octave_idx_type> elt_idx = get_elt_idx (idx, result_idx); |
4749
|
3191 |
|
3192 lhs.elem (elt_idx) = rhs.elem (i); |
|
3193 |
|
3194 increment_index (result_idx, frozen_len); |
|
3195 } |
|
3196 } |
|
3197 } |
|
3198 } |
4743
|
3199 } |
4517
|
3200 } |
4745
|
3201 |
5632
|
3202 lhs.clear_index (); |
|
3203 |
5052
|
3204 if (retval != 0) |
5516
|
3205 lhs = lhs.reshape (final_lhs_dims); |
4517
|
3206 } |
|
3207 |
5052
|
3208 if (retval != 0) |
|
3209 lhs.chop_trailing_singletons (); |
4757
|
3210 |
4517
|
3211 lhs.clear_index (); |
|
3212 |
|
3213 return retval; |
|
3214 } |
|
3215 |
|
3216 template <class T> |
|
3217 void |
3933
|
3218 Array<T>::print_info (std::ostream& os, const std::string& prefix) const |
|
3219 { |
|
3220 os << prefix << "rep address: " << rep << "\n" |
|
3221 << prefix << "rep->len: " << rep->len << "\n" |
|
3222 << prefix << "rep->data: " << static_cast<void *> (rep->data) << "\n" |
|
3223 << prefix << "rep->count: " << rep->count << "\n"; |
4513
|
3224 |
|
3225 // 2D info: |
|
3226 // |
4657
|
3227 // << pefix << "rows: " << rows () << "\n" |
4513
|
3228 // << prefix << "cols: " << cols () << "\n"; |
3933
|
3229 } |
|
3230 |
237
|
3231 /* |
|
3232 ;;; Local Variables: *** |
|
3233 ;;; mode: C++ *** |
|
3234 ;;; End: *** |
|
3235 */ |