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