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 |
1360
|
43 // One dimensional array class. Handles the reference counting for |
|
44 // all the derived classes. |
237
|
45 |
|
46 template <class T> |
1619
|
47 Array<T>::~Array (void) |
|
48 { |
|
49 if (--rep->count <= 0) |
|
50 delete rep; |
|
51 |
|
52 delete [] idx; |
4513
|
53 } |
|
54 |
4532
|
55 template <class T> |
|
56 Array<T> |
|
57 Array<T>::squeeze (void) const |
|
58 { |
|
59 Array<T> retval = *this; |
|
60 |
|
61 bool dims_changed = false; |
|
62 |
|
63 dim_vector new_dimensions = dimensions; |
|
64 |
|
65 int k = 0; |
|
66 |
|
67 for (int i = 0; i < ndims (); i++) |
|
68 { |
|
69 if (dimensions(i) == 1) |
|
70 dims_changed = true; |
|
71 else |
|
72 new_dimensions(k++) = dimensions(i); |
|
73 } |
|
74 |
|
75 if (dims_changed) |
|
76 { |
|
77 if (k == 0) |
|
78 new_dimensions = dim_vector (1); |
|
79 else |
|
80 new_dimensions.resize (k); |
|
81 |
|
82 retval.make_unique (); |
|
83 |
|
84 retval.dimensions = new_dimensions; |
|
85 } |
|
86 |
|
87 return retval; |
|
88 } |
|
89 |
4513
|
90 // A guess (should be quite conservative). |
|
91 #define MALLOC_OVERHEAD 1024 |
|
92 |
|
93 template <class T> |
|
94 int |
|
95 Array<T>::get_size (int r, int c) |
|
96 { |
|
97 // XXX KLUGE XXX |
|
98 |
|
99 // If an allocation of an array with r * c elements of type T |
|
100 // would cause an overflow in the allocator when computing the |
|
101 // size of the allocation, then return a value which, although |
|
102 // not equivalent to the actual request, should be too large for |
|
103 // most current hardware, but not so large to cause the |
|
104 // allocator to barf on computing retval * sizeof (T). |
|
105 |
|
106 static int nl; |
|
107 static double dl |
|
108 = frexp (static_cast<double> |
|
109 (INT_MAX - MALLOC_OVERHEAD) / sizeof (T), &nl); |
|
110 |
|
111 // This value should be an integer. If we return this value and |
|
112 // things work the way we expect, we should be paying a visit to |
|
113 // new_handler in no time flat. |
|
114 static int max_items = static_cast<int> (ldexp (dl, nl)); |
|
115 |
|
116 int nr, nc; |
|
117 double dr = frexp (static_cast<double> (r), &nr); |
|
118 double dc = frexp (static_cast<double> (c), &nc); |
|
119 |
|
120 int nt = nr + nc; |
|
121 double dt = dr * dc; |
|
122 |
4532
|
123 if (dt < 0.5) |
4513
|
124 { |
|
125 nt--; |
|
126 dt *= 2; |
|
127 } |
|
128 |
|
129 return (nt < nl || (nt == nl && dt < dl)) ? r * c : max_items; |
237
|
130 } |
|
131 |
|
132 template <class T> |
4513
|
133 int |
|
134 Array<T>::get_size (int r, int c, int p) |
237
|
135 { |
4513
|
136 // XXX KLUGE XXX |
|
137 |
|
138 // If an allocation of an array with r * c * p elements of type T |
|
139 // would cause an overflow in the allocator when computing the |
|
140 // size of the allocation, then return a value which, although |
|
141 // not equivalent to the actual request, should be too large for |
|
142 // most current hardware, but not so large to cause the |
|
143 // allocator to barf on computing retval * sizeof (T). |
|
144 |
|
145 static int nl; |
|
146 static double dl |
|
147 = frexp (static_cast<double> |
|
148 (INT_MAX - MALLOC_OVERHEAD) / sizeof (T), &nl); |
|
149 |
|
150 // This value should be an integer. If we return this value and |
|
151 // things work the way we expect, we should be paying a visit to |
|
152 // new_handler in no time flat. |
|
153 static int max_items = static_cast<int> (ldexp (dl, nl)); |
|
154 |
|
155 int nr, nc, np; |
|
156 double dr = frexp (static_cast<double> (r), &nr); |
|
157 double dc = frexp (static_cast<double> (c), &nc); |
|
158 double dp = frexp (static_cast<double> (p), &np); |
|
159 |
|
160 int nt = nr + nc + np; |
|
161 double dt = dr * dc * dp; |
|
162 |
4532
|
163 if (dt < 0.5) |
659
|
164 { |
4513
|
165 nt--; |
|
166 dt *= 2; |
237
|
167 |
4532
|
168 if (dt < 0.5) |
|
169 { |
|
170 nt--; |
|
171 dt *= 2; |
|
172 } |
659
|
173 } |
1619
|
174 |
4513
|
175 return (nt < nl || (nt == nl && dt < dl)) ? r * c * p : max_items; |
237
|
176 } |
|
177 |
|
178 template <class T> |
4513
|
179 int |
|
180 Array<T>::get_size (const dim_vector& ra_idx) |
237
|
181 { |
4513
|
182 // XXX KLUGE XXX |
|
183 |
|
184 // If an allocation of an array with r * c elements of type T |
|
185 // would cause an overflow in the allocator when computing the |
|
186 // size of the allocation, then return a value which, although |
|
187 // not equivalent to the actual request, should be too large for |
|
188 // most current hardware, but not so large to cause the |
|
189 // allocator to barf on computing retval * sizeof (T). |
|
190 |
|
191 static int nl; |
|
192 static double dl |
|
193 = frexp (static_cast<double> |
|
194 (INT_MAX - MALLOC_OVERHEAD) / sizeof (T), &nl); |
|
195 |
|
196 // This value should be an integer. If we return this value and |
|
197 // things work the way we expect, we should be paying a visit to |
|
198 // new_handler in no time flat. |
|
199 |
|
200 static int max_items = static_cast<int> (ldexp (dl, nl)); |
|
201 |
|
202 int retval = max_items; |
|
203 |
|
204 int n = ra_idx.length (); |
|
205 |
|
206 int nt = 0; |
|
207 double dt = 1; |
|
208 |
|
209 for (int i = 0; i < n; i++) |
237
|
210 { |
4513
|
211 int nra_idx; |
|
212 double dra_idx = frexp (static_cast<double> (ra_idx(i)), &nra_idx); |
|
213 |
|
214 nt += nra_idx; |
|
215 dt *= dra_idx; |
4532
|
216 |
|
217 if (dt < 0.5) |
|
218 { |
|
219 nt--; |
|
220 dt *= 2; |
|
221 } |
237
|
222 } |
|
223 |
4513
|
224 if (nt < nl || (nt == nl && dt < dl)) |
|
225 { |
|
226 retval = 1; |
|
227 |
|
228 for (int i = 0; i < n; i++) |
|
229 retval *= ra_idx(i); |
|
230 } |
|
231 |
|
232 return retval; |
237
|
233 } |
|
234 |
4513
|
235 #undef MALLOC_OVERHEAD |
|
236 |
237
|
237 template <class T> |
4513
|
238 int |
|
239 Array<T>::compute_index (const Array<int>& ra_idx) const |
237
|
240 { |
4513
|
241 int retval = -1; |
|
242 |
|
243 int n = dimensions.length (); |
|
244 |
|
245 if (n > 0 && n == ra_idx.length ()) |
237
|
246 { |
4513
|
247 retval = ra_idx(--n); |
237
|
248 |
4513
|
249 while (--n >= 0) |
|
250 { |
|
251 retval *= dimensions(n); |
|
252 retval += ra_idx(n); |
|
253 } |
|
254 } |
|
255 else |
|
256 (*current_liboctave_error_handler) |
|
257 ("Array<T>::compute_index: invalid ra_idxing operation"); |
237
|
258 |
4513
|
259 return retval; |
237
|
260 } |
|
261 |
2049
|
262 template <class T> |
|
263 T |
2109
|
264 Array<T>::range_error (const char *fcn, int n) const |
2049
|
265 { |
2109
|
266 (*current_liboctave_error_handler) ("%s (%d): range error", fcn, n); |
2049
|
267 return T (); |
|
268 } |
|
269 |
|
270 template <class T> |
|
271 T& |
2109
|
272 Array<T>::range_error (const char *fcn, int n) |
2049
|
273 { |
2109
|
274 (*current_liboctave_error_handler) ("%s (%d): range error", fcn, n); |
2049
|
275 static T foo; |
|
276 return foo; |
|
277 } |
|
278 |
3933
|
279 template <class T> |
4513
|
280 T |
|
281 Array<T>::range_error (const char *fcn, int i, int j) const |
|
282 { |
|
283 (*current_liboctave_error_handler) |
|
284 ("%s (%d, %d): range error", fcn, i, j); |
|
285 return T (); |
|
286 } |
|
287 |
|
288 template <class T> |
|
289 T& |
|
290 Array<T>::range_error (const char *fcn, int i, int j) |
|
291 { |
|
292 (*current_liboctave_error_handler) |
|
293 ("%s (%d, %d): range error", fcn, i, j); |
|
294 static T foo; |
|
295 return foo; |
|
296 } |
|
297 |
|
298 template <class T> |
|
299 T |
|
300 Array<T>::range_error (const char *fcn, int i, int j, int k) const |
|
301 { |
|
302 (*current_liboctave_error_handler) |
|
303 ("%s (%d, %d, %d): range error", fcn, i, j, k); |
|
304 return T (); |
|
305 } |
|
306 |
|
307 template <class T> |
|
308 T& |
|
309 Array<T>::range_error (const char *fcn, int i, int j, int k) |
|
310 { |
|
311 (*current_liboctave_error_handler) |
|
312 ("%s (%d, %d, %d): range error", fcn, i, j, k); |
|
313 static T foo; |
|
314 return foo; |
|
315 } |
|
316 |
|
317 template <class T> |
|
318 T |
|
319 Array<T>::range_error (const char *fcn, const Array<int>& ra_idx) const |
|
320 { |
|
321 // XXX FIXME XXX -- report index values too! |
|
322 |
|
323 (*current_liboctave_error_handler) ("range error in Array"); |
|
324 |
|
325 return T (); |
|
326 } |
|
327 |
|
328 template <class T> |
|
329 T& |
|
330 Array<T>::range_error (const char *fcn, const Array<int>& ra_idx) |
|
331 { |
|
332 // XXX FIXME XXX -- report index values too! |
|
333 |
|
334 (*current_liboctave_error_handler) ("range error in Array"); |
|
335 |
|
336 static T foo; |
|
337 return foo; |
|
338 } |
|
339 |
|
340 template <class T> |
4567
|
341 Array<T> |
|
342 Array<T>::reshape (const dim_vector& new_dims) const |
|
343 { |
|
344 Array<T> retval; |
|
345 |
|
346 if (dimensions != new_dims) |
|
347 { |
|
348 if (dimensions.numel () == new_dims.numel ()) |
|
349 retval = Array<T> (*this, new_dims); |
|
350 else |
|
351 (*current_liboctave_error_handler) ("reshape: size mismatch"); |
|
352 } |
|
353 |
|
354 return retval; |
|
355 } |
|
356 |
|
357 template <class T> |
4513
|
358 void |
|
359 Array<T>::resize_no_fill (int n) |
|
360 { |
|
361 if (n < 0) |
|
362 { |
|
363 (*current_liboctave_error_handler) |
|
364 ("can't resize to negative dimension"); |
|
365 return; |
|
366 } |
|
367 |
|
368 if (n == length ()) |
|
369 return; |
|
370 |
|
371 typename Array<T>::ArrayRep *old_rep = rep; |
|
372 const T *old_data = data (); |
|
373 int old_len = length (); |
|
374 |
|
375 rep = new typename Array<T>::ArrayRep (n); |
|
376 |
|
377 dimensions = dim_vector (n); |
|
378 |
|
379 if (old_data && old_len > 0) |
|
380 { |
|
381 int min_len = old_len < n ? old_len : n; |
|
382 |
|
383 for (int i = 0; i < min_len; i++) |
|
384 xelem (i) = old_data[i]; |
|
385 } |
|
386 |
|
387 if (--old_rep->count <= 0) |
|
388 delete old_rep; |
|
389 } |
|
390 |
|
391 template <class T> |
|
392 void |
|
393 Array<T>::resize_no_fill (const dim_vector& dims) |
|
394 { |
|
395 int n = dims.length (); |
|
396 |
|
397 for (int i = 0; i < n; i++) |
|
398 { |
|
399 if (dims(i) < 0) |
|
400 { |
|
401 (*current_liboctave_error_handler) |
|
402 ("can't resize to negative dimension"); |
|
403 return; |
|
404 } |
|
405 } |
|
406 |
4548
|
407 bool same_size = true; |
|
408 |
|
409 if (dimensions.length () != n) |
|
410 { |
|
411 same_size = false; |
|
412 } |
|
413 else |
4513
|
414 { |
4548
|
415 for (int i = 0; i < n; i++) |
4513
|
416 { |
4548
|
417 if (dims(i) != dimensions(i)) |
|
418 { |
|
419 same_size = false; |
|
420 break; |
|
421 } |
4513
|
422 } |
|
423 } |
|
424 |
4548
|
425 if (same_size) |
4513
|
426 return; |
|
427 |
|
428 int old_len = length (); |
|
429 |
|
430 typename Array<T>::ArrayRep *old_rep = rep; |
|
431 const T *old_data = data (); |
|
432 |
|
433 rep = new typename Array<T>::ArrayRep (get_size (dims)); |
|
434 |
|
435 dimensions = dims; |
|
436 |
|
437 Array<int> ra_idx (dimensions.length (), 0); |
|
438 |
|
439 for (int i = 0; i < old_len; i++) |
|
440 { |
|
441 if (index_in_bounds (ra_idx, dimensions)) |
|
442 xelem (ra_idx) = old_data[i]; |
|
443 |
|
444 increment_index (ra_idx, dimensions); |
|
445 } |
|
446 |
|
447 if (--old_rep->count <= 0) |
|
448 delete old_rep; |
|
449 } |
|
450 |
|
451 template <class T> |
|
452 void |
|
453 Array<T>::resize_no_fill (int r, int c) |
|
454 { |
|
455 if (r < 0 || c < 0) |
|
456 { |
|
457 (*current_liboctave_error_handler) |
|
458 ("can't resize to negative dimension"); |
|
459 return; |
|
460 } |
|
461 |
4548
|
462 int n = ndims (); |
|
463 |
|
464 if (n == 0) |
|
465 dimensions = dim_vector (0, 0); |
|
466 |
|
467 assert (ndims () == 2); |
|
468 |
4513
|
469 if (r == dim1 () && c == dim2 ()) |
|
470 return; |
|
471 |
|
472 typename Array<T>::ArrayRep *old_rep = Array<T>::rep; |
|
473 const T *old_data = data (); |
|
474 |
|
475 int old_d1 = dim1 (); |
|
476 int old_d2 = dim2 (); |
|
477 int old_len = length (); |
|
478 |
|
479 rep = new typename Array<T>::ArrayRep (get_size (r, c)); |
|
480 |
|
481 dimensions = dim_vector (r, c); |
|
482 |
|
483 if (old_data && old_len > 0) |
|
484 { |
|
485 int min_r = old_d1 < r ? old_d1 : r; |
|
486 int min_c = old_d2 < c ? old_d2 : c; |
|
487 |
|
488 for (int j = 0; j < min_c; j++) |
|
489 for (int i = 0; i < min_r; i++) |
|
490 xelem (i, j) = old_data[old_d1*j+i]; |
|
491 } |
|
492 |
|
493 if (--old_rep->count <= 0) |
|
494 delete old_rep; |
|
495 } |
|
496 |
|
497 template <class T> |
|
498 void |
|
499 Array<T>::resize_no_fill (int r, int c, int p) |
|
500 { |
|
501 if (r < 0 || c < 0 || p < 0) |
|
502 { |
|
503 (*current_liboctave_error_handler) |
|
504 ("can't resize to negative dimension"); |
|
505 return; |
|
506 } |
|
507 |
4548
|
508 int n = ndims (); |
|
509 |
|
510 if (n == 0) |
|
511 dimensions = dim_vector (0, 0, 0); |
|
512 |
|
513 assert (ndims () == 3); |
|
514 |
4513
|
515 if (r == dim1 () && c == dim2 () && p == dim3 ()) |
|
516 return; |
|
517 |
|
518 typename Array<T>::ArrayRep *old_rep = rep; |
|
519 const T *old_data = data (); |
|
520 |
|
521 int old_d1 = dim1 (); |
|
522 int old_d2 = dim2 (); |
|
523 int old_d3 = dim3 (); |
|
524 int old_len = length (); |
|
525 |
|
526 int ts = get_size (get_size (r, c), p); |
|
527 |
|
528 rep = new typename Array<T>::ArrayRep (ts); |
|
529 |
|
530 dimensions = dim_vector (r, c, p); |
|
531 |
|
532 if (old_data && old_len > 0) |
|
533 { |
|
534 int min_r = old_d1 < r ? old_d1 : r; |
|
535 int min_c = old_d2 < c ? old_d2 : c; |
|
536 int min_p = old_d3 < p ? old_d3 : p; |
|
537 |
|
538 for (int k = 0; k < min_p; k++) |
|
539 for (int j = 0; j < min_c; j++) |
|
540 for (int i = 0; i < min_r; i++) |
|
541 xelem (i, j, k) = old_data[old_d1*(old_d2*k+j)+i]; |
|
542 } |
|
543 |
|
544 if (--old_rep->count <= 0) |
|
545 delete old_rep; |
|
546 } |
|
547 |
|
548 template <class T> |
|
549 void |
|
550 Array<T>::resize_and_fill (int n, const T& val) |
|
551 { |
|
552 if (n < 0) |
|
553 { |
|
554 (*current_liboctave_error_handler) |
|
555 ("can't resize to negative dimension"); |
|
556 return; |
|
557 } |
|
558 |
|
559 if (n == length ()) |
|
560 return; |
|
561 |
|
562 typename Array<T>::ArrayRep *old_rep = rep; |
|
563 const T *old_data = data (); |
|
564 int old_len = length (); |
|
565 |
|
566 rep = new typename Array<T>::ArrayRep (n); |
|
567 |
|
568 dimensions = dim_vector (n); |
|
569 |
|
570 int min_len = old_len < n ? old_len : n; |
|
571 |
|
572 if (old_data && old_len > 0) |
|
573 { |
|
574 for (int i = 0; i < min_len; i++) |
|
575 xelem (i) = old_data[i]; |
|
576 } |
|
577 |
|
578 for (int i = old_len; i < n; i++) |
|
579 xelem (i) = val; |
|
580 |
|
581 if (--old_rep->count <= 0) |
|
582 delete old_rep; |
|
583 } |
|
584 |
|
585 template <class T> |
|
586 void |
|
587 Array<T>::resize_and_fill (int r, int c, const T& val) |
|
588 { |
|
589 if (r < 0 || c < 0) |
|
590 { |
|
591 (*current_liboctave_error_handler) |
|
592 ("can't resize to negative dimension"); |
|
593 return; |
|
594 } |
|
595 |
4548
|
596 if (ndims () == 0) |
|
597 dimensions = dim_vector (0, 0); |
|
598 |
|
599 assert (ndims () == 2); |
|
600 |
4513
|
601 if (r == dim1 () && c == dim2 ()) |
|
602 return; |
|
603 |
|
604 typename Array<T>::ArrayRep *old_rep = Array<T>::rep; |
|
605 const T *old_data = data (); |
|
606 |
|
607 int old_d1 = dim1 (); |
|
608 int old_d2 = dim2 (); |
|
609 int old_len = length (); |
|
610 |
|
611 rep = new typename Array<T>::ArrayRep (get_size (r, c)); |
|
612 |
|
613 dimensions = dim_vector (r, c); |
|
614 |
|
615 int min_r = old_d1 < r ? old_d1 : r; |
|
616 int min_c = old_d2 < c ? old_d2 : c; |
|
617 |
|
618 if (old_data && old_len > 0) |
|
619 { |
|
620 for (int j = 0; j < min_c; j++) |
|
621 for (int i = 0; i < min_r; i++) |
|
622 xelem (i, j) = old_data[old_d1*j+i]; |
|
623 } |
|
624 |
|
625 for (int j = 0; j < min_c; j++) |
|
626 for (int i = min_r; i < r; i++) |
|
627 xelem (i, j) = val; |
|
628 |
|
629 for (int j = min_c; j < c; j++) |
|
630 for (int i = 0; i < r; i++) |
|
631 xelem (i, j) = val; |
|
632 |
|
633 if (--old_rep->count <= 0) |
|
634 delete old_rep; |
|
635 } |
|
636 |
|
637 template <class T> |
|
638 void |
|
639 Array<T>::resize_and_fill (int r, int c, int p, const T& val) |
|
640 { |
|
641 if (r < 0 || c < 0 || p < 0) |
|
642 { |
|
643 (*current_liboctave_error_handler) |
|
644 ("can't resize to negative dimension"); |
|
645 return; |
|
646 } |
|
647 |
4548
|
648 if (ndims () == 0) |
|
649 dimensions = dim_vector (0, 0, 0); |
|
650 |
|
651 assert (ndims () == 3); |
|
652 |
4513
|
653 if (r == dim1 () && c == dim2 () && p == dim3 ()) |
|
654 return; |
|
655 |
|
656 typename Array<T>::ArrayRep *old_rep = rep; |
|
657 const T *old_data = data (); |
|
658 |
|
659 int old_d1 = dim1 (); |
|
660 int old_d2 = dim2 (); |
|
661 int old_d3 = dim3 (); |
|
662 |
|
663 int old_len = length (); |
|
664 |
|
665 int ts = get_size (get_size (r, c), p); |
|
666 |
|
667 rep = new typename Array<T>::ArrayRep (ts); |
|
668 |
|
669 dimensions = dim_vector (r, c, p); |
|
670 |
|
671 int min_r = old_d1 < r ? old_d1 : r; |
|
672 int min_c = old_d2 < c ? old_d2 : c; |
|
673 int min_p = old_d3 < p ? old_d3 : p; |
|
674 |
|
675 if (old_data && old_len > 0) |
|
676 for (int k = 0; k < min_p; k++) |
|
677 for (int j = 0; j < min_c; j++) |
|
678 for (int i = 0; i < min_r; i++) |
|
679 xelem (i, j, k) = old_data[old_d1*(old_d2*k+j)+i]; |
|
680 |
|
681 // XXX FIXME XXX -- if the copy constructor is expensive, this may |
|
682 // win. Otherwise, it may make more sense to just copy the value |
|
683 // everywhere when making the new ArrayRep. |
|
684 |
|
685 for (int k = 0; k < min_p; k++) |
|
686 for (int j = min_c; j < c; j++) |
|
687 for (int i = 0; i < min_r; i++) |
|
688 xelem (i, j, k) = val; |
|
689 |
|
690 for (int k = 0; k < min_p; k++) |
|
691 for (int j = 0; j < c; j++) |
|
692 for (int i = min_r; i < r; i++) |
|
693 xelem (i, j, k) = val; |
|
694 |
|
695 for (int k = min_p; k < p; k++) |
|
696 for (int j = 0; j < c; j++) |
|
697 for (int i = 0; i < r; i++) |
|
698 xelem (i, j, k) = val; |
|
699 |
|
700 if (--old_rep->count <= 0) |
|
701 delete old_rep; |
|
702 } |
|
703 |
|
704 template <class T> |
|
705 void |
|
706 Array<T>::resize_and_fill (const dim_vector& dims, const T& val) |
|
707 { |
|
708 int n = dims.length (); |
|
709 |
|
710 for (int i = 0; i < n; i++) |
|
711 { |
|
712 if (dims(i) < 0) |
|
713 { |
|
714 (*current_liboctave_error_handler) |
|
715 ("can't resize to negative dimension"); |
|
716 return; |
|
717 } |
|
718 } |
|
719 |
4553
|
720 bool same_size = true; |
|
721 |
|
722 if (dimensions.length () != n) |
|
723 { |
|
724 same_size = false; |
|
725 } |
|
726 else |
4513
|
727 { |
4553
|
728 for (int i = 0; i < n; i++) |
4513
|
729 { |
4553
|
730 if (dims(i) != dimensions(i)) |
|
731 { |
|
732 same_size = false; |
|
733 break; |
|
734 } |
4513
|
735 } |
|
736 } |
|
737 |
4553
|
738 if (same_size) |
4513
|
739 return; |
|
740 |
|
741 typename Array<T>::ArrayRep *old_rep = rep; |
|
742 const T *old_data = data (); |
|
743 |
|
744 int old_len = length (); |
|
745 |
|
746 int len = get_size (dims); |
|
747 |
|
748 rep = new typename Array<T>::ArrayRep (len); |
|
749 |
|
750 dimensions = dims; |
|
751 |
|
752 Array<int> ra_idx (dimensions.length (), 0); |
|
753 |
|
754 // XXX FIXME XXX -- it is much simpler to fill the whole array |
|
755 // first, but probably slower for large arrays, or if the assignment |
|
756 // operator for the type T is expensive. OTOH, the logic for |
|
757 // deciding whether an element needs the copied value or the filled |
|
758 // value might be more expensive. |
|
759 |
|
760 for (int i = 0; i < len; i++) |
|
761 rep->elem (i) = val; |
|
762 |
|
763 for (int i = 0; i < old_len; i++) |
|
764 { |
|
765 if (index_in_bounds (ra_idx, dimensions)) |
|
766 xelem (ra_idx) = old_data[i]; |
|
767 |
|
768 increment_index (ra_idx, dimensions); |
|
769 } |
|
770 |
|
771 if (--old_rep->count <= 0) |
|
772 delete old_rep; |
|
773 } |
|
774 |
|
775 template <class T> |
|
776 Array<T>& |
|
777 Array<T>::insert (const Array<T>& a, int r, int c) |
|
778 { |
|
779 int a_rows = a.rows (); |
|
780 int a_cols = a.cols (); |
|
781 |
|
782 if (r < 0 || r + a_rows > rows () || c < 0 || c + a_cols > cols ()) |
|
783 { |
|
784 (*current_liboctave_error_handler) ("range error for insert"); |
|
785 return *this; |
|
786 } |
|
787 |
|
788 for (int j = 0; j < a_cols; j++) |
|
789 for (int i = 0; i < a_rows; i++) |
|
790 elem (r+i, c+j) = a.elem (i, j); |
|
791 |
|
792 return *this; |
|
793 } |
|
794 |
|
795 template <class T> |
|
796 Array<T>& |
|
797 Array<T>::insert (const Array<T>& a, const Array<int>& ra_idx) |
|
798 { |
|
799 int n = ra_idx.length (); |
|
800 |
|
801 if (n == dimensions.length ()) |
|
802 { |
|
803 dim_vector a_dims = a.dims (); |
|
804 |
|
805 for (int i = 0; i < n; i++) |
|
806 { |
|
807 if (ra_idx(i) < 0 || ra_idx(i) + a_dims(i) > dimensions(i)) |
|
808 { |
|
809 (*current_liboctave_error_handler) |
|
810 ("Array<T>::insert: range error for insert"); |
|
811 return *this; |
|
812 } |
|
813 } |
|
814 |
|
815 #if 0 |
|
816 // XXX FIXME XXX -- need to copy elements |
|
817 |
|
818 for (int j = 0; j < a_cols; j++) |
|
819 for (int i = 0; i < a_rows; i++) |
|
820 elem (r+i, c+j) = a.elem (i, j); |
|
821 #endif |
|
822 |
|
823 } |
|
824 else |
|
825 (*current_liboctave_error_handler) |
|
826 ("Array<T>::insert: invalid indexing operation"); |
|
827 |
|
828 return *this; |
|
829 } |
|
830 |
|
831 template <class T> |
|
832 Array<T> |
|
833 Array<T>::transpose (void) const |
|
834 { |
4548
|
835 assert (ndims () == 2); |
|
836 |
4513
|
837 int nr = dim1 (); |
|
838 int nc = dim2 (); |
|
839 |
|
840 if (nr > 1 && nc > 1) |
|
841 { |
|
842 Array<T> result (dim_vector (nc, nr)); |
|
843 |
|
844 for (int j = 0; j < nc; j++) |
|
845 for (int i = 0; i < nr; i++) |
|
846 result.xelem (j, i) = xelem (i, j); |
|
847 |
|
848 return result; |
|
849 } |
|
850 else |
|
851 { |
|
852 // Fast transpose for vectors and empty matrices |
|
853 return Array<T> (*this, dim_vector (nc, nr)); |
|
854 } |
|
855 } |
|
856 |
|
857 template <class T> |
|
858 T * |
|
859 Array<T>::fortran_vec (void) |
|
860 { |
|
861 if (rep->count > 1) |
|
862 { |
|
863 --rep->count; |
|
864 rep = new typename Array<T>::ArrayRep (*rep); |
|
865 } |
|
866 return rep->data; |
|
867 } |
|
868 |
|
869 template <class T> |
3933
|
870 void |
4517
|
871 Array<T>::maybe_delete_dims (void) |
|
872 { |
|
873 int ndims = dimensions.length (); |
|
874 |
|
875 dim_vector new_dims (1, 1); |
|
876 |
|
877 bool delete_dims = true; |
|
878 |
|
879 for (int i = ndims - 1; i >= 0; i--) |
|
880 { |
|
881 if (delete_dims) |
|
882 { |
|
883 if (dimensions(i) != 1) |
|
884 { |
|
885 delete_dims = false; |
|
886 |
|
887 new_dims = dim_vector (i + 1, dimensions(i)); |
|
888 } |
|
889 } |
|
890 else |
|
891 new_dims(i) = dimensions(i); |
|
892 } |
4530
|
893 |
4517
|
894 if (ndims != new_dims.length ()) |
|
895 dimensions = new_dims; |
|
896 } |
|
897 |
|
898 template <class T> |
|
899 void |
|
900 Array<T>::clear_index (void) |
|
901 { |
|
902 delete [] idx; |
|
903 idx = 0; |
|
904 idx_count = 0; |
|
905 } |
|
906 |
|
907 template <class T> |
|
908 void |
|
909 Array<T>::set_index (const idx_vector& idx_arg) |
|
910 { |
|
911 int nd = ndims (); |
|
912 |
|
913 if (! idx && nd > 0) |
|
914 idx = new idx_vector [nd]; |
|
915 |
|
916 if (idx_count < nd) |
|
917 { |
|
918 idx[idx_count++] = idx_arg; |
|
919 } |
|
920 else |
|
921 { |
|
922 idx_vector *new_idx = new idx_vector [idx_count+1]; |
|
923 |
|
924 for (int i = 0; i < idx_count; i++) |
|
925 new_idx[i] = idx[i]; |
|
926 |
|
927 new_idx[idx_count++] = idx_arg; |
|
928 |
|
929 delete [] idx; |
|
930 |
|
931 idx = new_idx; |
|
932 } |
|
933 } |
|
934 |
|
935 template <class T> |
|
936 void |
|
937 Array<T>::maybe_delete_elements (idx_vector& idx_arg) |
|
938 { |
|
939 switch (ndims ()) |
|
940 { |
|
941 case 1: |
|
942 maybe_delete_elements_1 (idx_arg); |
|
943 break; |
|
944 |
|
945 case 2: |
|
946 maybe_delete_elements_2 (idx_arg); |
|
947 break; |
|
948 |
|
949 default: |
|
950 (*current_liboctave_error_handler) |
|
951 ("Array<T>::maybe_delete_elements: invalid operation"); |
|
952 break; |
|
953 } |
|
954 } |
|
955 |
|
956 template <class T> |
|
957 void |
|
958 Array<T>::maybe_delete_elements_1 (idx_vector& idx_arg) |
|
959 { |
|
960 int len = length (); |
|
961 |
|
962 if (len == 0) |
|
963 return; |
|
964 |
|
965 if (idx_arg.is_colon_equiv (len, 1)) |
|
966 resize_no_fill (0); |
|
967 else |
|
968 { |
|
969 int num_to_delete = idx_arg.length (len); |
|
970 |
|
971 if (num_to_delete != 0) |
|
972 { |
|
973 int new_len = len; |
|
974 |
|
975 int iidx = 0; |
|
976 |
|
977 for (int i = 0; i < len; i++) |
|
978 if (i == idx_arg.elem (iidx)) |
|
979 { |
|
980 iidx++; |
|
981 new_len--; |
|
982 |
|
983 if (iidx == num_to_delete) |
|
984 break; |
|
985 } |
|
986 |
|
987 if (new_len > 0) |
|
988 { |
|
989 T *new_data = new T [new_len]; |
|
990 |
|
991 int ii = 0; |
|
992 iidx = 0; |
|
993 for (int i = 0; i < len; i++) |
|
994 { |
|
995 if (iidx < num_to_delete && i == idx_arg.elem (iidx)) |
|
996 iidx++; |
|
997 else |
|
998 { |
|
999 new_data[ii] = elem (i); |
|
1000 ii++; |
|
1001 } |
|
1002 } |
|
1003 |
|
1004 if (--rep->count <= 0) |
|
1005 delete rep; |
|
1006 |
|
1007 rep = new typename Array<T>::ArrayRep (new_data, new_len); |
|
1008 |
|
1009 dimensions.resize (1); |
|
1010 dimensions(0) = new_len; |
|
1011 } |
|
1012 else |
|
1013 (*current_liboctave_error_handler) |
|
1014 ("A(idx) = []: index out of range"); |
|
1015 } |
|
1016 } |
|
1017 } |
|
1018 |
|
1019 template <class T> |
|
1020 void |
|
1021 Array<T>::maybe_delete_elements_2 (idx_vector& idx_arg) |
|
1022 { |
4548
|
1023 assert (ndims () == 2); |
|
1024 |
4517
|
1025 int nr = dim1 (); |
|
1026 int nc = dim2 (); |
|
1027 |
|
1028 if (nr == 0 && nc == 0) |
|
1029 return; |
|
1030 |
|
1031 int n; |
|
1032 if (nr == 1) |
|
1033 n = nc; |
|
1034 else if (nc == 1) |
|
1035 n = nr; |
|
1036 else |
|
1037 { |
|
1038 (*current_liboctave_error_handler) |
|
1039 ("A(idx) = []: expecting A to be row or column vector or scalar"); |
|
1040 |
|
1041 return; |
|
1042 } |
|
1043 |
|
1044 if (idx_arg.is_colon_equiv (n, 1)) |
|
1045 { |
|
1046 // Either A(:) = [] or A(idx) = [] with idx enumerating all |
|
1047 // elements, so we delete all elements and return [](0x0). To |
|
1048 // preserve the orientation of the vector, you have to use |
|
1049 // A(idx,:) = [] (delete rows) or A(:,idx) (delete columns). |
|
1050 |
|
1051 resize_no_fill (0, 0); |
|
1052 return; |
|
1053 } |
|
1054 |
|
1055 idx_arg.sort (true); |
|
1056 |
|
1057 int num_to_delete = idx_arg.length (n); |
|
1058 |
|
1059 if (num_to_delete != 0) |
|
1060 { |
|
1061 int new_n = n; |
|
1062 |
|
1063 int iidx = 0; |
|
1064 |
|
1065 for (int i = 0; i < n; i++) |
|
1066 if (i == idx_arg.elem (iidx)) |
|
1067 { |
|
1068 iidx++; |
|
1069 new_n--; |
|
1070 |
|
1071 if (iidx == num_to_delete) |
|
1072 break; |
|
1073 } |
|
1074 |
|
1075 if (new_n > 0) |
|
1076 { |
|
1077 T *new_data = new T [new_n]; |
|
1078 |
|
1079 int ii = 0; |
|
1080 iidx = 0; |
|
1081 for (int i = 0; i < n; i++) |
|
1082 { |
|
1083 if (iidx < num_to_delete && i == idx_arg.elem (iidx)) |
|
1084 iidx++; |
|
1085 else |
|
1086 { |
|
1087 if (nr == 1) |
|
1088 new_data[ii] = elem (0, i); |
|
1089 else |
|
1090 new_data[ii] = elem (i, 0); |
|
1091 |
|
1092 ii++; |
|
1093 } |
|
1094 } |
|
1095 |
|
1096 if (--(Array<T>::rep)->count <= 0) |
|
1097 delete Array<T>::rep; |
|
1098 |
|
1099 Array<T>::rep = new typename Array<T>::ArrayRep (new_data, new_n); |
|
1100 |
|
1101 dimensions.resize (2); |
|
1102 |
|
1103 if (nr == 1) |
|
1104 { |
|
1105 dimensions(0) = 1; |
|
1106 dimensions(1) = new_n; |
|
1107 } |
|
1108 else |
|
1109 { |
|
1110 dimensions(0) = new_n; |
|
1111 dimensions(1) = 1; |
|
1112 } |
|
1113 } |
|
1114 else |
|
1115 (*current_liboctave_error_handler) |
|
1116 ("A(idx) = []: index out of range"); |
|
1117 } |
|
1118 } |
|
1119 |
|
1120 template <class T> |
|
1121 void |
|
1122 Array<T>::maybe_delete_elements (idx_vector& idx_i, idx_vector& idx_j) |
|
1123 { |
4548
|
1124 assert (ndims () == 2); |
|
1125 |
4517
|
1126 int nr = dim1 (); |
|
1127 int nc = dim2 (); |
|
1128 |
|
1129 if (nr == 0 && nc == 0) |
|
1130 return; |
|
1131 |
|
1132 if (idx_i.is_colon ()) |
|
1133 { |
|
1134 if (idx_j.is_colon ()) |
|
1135 { |
|
1136 // A(:,:) -- We are deleting columns and rows, so the result |
|
1137 // is [](0x0). |
|
1138 |
|
1139 resize_no_fill (0, 0); |
|
1140 return; |
|
1141 } |
|
1142 |
|
1143 if (idx_j.is_colon_equiv (nc, 1)) |
|
1144 { |
|
1145 // A(:,j) -- We are deleting columns by enumerating them, |
|
1146 // If we enumerate all of them, we should have zero columns |
|
1147 // with the same number of rows that we started with. |
|
1148 |
|
1149 resize_no_fill (nr, 0); |
|
1150 return; |
|
1151 } |
|
1152 } |
|
1153 |
|
1154 if (idx_j.is_colon () && idx_i.is_colon_equiv (nr, 1)) |
|
1155 { |
|
1156 // A(i,:) -- We are deleting rows by enumerating them. If we |
|
1157 // enumerate all of them, we should have zero rows with the |
|
1158 // same number of columns that we started with. |
|
1159 |
|
1160 resize_no_fill (0, nc); |
|
1161 return; |
|
1162 } |
|
1163 |
|
1164 if (idx_i.is_colon_equiv (nr, 1)) |
|
1165 { |
|
1166 if (idx_j.is_colon_equiv (nc, 1)) |
|
1167 resize_no_fill (0, 0); |
|
1168 else |
|
1169 { |
|
1170 idx_j.sort (true); |
|
1171 |
|
1172 int num_to_delete = idx_j.length (nc); |
|
1173 |
|
1174 if (num_to_delete != 0) |
|
1175 { |
|
1176 if (nr == 1 && num_to_delete == nc) |
|
1177 resize_no_fill (0, 0); |
|
1178 else |
|
1179 { |
|
1180 int new_nc = nc; |
|
1181 |
|
1182 int iidx = 0; |
|
1183 |
|
1184 for (int j = 0; j < nc; j++) |
|
1185 if (j == idx_j.elem (iidx)) |
|
1186 { |
|
1187 iidx++; |
|
1188 new_nc--; |
|
1189 |
|
1190 if (iidx == num_to_delete) |
|
1191 break; |
|
1192 } |
|
1193 |
|
1194 if (new_nc > 0) |
|
1195 { |
|
1196 T *new_data = new T [nr * new_nc]; |
|
1197 |
|
1198 int jj = 0; |
|
1199 iidx = 0; |
|
1200 for (int j = 0; j < nc; j++) |
|
1201 { |
|
1202 if (iidx < num_to_delete && j == idx_j.elem (iidx)) |
|
1203 iidx++; |
|
1204 else |
|
1205 { |
|
1206 for (int i = 0; i < nr; i++) |
|
1207 new_data[nr*jj+i] = elem (i, j); |
|
1208 jj++; |
|
1209 } |
|
1210 } |
|
1211 |
|
1212 if (--(Array<T>::rep)->count <= 0) |
|
1213 delete Array<T>::rep; |
|
1214 |
|
1215 Array<T>::rep = new typename Array<T>::ArrayRep (new_data, nr * new_nc); |
|
1216 |
|
1217 dimensions.resize (2); |
|
1218 dimensions(1) = new_nc; |
|
1219 } |
|
1220 else |
|
1221 (*current_liboctave_error_handler) |
|
1222 ("A(idx) = []: index out of range"); |
|
1223 } |
|
1224 } |
|
1225 } |
|
1226 } |
|
1227 else if (idx_j.is_colon_equiv (nc, 1)) |
|
1228 { |
|
1229 if (idx_i.is_colon_equiv (nr, 1)) |
|
1230 resize_no_fill (0, 0); |
|
1231 else |
|
1232 { |
|
1233 idx_i.sort (true); |
|
1234 |
|
1235 int num_to_delete = idx_i.length (nr); |
|
1236 |
|
1237 if (num_to_delete != 0) |
|
1238 { |
|
1239 if (nc == 1 && num_to_delete == nr) |
|
1240 resize_no_fill (0, 0); |
|
1241 else |
|
1242 { |
|
1243 int new_nr = nr; |
|
1244 |
|
1245 int iidx = 0; |
|
1246 |
|
1247 for (int i = 0; i < nr; i++) |
|
1248 if (i == idx_i.elem (iidx)) |
|
1249 { |
|
1250 iidx++; |
|
1251 new_nr--; |
|
1252 |
|
1253 if (iidx == num_to_delete) |
|
1254 break; |
|
1255 } |
|
1256 |
|
1257 if (new_nr > 0) |
|
1258 { |
|
1259 T *new_data = new T [new_nr * nc]; |
|
1260 |
|
1261 int ii = 0; |
|
1262 iidx = 0; |
|
1263 for (int i = 0; i < nr; i++) |
|
1264 { |
|
1265 if (iidx < num_to_delete && i == idx_i.elem (iidx)) |
|
1266 iidx++; |
|
1267 else |
|
1268 { |
|
1269 for (int j = 0; j < nc; j++) |
|
1270 new_data[new_nr*j+ii] = elem (i, j); |
|
1271 ii++; |
|
1272 } |
|
1273 } |
|
1274 |
|
1275 if (--(Array<T>::rep)->count <= 0) |
|
1276 delete Array<T>::rep; |
|
1277 |
|
1278 Array<T>::rep = new typename Array<T>::ArrayRep (new_data, new_nr * nc); |
|
1279 |
|
1280 dimensions.resize (2); |
|
1281 dimensions(0) = new_nr; |
|
1282 } |
|
1283 else |
|
1284 (*current_liboctave_error_handler) |
|
1285 ("A(idx) = []: index out of range"); |
|
1286 } |
|
1287 } |
|
1288 } |
|
1289 } |
|
1290 } |
|
1291 |
|
1292 template <class T> |
|
1293 void |
|
1294 Array<T>::maybe_delete_elements (idx_vector&, idx_vector&, idx_vector&) |
|
1295 { |
|
1296 assert (0); |
|
1297 } |
|
1298 |
|
1299 template <class T> |
|
1300 void |
|
1301 Array<T>::maybe_delete_elements (Array<idx_vector>& idx, const T& rfv) |
|
1302 { |
|
1303 int n_idx = idx.length (); |
|
1304 |
|
1305 dim_vector lhs_dims = dims (); |
|
1306 |
|
1307 dim_vector idx_is_colon; |
|
1308 idx_is_colon.resize (n_idx); |
|
1309 |
|
1310 dim_vector idx_is_colon_equiv; |
|
1311 idx_is_colon_equiv.resize (n_idx); |
|
1312 |
|
1313 // Initialization of colon arrays. |
|
1314 |
|
1315 for (int i = 0; i < n_idx; i++) |
|
1316 { |
|
1317 idx_is_colon_equiv(i) = idx(i).is_colon_equiv (lhs_dims(i), 1); |
|
1318 |
|
1319 idx_is_colon(i) = idx(i).is_colon (); |
|
1320 } |
|
1321 |
|
1322 if (all_ones (idx_is_colon) || all_ones (idx_is_colon_equiv)) |
|
1323 { |
|
1324 // A(:,:,:) -- we are deleting elements in all dimensions, so |
|
1325 // the result is [](0x0x0). |
|
1326 |
|
1327 dim_vector zeros; |
|
1328 zeros.resize (n_idx); |
|
1329 |
|
1330 for (int i = 0; i < n_idx; i++) |
|
1331 zeros(i) = 0; |
|
1332 |
|
1333 resize (zeros, rfv); |
|
1334 } |
|
1335 |
|
1336 else if (num_ones (idx_is_colon) == n_idx - 1 |
|
1337 && num_ones (idx_is_colon_equiv) == n_idx) |
|
1338 { |
|
1339 // A(:,:,j) -- we are deleting elements in one dimension by |
|
1340 // enumerating them. |
|
1341 // |
|
1342 // If we enumerate all of the elements, we should have zero |
|
1343 // elements in that dimension with the same number of elements |
|
1344 // in the other dimensions that we started with. |
|
1345 |
|
1346 dim_vector temp_dims; |
|
1347 temp_dims.resize (n_idx); |
|
1348 |
|
1349 for (int i = 0; i < n_idx; i++) |
|
1350 { |
|
1351 if (idx_is_colon (i)) |
|
1352 temp_dims (i) = lhs_dims (i); |
|
1353 else |
|
1354 temp_dims (i) = 0; |
|
1355 } |
|
1356 |
|
1357 resize (temp_dims); |
|
1358 } |
|
1359 else if (num_ones (idx_is_colon) == n_idx - 1) |
|
1360 { |
|
1361 // We have colons in all indices except for one. |
|
1362 // This index tells us which slice to delete |
|
1363 |
|
1364 int non_col = 0; |
|
1365 |
|
1366 // Find the non-colon column. |
|
1367 |
|
1368 for (int i = 0; i < n_idx; i++) |
|
1369 { |
|
1370 if (! idx_is_colon (i)) |
|
1371 non_col = i; |
|
1372 } |
|
1373 |
|
1374 // The length of the non-colon dimension. |
|
1375 |
|
1376 int non_col_dim = lhs_dims (non_col); |
|
1377 |
|
1378 idx(non_col).sort (true); |
|
1379 |
|
1380 int num_to_delete = idx(non_col).length (lhs_dims (non_col)); |
|
1381 |
|
1382 if (num_to_delete > 0) |
|
1383 { |
4530
|
1384 int temp = num_ones (lhs_dims); |
4517
|
1385 |
|
1386 if (non_col_dim == 1) |
|
1387 temp--; |
|
1388 |
|
1389 if (temp == n_idx - 1 && num_to_delete == non_col_dim) |
|
1390 { |
|
1391 // We have A with (1x1x4), where A(1,:,1:4) |
|
1392 // Delete all (0x0x0) |
|
1393 |
|
1394 dim_vector zero_dims (n_idx, 0); |
|
1395 |
|
1396 resize (zero_dims, rfv); |
|
1397 } |
|
1398 else |
|
1399 { |
|
1400 // New length of non-colon dimension |
|
1401 // (calculated in the next for loop) |
|
1402 |
|
1403 int new_dim = non_col_dim; |
|
1404 |
|
1405 int iidx = 0; |
|
1406 |
|
1407 for (int j = 0; j < non_col_dim; j++) |
|
1408 if (j == idx(non_col).elem (iidx)) |
|
1409 { |
|
1410 iidx++; |
|
1411 |
|
1412 new_dim--; |
|
1413 |
|
1414 if (iidx == num_to_delete) |
|
1415 break; |
|
1416 } |
|
1417 |
|
1418 // Creating the new nd array after deletions. |
|
1419 |
|
1420 if (new_dim > 0) |
|
1421 { |
|
1422 // Calculate number of elements in new array. |
|
1423 |
|
1424 int num_new_elem=1; |
|
1425 |
|
1426 for (int i = 0; i < n_idx; i++) |
|
1427 { |
|
1428 if (i == non_col) |
|
1429 num_new_elem *= new_dim; |
|
1430 |
|
1431 else |
|
1432 num_new_elem *= lhs_dims(i); |
|
1433 } |
|
1434 |
|
1435 T *new_data = new T [num_new_elem]; |
4530
|
1436 |
4517
|
1437 Array<int> result_idx (lhs_dims.length (), 0); |
|
1438 |
|
1439 dim_vector lhs_inc; |
|
1440 lhs_inc.resize (lhs_dims.length ()); |
|
1441 |
|
1442 for (int i = 0; i < lhs_dims.length (); i++) |
|
1443 lhs_inc(i) = lhs_dims(i) + 1; |
|
1444 |
|
1445 dim_vector new_lhs_dim = lhs_dims; |
|
1446 |
|
1447 new_lhs_dim(non_col) = new_dim; |
|
1448 |
|
1449 int num_elem = 1; |
|
1450 |
|
1451 int numidx = 0; |
|
1452 |
|
1453 int n = length (); |
|
1454 |
|
1455 for (int i =0; i < lhs_dims.length (); i++) |
|
1456 if (i != non_col) |
|
1457 num_elem *= lhs_dims (i); |
|
1458 |
|
1459 num_elem *= idx(non_col).capacity (); |
|
1460 |
|
1461 for (int i = 0; i < n; i++) |
|
1462 { |
|
1463 if (numidx < num_elem |
|
1464 && is_in (result_idx(non_col), idx(non_col))) |
|
1465 numidx++; |
|
1466 |
|
1467 else |
|
1468 { |
|
1469 Array<int> temp_result_idx = result_idx; |
|
1470 |
|
1471 int num_lgt |
|
1472 = how_many_lgt (result_idx(non_col), idx(non_col)); |
|
1473 |
|
1474 temp_result_idx(non_col) -= num_lgt; |
|
1475 |
|
1476 int kidx |
|
1477 = ::compute_index (temp_result_idx, new_lhs_dim); |
|
1478 |
|
1479 new_data[kidx] = elem (result_idx); |
|
1480 } |
|
1481 |
|
1482 increment_index (result_idx, lhs_dims); |
|
1483 } |
|
1484 |
|
1485 if (--rep->count <= 0) |
|
1486 delete rep; |
|
1487 |
|
1488 rep = new typename Array<T>::ArrayRep (new_data, |
|
1489 num_new_elem); |
|
1490 |
|
1491 dimensions = new_lhs_dim; |
|
1492 } |
|
1493 } |
|
1494 } |
|
1495 } |
4530
|
1496 else if (num_ones (idx_is_colon) < n_idx) |
4517
|
1497 { |
|
1498 (*current_liboctave_error_handler) |
4530
|
1499 ("A null assignment can have only one non-colon index"); |
4517
|
1500 } |
|
1501 } |
|
1502 |
|
1503 template <class T> |
|
1504 Array<T> |
|
1505 Array<T>::value (void) |
|
1506 { |
|
1507 Array<T> retval; |
|
1508 |
|
1509 int n_idx = index_count (); |
|
1510 |
|
1511 if (n_idx == 2) |
|
1512 { |
|
1513 idx_vector *tmp = get_idx (); |
|
1514 |
|
1515 idx_vector idx_i = tmp[0]; |
|
1516 idx_vector idx_j = tmp[1]; |
|
1517 |
|
1518 retval = index (idx_i, idx_j); |
|
1519 } |
|
1520 else if (n_idx == 1) |
|
1521 { |
|
1522 retval = index (idx[0]); |
|
1523 } |
|
1524 else |
|
1525 (*current_liboctave_error_handler) |
|
1526 ("Array<T>::value: invalid number of indices specified"); |
|
1527 |
|
1528 clear_index (); |
|
1529 |
|
1530 return retval; |
|
1531 } |
|
1532 |
|
1533 template <class T> |
|
1534 Array<T> |
|
1535 Array<T>::index (idx_vector& idx_arg, int resize_ok, const T& rfv) const |
|
1536 { |
|
1537 Array<T> retval; |
|
1538 |
|
1539 switch (ndims ()) |
|
1540 { |
|
1541 case 1: |
|
1542 retval = index1 (idx_arg, resize_ok, rfv); |
|
1543 break; |
|
1544 |
|
1545 case 2: |
|
1546 retval = index2 (idx_arg, resize_ok, rfv); |
|
1547 break; |
|
1548 |
|
1549 default: |
4530
|
1550 retval = indexN (idx_arg, resize_ok, rfv); |
4517
|
1551 break; |
|
1552 } |
|
1553 |
|
1554 return retval; |
|
1555 } |
|
1556 |
|
1557 template <class T> |
|
1558 Array<T> |
|
1559 Array<T>::index1 (idx_vector& idx_arg, int resize_ok, const T& rfv) const |
|
1560 { |
|
1561 Array<T> retval; |
|
1562 |
|
1563 int len = length (); |
|
1564 |
|
1565 int n = idx_arg.freeze (len, "vector", resize_ok); |
|
1566 |
|
1567 if (idx_arg) |
|
1568 { |
|
1569 if (idx_arg.is_colon_equiv (len)) |
|
1570 { |
|
1571 retval = *this; |
|
1572 } |
|
1573 else if (n == 0) |
|
1574 { |
|
1575 retval.resize_no_fill (0); |
|
1576 } |
|
1577 else if (len == 1 && n > 1 |
|
1578 && idx_arg.one_zero_only () |
|
1579 && idx_arg.ones_count () == n) |
|
1580 { |
4548
|
1581 retval.resize_and_fill (n, elem (0)); |
4517
|
1582 } |
|
1583 else |
|
1584 { |
|
1585 retval.resize_no_fill (n); |
|
1586 |
|
1587 for (int i = 0; i < n; i++) |
|
1588 { |
|
1589 int ii = idx_arg.elem (i); |
|
1590 if (ii >= len) |
|
1591 retval.elem (i) = rfv; |
|
1592 else |
|
1593 retval.elem (i) = elem (ii); |
|
1594 } |
|
1595 } |
|
1596 } |
|
1597 |
|
1598 // idx_vector::freeze() printed an error message for us. |
|
1599 |
|
1600 return retval; |
|
1601 } |
|
1602 |
|
1603 template <class T> |
|
1604 Array<T> |
|
1605 Array<T>::index2 (idx_vector& idx_arg, int resize_ok, const T& rfv) const |
|
1606 { |
|
1607 Array<T> retval; |
|
1608 |
4548
|
1609 assert (ndims () == 2); |
|
1610 |
4517
|
1611 int nr = dim1 (); |
|
1612 int nc = dim2 (); |
|
1613 |
|
1614 int orig_len = nr * nc; |
|
1615 |
|
1616 int idx_orig_rows = idx_arg.orig_rows (); |
|
1617 int idx_orig_columns = idx_arg.orig_columns (); |
|
1618 |
|
1619 if (idx_arg.is_colon ()) |
|
1620 { |
|
1621 // Fast magic colon processing. |
|
1622 |
|
1623 int result_nr = nr * nc; |
|
1624 int result_nc = 1; |
|
1625 |
|
1626 retval = Array<T> (*this, dim_vector (result_nr, result_nc)); |
|
1627 } |
|
1628 else if (nr == 1 && nc == 1) |
|
1629 { |
|
1630 Array<T> tmp = Array<T>::index1 (idx_arg, resize_ok); |
|
1631 |
|
1632 if (tmp.length () != 0) |
|
1633 retval = Array<T> (tmp, dim_vector (idx_orig_rows, idx_orig_columns)); |
|
1634 else |
|
1635 retval = Array<T> (tmp, dim_vector (0, 0)); |
|
1636 } |
|
1637 else if (nr == 1 || nc == 1) |
|
1638 { |
|
1639 // If indexing a vector with a matrix, return value has same |
|
1640 // shape as the index. Otherwise, it has same orientation as |
|
1641 // indexed object. |
|
1642 |
|
1643 Array<T> tmp = index1 (idx_arg, resize_ok); |
|
1644 |
|
1645 int len = tmp.length (); |
|
1646 |
|
1647 if (len == 0) |
|
1648 { |
|
1649 if (idx_orig_rows == 0 || idx_orig_columns == 0) |
|
1650 retval = Array<T> (dim_vector (idx_orig_rows, idx_orig_columns)); |
|
1651 else if (nr == 1) |
|
1652 retval = Array<T> (dim_vector (1, 0)); |
|
1653 else |
|
1654 retval = Array<T> (dim_vector (0, 1)); |
|
1655 } |
|
1656 else |
|
1657 { |
|
1658 if (idx_orig_rows == 1 || idx_orig_columns == 1) |
|
1659 { |
|
1660 if (nr == 1) |
|
1661 retval = Array<T> (tmp, dim_vector (1, len)); |
|
1662 else |
|
1663 retval = Array<T> (tmp, dim_vector (len, 1)); |
|
1664 } |
|
1665 else |
|
1666 retval = Array<T> (tmp, dim_vector (idx_orig_rows, idx_orig_columns)); |
|
1667 } |
|
1668 } |
|
1669 else |
|
1670 { |
|
1671 if (liboctave_wfi_flag |
|
1672 && ! (idx_arg.one_zero_only () |
|
1673 && idx_orig_rows == nr |
|
1674 && idx_orig_columns == nc)) |
|
1675 (*current_liboctave_warning_handler) ("single index used for matrix"); |
|
1676 |
|
1677 // This code is only for indexing matrices. The vector |
|
1678 // cases are handled above. |
|
1679 |
|
1680 idx_arg.freeze (nr * nc, "matrix", resize_ok); |
|
1681 |
|
1682 if (idx_arg) |
|
1683 { |
|
1684 int result_nr = idx_orig_rows; |
|
1685 int result_nc = idx_orig_columns; |
|
1686 |
|
1687 if (idx_arg.one_zero_only ()) |
|
1688 { |
|
1689 result_nr = idx_arg.ones_count (); |
|
1690 result_nc = (result_nr > 0 ? 1 : 0); |
|
1691 } |
|
1692 |
|
1693 retval.resize_no_fill (result_nr, result_nc); |
|
1694 |
|
1695 int k = 0; |
|
1696 for (int j = 0; j < result_nc; j++) |
|
1697 { |
|
1698 for (int i = 0; i < result_nr; i++) |
|
1699 { |
|
1700 int ii = idx_arg.elem (k++); |
|
1701 if (ii >= orig_len) |
|
1702 retval.elem (i, j) = rfv; |
|
1703 else |
|
1704 { |
|
1705 int fr = ii % nr; |
|
1706 int fc = (ii - fr) / nr; |
|
1707 retval.elem (i, j) = elem (fr, fc); |
|
1708 } |
|
1709 } |
|
1710 } |
|
1711 } |
|
1712 // idx_vector::freeze() printed an error message for us. |
|
1713 } |
|
1714 |
|
1715 return retval; |
|
1716 } |
|
1717 |
|
1718 template <class T> |
|
1719 Array<T> |
4530
|
1720 Array<T>::indexN (idx_vector& ra_idx, int resize_ok, const T& rfv) const |
|
1721 { |
|
1722 Array<T> retval; |
|
1723 |
|
1724 int n_dims = dims ().length (); |
|
1725 |
|
1726 int orig_len = number_of_elements (dims ()); |
|
1727 |
|
1728 Array<int> idx_orig_dimsXXX = ra_idx.orig_dimensions (); |
|
1729 |
|
1730 dim_vector idx_orig_dims; |
|
1731 |
4548
|
1732 idx_orig_dims.resize (idx_orig_dimsXXX.length ()); |
|
1733 |
|
1734 for (int i = 0; i < idx_orig_dimsXXX.length (); i++) |
4530
|
1735 idx_orig_dims(i) = idx_orig_dimsXXX(i); |
|
1736 |
|
1737 if (ra_idx.is_colon ()) |
|
1738 { |
4548
|
1739 dim_vector idx (orig_len); |
4530
|
1740 |
|
1741 retval = Array<T> (*this, idx); |
|
1742 } |
|
1743 else if (length () == 1) |
|
1744 { |
|
1745 // Only one element in array. |
|
1746 |
|
1747 Array<T> tmp = Array<T>::index (ra_idx, resize_ok); |
|
1748 |
|
1749 if (tmp.length () != 0) |
|
1750 retval = Array<T> (tmp, idx_orig_dims); |
|
1751 else |
4548
|
1752 retval = Array<T> (tmp, dim_vector (0)); |
4530
|
1753 } |
|
1754 else if (vector_equivalent (dims ())) |
|
1755 { |
|
1756 // We're getting elements from a vector equivalent i.e. (1x4x1). |
|
1757 |
|
1758 Array<T> tmp = Array<T>::index (ra_idx, resize_ok); |
|
1759 |
|
1760 int len = tmp.length (); |
|
1761 |
|
1762 if (len == 0) |
|
1763 { |
|
1764 if (any_zero_len (idx_orig_dims)) |
|
1765 retval = Array<T> (idx_orig_dims); |
|
1766 else |
|
1767 { |
|
1768 dim_vector new_dims; |
|
1769 new_dims.resize (n_dims); |
|
1770 |
|
1771 for (int i = 0; i < n_dims; i++) |
|
1772 { |
|
1773 if ((dims ())(i) == 1) |
|
1774 new_dims(i) = 1; |
|
1775 } |
|
1776 |
|
1777 retval = Array<T> (new_dims); |
|
1778 } |
|
1779 } |
|
1780 else |
|
1781 { |
|
1782 if (vector_equivalent(idx_orig_dims)) |
|
1783 { |
|
1784 // Array<int> index (n_dims, len); |
|
1785 dim_vector new_dims; |
|
1786 |
|
1787 new_dims.resize (n_dims); |
|
1788 |
|
1789 for (int i = 0; i < n_dims; i++) |
|
1790 { |
|
1791 if ((dims ())(i) == 1) |
|
1792 new_dims(i) = 1; |
|
1793 } |
|
1794 |
|
1795 retval = Array<T> (tmp, new_dims); |
|
1796 } |
|
1797 else |
|
1798 retval = Array<T> (tmp, idx_orig_dims); |
|
1799 |
|
1800 (*current_liboctave_error_handler) |
|
1801 ("I do not know what to do here yet!"); |
|
1802 } |
|
1803 } |
|
1804 else if (liboctave_wfi_flag || |
|
1805 (ra_idx.one_zero_only () && equal_arrays (idx_orig_dims, dims ()))) |
|
1806 { |
|
1807 // This code is only for indexing nd-arrays. The vector |
|
1808 // cases are handled above. |
|
1809 |
|
1810 ra_idx.freeze (orig_len, "nd-array", resize_ok); |
|
1811 |
|
1812 if (ra_idx) |
|
1813 { |
|
1814 dim_vector result_dims (idx_orig_dims); |
|
1815 |
|
1816 if (ra_idx.one_zero_only ()) |
|
1817 { |
|
1818 for (int i = 0; i < result_dims.length(); i++) |
|
1819 { |
|
1820 if (i == 0) |
|
1821 result_dims(i) = ra_idx.ones_count (); |
|
1822 else if (result_dims(0) > 0) |
|
1823 result_dims(i) = 1; |
|
1824 else |
|
1825 result_dims(i) = 0; |
|
1826 } |
|
1827 } |
|
1828 |
|
1829 retval.resize (result_dims); |
|
1830 |
|
1831 int n = number_of_elements (result_dims); |
|
1832 |
|
1833 int r_dims = result_dims.length (); |
|
1834 |
|
1835 Array<int> index (r_dims, 0); |
|
1836 |
|
1837 int k = 0; |
|
1838 |
|
1839 for (int i = 0; i < n; i++) |
|
1840 { |
|
1841 int ii = ra_idx.elem (k++); |
|
1842 |
|
1843 if (ii >= orig_len) |
|
1844 retval.elem (index) = rfv; |
|
1845 else |
|
1846 { |
|
1847 Array<int> temp = get_ra_idx (ii, dims ()); |
|
1848 |
|
1849 retval.elem (index) = elem (temp); |
|
1850 } |
|
1851 if (i != n - 1) |
|
1852 increment_index (index, result_dims); |
|
1853 } |
|
1854 } |
|
1855 } |
|
1856 else if (ra_idx.capacity () == 1) |
|
1857 { |
|
1858 // i.e. A(8) for A(3x3x3) |
|
1859 |
|
1860 ra_idx.freeze (orig_len, "nd-array", resize_ok); |
|
1861 |
|
1862 if (ra_idx) |
|
1863 { |
|
1864 int r_idx = ra_idx(0); |
|
1865 |
|
1866 Array<int> idx = get_ra_idx (r_idx, dims ()); |
|
1867 |
|
1868 dim_vector new_dims (1); |
|
1869 |
|
1870 // This shouldn't be needed. |
|
1871 |
|
1872 Array<int> e (idx.length ()); |
|
1873 |
|
1874 for (int i = 0; i < idx.length();i++) |
|
1875 e(i) = idx(i); |
|
1876 |
|
1877 // Should be able to call elem (idx). |
|
1878 |
|
1879 retval = Array<T> (new_dims, elem (e)); |
|
1880 } |
|
1881 } |
|
1882 else |
|
1883 (*current_liboctave_error_handler) |
|
1884 ("single index only valid for row or column vector. ra_idx.cap () = &d", |
|
1885 ra_idx.capacity ()); |
|
1886 |
|
1887 return retval; |
|
1888 } |
|
1889 |
|
1890 template <class T> |
|
1891 Array<T> |
4517
|
1892 Array<T>::index (idx_vector& idx_i, idx_vector& idx_j, int resize_ok, |
|
1893 const T& rfv) const |
|
1894 { |
|
1895 Array<T> retval; |
|
1896 |
4548
|
1897 assert (ndims () == 2); |
|
1898 |
4517
|
1899 int nr = dim1 (); |
|
1900 int nc = dim2 (); |
|
1901 |
|
1902 int n = idx_i.freeze (nr, "row", resize_ok); |
|
1903 int m = idx_j.freeze (nc, "column", resize_ok); |
|
1904 |
|
1905 if (idx_i && idx_j) |
|
1906 { |
|
1907 if (idx_i.orig_empty () || idx_j.orig_empty () || n == 0 || m == 0) |
|
1908 { |
|
1909 retval.resize_no_fill (n, m); |
|
1910 } |
|
1911 else if (idx_i.is_colon_equiv (nr) && idx_j.is_colon_equiv (nc)) |
|
1912 { |
|
1913 retval = *this; |
|
1914 } |
|
1915 else |
|
1916 { |
|
1917 retval.resize_no_fill (n, m); |
|
1918 |
|
1919 for (int j = 0; j < m; j++) |
|
1920 { |
|
1921 int jj = idx_j.elem (j); |
|
1922 for (int i = 0; i < n; i++) |
|
1923 { |
|
1924 int ii = idx_i.elem (i); |
|
1925 if (ii >= nr || jj >= nc) |
|
1926 retval.elem (i, j) = rfv; |
|
1927 else |
|
1928 retval.elem (i, j) = elem (ii, jj); |
|
1929 } |
|
1930 } |
|
1931 } |
|
1932 } |
|
1933 |
|
1934 // idx_vector::freeze() printed an error message for us. |
|
1935 |
|
1936 return retval; |
|
1937 } |
|
1938 |
|
1939 #include "ArrayN-inline.h" |
|
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 */ |