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