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 |
6388
|
1682 int n_lhs_dims = lhs_dims.length (); |
|
1683 |
4821
|
1684 if (lhs_dims.all_zero ()) |
|
1685 return; |
|
1686 |
6384
|
1687 if (n_idx == 1 && ra_idx(0).is_colon ()) |
|
1688 { |
|
1689 resize (dim_vector (0, 0), rfv); |
|
1690 return; |
|
1691 } |
|
1692 |
6388
|
1693 if (n_idx > n_lhs_dims) |
|
1694 { |
|
1695 for (int i = n_idx; i < n_lhs_dims; i++) |
|
1696 { |
|
1697 // Ensure that extra indices are either colon or 1. |
|
1698 |
|
1699 if (! ra_idx(i).is_colon_equiv (1, 1)) |
|
1700 { |
|
1701 (*current_liboctave_error_handler) |
|
1702 ("index exceeds array dimensions"); |
|
1703 return; |
|
1704 } |
|
1705 } |
|
1706 |
|
1707 ra_idx.resize (n_lhs_dims); |
|
1708 |
|
1709 n_idx = n_lhs_dims; |
|
1710 } |
4757
|
1711 |
4740
|
1712 Array<int> idx_is_colon (n_idx, 0); |
|
1713 |
|
1714 Array<int> idx_is_colon_equiv (n_idx, 0); |
4517
|
1715 |
|
1716 // Initialization of colon arrays. |
4757
|
1717 |
5275
|
1718 for (octave_idx_type i = 0; i < n_idx; i++) |
4517
|
1719 { |
4585
|
1720 idx_is_colon_equiv(i) = ra_idx(i).is_colon_equiv (lhs_dims(i), 1); |
|
1721 |
|
1722 idx_is_colon(i) = ra_idx(i).is_colon (); |
4517
|
1723 } |
|
1724 |
4755
|
1725 bool idx_ok = true; |
|
1726 |
|
1727 // Check for index out of bounds. |
|
1728 |
5275
|
1729 for (octave_idx_type i = 0 ; i < n_idx - 1; i++) |
4517
|
1730 { |
4755
|
1731 if (! (idx_is_colon(i) || idx_is_colon_equiv(i))) |
|
1732 { |
|
1733 ra_idx(i).sort (true); |
4757
|
1734 |
4755
|
1735 if (ra_idx(i).max () > lhs_dims(i)) |
|
1736 { |
|
1737 (*current_liboctave_error_handler) |
|
1738 ("index exceeds array dimensions"); |
4757
|
1739 |
4755
|
1740 idx_ok = false; |
|
1741 break; |
|
1742 } |
|
1743 else if (ra_idx(i).min () < 0) // I believe this is checked elsewhere |
|
1744 { |
|
1745 (*current_liboctave_error_handler) |
|
1746 ("index must be one or larger"); |
|
1747 |
|
1748 idx_ok = false; |
|
1749 break; |
|
1750 } |
|
1751 } |
4517
|
1752 } |
4757
|
1753 |
4755
|
1754 if (n_idx <= n_lhs_dims) |
4517
|
1755 { |
5275
|
1756 octave_idx_type last_idx = ra_idx(n_idx-1).max (); |
|
1757 |
|
1758 octave_idx_type sum_el = lhs_dims(n_idx-1); |
|
1759 |
|
1760 for (octave_idx_type i = n_idx; i < n_lhs_dims; i++) |
4755
|
1761 sum_el *= lhs_dims(i); |
|
1762 |
|
1763 if (last_idx > sum_el - 1) |
|
1764 { |
|
1765 (*current_liboctave_error_handler) |
|
1766 ("index exceeds array dimensions"); |
|
1767 |
|
1768 idx_ok = false; |
|
1769 } |
4757
|
1770 } |
4755
|
1771 |
|
1772 if (idx_ok) |
|
1773 { |
|
1774 if (n_idx > 1 |
|
1775 && (all_ones (idx_is_colon) || all_ones (idx_is_colon_equiv))) |
4517
|
1776 { |
4755
|
1777 // A(:,:,:) -- we are deleting elements in all dimensions, so |
|
1778 // the result is [](0x0x0). |
|
1779 |
|
1780 dim_vector zeros; |
|
1781 zeros.resize (n_idx); |
|
1782 |
|
1783 for (int i = 0; i < n_idx; i++) |
|
1784 zeros(i) = 0; |
|
1785 |
|
1786 resize (zeros, rfv); |
4517
|
1787 } |
|
1788 |
4755
|
1789 else if (n_idx > 1 |
|
1790 && num_ones (idx_is_colon) == n_idx - 1 |
|
1791 && num_ones (idx_is_colon_equiv) == n_idx) |
|
1792 { |
|
1793 // A(:,:,j) -- we are deleting elements in one dimension by |
|
1794 // enumerating them. |
|
1795 // |
|
1796 // If we enumerate all of the elements, we should have zero |
|
1797 // elements in that dimension with the same number of elements |
|
1798 // in the other dimensions that we started with. |
|
1799 |
|
1800 dim_vector temp_dims; |
|
1801 temp_dims.resize (n_idx); |
|
1802 |
5275
|
1803 for (octave_idx_type i = 0; i < n_idx; i++) |
4755
|
1804 { |
|
1805 if (idx_is_colon (i)) |
|
1806 temp_dims(i) = lhs_dims(i); |
|
1807 else |
|
1808 temp_dims(i) = 0; |
|
1809 } |
|
1810 |
|
1811 resize (temp_dims); |
|
1812 } |
|
1813 else if (n_idx > 1 && num_ones (idx_is_colon) == n_idx - 1) |
4741
|
1814 { |
4755
|
1815 // We have colons in all indices except for one. |
|
1816 // This index tells us which slice to delete |
|
1817 |
|
1818 if (n_idx < n_lhs_dims) |
|
1819 { |
|
1820 // Collapse dimensions beyond last index. |
|
1821 |
5781
|
1822 if (! (ra_idx(n_idx-1).is_colon ())) |
|
1823 (*current_liboctave_warning_with_id_handler) |
|
1824 ("Octave:fortran-indexing", |
|
1825 "fewer indices than dimensions for N-d array"); |
4755
|
1826 |
5275
|
1827 for (octave_idx_type i = n_idx; i < n_lhs_dims; i++) |
4755
|
1828 lhs_dims(n_idx-1) *= lhs_dims(i); |
|
1829 |
|
1830 lhs_dims.resize (n_idx); |
|
1831 |
|
1832 // Reshape *this. |
|
1833 dimensions = lhs_dims; |
|
1834 } |
|
1835 |
|
1836 int non_col = 0; |
|
1837 |
|
1838 // Find the non-colon column. |
|
1839 |
5275
|
1840 for (octave_idx_type i = 0; i < n_idx; i++) |
4755
|
1841 { |
|
1842 if (! idx_is_colon(i)) |
|
1843 non_col = i; |
|
1844 } |
|
1845 |
|
1846 // The length of the non-colon dimension. |
|
1847 |
5275
|
1848 octave_idx_type non_col_dim = lhs_dims (non_col); |
|
1849 |
|
1850 octave_idx_type num_to_delete = ra_idx(non_col).length (lhs_dims (non_col)); |
4755
|
1851 |
|
1852 if (num_to_delete > 0) |
|
1853 { |
|
1854 int temp = lhs_dims.num_ones (); |
|
1855 |
|
1856 if (non_col_dim == 1) |
|
1857 temp--; |
|
1858 |
|
1859 if (temp == n_idx - 1 && num_to_delete == non_col_dim) |
|
1860 { |
|
1861 // We have A with (1x1x4), where A(1,:,1:4) |
|
1862 // Delete all (0x0x0) |
|
1863 |
|
1864 dim_vector zero_dims (n_idx, 0); |
|
1865 |
|
1866 resize (zero_dims, rfv); |
|
1867 } |
|
1868 else |
|
1869 { |
|
1870 // New length of non-colon dimension |
|
1871 // (calculated in the next for loop) |
|
1872 |
5275
|
1873 octave_idx_type new_dim = non_col_dim; |
|
1874 |
|
1875 octave_idx_type iidx = 0; |
|
1876 |
|
1877 for (octave_idx_type j = 0; j < non_col_dim; j++) |
4755
|
1878 if (j == ra_idx(non_col).elem (iidx)) |
|
1879 { |
|
1880 iidx++; |
|
1881 |
|
1882 new_dim--; |
|
1883 |
|
1884 if (iidx == num_to_delete) |
|
1885 break; |
|
1886 } |
|
1887 |
|
1888 // Creating the new nd array after deletions. |
|
1889 |
|
1890 if (new_dim > 0) |
|
1891 { |
|
1892 // Calculate number of elements in new array. |
|
1893 |
5275
|
1894 octave_idx_type num_new_elem=1; |
4755
|
1895 |
|
1896 for (int i = 0; i < n_idx; i++) |
|
1897 { |
|
1898 if (i == non_col) |
|
1899 num_new_elem *= new_dim; |
|
1900 |
|
1901 else |
|
1902 num_new_elem *= lhs_dims(i); |
|
1903 } |
|
1904 |
|
1905 T *new_data = new T [num_new_elem]; |
|
1906 |
5275
|
1907 Array<octave_idx_type> result_idx (n_lhs_dims, 0); |
4755
|
1908 |
|
1909 dim_vector new_lhs_dim = lhs_dims; |
|
1910 |
|
1911 new_lhs_dim(non_col) = new_dim; |
|
1912 |
5275
|
1913 octave_idx_type num_elem = 1; |
|
1914 |
|
1915 octave_idx_type numidx = 0; |
|
1916 |
|
1917 octave_idx_type n = length (); |
4755
|
1918 |
|
1919 for (int i = 0; i < n_lhs_dims; i++) |
|
1920 if (i != non_col) |
|
1921 num_elem *= lhs_dims(i); |
|
1922 |
|
1923 num_elem *= ra_idx(non_col).capacity (); |
|
1924 |
5275
|
1925 for (octave_idx_type i = 0; i < n; i++) |
4755
|
1926 { |
|
1927 if (numidx < num_elem |
|
1928 && is_in (result_idx(non_col), ra_idx(non_col))) |
|
1929 numidx++; |
|
1930 |
|
1931 else |
|
1932 { |
5275
|
1933 Array<octave_idx_type> temp_result_idx = result_idx; |
|
1934 |
|
1935 octave_idx_type num_lgt = how_many_lgt (result_idx(non_col), |
4755
|
1936 ra_idx(non_col)); |
|
1937 |
|
1938 temp_result_idx(non_col) -= num_lgt; |
|
1939 |
5275
|
1940 octave_idx_type kidx |
4755
|
1941 = ::compute_index (temp_result_idx, new_lhs_dim); |
|
1942 |
|
1943 new_data[kidx] = elem (result_idx); |
|
1944 } |
|
1945 |
|
1946 increment_index (result_idx, lhs_dims); |
|
1947 } |
|
1948 |
|
1949 if (--rep->count <= 0) |
|
1950 delete rep; |
|
1951 |
|
1952 rep = new typename Array<T>::ArrayRep (new_data, |
|
1953 num_new_elem); |
|
1954 |
|
1955 dimensions = new_lhs_dim; |
|
1956 } |
|
1957 } |
|
1958 } |
4517
|
1959 } |
4755
|
1960 else if (n_idx == 1) |
4517
|
1961 { |
4821
|
1962 // This handle cases where we only have one index (not |
|
1963 // colon). The index denotes which elements we should |
|
1964 // delete in the array which can be of any dimension. We |
|
1965 // return a column vector, except for the case where we are |
|
1966 // operating on a row vector. The elements are numerated |
|
1967 // column by column. |
4755
|
1968 // |
|
1969 // A(3,3,3)=2; |
|
1970 // A(3:5) = []; A(6)=[] |
4757
|
1971 |
5275
|
1972 octave_idx_type lhs_numel = numel (); |
4757
|
1973 |
4821
|
1974 idx_vector idx_vec = ra_idx(0); |
|
1975 |
5781
|
1976 idx_vec.freeze (lhs_numel, 0, true); |
4821
|
1977 |
|
1978 idx_vec.sort (true); |
|
1979 |
5275
|
1980 octave_idx_type num_to_delete = idx_vec.length (lhs_numel); |
4821
|
1981 |
|
1982 if (num_to_delete > 0) |
4517
|
1983 { |
5275
|
1984 octave_idx_type new_numel = lhs_numel - num_to_delete; |
4821
|
1985 |
|
1986 T *new_data = new T[new_numel]; |
|
1987 |
5275
|
1988 Array<octave_idx_type> lhs_ra_idx (ndims (), 0); |
|
1989 |
|
1990 octave_idx_type ii = 0; |
|
1991 octave_idx_type iidx = 0; |
|
1992 |
|
1993 for (octave_idx_type i = 0; i < lhs_numel; i++) |
4755
|
1994 { |
4821
|
1995 if (iidx < num_to_delete && i == idx_vec.elem (iidx)) |
|
1996 { |
|
1997 iidx++; |
|
1998 } |
|
1999 else |
|
2000 { |
|
2001 new_data[ii++] = elem (lhs_ra_idx); |
|
2002 } |
|
2003 |
|
2004 increment_index (lhs_ra_idx, lhs_dims); |
|
2005 } |
|
2006 |
|
2007 if (--(Array<T>::rep)->count <= 0) |
|
2008 delete Array<T>::rep; |
|
2009 |
|
2010 Array<T>::rep = new typename Array<T>::ArrayRep (new_data, new_numel); |
|
2011 |
|
2012 dimensions.resize (2); |
|
2013 |
|
2014 if (lhs_dims.length () == 2 && lhs_dims(1) == 1) |
|
2015 { |
|
2016 dimensions(0) = new_numel; |
|
2017 dimensions(1) = 1; |
4755
|
2018 } |
|
2019 else |
|
2020 { |
4821
|
2021 dimensions(0) = 1; |
|
2022 dimensions(1) = new_numel; |
4755
|
2023 } |
4517
|
2024 } |
|
2025 } |
4755
|
2026 else if (num_ones (idx_is_colon) < n_idx) |
|
2027 { |
|
2028 (*current_liboctave_error_handler) |
|
2029 ("a null assignment can have only one non-colon index"); |
|
2030 } |
4517
|
2031 } |
|
2032 } |
|
2033 |
|
2034 template <class T> |
|
2035 Array<T> |
|
2036 Array<T>::value (void) |
|
2037 { |
|
2038 Array<T> retval; |
|
2039 |
|
2040 int n_idx = index_count (); |
|
2041 |
|
2042 if (n_idx == 2) |
|
2043 { |
|
2044 idx_vector *tmp = get_idx (); |
|
2045 |
|
2046 idx_vector idx_i = tmp[0]; |
|
2047 idx_vector idx_j = tmp[1]; |
|
2048 |
|
2049 retval = index (idx_i, idx_j); |
|
2050 } |
|
2051 else if (n_idx == 1) |
|
2052 { |
|
2053 retval = index (idx[0]); |
|
2054 } |
|
2055 else |
|
2056 (*current_liboctave_error_handler) |
|
2057 ("Array<T>::value: invalid number of indices specified"); |
|
2058 |
|
2059 clear_index (); |
|
2060 |
|
2061 return retval; |
|
2062 } |
|
2063 |
|
2064 template <class T> |
|
2065 Array<T> |
|
2066 Array<T>::index (idx_vector& idx_arg, int resize_ok, const T& rfv) const |
|
2067 { |
|
2068 Array<T> retval; |
|
2069 |
5081
|
2070 dim_vector dv = idx_arg.orig_dimensions (); |
|
2071 |
|
2072 if (dv.length () > 2 || ndims () > 2) |
|
2073 retval = indexN (idx_arg, resize_ok, rfv); |
|
2074 else |
4517
|
2075 { |
5081
|
2076 switch (ndims ()) |
|
2077 { |
|
2078 case 1: |
|
2079 retval = index1 (idx_arg, resize_ok, rfv); |
|
2080 break; |
|
2081 |
|
2082 case 2: |
|
2083 retval = index2 (idx_arg, resize_ok, rfv); |
|
2084 break; |
|
2085 |
|
2086 default: |
|
2087 (*current_liboctave_error_handler) |
|
2088 ("invalid array (internal error)"); |
|
2089 break; |
|
2090 } |
4517
|
2091 } |
|
2092 |
|
2093 return retval; |
|
2094 } |
|
2095 |
|
2096 template <class T> |
|
2097 Array<T> |
|
2098 Array<T>::index1 (idx_vector& idx_arg, int resize_ok, const T& rfv) const |
|
2099 { |
|
2100 Array<T> retval; |
|
2101 |
5275
|
2102 octave_idx_type len = length (); |
|
2103 |
|
2104 octave_idx_type n = idx_arg.freeze (len, "vector", resize_ok); |
4517
|
2105 |
|
2106 if (idx_arg) |
|
2107 { |
|
2108 if (idx_arg.is_colon_equiv (len)) |
|
2109 { |
|
2110 retval = *this; |
|
2111 } |
|
2112 else if (n == 0) |
|
2113 { |
|
2114 retval.resize_no_fill (0); |
|
2115 } |
|
2116 else if (len == 1 && n > 1 |
|
2117 && idx_arg.one_zero_only () |
|
2118 && idx_arg.ones_count () == n) |
|
2119 { |
4548
|
2120 retval.resize_and_fill (n, elem (0)); |
4517
|
2121 } |
|
2122 else |
|
2123 { |
|
2124 retval.resize_no_fill (n); |
|
2125 |
5275
|
2126 for (octave_idx_type i = 0; i < n; i++) |
4517
|
2127 { |
5275
|
2128 octave_idx_type ii = idx_arg.elem (i); |
4517
|
2129 if (ii >= len) |
|
2130 retval.elem (i) = rfv; |
|
2131 else |
|
2132 retval.elem (i) = elem (ii); |
|
2133 } |
|
2134 } |
|
2135 } |
|
2136 |
|
2137 // idx_vector::freeze() printed an error message for us. |
|
2138 |
|
2139 return retval; |
|
2140 } |
|
2141 |
|
2142 template <class T> |
|
2143 Array<T> |
|
2144 Array<T>::index2 (idx_vector& idx_arg, int resize_ok, const T& rfv) const |
|
2145 { |
|
2146 Array<T> retval; |
|
2147 |
4548
|
2148 assert (ndims () == 2); |
|
2149 |
5275
|
2150 octave_idx_type nr = dim1 (); |
|
2151 octave_idx_type nc = dim2 (); |
|
2152 |
|
2153 octave_idx_type orig_len = nr * nc; |
4517
|
2154 |
4832
|
2155 dim_vector idx_orig_dims = idx_arg.orig_dimensions (); |
|
2156 |
5275
|
2157 octave_idx_type idx_orig_rows = idx_arg.orig_rows (); |
|
2158 octave_idx_type idx_orig_columns = idx_arg.orig_columns (); |
4517
|
2159 |
|
2160 if (idx_arg.is_colon ()) |
|
2161 { |
|
2162 // Fast magic colon processing. |
|
2163 |
5275
|
2164 octave_idx_type result_nr = nr * nc; |
|
2165 octave_idx_type result_nc = 1; |
4517
|
2166 |
|
2167 retval = Array<T> (*this, dim_vector (result_nr, result_nc)); |
|
2168 } |
|
2169 else if (nr == 1 && nc == 1) |
|
2170 { |
|
2171 Array<T> tmp = Array<T>::index1 (idx_arg, resize_ok); |
|
2172 |
5275
|
2173 octave_idx_type len = tmp.length (); |
4828
|
2174 |
|
2175 if (len == 0 && idx_arg.one_zero_only ()) |
|
2176 retval = Array<T> (tmp, dim_vector (0, 0)); |
4876
|
2177 else if (len >= idx_orig_dims.numel ()) |
4832
|
2178 retval = Array<T> (tmp, idx_orig_dims); |
4517
|
2179 } |
|
2180 else if (nr == 1 || nc == 1) |
|
2181 { |
|
2182 // If indexing a vector with a matrix, return value has same |
|
2183 // shape as the index. Otherwise, it has same orientation as |
|
2184 // indexed object. |
|
2185 |
4828
|
2186 Array<T> tmp = Array<T>::index1 (idx_arg, resize_ok); |
4517
|
2187 |
5275
|
2188 octave_idx_type len = tmp.length (); |
4517
|
2189 |
4827
|
2190 if ((len != 0 && idx_arg.one_zero_only ()) |
|
2191 || idx_orig_rows == 1 || idx_orig_columns == 1) |
4517
|
2192 { |
4827
|
2193 if (nr == 1) |
|
2194 retval = Array<T> (tmp, dim_vector (1, len)); |
4517
|
2195 else |
4827
|
2196 retval = Array<T> (tmp, dim_vector (len, 1)); |
4517
|
2197 } |
4876
|
2198 else if (len >= idx_orig_dims.numel ()) |
4832
|
2199 retval = Array<T> (tmp, idx_orig_dims); |
4517
|
2200 } |
|
2201 else |
|
2202 { |
5781
|
2203 if (! (idx_arg.one_zero_only () |
|
2204 && idx_orig_rows == nr |
|
2205 && idx_orig_columns == nc)) |
|
2206 (*current_liboctave_warning_with_id_handler) |
|
2207 ("Octave:fortran-indexing", "single index used for matrix"); |
4517
|
2208 |
|
2209 // This code is only for indexing matrices. The vector |
|
2210 // cases are handled above. |
|
2211 |
|
2212 idx_arg.freeze (nr * nc, "matrix", resize_ok); |
|
2213 |
|
2214 if (idx_arg) |
|
2215 { |
5275
|
2216 octave_idx_type result_nr = idx_orig_rows; |
|
2217 octave_idx_type result_nc = idx_orig_columns; |
4517
|
2218 |
|
2219 if (idx_arg.one_zero_only ()) |
|
2220 { |
|
2221 result_nr = idx_arg.ones_count (); |
|
2222 result_nc = (result_nr > 0 ? 1 : 0); |
|
2223 } |
|
2224 |
|
2225 retval.resize_no_fill (result_nr, result_nc); |
|
2226 |
5275
|
2227 octave_idx_type k = 0; |
|
2228 for (octave_idx_type j = 0; j < result_nc; j++) |
4517
|
2229 { |
5275
|
2230 for (octave_idx_type i = 0; i < result_nr; i++) |
4517
|
2231 { |
5275
|
2232 octave_idx_type ii = idx_arg.elem (k++); |
4517
|
2233 if (ii >= orig_len) |
|
2234 retval.elem (i, j) = rfv; |
|
2235 else |
|
2236 { |
5275
|
2237 octave_idx_type fr = ii % nr; |
|
2238 octave_idx_type fc = (ii - fr) / nr; |
4517
|
2239 retval.elem (i, j) = elem (fr, fc); |
|
2240 } |
|
2241 } |
|
2242 } |
|
2243 } |
|
2244 // idx_vector::freeze() printed an error message for us. |
|
2245 } |
|
2246 |
|
2247 return retval; |
|
2248 } |
|
2249 |
|
2250 template <class T> |
|
2251 Array<T> |
4530
|
2252 Array<T>::indexN (idx_vector& ra_idx, int resize_ok, const T& rfv) const |
|
2253 { |
|
2254 Array<T> retval; |
|
2255 |
5519
|
2256 dim_vector dv = dims (); |
|
2257 |
|
2258 int n_dims = dv.length (); |
|
2259 |
|
2260 octave_idx_type orig_len = dv.numel (); |
4530
|
2261 |
4757
|
2262 dim_vector idx_orig_dims = ra_idx.orig_dimensions (); |
4530
|
2263 |
|
2264 if (ra_idx.is_colon ()) |
|
2265 { |
4651
|
2266 // Fast magic colon processing. |
|
2267 |
|
2268 retval = Array<T> (*this, dim_vector (orig_len, 1)); |
4530
|
2269 } |
4651
|
2270 else |
4530
|
2271 { |
5519
|
2272 bool vec_equiv = vector_equivalent (dv); |
|
2273 |
|
2274 if (! vec_equiv |
4651
|
2275 && ! (ra_idx.is_colon () |
5519
|
2276 || (ra_idx.one_zero_only () && idx_orig_dims == dv))) |
5781
|
2277 (*current_liboctave_warning_with_id_handler) |
|
2278 ("Octave:fortran-indexing", "single index used for N-d array"); |
4530
|
2279 |
5519
|
2280 octave_idx_type frozen_len |
|
2281 = ra_idx.freeze (orig_len, "nd-array", resize_ok); |
4530
|
2282 |
|
2283 if (ra_idx) |
4757
|
2284 { |
5519
|
2285 dim_vector result_dims; |
|
2286 |
|
2287 if (vec_equiv) |
|
2288 { |
|
2289 result_dims = dv; |
|
2290 |
|
2291 for (int i = 0; i < n_dims; i++) |
|
2292 { |
|
2293 if (result_dims(i) != 1) |
|
2294 { |
|
2295 // All but this dim should be one. |
|
2296 result_dims(i) = frozen_len; |
|
2297 break; |
|
2298 } |
|
2299 } |
|
2300 } |
|
2301 else |
|
2302 result_dims = idx_orig_dims; |
4530
|
2303 |
|
2304 if (ra_idx.one_zero_only ()) |
|
2305 { |
4651
|
2306 result_dims.resize (2); |
5275
|
2307 octave_idx_type ntot = ra_idx.ones_count (); |
4651
|
2308 result_dims(0) = ntot; |
|
2309 result_dims(1) = (ntot > 0 ? 1 : 0); |
4530
|
2310 } |
|
2311 |
4673
|
2312 result_dims.chop_trailing_singletons (); |
|
2313 |
4530
|
2314 retval.resize (result_dims); |
|
2315 |
5275
|
2316 octave_idx_type n = result_dims.numel (); |
4530
|
2317 |
5275
|
2318 octave_idx_type k = 0; |
|
2319 |
|
2320 for (octave_idx_type i = 0; i < n; i++) |
4530
|
2321 { |
5275
|
2322 octave_idx_type ii = ra_idx.elem (k++); |
4530
|
2323 |
|
2324 if (ii >= orig_len) |
5535
|
2325 retval.elem (i) = rfv; |
4530
|
2326 else |
5535
|
2327 retval.elem (i) = elem (ii); |
4530
|
2328 } |
|
2329 } |
|
2330 } |
|
2331 |
|
2332 return retval; |
|
2333 } |
|
2334 |
|
2335 template <class T> |
|
2336 Array<T> |
4517
|
2337 Array<T>::index (idx_vector& idx_i, idx_vector& idx_j, int resize_ok, |
|
2338 const T& rfv) const |
|
2339 { |
|
2340 Array<T> retval; |
|
2341 |
4548
|
2342 assert (ndims () == 2); |
|
2343 |
5275
|
2344 octave_idx_type nr = dim1 (); |
|
2345 octave_idx_type nc = dim2 (); |
|
2346 |
|
2347 octave_idx_type n = idx_i.freeze (nr, "row", resize_ok); |
|
2348 octave_idx_type m = idx_j.freeze (nc, "column", resize_ok); |
4517
|
2349 |
|
2350 if (idx_i && idx_j) |
|
2351 { |
|
2352 if (idx_i.orig_empty () || idx_j.orig_empty () || n == 0 || m == 0) |
|
2353 { |
|
2354 retval.resize_no_fill (n, m); |
|
2355 } |
|
2356 else if (idx_i.is_colon_equiv (nr) && idx_j.is_colon_equiv (nc)) |
|
2357 { |
|
2358 retval = *this; |
|
2359 } |
|
2360 else |
|
2361 { |
|
2362 retval.resize_no_fill (n, m); |
|
2363 |
5275
|
2364 for (octave_idx_type j = 0; j < m; j++) |
4517
|
2365 { |
5275
|
2366 octave_idx_type jj = idx_j.elem (j); |
|
2367 for (octave_idx_type i = 0; i < n; i++) |
4517
|
2368 { |
5275
|
2369 octave_idx_type ii = idx_i.elem (i); |
4517
|
2370 if (ii >= nr || jj >= nc) |
|
2371 retval.elem (i, j) = rfv; |
|
2372 else |
|
2373 retval.elem (i, j) = elem (ii, jj); |
|
2374 } |
|
2375 } |
|
2376 } |
|
2377 } |
|
2378 |
|
2379 // idx_vector::freeze() printed an error message for us. |
|
2380 |
|
2381 return retval; |
|
2382 } |
|
2383 |
|
2384 template <class T> |
|
2385 Array<T> |
5992
|
2386 Array<T>::index (Array<idx_vector>& ra_idx, int resize_ok, const T& rfv) const |
4517
|
2387 { |
4530
|
2388 // This function handles all calls with more than one idx. |
|
2389 // For (3x3x3), the call can be A(2,5), A(2,:,:), A(3,2,3) etc. |
|
2390 |
4517
|
2391 Array<T> retval; |
|
2392 |
|
2393 int n_dims = dimensions.length (); |
|
2394 |
4737
|
2395 // Remove trailing singletons in ra_idx, but leave at least ndims |
|
2396 // elements. |
|
2397 |
5275
|
2398 octave_idx_type ra_idx_len = ra_idx.length (); |
4737
|
2399 |
4887
|
2400 bool trim_trailing_singletons = true; |
5275
|
2401 for (octave_idx_type j = ra_idx_len; j > n_dims; j--) |
4737
|
2402 { |
4887
|
2403 idx_vector iidx = ra_idx (ra_idx_len-1); |
|
2404 if (iidx.capacity () == 1 && trim_trailing_singletons) |
4737
|
2405 ra_idx_len--; |
|
2406 else |
4887
|
2407 trim_trailing_singletons = false; |
|
2408 |
5992
|
2409 if (! resize_ok) |
|
2410 { |
|
2411 for (octave_idx_type i = 0; i < iidx.capacity (); i++) |
|
2412 if (iidx (i) != 0) |
|
2413 { |
|
2414 (*current_liboctave_error_handler) |
|
2415 ("index exceeds N-d array dimensions"); |
|
2416 |
|
2417 return retval; |
|
2418 } |
|
2419 } |
4737
|
2420 } |
|
2421 |
|
2422 ra_idx.resize (ra_idx_len); |
|
2423 |
4887
|
2424 dim_vector new_dims = dims (); |
|
2425 dim_vector frozen_lengths; |
|
2426 |
|
2427 if (! any_orig_empty (ra_idx) && ra_idx_len < n_dims) |
|
2428 frozen_lengths = short_freeze (ra_idx, dimensions, resize_ok); |
|
2429 else |
4517
|
2430 { |
4887
|
2431 new_dims.resize (ra_idx_len, 1); |
|
2432 frozen_lengths = freeze (ra_idx, new_dims, resize_ok); |
4530
|
2433 } |
|
2434 |
4887
|
2435 if (all_ok (ra_idx)) |
4530
|
2436 { |
4887
|
2437 if (any_orig_empty (ra_idx) || frozen_lengths.any_zero ()) |
|
2438 { |
|
2439 frozen_lengths.chop_trailing_singletons (); |
|
2440 |
|
2441 retval.resize (frozen_lengths); |
|
2442 } |
|
2443 else if (frozen_lengths.length () == n_dims |
|
2444 && all_colon_equiv (ra_idx, dimensions)) |
|
2445 { |
|
2446 retval = *this; |
|
2447 } |
|
2448 else |
4517
|
2449 { |
4887
|
2450 dim_vector frozen_lengths_for_resize = frozen_lengths; |
|
2451 |
|
2452 frozen_lengths_for_resize.chop_trailing_singletons (); |
|
2453 |
|
2454 retval.resize (frozen_lengths_for_resize); |
|
2455 |
5275
|
2456 octave_idx_type n = retval.length (); |
|
2457 |
|
2458 Array<octave_idx_type> result_idx (ra_idx.length (), 0); |
|
2459 |
|
2460 Array<octave_idx_type> elt_idx; |
|
2461 |
|
2462 for (octave_idx_type i = 0; i < n; i++) |
4530
|
2463 { |
4887
|
2464 elt_idx = get_elt_idx (ra_idx, result_idx); |
|
2465 |
5275
|
2466 octave_idx_type numelem_elt = get_scalar_idx (elt_idx, new_dims); |
4887
|
2467 |
5992
|
2468 if (numelem_elt >= length () || numelem_elt < 0) |
|
2469 retval.elem (i) = rfv; |
4887
|
2470 else |
|
2471 retval.elem (i) = elem (numelem_elt); |
|
2472 |
|
2473 increment_index (result_idx, frozen_lengths); |
|
2474 |
4517
|
2475 } |
|
2476 } |
|
2477 } |
|
2478 |
|
2479 return retval; |
|
2480 } |
|
2481 |
5775
|
2482 // FIXME -- this is a mess. |
4517
|
2483 |
|
2484 template <class LT, class RT> |
|
2485 int |
|
2486 assign (Array<LT>& lhs, const Array<RT>& rhs, const LT& rfv) |
|
2487 { |
6388
|
2488 int n_idx = lhs.index_count (); |
|
2489 |
|
2490 // kluge... |
|
2491 if (lhs.ndims () == 0) |
|
2492 lhs.resize_no_fill (0, 0); |
|
2493 |
|
2494 return (lhs.ndims () == 2 |
|
2495 && (n_idx == 1 |
|
2496 || (n_idx < 3 |
|
2497 && rhs.ndims () == 2 |
|
2498 && rhs.rows () == 0 && rhs.columns () == 0))) |
|
2499 ? assign2 (lhs, rhs, rfv) : assignN (lhs, rhs, rfv); |
4517
|
2500 } |
|
2501 |
|
2502 template <class LT, class RT> |
|
2503 int |
|
2504 assign1 (Array<LT>& lhs, const Array<RT>& rhs, const LT& rfv) |
|
2505 { |
|
2506 int retval = 1; |
|
2507 |
|
2508 idx_vector *tmp = lhs.get_idx (); |
|
2509 |
|
2510 idx_vector lhs_idx = tmp[0]; |
|
2511 |
5275
|
2512 octave_idx_type lhs_len = lhs.length (); |
|
2513 octave_idx_type rhs_len = rhs.length (); |
|
2514 |
5781
|
2515 octave_idx_type n = lhs_idx.freeze (lhs_len, "vector", true); |
4517
|
2516 |
|
2517 if (n != 0) |
|
2518 { |
6389
|
2519 dim_vector lhs_dims = lhs.dims (); |
|
2520 |
|
2521 if (lhs_len == 0 && ! lhs_dims.all_zero ()) |
|
2522 { |
|
2523 (*current_liboctave_error_handler) |
|
2524 ("A(I) = X: unable to resize A"); |
|
2525 |
|
2526 retval = 0; |
|
2527 } |
4517
|
2528 else |
|
2529 { |
6392
|
2530 if (rhs_len == n || rhs_len == 1) |
|
2531 { |
|
2532 octave_idx_type max_idx = lhs_idx.max () + 1; |
|
2533 if (max_idx > lhs_len) |
|
2534 lhs.resize_and_fill (max_idx, rfv); |
|
2535 } |
|
2536 |
|
2537 if (rhs_len == n) |
|
2538 { |
|
2539 for (octave_idx_type i = 0; i < n; i++) |
|
2540 { |
|
2541 octave_idx_type ii = lhs_idx.elem (i); |
|
2542 lhs.elem (ii) = rhs.elem (i); |
|
2543 } |
|
2544 } |
|
2545 else if (rhs_len == 1) |
|
2546 { |
|
2547 RT scalar = rhs.elem (0); |
|
2548 |
|
2549 for (octave_idx_type i = 0; i < n; i++) |
|
2550 { |
|
2551 octave_idx_type ii = lhs_idx.elem (i); |
|
2552 lhs.elem (ii) = scalar; |
|
2553 } |
|
2554 } |
|
2555 else |
|
2556 { |
|
2557 (*current_liboctave_error_handler) |
|
2558 ("A(I) = X: X must be a scalar or a vector with same length as I"); |
|
2559 |
|
2560 retval = 0; |
|
2561 } |
4517
|
2562 } |
|
2563 } |
|
2564 else if (lhs_idx.is_colon ()) |
|
2565 { |
6384
|
2566 dim_vector lhs_dims = lhs.dims (); |
|
2567 |
|
2568 if (lhs_dims.all_zero ()) |
4517
|
2569 { |
|
2570 lhs.resize_no_fill (rhs_len); |
|
2571 |
5275
|
2572 for (octave_idx_type i = 0; i < rhs_len; i++) |
4517
|
2573 lhs.elem (i) = rhs.elem (i); |
|
2574 } |
6384
|
2575 else if (rhs_len != 1) |
4517
|
2576 (*current_liboctave_error_handler) |
|
2577 ("A(:) = X: A must be the same size as X"); |
|
2578 } |
|
2579 else if (! (rhs_len == 1 || rhs_len == 0)) |
|
2580 { |
|
2581 (*current_liboctave_error_handler) |
|
2582 ("A([]) = X: X must also be an empty matrix or a scalar"); |
|
2583 |
|
2584 retval = 0; |
|
2585 } |
|
2586 |
|
2587 lhs.clear_index (); |
|
2588 |
|
2589 return retval; |
|
2590 } |
|
2591 |
|
2592 #define MAYBE_RESIZE_LHS \ |
|
2593 do \ |
|
2594 { \ |
5275
|
2595 octave_idx_type max_row_idx = idx_i_is_colon ? rhs_nr : idx_i.max () + 1; \ |
|
2596 octave_idx_type max_col_idx = idx_j_is_colon ? rhs_nc : idx_j.max () + 1; \ |
4517
|
2597 \ |
5275
|
2598 octave_idx_type new_nr = max_row_idx > lhs_nr ? max_row_idx : lhs_nr; \ |
|
2599 octave_idx_type new_nc = max_col_idx > lhs_nc ? max_col_idx : lhs_nc; \ |
4517
|
2600 \ |
|
2601 lhs.resize_and_fill (new_nr, new_nc, rfv); \ |
|
2602 } \ |
|
2603 while (0) |
|
2604 |
|
2605 template <class LT, class RT> |
|
2606 int |
|
2607 assign2 (Array<LT>& lhs, const Array<RT>& rhs, const LT& rfv) |
|
2608 { |
|
2609 int retval = 1; |
|
2610 |
|
2611 int n_idx = lhs.index_count (); |
|
2612 |
5275
|
2613 octave_idx_type lhs_nr = lhs.rows (); |
|
2614 octave_idx_type lhs_nc = lhs.cols (); |
4517
|
2615 |
5047
|
2616 Array<RT> xrhs = rhs; |
|
2617 |
5275
|
2618 octave_idx_type rhs_nr = xrhs.rows (); |
|
2619 octave_idx_type rhs_nc = xrhs.cols (); |
5047
|
2620 |
|
2621 if (xrhs.ndims () > 2) |
4707
|
2622 { |
5047
|
2623 xrhs = xrhs.squeeze (); |
|
2624 |
|
2625 dim_vector dv_tmp = xrhs.dims (); |
4709
|
2626 |
4708
|
2627 switch (dv_tmp.length ()) |
4707
|
2628 { |
4708
|
2629 case 1: |
5775
|
2630 // FIXME -- this case should be unnecessary, because |
5047
|
2631 // squeeze should always return an object with 2 dimensions. |
4708
|
2632 if (rhs_nr == 1) |
|
2633 rhs_nc = dv_tmp.elem (0); |
|
2634 break; |
4709
|
2635 |
4708
|
2636 case 2: |
4707
|
2637 rhs_nr = dv_tmp.elem (0); |
|
2638 rhs_nc = dv_tmp.elem (1); |
4708
|
2639 break; |
|
2640 |
|
2641 default: |
|
2642 (*current_liboctave_error_handler) |
|
2643 ("Array<T>::assign2: Dimension mismatch"); |
4709
|
2644 return 0; |
4707
|
2645 } |
|
2646 } |
4517
|
2647 |
6384
|
2648 bool rhs_is_scalar = rhs_nr == 1 && rhs_nc == 1; |
|
2649 |
4517
|
2650 idx_vector *tmp = lhs.get_idx (); |
|
2651 |
|
2652 idx_vector idx_i; |
|
2653 idx_vector idx_j; |
|
2654 |
|
2655 if (n_idx > 1) |
|
2656 idx_j = tmp[1]; |
|
2657 |
|
2658 if (n_idx > 0) |
|
2659 idx_i = tmp[0]; |
|
2660 |
|
2661 if (n_idx == 2) |
|
2662 { |
5781
|
2663 octave_idx_type n = idx_i.freeze (lhs_nr, "row", true); |
|
2664 octave_idx_type m = idx_j.freeze (lhs_nc, "column", true); |
4517
|
2665 |
|
2666 int idx_i_is_colon = idx_i.is_colon (); |
|
2667 int idx_j_is_colon = idx_j.is_colon (); |
|
2668 |
6384
|
2669 if (lhs_nr == 0 && lhs_nc == 0) |
|
2670 { |
|
2671 if (idx_i_is_colon) |
|
2672 n = rhs_nr; |
|
2673 |
|
2674 if (idx_j_is_colon) |
|
2675 m = rhs_nc; |
|
2676 } |
4517
|
2677 |
|
2678 if (idx_i && idx_j) |
|
2679 { |
|
2680 if (rhs_nr == 0 && rhs_nc == 0) |
|
2681 { |
|
2682 lhs.maybe_delete_elements (idx_i, idx_j); |
|
2683 } |
|
2684 else |
|
2685 { |
6384
|
2686 if (rhs_is_scalar && n >= 0 && m >= 0) |
4517
|
2687 { |
4534
|
2688 // No need to do anything if either of the indices |
|
2689 // are empty. |
|
2690 |
|
2691 if (n > 0 && m > 0) |
4517
|
2692 { |
4534
|
2693 MAYBE_RESIZE_LHS; |
|
2694 |
5047
|
2695 RT scalar = xrhs.elem (0, 0); |
4534
|
2696 |
5275
|
2697 for (octave_idx_type j = 0; j < m; j++) |
4517
|
2698 { |
5275
|
2699 octave_idx_type jj = idx_j.elem (j); |
|
2700 for (octave_idx_type i = 0; i < n; i++) |
4534
|
2701 { |
5275
|
2702 octave_idx_type ii = idx_i.elem (i); |
4534
|
2703 lhs.elem (ii, jj) = scalar; |
|
2704 } |
4517
|
2705 } |
|
2706 } |
|
2707 } |
6072
|
2708 else if ((n == 1 || m == 1) |
|
2709 && (rhs_nr == 1 || rhs_nc == 1) |
|
2710 && n * m == rhs_nr * rhs_nc) |
|
2711 { |
6384
|
2712 MAYBE_RESIZE_LHS; |
|
2713 |
6072
|
2714 if (n > 0 && m > 0) |
|
2715 { |
|
2716 octave_idx_type k = 0; |
|
2717 |
|
2718 for (octave_idx_type j = 0; j < m; j++) |
|
2719 { |
|
2720 octave_idx_type jj = idx_j.elem (j); |
|
2721 for (octave_idx_type i = 0; i < n; i++) |
|
2722 { |
|
2723 octave_idx_type ii = idx_i.elem (i); |
|
2724 lhs.elem (ii, jj) = xrhs.elem (k++); |
|
2725 } |
|
2726 } |
|
2727 } |
|
2728 } |
4517
|
2729 else if (n == rhs_nr && m == rhs_nc) |
|
2730 { |
6384
|
2731 MAYBE_RESIZE_LHS; |
|
2732 |
4517
|
2733 if (n > 0 && m > 0) |
|
2734 { |
5275
|
2735 for (octave_idx_type j = 0; j < m; j++) |
4517
|
2736 { |
5275
|
2737 octave_idx_type jj = idx_j.elem (j); |
|
2738 for (octave_idx_type i = 0; i < n; i++) |
4517
|
2739 { |
5275
|
2740 octave_idx_type ii = idx_i.elem (i); |
5047
|
2741 lhs.elem (ii, jj) = xrhs.elem (i, j); |
4517
|
2742 } |
|
2743 } |
|
2744 } |
|
2745 } |
|
2746 else if (n == 0 && m == 0) |
|
2747 { |
6384
|
2748 if (! (rhs_is_scalar || (rhs_nr == 0 || rhs_nc == 0))) |
4517
|
2749 { |
|
2750 (*current_liboctave_error_handler) |
|
2751 ("A([], []) = X: X must be an empty matrix or a scalar"); |
|
2752 |
|
2753 retval = 0; |
|
2754 } |
|
2755 } |
|
2756 else |
|
2757 { |
|
2758 (*current_liboctave_error_handler) |
|
2759 ("A(I, J) = X: X must be a scalar or the number of elements in I must"); |
|
2760 (*current_liboctave_error_handler) |
|
2761 ("match the number of rows in X and the number of elements in J must"); |
|
2762 (*current_liboctave_error_handler) |
|
2763 ("match the number of columns in X"); |
|
2764 |
|
2765 retval = 0; |
|
2766 } |
|
2767 } |
|
2768 } |
|
2769 // idx_vector::freeze() printed an error message for us. |
|
2770 } |
|
2771 else if (n_idx == 1) |
|
2772 { |
|
2773 int lhs_is_empty = lhs_nr == 0 || lhs_nc == 0; |
|
2774 |
|
2775 if (lhs_is_empty || (lhs_nr == 1 && lhs_nc == 1)) |
|
2776 { |
5275
|
2777 octave_idx_type lhs_len = lhs.length (); |
|
2778 |
6384
|
2779 idx_i.freeze (lhs_len, 0, true); |
4517
|
2780 |
|
2781 if (idx_i) |
|
2782 { |
|
2783 if (rhs_nr == 0 && rhs_nc == 0) |
|
2784 { |
6384
|
2785 lhs.maybe_delete_elements (idx_i); |
4517
|
2786 } |
|
2787 else |
|
2788 { |
5781
|
2789 if (lhs_is_empty |
|
2790 && idx_i.is_colon () |
|
2791 && ! (rhs_nr == 1 || rhs_nc == 1)) |
4517
|
2792 { |
5781
|
2793 (*current_liboctave_warning_with_id_handler) |
|
2794 ("Octave:fortran-indexing", |
|
2795 "A(:) = X: X is not a vector or scalar"); |
|
2796 } |
|
2797 else |
|
2798 { |
|
2799 octave_idx_type idx_nr = idx_i.orig_rows (); |
|
2800 octave_idx_type idx_nc = idx_i.orig_columns (); |
|
2801 |
|
2802 if (! (rhs_nr == idx_nr && rhs_nc == idx_nc)) |
|
2803 (*current_liboctave_warning_with_id_handler) |
|
2804 ("Octave:fortran-indexing", |
|
2805 "A(I) = X: X does not have same shape as I"); |
4517
|
2806 } |
|
2807 |
5047
|
2808 if (assign1 (lhs, xrhs, rfv)) |
4517
|
2809 { |
5275
|
2810 octave_idx_type len = lhs.length (); |
4517
|
2811 |
|
2812 if (len > 0) |
|
2813 { |
|
2814 // The following behavior is much simplified |
|
2815 // over previous versions of Octave. It |
|
2816 // seems to be compatible with Matlab. |
|
2817 |
|
2818 lhs.dimensions = dim_vector (1, lhs.length ()); |
|
2819 } |
|
2820 } |
|
2821 else |
|
2822 retval = 0; |
|
2823 } |
|
2824 } |
|
2825 // idx_vector::freeze() printed an error message for us. |
|
2826 } |
|
2827 else if (lhs_nr == 1) |
|
2828 { |
5781
|
2829 idx_i.freeze (lhs_nc, "vector", true); |
4517
|
2830 |
|
2831 if (idx_i) |
|
2832 { |
|
2833 if (rhs_nr == 0 && rhs_nc == 0) |
|
2834 lhs.maybe_delete_elements (idx_i); |
|
2835 else |
|
2836 { |
5047
|
2837 if (assign1 (lhs, xrhs, rfv)) |
4517
|
2838 lhs.dimensions = dim_vector (1, lhs.length ()); |
|
2839 else |
|
2840 retval = 0; |
|
2841 } |
|
2842 } |
|
2843 // idx_vector::freeze() printed an error message for us. |
|
2844 } |
|
2845 else if (lhs_nc == 1) |
|
2846 { |
5781
|
2847 idx_i.freeze (lhs_nr, "vector", true); |
4517
|
2848 |
|
2849 if (idx_i) |
|
2850 { |
|
2851 if (rhs_nr == 0 && rhs_nc == 0) |
|
2852 lhs.maybe_delete_elements (idx_i); |
|
2853 else |
|
2854 { |
5047
|
2855 if (assign1 (lhs, xrhs, rfv)) |
4517
|
2856 lhs.dimensions = dim_vector (lhs.length (), 1); |
|
2857 else |
|
2858 retval = 0; |
|
2859 } |
|
2860 } |
|
2861 // idx_vector::freeze() printed an error message for us. |
|
2862 } |
|
2863 else |
|
2864 { |
5781
|
2865 if (! (idx_i.is_colon () |
|
2866 || (idx_i.one_zero_only () |
|
2867 && idx_i.orig_rows () == lhs_nr |
|
2868 && idx_i.orig_columns () == lhs_nc))) |
|
2869 (*current_liboctave_warning_with_id_handler) |
|
2870 ("Octave:fortran-indexing", "single index used for matrix"); |
4517
|
2871 |
5275
|
2872 octave_idx_type len = idx_i.freeze (lhs_nr * lhs_nc, "matrix"); |
4517
|
2873 |
|
2874 if (idx_i) |
|
2875 { |
4756
|
2876 if (rhs_nr == 0 && rhs_nc == 0) |
|
2877 lhs.maybe_delete_elements (idx_i); |
|
2878 else if (len == 0) |
4517
|
2879 { |
6384
|
2880 if (! (rhs_is_scalar || (rhs_nr == 0 || rhs_nc == 0))) |
4517
|
2881 (*current_liboctave_error_handler) |
|
2882 ("A([]) = X: X must be an empty matrix or scalar"); |
|
2883 } |
|
2884 else if (len == rhs_nr * rhs_nc) |
|
2885 { |
6384
|
2886 for (octave_idx_type i = 0; i < len; i++) |
4517
|
2887 { |
6384
|
2888 octave_idx_type ii = idx_i.elem (i); |
|
2889 lhs.elem (ii) = xrhs.elem (i); |
4517
|
2890 } |
|
2891 } |
6384
|
2892 else if (rhs_is_scalar) |
4517
|
2893 { |
|
2894 RT scalar = rhs.elem (0, 0); |
|
2895 |
5275
|
2896 for (octave_idx_type i = 0; i < len; i++) |
4517
|
2897 { |
5275
|
2898 octave_idx_type ii = idx_i.elem (i); |
4716
|
2899 lhs.elem (ii) = scalar; |
4517
|
2900 } |
|
2901 } |
|
2902 else |
|
2903 { |
|
2904 (*current_liboctave_error_handler) |
|
2905 ("A(I) = X: X must be a scalar or a matrix with the same size as I"); |
|
2906 |
|
2907 retval = 0; |
|
2908 } |
|
2909 } |
|
2910 // idx_vector::freeze() printed an error message for us. |
|
2911 } |
|
2912 } |
|
2913 else |
|
2914 { |
|
2915 (*current_liboctave_error_handler) |
|
2916 ("invalid number of indices for matrix expression"); |
|
2917 |
|
2918 retval = 0; |
|
2919 } |
|
2920 |
|
2921 lhs.clear_index (); |
|
2922 |
|
2923 return retval; |
|
2924 } |
|
2925 |
|
2926 template <class LT, class RT> |
|
2927 int |
|
2928 assignN (Array<LT>& lhs, const Array<RT>& rhs, const LT& rfv) |
|
2929 { |
|
2930 int retval = 1; |
|
2931 |
4746
|
2932 dim_vector rhs_dims = rhs.dims (); |
|
2933 |
5275
|
2934 octave_idx_type rhs_dims_len = rhs_dims.length (); |
4746
|
2935 |
|
2936 bool rhs_is_scalar = is_scalar (rhs_dims); |
|
2937 |
4517
|
2938 int n_idx = lhs.index_count (); |
|
2939 |
4745
|
2940 idx_vector *idx_vex = lhs.get_idx (); |
|
2941 |
|
2942 Array<idx_vector> idx = conv_to_array (idx_vex, n_idx); |
4517
|
2943 |
4743
|
2944 if (rhs_dims_len == 2 && rhs_dims(0) == 0 && rhs_dims(1) == 0) |
4517
|
2945 { |
|
2946 lhs.maybe_delete_elements (idx, rfv); |
|
2947 } |
5285
|
2948 else if (n_idx == 0) |
|
2949 { |
|
2950 (*current_liboctave_error_handler) |
|
2951 ("invalid number of indices for matrix expression"); |
|
2952 |
|
2953 retval = 0; |
|
2954 } |
4657
|
2955 else if (n_idx == 1) |
4517
|
2956 { |
4657
|
2957 idx_vector iidx = idx(0); |
|
2958 |
5781
|
2959 if (! (iidx.is_colon () |
|
2960 || (iidx.one_zero_only () |
|
2961 && iidx.orig_dimensions () == lhs.dims ()))) |
|
2962 (*current_liboctave_warning_with_id_handler) |
|
2963 ("Octave:fortran-indexing", "single index used for N-d array"); |
4657
|
2964 |
5275
|
2965 octave_idx_type lhs_len = lhs.length (); |
|
2966 |
|
2967 octave_idx_type len = iidx.freeze (lhs_len, "N-d arrray"); |
4657
|
2968 |
|
2969 if (iidx) |
4533
|
2970 { |
4657
|
2971 if (len == 0) |
4656
|
2972 { |
5039
|
2973 if (! (rhs_dims.all_ones () || rhs_dims.any_zero ())) |
4743
|
2974 { |
|
2975 (*current_liboctave_error_handler) |
|
2976 ("A([]) = X: X must be an empty matrix or scalar"); |
|
2977 |
|
2978 retval = 0; |
|
2979 } |
4657
|
2980 } |
|
2981 else if (len == rhs.length ()) |
|
2982 { |
5275
|
2983 for (octave_idx_type i = 0; i < len; i++) |
4656
|
2984 { |
5275
|
2985 octave_idx_type ii = iidx.elem (i); |
4657
|
2986 |
|
2987 lhs.elem (ii) = rhs.elem (i); |
4656
|
2988 } |
|
2989 } |
4716
|
2990 else if (rhs_is_scalar) |
4657
|
2991 { |
|
2992 RT scalar = rhs.elem (0); |
|
2993 |
5275
|
2994 for (octave_idx_type i = 0; i < len; i++) |
4657
|
2995 { |
5275
|
2996 octave_idx_type ii = iidx.elem (i); |
4657
|
2997 |
|
2998 lhs.elem (ii) = scalar; |
|
2999 } |
|
3000 } |
|
3001 else |
|
3002 { |
|
3003 (*current_liboctave_error_handler) |
4702
|
3004 ("A(I) = X: X must be a scalar or a matrix with the same size as I"); |
|
3005 |
4657
|
3006 retval = 0; |
|
3007 } |
|
3008 |
4656
|
3009 // idx_vector::freeze() printed an error message for us. |
4533
|
3010 } |
4702
|
3011 } |
4743
|
3012 else |
4702
|
3013 { |
4746
|
3014 // Maybe expand to more dimensions. |
|
3015 |
|
3016 dim_vector lhs_dims = lhs.dims (); |
|
3017 |
5275
|
3018 octave_idx_type lhs_dims_len = lhs_dims.length (); |
4746
|
3019 |
|
3020 dim_vector final_lhs_dims = lhs_dims; |
|
3021 |
|
3022 dim_vector frozen_len; |
|
3023 |
5275
|
3024 octave_idx_type orig_lhs_dims_len = lhs_dims_len; |
4747
|
3025 |
|
3026 bool orig_empty = lhs_dims.all_zero (); |
|
3027 |
|
3028 if (n_idx < lhs_dims_len) |
4517
|
3029 { |
5052
|
3030 // Collapse dimensions beyond last index. Note that we |
|
3031 // delay resizing LHS until we know that the assignment will |
|
3032 // succeed. |
4747
|
3033 |
5781
|
3034 if (! (idx(n_idx-1).is_colon ())) |
|
3035 (*current_liboctave_warning_with_id_handler) |
|
3036 ("Octave:fortran-indexing", |
|
3037 "fewer indices than dimensions for N-d array"); |
4747
|
3038 |
|
3039 for (int i = n_idx; i < lhs_dims_len; i++) |
|
3040 lhs_dims(n_idx-1) *= lhs_dims(i); |
|
3041 |
|
3042 lhs_dims.resize (n_idx); |
|
3043 |
|
3044 lhs_dims_len = lhs_dims.length (); |
|
3045 } |
|
3046 |
|
3047 // Resize. |
|
3048 |
|
3049 dim_vector new_dims; |
|
3050 new_dims.resize (n_idx); |
|
3051 |
5264
|
3052 if (orig_empty) |
4747
|
3053 { |
5264
|
3054 int k = 0; |
|
3055 for (int i = 0; i < n_idx; i++) |
4746
|
3056 { |
4747
|
3057 // If index is a colon, resizing to RHS dimensions is |
|
3058 // allowed because we started out empty. |
4746
|
3059 |
5264
|
3060 if (idx(i).is_colon ()) |
|
3061 { |
|
3062 if (k < rhs_dims.length ()) |
|
3063 new_dims(i) = rhs_dims(k++); |
|
3064 else |
5379
|
3065 new_dims(i) = 1; |
|
3066 } |
|
3067 else |
|
3068 { |
|
3069 octave_idx_type nelem = idx(i).capacity (); |
|
3070 |
|
3071 if (nelem >= 1 |
6388
|
3072 && ((k < rhs_dims.length () && nelem == rhs_dims(k)) |
|
3073 || rhs_is_scalar)) |
5379
|
3074 k++; |
6388
|
3075 else if (! (nelem == 1 || rhs_is_scalar)) |
5264
|
3076 { |
|
3077 (*current_liboctave_error_handler) |
5379
|
3078 ("A(IDX-LIST) = RHS: mismatched index and RHS dimension"); |
5264
|
3079 return retval; |
|
3080 } |
5379
|
3081 |
6388
|
3082 new_dims(i) = idx(i).orig_empty () ? 0 : idx(i).max () + 1; |
5264
|
3083 } |
4746
|
3084 } |
5264
|
3085 } |
|
3086 else |
|
3087 { |
|
3088 for (int i = 0; i < n_idx; i++) |
4746
|
3089 { |
4747
|
3090 // We didn't start out with all zero dimensions, so if |
|
3091 // index is a colon, it refers to the current LHS |
|
3092 // dimension. Otherwise, it is OK to enlarge to a |
5264
|
3093 // dimension given by the largest index, but if that |
4898
|
3094 // index is a colon the new dimension is singleton. |
4749
|
3095 |
|
3096 if (i < lhs_dims_len |
6481
|
3097 && (idx(i).is_colon () |
|
3098 || idx(i).orig_empty () |
|
3099 || idx(i).max () < lhs_dims(i))) |
4749
|
3100 new_dims(i) = lhs_dims(i); |
|
3101 else if (! idx(i).is_colon ()) |
|
3102 new_dims(i) = idx(i).max () + 1; |
|
3103 else |
4898
|
3104 new_dims(i) = 1; |
4745
|
3105 } |
4747
|
3106 } |
|
3107 |
4749
|
3108 if (retval != 0) |
4747
|
3109 { |
4749
|
3110 if (! orig_empty |
|
3111 && n_idx < orig_lhs_dims_len |
|
3112 && new_dims(n_idx-1) != lhs_dims(n_idx-1)) |
4702
|
3113 { |
4749
|
3114 // We reshaped and the last dimension changed. This has to |
|
3115 // be an error, because we don't know how to undo that |
|
3116 // later... |
|
3117 |
|
3118 (*current_liboctave_error_handler) |
|
3119 ("array index %d (= %d) for assignment requires invalid resizing operation", |
|
3120 n_idx, new_dims(n_idx-1)); |
|
3121 |
|
3122 retval = 0; |
4743
|
3123 } |
|
3124 else |
|
3125 { |
5052
|
3126 // Determine final dimensions for LHS and reset the |
|
3127 // current size of the LHS. Note that we delay actually |
|
3128 // resizing LHS until we know that the assignment will |
|
3129 // succeed. |
|
3130 |
4749
|
3131 if (n_idx < orig_lhs_dims_len) |
4743
|
3132 { |
4749
|
3133 for (int i = 0; i < n_idx-1; i++) |
|
3134 final_lhs_dims(i) = new_dims(i); |
4747
|
3135 } |
|
3136 else |
4749
|
3137 final_lhs_dims = new_dims; |
|
3138 |
5837
|
3139 lhs_dims_len = new_dims.length (); |
|
3140 |
|
3141 frozen_len = freeze (idx, new_dims, true); |
4749
|
3142 |
6141
|
3143 for (int i = 0; i < idx.length (); i++) |
|
3144 { |
|
3145 if (! idx(i)) |
|
3146 { |
|
3147 retval = 0; |
|
3148 lhs.clear_index (); |
|
3149 return retval; |
|
3150 } |
|
3151 } |
|
3152 |
4749
|
3153 if (rhs_is_scalar) |
4747
|
3154 { |
5837
|
3155 if (n_idx < orig_lhs_dims_len) |
|
3156 lhs = lhs.reshape (lhs_dims); |
|
3157 |
5052
|
3158 lhs.resize_and_fill (new_dims, rfv); |
|
3159 |
4747
|
3160 if (! final_lhs_dims.any_zero ()) |
|
3161 { |
4749
|
3162 RT scalar = rhs.elem (0); |
|
3163 |
6388
|
3164 if (n_idx == 1) |
|
3165 { |
|
3166 idx_vector iidx = idx(0); |
|
3167 |
|
3168 octave_idx_type len = frozen_len(0); |
|
3169 |
|
3170 for (octave_idx_type i = 0; i < len; i++) |
|
3171 { |
|
3172 octave_idx_type ii = iidx.elem (i); |
|
3173 |
|
3174 lhs.elem (ii) = scalar; |
|
3175 } |
|
3176 } |
|
3177 else if (lhs_dims_len == 2 && n_idx == 2) |
4747
|
3178 { |
6388
|
3179 idx_vector idx_i = idx(0); |
|
3180 idx_vector idx_j = idx(1); |
|
3181 |
|
3182 octave_idx_type i_len = frozen_len(0); |
|
3183 octave_idx_type j_len = frozen_len(1); |
|
3184 |
|
3185 for (octave_idx_type j = 0; j < j_len; j++) |
|
3186 { |
|
3187 octave_idx_type jj = idx_j.elem (j); |
|
3188 for (octave_idx_type i = 0; i < i_len; i++) |
|
3189 { |
|
3190 octave_idx_type ii = idx_i.elem (i); |
|
3191 lhs.elem (ii, jj) = scalar; |
|
3192 } |
|
3193 } |
|
3194 } |
|
3195 else |
|
3196 { |
|
3197 octave_idx_type n = Array<LT>::get_size (frozen_len); |
|
3198 |
|
3199 Array<octave_idx_type> result_idx (lhs_dims_len, 0); |
|
3200 |
|
3201 for (octave_idx_type i = 0; i < n; i++) |
|
3202 { |
|
3203 Array<octave_idx_type> elt_idx = get_elt_idx (idx, result_idx); |
|
3204 |
|
3205 lhs.elem (elt_idx) = scalar; |
|
3206 |
|
3207 increment_index (result_idx, frozen_len); |
|
3208 } |
4747
|
3209 } |
|
3210 } |
4743
|
3211 } |
4749
|
3212 else |
|
3213 { |
|
3214 // RHS is matrix or higher dimension. |
|
3215 |
5275
|
3216 octave_idx_type n = Array<LT>::get_size (frozen_len); |
5264
|
3217 |
|
3218 if (n != rhs.numel ()) |
4749
|
3219 { |
|
3220 (*current_liboctave_error_handler) |
|
3221 ("A(IDX-LIST) = X: X must be a scalar or size of X must equal number of elements indexed by IDX-LIST"); |
|
3222 |
|
3223 retval = 0; |
|
3224 } |
|
3225 else |
|
3226 { |
5837
|
3227 if (n_idx < orig_lhs_dims_len) |
|
3228 lhs = lhs.reshape (lhs_dims); |
|
3229 |
5052
|
3230 lhs.resize_and_fill (new_dims, rfv); |
|
3231 |
4749
|
3232 if (! final_lhs_dims.any_zero ()) |
|
3233 { |
6388
|
3234 if (n_idx == 1) |
|
3235 { |
|
3236 idx_vector iidx = idx(0); |
|
3237 |
|
3238 octave_idx_type len = frozen_len(0); |
|
3239 |
|
3240 for (octave_idx_type i = 0; i < len; i++) |
|
3241 { |
|
3242 octave_idx_type ii = iidx.elem (i); |
|
3243 |
|
3244 lhs.elem (ii) = rhs.elem (i); |
|
3245 } |
|
3246 } |
|
3247 else if (lhs_dims_len == 2 && n_idx == 2) |
4749
|
3248 { |
6388
|
3249 idx_vector idx_i = idx(0); |
|
3250 idx_vector idx_j = idx(1); |
|
3251 |
|
3252 octave_idx_type i_len = frozen_len(0); |
|
3253 octave_idx_type j_len = frozen_len(1); |
|
3254 octave_idx_type k = 0; |
|
3255 |
|
3256 for (octave_idx_type j = 0; j < j_len; j++) |
|
3257 { |
|
3258 octave_idx_type jj = idx_j.elem (j); |
|
3259 for (octave_idx_type i = 0; i < i_len; i++) |
|
3260 { |
|
3261 octave_idx_type ii = idx_i.elem (i); |
|
3262 lhs.elem (ii, jj) = rhs.elem (k++); |
|
3263 } |
|
3264 } |
|
3265 } |
|
3266 else |
|
3267 { |
|
3268 n = Array<LT>::get_size (frozen_len); |
|
3269 |
|
3270 Array<octave_idx_type> result_idx (lhs_dims_len, 0); |
|
3271 |
|
3272 for (octave_idx_type i = 0; i < n; i++) |
|
3273 { |
|
3274 Array<octave_idx_type> elt_idx = get_elt_idx (idx, result_idx); |
|
3275 |
|
3276 lhs.elem (elt_idx) = rhs.elem (i); |
|
3277 |
|
3278 increment_index (result_idx, frozen_len); |
|
3279 } |
4749
|
3280 } |
|
3281 } |
|
3282 } |
|
3283 } |
4743
|
3284 } |
4517
|
3285 } |
4745
|
3286 |
5632
|
3287 lhs.clear_index (); |
|
3288 |
5052
|
3289 if (retval != 0) |
5516
|
3290 lhs = lhs.reshape (final_lhs_dims); |
4517
|
3291 } |
|
3292 |
5052
|
3293 if (retval != 0) |
|
3294 lhs.chop_trailing_singletons (); |
4757
|
3295 |
4517
|
3296 lhs.clear_index (); |
|
3297 |
|
3298 return retval; |
|
3299 } |
|
3300 |
|
3301 template <class T> |
|
3302 void |
3933
|
3303 Array<T>::print_info (std::ostream& os, const std::string& prefix) const |
|
3304 { |
|
3305 os << prefix << "rep address: " << rep << "\n" |
|
3306 << prefix << "rep->len: " << rep->len << "\n" |
|
3307 << prefix << "rep->data: " << static_cast<void *> (rep->data) << "\n" |
|
3308 << prefix << "rep->count: " << rep->count << "\n"; |
4513
|
3309 |
|
3310 // 2D info: |
|
3311 // |
4657
|
3312 // << pefix << "rows: " << rows () << "\n" |
4513
|
3313 // << prefix << "cols: " << cols () << "\n"; |
3933
|
3314 } |
|
3315 |
237
|
3316 /* |
|
3317 ;;; Local Variables: *** |
|
3318 ;;; mode: C++ *** |
|
3319 ;;; End: *** |
|
3320 */ |