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