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