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