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