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