Mercurial > octave
comparison liboctave/array/idx-vector.h @ 17769:49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
* liboctave/array/Array-C.cc, liboctave/array/Array-b.cc,
liboctave/array/Array-ch.cc, liboctave/array/Array-d.cc,
liboctave/array/Array-f.cc, liboctave/array/Array-fC.cc,
liboctave/array/Array-util.cc, liboctave/array/Array-util.h,
liboctave/array/Array.cc, liboctave/array/Array.h, liboctave/array/Array3.h,
liboctave/array/CColVector.cc, liboctave/array/CColVector.h,
liboctave/array/CDiagMatrix.cc, liboctave/array/CDiagMatrix.h,
liboctave/array/CMatrix.cc, liboctave/array/CMatrix.h,
liboctave/array/CNDArray.cc, liboctave/array/CNDArray.h,
liboctave/array/CRowVector.cc, liboctave/array/CRowVector.h,
liboctave/array/CSparse.cc, liboctave/array/CSparse.h,
liboctave/array/DiagArray2.h, liboctave/array/MArray.cc,
liboctave/array/MArray.h, liboctave/array/MDiagArray2.cc,
liboctave/array/MDiagArray2.h, liboctave/array/MSparse.cc,
liboctave/array/MSparse.h, liboctave/array/MatrixType.cc,
liboctave/array/MatrixType.h, liboctave/array/PermMatrix.h,
liboctave/array/Range.cc, liboctave/array/Range.h, liboctave/array/Sparse.cc,
liboctave/array/Sparse.h, liboctave/array/boolMatrix.cc,
liboctave/array/boolMatrix.h, liboctave/array/boolNDArray.cc,
liboctave/array/boolNDArray.h, liboctave/array/boolSparse.cc,
liboctave/array/boolSparse.h, liboctave/array/chMatrix.cc,
liboctave/array/chMatrix.h, liboctave/array/chNDArray.cc,
liboctave/array/chNDArray.h, liboctave/array/dColVector.h,
liboctave/array/dDiagMatrix.cc, liboctave/array/dDiagMatrix.h,
liboctave/array/dMatrix.cc, liboctave/array/dMatrix.h,
liboctave/array/dNDArray.cc, liboctave/array/dNDArray.h,
liboctave/array/dRowVector.h, liboctave/array/dSparse.cc,
liboctave/array/dSparse.h, liboctave/array/dim-vector.cc,
liboctave/array/dim-vector.h, liboctave/array/fCColVector.cc,
liboctave/array/fCColVector.h, liboctave/array/fCDiagMatrix.cc,
liboctave/array/fCDiagMatrix.h, liboctave/array/fCMatrix.cc,
liboctave/array/fCMatrix.h, liboctave/array/fCNDArray.cc,
liboctave/array/fCNDArray.h, liboctave/array/fCRowVector.cc,
liboctave/array/fCRowVector.h, liboctave/array/fColVector.h,
liboctave/array/fDiagMatrix.cc, liboctave/array/fDiagMatrix.h,
liboctave/array/fMatrix.cc, liboctave/array/fMatrix.h,
liboctave/array/fNDArray.cc, liboctave/array/fNDArray.h,
liboctave/array/fRowVector.h, liboctave/array/idx-vector.cc,
liboctave/array/idx-vector.h, liboctave/array/intNDArray.cc,
liboctave/array/intNDArray.h, liboctave/cruft/misc/blaswrap.c,
liboctave/cruft/misc/quit.cc, liboctave/numeric/CmplxCHOL.cc,
liboctave/numeric/CmplxCHOL.h, liboctave/numeric/CmplxGEPBAL.cc,
liboctave/numeric/CmplxGEPBAL.h, liboctave/numeric/CmplxHESS.h,
liboctave/numeric/CmplxLU.cc, liboctave/numeric/CmplxLU.h,
liboctave/numeric/CmplxQR.cc, liboctave/numeric/CmplxQRP.cc,
liboctave/numeric/CmplxQRP.h, liboctave/numeric/CmplxSCHUR.h,
liboctave/numeric/CmplxSVD.cc, liboctave/numeric/CmplxSVD.h,
liboctave/numeric/CollocWt.h, liboctave/numeric/DAE.h,
liboctave/numeric/DAEFunc.h, liboctave/numeric/DAERT.h,
liboctave/numeric/DAERTFunc.h, liboctave/numeric/DASPK.cc,
liboctave/numeric/DASRT.cc, liboctave/numeric/DASRT.h,
liboctave/numeric/DASSL.cc, liboctave/numeric/DET.h, liboctave/numeric/EIG.cc,
liboctave/numeric/EIG.h, liboctave/numeric/LSODE.cc, liboctave/numeric/ODE.h,
liboctave/numeric/ODEFunc.h, liboctave/numeric/ODES.h,
liboctave/numeric/ODESFunc.h, liboctave/numeric/Quad.cc,
liboctave/numeric/Quad.h, liboctave/numeric/SparseCmplxCHOL.h,
liboctave/numeric/SparseCmplxLU.cc, liboctave/numeric/SparseCmplxLU.h,
liboctave/numeric/SparseCmplxQR.cc, liboctave/numeric/SparseCmplxQR.h,
liboctave/numeric/SparseQR.cc, liboctave/numeric/SparseQR.h,
liboctave/numeric/SparsedbleCHOL.h, liboctave/numeric/SparsedbleLU.cc,
liboctave/numeric/SparsedbleLU.h, liboctave/numeric/base-aepbal.h,
liboctave/numeric/base-dae.h, liboctave/numeric/base-de.h,
liboctave/numeric/base-lu.cc, liboctave/numeric/base-lu.h,
liboctave/numeric/base-min.h, liboctave/numeric/base-qr.h,
liboctave/numeric/bsxfun.h, liboctave/numeric/dbleCHOL.cc,
liboctave/numeric/dbleCHOL.h, liboctave/numeric/dbleGEPBAL.h,
liboctave/numeric/dbleHESS.h, liboctave/numeric/dbleLU.cc,
liboctave/numeric/dbleLU.h, liboctave/numeric/dbleQR.cc,
liboctave/numeric/dbleQRP.cc, liboctave/numeric/dbleQRP.h,
liboctave/numeric/dbleSCHUR.cc, liboctave/numeric/dbleSCHUR.h,
liboctave/numeric/dbleSVD.cc, liboctave/numeric/dbleSVD.h,
liboctave/numeric/eigs-base.cc, liboctave/numeric/fCmplxAEPBAL.cc,
liboctave/numeric/fCmplxAEPBAL.h, liboctave/numeric/fCmplxCHOL.cc,
liboctave/numeric/fCmplxCHOL.h, liboctave/numeric/fCmplxGEPBAL.cc,
liboctave/numeric/fCmplxGEPBAL.h, liboctave/numeric/fCmplxHESS.h,
liboctave/numeric/fCmplxLU.cc, liboctave/numeric/fCmplxLU.h,
liboctave/numeric/fCmplxQR.cc, liboctave/numeric/fCmplxQR.h,
liboctave/numeric/fCmplxQRP.cc, liboctave/numeric/fCmplxQRP.h,
liboctave/numeric/fCmplxSCHUR.cc, liboctave/numeric/fCmplxSCHUR.h,
liboctave/numeric/fCmplxSVD.h, liboctave/numeric/fEIG.cc,
liboctave/numeric/fEIG.h, liboctave/numeric/floatCHOL.cc,
liboctave/numeric/floatCHOL.h, liboctave/numeric/floatGEPBAL.cc,
liboctave/numeric/floatGEPBAL.h, liboctave/numeric/floatHESS.h,
liboctave/numeric/floatLU.cc, liboctave/numeric/floatLU.h,
liboctave/numeric/floatQR.cc, liboctave/numeric/floatQRP.cc,
liboctave/numeric/floatQRP.h, liboctave/numeric/floatSCHUR.cc,
liboctave/numeric/floatSCHUR.h, liboctave/numeric/floatSVD.cc,
liboctave/numeric/floatSVD.h, liboctave/numeric/lo-mappers.cc,
liboctave/numeric/lo-mappers.h, liboctave/numeric/lo-specfun.cc,
liboctave/numeric/lo-specfun.h, liboctave/numeric/oct-convn.cc,
liboctave/numeric/oct-fftw.cc, liboctave/numeric/oct-fftw.h,
liboctave/numeric/oct-norm.cc, liboctave/numeric/oct-rand.cc,
liboctave/numeric/oct-rand.h, liboctave/numeric/randgamma.c,
liboctave/numeric/randgamma.h, liboctave/numeric/randmtzig.c,
liboctave/numeric/randpoisson.c, liboctave/numeric/randpoisson.h,
liboctave/numeric/sparse-base-chol.h, liboctave/numeric/sparse-base-lu.h,
liboctave/numeric/sparse-dmsolve.cc, liboctave/operators/Sparse-diag-op-defs.h,
liboctave/operators/Sparse-op-defs.h, liboctave/operators/mx-inlines.cc,
liboctave/system/dir-ops.h, liboctave/system/file-ops.cc,
liboctave/system/file-stat.cc, liboctave/system/file-stat.h,
liboctave/system/lo-sysdep.cc, liboctave/system/lo-sysdep.h,
liboctave/system/mach-info.cc, liboctave/system/mach-info.h,
liboctave/system/oct-env.cc, liboctave/system/oct-group.cc,
liboctave/system/oct-syscalls.cc, liboctave/system/oct-syscalls.h,
liboctave/system/oct-time.h, liboctave/system/tempname.c,
liboctave/util/action-container.h, liboctave/util/base-list.h,
liboctave/util/cmd-edit.cc, liboctave/util/cmd-edit.h,
liboctave/util/cmd-hist.cc, liboctave/util/cmd-hist.h,
liboctave/util/data-conv.cc, liboctave/util/data-conv.h,
liboctave/util/kpse.cc, liboctave/util/lo-array-gripes.cc,
liboctave/util/lo-cieee.c, liboctave/util/lo-regexp.cc,
liboctave/util/lo-utils.cc, liboctave/util/oct-alloc.cc,
liboctave/util/oct-base64.cc, liboctave/util/oct-binmap.h,
liboctave/util/oct-cmplx.h, liboctave/util/oct-glob.cc,
liboctave/util/oct-inttypes.cc, liboctave/util/oct-inttypes.h,
liboctave/util/oct-locbuf.cc, liboctave/util/oct-locbuf.h,
liboctave/util/oct-mem.h, liboctave/util/oct-mutex.cc,
liboctave/util/oct-refcount.h, liboctave/util/oct-shlib.cc,
liboctave/util/oct-shlib.h, liboctave/util/oct-sort.cc,
liboctave/util/oct-sort.h, liboctave/util/pathsearch.cc,
liboctave/util/pathsearch.h, liboctave/util/sparse-util.cc,
liboctave/util/str-vec.cc, liboctave/util/str-vec.h,
liboctave/util/unwind-prot.h, liboctave/util/url-transfer.cc,
liboctave/util/url-transfer.h: Use GNU style coding conventions.
author | Rik <rik@octave.org> |
---|---|
date | Sat, 26 Oct 2013 18:57:05 -0700 |
parents | d63878346099 |
children | 8e056300994b |
comparison
equal
deleted
inserted
replaced
17768:271c0cce0f64 | 17769:49a5a4be04a1 |
---|---|
53 idx_vector | 53 idx_vector |
54 { | 54 { |
55 public: | 55 public: |
56 | 56 |
57 enum idx_class_type | 57 enum idx_class_type |
58 { | 58 { |
59 class_invalid = -1, | 59 class_invalid = -1, |
60 class_colon = 0, | 60 class_colon = 0, |
61 class_range, | 61 class_range, |
62 class_scalar, | 62 class_scalar, |
63 class_vector, | 63 class_vector, |
64 class_mask | 64 class_mask |
65 }; | 65 }; |
66 | 66 |
67 template<class T> friend class std::auto_ptr; | 67 template<class T> friend class std::auto_ptr; |
68 | 68 |
69 private: | 69 private: |
70 | 70 |
96 virtual idx_base_rep *sort_idx (Array<octave_idx_type>&) = 0; | 96 virtual idx_base_rep *sort_idx (Array<octave_idx_type>&) = 0; |
97 | 97 |
98 // Checks whether the index is colon or a range equivalent to colon. | 98 // Checks whether the index is colon or a range equivalent to colon. |
99 virtual bool is_colon_equiv (octave_idx_type) const { return false; } | 99 virtual bool is_colon_equiv (octave_idx_type) const { return false; } |
100 | 100 |
101 // The original dimensions of this object (used when subscribing by matrices). | 101 // The original dimensions of object (used when subscribing by matrices). |
102 virtual dim_vector orig_dimensions (void) const { return dim_vector (); } | 102 virtual dim_vector orig_dimensions (void) const { return dim_vector (); } |
103 | 103 |
104 // i/o | 104 // i/o |
105 virtual std::ostream& print (std::ostream& os) const = 0; | 105 virtual std::ostream& print (std::ostream& os) const = 0; |
106 | 106 |
134 octave_idx_type extent (octave_idx_type n) const { return n; } | 134 octave_idx_type extent (octave_idx_type n) const { return n; } |
135 | 135 |
136 idx_class_type idx_class (void) const { return class_colon; } | 136 idx_class_type idx_class (void) const { return class_colon; } |
137 | 137 |
138 idx_base_rep *sort_uniq_clone (bool = false) | 138 idx_base_rep *sort_uniq_clone (bool = false) |
139 { count++; return this; } | 139 { count++; return this; } |
140 | 140 |
141 idx_base_rep *sort_idx (Array<octave_idx_type>&); | 141 idx_base_rep *sort_idx (Array<octave_idx_type>&); |
142 | 142 |
143 bool is_colon_equiv (octave_idx_type) const { return true; } | 143 bool is_colon_equiv (octave_idx_type) const { return true; } |
144 | 144 |
172 octave_idx_type _step); | 172 octave_idx_type _step); |
173 | 173 |
174 idx_range_rep (const Range&); | 174 idx_range_rep (const Range&); |
175 | 175 |
176 octave_idx_type xelem (octave_idx_type i) const | 176 octave_idx_type xelem (octave_idx_type i) const |
177 { return start + i * step; } | 177 { return start + i * step; } |
178 | 178 |
179 octave_idx_type checkelem (octave_idx_type i) const; | 179 octave_idx_type checkelem (octave_idx_type i) const; |
180 | 180 |
181 octave_idx_type length (octave_idx_type) const { return len; } | 181 octave_idx_type length (octave_idx_type) const { return len; } |
182 | 182 |
183 octave_idx_type extent (octave_idx_type n) const | 183 octave_idx_type extent (octave_idx_type n) const |
184 { return len ? std::max (n, (start + 1 + (step < 0 ? 0 : step * (len - 1)))) : n; } | 184 { return len ? std::max (n, (start + 1 + (step < 0 ? 0 : step * (len - 1)))) |
185 : n; } | |
185 | 186 |
186 idx_class_type idx_class (void) const { return class_range; } | 187 idx_class_type idx_class (void) const { return class_range; } |
187 | 188 |
188 idx_base_rep *sort_uniq_clone (bool uniq = false); | 189 idx_base_rep *sort_uniq_clone (bool uniq = false); |
189 | 190 |
190 idx_base_rep *sort_idx (Array<octave_idx_type>&); | 191 idx_base_rep *sort_idx (Array<octave_idx_type>&); |
191 | 192 |
192 bool is_colon_equiv (octave_idx_type n) const | 193 bool is_colon_equiv (octave_idx_type n) const |
193 { return start == 0 && step == 1 && len == n; } | 194 { return start == 0 && step == 1 && len == n; } |
194 | 195 |
195 dim_vector orig_dimensions (void) const | 196 dim_vector orig_dimensions (void) const |
196 { return dim_vector (1, len); } | 197 { return dim_vector (1, len); } |
197 | 198 |
198 octave_idx_type get_start (void) const { return start; } | 199 octave_idx_type get_start (void) const { return start; } |
199 | 200 |
200 octave_idx_type get_step (void) const { return step; } | 201 octave_idx_type get_step (void) const { return step; } |
201 | 202 |
238 octave_idx_type checkelem (octave_idx_type i) const; | 239 octave_idx_type checkelem (octave_idx_type i) const; |
239 | 240 |
240 octave_idx_type length (octave_idx_type) const { return 1; } | 241 octave_idx_type length (octave_idx_type) const { return 1; } |
241 | 242 |
242 octave_idx_type extent (octave_idx_type n) const | 243 octave_idx_type extent (octave_idx_type n) const |
243 { return std::max (n, data + 1); } | 244 { return std::max (n, data + 1); } |
244 | 245 |
245 idx_class_type idx_class (void) const { return class_scalar; } | 246 idx_class_type idx_class (void) const { return class_scalar; } |
246 | 247 |
247 idx_base_rep *sort_uniq_clone (bool = false) | 248 idx_base_rep *sort_uniq_clone (bool = false) |
248 { count++; return this; } | 249 { count++; return this; } |
249 | 250 |
250 idx_base_rep *sort_idx (Array<octave_idx_type>&); | 251 idx_base_rep *sort_idx (Array<octave_idx_type>&); |
251 | 252 |
252 bool is_colon_equiv (octave_idx_type n) const | 253 bool is_colon_equiv (octave_idx_type n) const |
253 { return n == 1 && data == 0; } | 254 { return n == 1 && data == 0; } |
254 | 255 |
255 dim_vector orig_dimensions (void) const { return dim_vector (1, 1); } | 256 dim_vector orig_dimensions (void) const { return dim_vector (1, 1); } |
256 | 257 |
257 octave_idx_type get_data (void) const { return data; } | 258 octave_idx_type get_data (void) const { return data; } |
258 | 259 |
283 octave_idx_type _ext, const dim_vector& od, direct) | 284 octave_idx_type _ext, const dim_vector& od, direct) |
284 : data (_data), len (_len), ext (_ext), aowner (0), orig_dims (od) { } | 285 : data (_data), len (_len), ext (_ext), aowner (0), orig_dims (od) { } |
285 | 286 |
286 idx_vector_rep (void) | 287 idx_vector_rep (void) |
287 : data (0), len (0), ext (0), aowner (0), orig_dims () | 288 : data (0), len (0), ext (0), aowner (0), orig_dims () |
288 { } | 289 { } |
289 | 290 |
290 // Zero-based constructor. | 291 // Zero-based constructor. |
291 idx_vector_rep (const Array<octave_idx_type>& inda); | 292 idx_vector_rep (const Array<octave_idx_type>& inda); |
292 | 293 |
293 idx_vector_rep (const Array<octave_idx_type>& inda, | 294 idx_vector_rep (const Array<octave_idx_type>& inda, |
309 octave_idx_type checkelem (octave_idx_type i) const; | 310 octave_idx_type checkelem (octave_idx_type i) const; |
310 | 311 |
311 octave_idx_type length (octave_idx_type) const { return len; } | 312 octave_idx_type length (octave_idx_type) const { return len; } |
312 | 313 |
313 octave_idx_type extent (octave_idx_type n) const | 314 octave_idx_type extent (octave_idx_type n) const |
314 { return std::max (n, ext); } | 315 { return std::max (n, ext); } |
315 | 316 |
316 idx_class_type idx_class (void) const { return class_vector; } | 317 idx_class_type idx_class (void) const { return class_vector; } |
317 | 318 |
318 idx_base_rep *sort_uniq_clone (bool uniq = false); | 319 idx_base_rep *sort_uniq_clone (bool uniq = false); |
319 | 320 |
364 aowner (0), orig_dims (od) { } | 365 aowner (0), orig_dims (od) { } |
365 | 366 |
366 idx_mask_rep (void) | 367 idx_mask_rep (void) |
367 : data (0), len (0), ext (0), lsti (-1), lste (-1), aowner (0), | 368 : data (0), len (0), ext (0), lsti (-1), lste (-1), aowner (0), |
368 orig_dims () | 369 orig_dims () |
369 { } | 370 { } |
370 | 371 |
371 idx_mask_rep (bool); | 372 idx_mask_rep (bool); |
372 | 373 |
373 idx_mask_rep (const Array<bool>&, octave_idx_type = -1); | 374 idx_mask_rep (const Array<bool>&, octave_idx_type = -1); |
374 | 375 |
379 octave_idx_type checkelem (octave_idx_type i) const; | 380 octave_idx_type checkelem (octave_idx_type i) const; |
380 | 381 |
381 octave_idx_type length (octave_idx_type) const { return len; } | 382 octave_idx_type length (octave_idx_type) const { return len; } |
382 | 383 |
383 octave_idx_type extent (octave_idx_type n) const | 384 octave_idx_type extent (octave_idx_type n) const |
384 { return std::max (n, ext); } | 385 { return std::max (n, ext); } |
385 | 386 |
386 idx_class_type idx_class (void) const { return class_mask; } | 387 idx_class_type idx_class (void) const { return class_mask; } |
387 | 388 |
388 idx_base_rep *sort_uniq_clone (bool = false) | 389 idx_base_rep *sort_uniq_clone (bool = false) |
389 { count++; return this; } | 390 { count++; return this; } |
390 | 391 |
391 idx_base_rep *sort_idx (Array<octave_idx_type>&); | 392 idx_base_rep *sort_idx (Array<octave_idx_type>&); |
392 | 393 |
393 dim_vector orig_dimensions (void) const { return orig_dims; } | 394 dim_vector orig_dimensions (void) const { return orig_dims; } |
394 | 395 |
395 bool is_colon_equiv (octave_idx_type n) const | 396 bool is_colon_equiv (octave_idx_type n) const |
396 { return len == n && ext == n; } | 397 { return len == n && ext == n; } |
397 | 398 |
398 const bool *get_data (void) const { return data; } | 399 const bool *get_data (void) const { return data; } |
399 | 400 |
400 std::ostream& print (std::ostream& os) const; | 401 std::ostream& print (std::ostream& os) const; |
401 | 402 |
435 idx_vector (idx_base_rep *r) : rep (r) { } | 436 idx_vector (idx_base_rep *r) : rep (r) { } |
436 | 437 |
437 // The shared empty vector representation (for fast default | 438 // The shared empty vector representation (for fast default |
438 // constructor). | 439 // constructor). |
439 static idx_vector_rep *nil_rep (void) | 440 static idx_vector_rep *nil_rep (void) |
440 { | 441 { |
441 static idx_vector_rep ivr; | 442 static idx_vector_rep ivr; |
442 return &ivr; | 443 return &ivr; |
443 } | 444 } |
444 | 445 |
445 // The shared empty vector representation with the error flag set. | 446 // The shared empty vector representation with the error flag set. |
446 static idx_vector_rep *err_rep (void) | 447 static idx_vector_rep *err_rep (void) |
447 { | 448 { |
448 static idx_vector_rep ivr; | 449 static idx_vector_rep ivr; |
449 ivr.err = true; | 450 ivr.err = true; |
450 return &ivr; | 451 return &ivr; |
451 } | 452 } |
452 | 453 |
453 // If there was an error in constructing the rep, replace it with | 454 // If there was an error in constructing the rep, replace it with |
454 // empty vector for safety. | 455 // empty vector for safety. |
455 void chkerr (void) | 456 void chkerr (void) |
456 { | 457 { |
457 if (rep->err) | 458 if (rep->err) |
458 { | 459 { |
459 if (--rep->count == 0) | 460 if (--rep->count == 0) |
460 delete rep; | 461 delete rep; |
461 rep = err_rep (); | 462 rep = err_rep (); |
462 rep->count++; | 463 rep->count++; |
463 } | 464 } |
464 } | 465 } |
465 | 466 |
466 public: | 467 public: |
467 | 468 |
468 // Fast empty constructor. | 469 // Fast empty constructor. |
469 idx_vector (void) : rep (nil_rep ()) { rep->count++; } | 470 idx_vector (void) : rep (nil_rep ()) { rep->count++; } |
470 | 471 |
471 // Zero-based constructors (for use from C++). | 472 // Zero-based constructors (for use from C++). |
472 idx_vector (octave_idx_type i) : rep (new idx_scalar_rep (i)) | 473 idx_vector (octave_idx_type i) : rep (new idx_scalar_rep (i)) |
473 { chkerr (); } | 474 { chkerr (); } |
474 | 475 |
475 idx_vector (octave_idx_type start, octave_idx_type limit, | 476 idx_vector (octave_idx_type start, octave_idx_type limit, |
476 octave_idx_type step = 1) | 477 octave_idx_type step = 1) |
477 : rep (new idx_range_rep (start, limit, step)) | 478 : rep (new idx_range_rep (start, limit, step)) |
478 { chkerr (); } | 479 { chkerr (); } |
479 | 480 |
480 static idx_vector | 481 static idx_vector |
481 make_range (octave_idx_type start, octave_idx_type step, | 482 make_range (octave_idx_type start, octave_idx_type step, |
482 octave_idx_type len) | 483 octave_idx_type len) |
483 { | 484 { |
484 return idx_vector (new idx_range_rep (start, len, step, DIRECT)); | 485 return idx_vector (new idx_range_rep (start, len, step, DIRECT)); |
485 } | 486 } |
486 | 487 |
487 idx_vector (const Array<octave_idx_type>& inda) | 488 idx_vector (const Array<octave_idx_type>& inda) |
488 : rep (new idx_vector_rep (inda)) | 489 : rep (new idx_vector_rep (inda)) |
489 { chkerr (); } | 490 { chkerr (); } |
490 | 491 |
491 // Directly pass extent, no checking. | 492 // Directly pass extent, no checking. |
492 idx_vector (const Array<octave_idx_type>& inda, octave_idx_type ext) | 493 idx_vector (const Array<octave_idx_type>& inda, octave_idx_type ext) |
493 : rep (new idx_vector_rep (inda, ext, DIRECT)) | 494 : rep (new idx_vector_rep (inda, ext, DIRECT)) |
494 { } | 495 { } |
495 | 496 |
496 // Colon is best constructed by simply copying (or referencing) this member. | 497 // Colon is best constructed by simply copying (or referencing) this member. |
497 static const idx_vector colon; | 498 static const idx_vector colon; |
498 | 499 |
499 // or passing ':' here | 500 // or passing ':' here |
511 // A scalar bool does not necessarily map to scalar index. | 512 // A scalar bool does not necessarily map to scalar index. |
512 idx_vector (bool x) : rep (new idx_mask_rep (x)) { chkerr (); } | 513 idx_vector (bool x) : rep (new idx_mask_rep (x)) { chkerr (); } |
513 | 514 |
514 template <class T> | 515 template <class T> |
515 idx_vector (const Array<octave_int<T> >& nda) : rep (new idx_vector_rep (nda)) | 516 idx_vector (const Array<octave_int<T> >& nda) : rep (new idx_vector_rep (nda)) |
516 { chkerr (); } | 517 { chkerr (); } |
517 | 518 |
518 idx_vector (const Array<double>& nda) : rep (new idx_vector_rep (nda)) | 519 idx_vector (const Array<double>& nda) : rep (new idx_vector_rep (nda)) |
519 { chkerr (); } | 520 { chkerr (); } |
520 | 521 |
521 idx_vector (const Array<float>& nda) : rep (new idx_vector_rep (nda)) | 522 idx_vector (const Array<float>& nda) : rep (new idx_vector_rep (nda)) |
522 { chkerr (); } | 523 { chkerr (); } |
523 | 524 |
524 idx_vector (const Array<bool>& nda); | 525 idx_vector (const Array<bool>& nda); |
525 | 526 |
526 idx_vector (const Range& r) | 527 idx_vector (const Range& r) |
527 : rep (new idx_range_rep (r)) | 528 : rep (new idx_range_rep (r)) |
528 { chkerr (); } | 529 { chkerr (); } |
529 | 530 |
530 idx_vector (const Sparse<bool>& nda) : rep (new idx_vector_rep (nda)) | 531 idx_vector (const Sparse<bool>& nda) : rep (new idx_vector_rep (nda)) |
531 { chkerr (); } | 532 { chkerr (); } |
532 | 533 |
533 idx_vector (const idx_vector& a) : rep (a.rep) { rep->count++; } | 534 idx_vector (const idx_vector& a) : rep (a.rep) { rep->count++; } |
534 | 535 |
535 ~idx_vector (void) | 536 ~idx_vector (void) |
536 { | 537 { |
537 if (--rep->count == 0) | 538 if (--rep->count == 0) |
538 delete rep; | 539 delete rep; |
539 } | 540 } |
540 | 541 |
541 idx_vector& operator = (const idx_vector& a) | 542 idx_vector& operator = (const idx_vector& a) |
542 { | 543 { |
543 if (this != &a) | 544 if (this != &a) |
544 { | 545 { |
545 if (--rep->count == 0) | 546 if (--rep->count == 0) |
546 delete rep; | 547 delete rep; |
547 | 548 |
548 rep = a.rep; | 549 rep = a.rep; |
549 rep->count++; | 550 rep->count++; |
550 } | 551 } |
551 return *this; | 552 return *this; |
552 } | 553 } |
553 | 554 |
554 idx_class_type idx_class (void) const { return rep->idx_class (); } | 555 idx_class_type idx_class (void) const { return rep->idx_class (); } |
555 | 556 |
556 octave_idx_type length (octave_idx_type n = 0) const | 557 octave_idx_type length (octave_idx_type n = 0) const |
557 { return rep->length (n); } | 558 { return rep->length (n); } |
558 | 559 |
559 octave_idx_type extent (octave_idx_type n) const | 560 octave_idx_type extent (octave_idx_type n) const |
560 { return rep->extent (n); } | 561 { return rep->extent (n); } |
561 | 562 |
562 octave_idx_type xelem (octave_idx_type n) const | 563 octave_idx_type xelem (octave_idx_type n) const |
563 { return rep->xelem (n); } | 564 { return rep->xelem (n); } |
564 | 565 |
565 octave_idx_type checkelem (octave_idx_type n) const | 566 octave_idx_type checkelem (octave_idx_type n) const |
566 { return rep->checkelem (n); } | 567 { return rep->checkelem (n); } |
567 | 568 |
568 octave_idx_type operator () (octave_idx_type n) const | 569 octave_idx_type operator () (octave_idx_type n) const |
569 { | 570 { |
570 #if defined (BOUNDS_CHECKING) | 571 #if defined (BOUNDS_CHECKING) |
571 return rep->checkelem (n); | 572 return rep->checkelem (n); |
572 #else | 573 #else |
573 return rep->xelem (n); | 574 return rep->xelem (n); |
574 #endif | 575 #endif |
575 } | 576 } |
576 | 577 |
577 operator bool (void) const | 578 operator bool (void) const |
578 { return ! rep->err; } | 579 { return ! rep->err; } |
579 | 580 |
580 bool is_colon (void) const | 581 bool is_colon (void) const |
581 { return rep->idx_class () == class_colon; } | 582 { return rep->idx_class () == class_colon; } |
582 | 583 |
583 bool is_scalar (void) const | 584 bool is_scalar (void) const |
584 { return rep->idx_class () == class_scalar; } | 585 { return rep->idx_class () == class_scalar; } |
585 | 586 |
586 bool is_range (void) const | 587 bool is_range (void) const |
587 { return rep->idx_class () == class_range; } | 588 { return rep->idx_class () == class_range; } |
588 | 589 |
589 bool is_colon_equiv (octave_idx_type n) const | 590 bool is_colon_equiv (octave_idx_type n) const |
590 { return rep->is_colon_equiv (n); } | 591 { return rep->is_colon_equiv (n); } |
591 | 592 |
592 idx_vector sorted (bool uniq = false) const | 593 idx_vector sorted (bool uniq = false) const |
593 { return idx_vector (rep->sort_uniq_clone (uniq)); } | 594 { return idx_vector (rep->sort_uniq_clone (uniq)); } |
594 | 595 |
595 idx_vector sorted (Array<octave_idx_type>& sidx) const | 596 idx_vector sorted (Array<octave_idx_type>& sidx) const |
596 { return idx_vector (rep->sort_idx (sidx)); } | 597 { return idx_vector (rep->sort_idx (sidx)); } |
597 | 598 |
598 dim_vector orig_dimensions (void) const { return rep->orig_dimensions (); } | 599 dim_vector orig_dimensions (void) const { return rep->orig_dimensions (); } |
599 | 600 |
600 octave_idx_type orig_rows (void) const | 601 octave_idx_type orig_rows (void) const |
601 { return orig_dimensions () (0); } | 602 { return orig_dimensions () (0); } |
602 | 603 |
603 octave_idx_type orig_columns (void) const | 604 octave_idx_type orig_columns (void) const |
604 { return orig_dimensions () (1); } | 605 { return orig_dimensions () (1); } |
605 | 606 |
606 int orig_empty (void) const | 607 int orig_empty (void) const |
607 { return (! is_colon () && orig_dimensions ().any_zero ()); } | 608 { return (! is_colon () && orig_dimensions ().any_zero ()); } |
608 | 609 |
609 // i/o | 610 // i/o |
610 | 611 |
611 std::ostream& print (std::ostream& os) const { return rep->print (os); } | 612 std::ostream& print (std::ostream& os) const { return rep->print (os); } |
612 | 613 |
613 friend std::ostream& operator << (std::ostream& os, const idx_vector& a) | 614 friend std::ostream& operator << (std::ostream& os, const idx_vector& a) |
614 { return a.print (os); } | 615 { return a.print (os); } |
615 | 616 |
616 // Slice with specializations. No checking of bounds! | 617 // Slice with specializations. No checking of bounds! |
617 // | 618 // |
618 // This is equivalent to the following loop (but much faster): | 619 // This is equivalent to the following loop (but much faster): |
619 // | 620 // |
622 // return i; | 623 // return i; |
623 // | 624 // |
624 template <class T> | 625 template <class T> |
625 octave_idx_type | 626 octave_idx_type |
626 index (const T *src, octave_idx_type n, T *dest) const | 627 index (const T *src, octave_idx_type n, T *dest) const |
627 { | 628 { |
628 octave_idx_type len = rep->length (n); | 629 octave_idx_type len = rep->length (n); |
629 | 630 |
630 switch (rep->idx_class ()) | 631 switch (rep->idx_class ()) |
631 { | 632 { |
632 case class_colon: | 633 case class_colon: |
633 copy_or_memcpy (len, src, dest); | 634 copy_or_memcpy (len, src, dest); |
634 break; | 635 break; |
635 | 636 |
636 case class_range: | 637 case class_range: |
637 { | 638 { |
638 idx_range_rep * r = dynamic_cast<idx_range_rep *> (rep); | 639 idx_range_rep * r = dynamic_cast<idx_range_rep *> (rep); |
639 octave_idx_type start = r->get_start (), step = r->get_step (); | 640 octave_idx_type start = r->get_start (), step = r->get_step (); |
640 const T *ssrc = src + start; | 641 const T *ssrc = src + start; |
641 if (step == 1) | 642 if (step == 1) |
642 copy_or_memcpy (len, ssrc, dest); | 643 copy_or_memcpy (len, ssrc, dest); |
643 else if (step == -1) | 644 else if (step == -1) |
644 std::reverse_copy (ssrc - len + 1, ssrc + 1, dest); | 645 std::reverse_copy (ssrc - len + 1, ssrc + 1, dest); |
645 else if (step == 0) | 646 else if (step == 0) |
646 std::fill_n (dest, len, *ssrc); | 647 std::fill_n (dest, len, *ssrc); |
647 else | 648 else |
648 { | 649 { |
649 for (octave_idx_type i = 0, j = 0; i < len; i++, j += step) | 650 for (octave_idx_type i = 0, j = 0; i < len; i++, j += step) |
650 dest[i] = ssrc[j]; | 651 dest[i] = ssrc[j]; |
651 } | 652 } |
652 } | 653 } |
653 break; | 654 break; |
654 | 655 |
655 case class_scalar: | 656 case class_scalar: |
656 { | 657 { |
657 idx_scalar_rep * r = dynamic_cast<idx_scalar_rep *> (rep); | 658 idx_scalar_rep * r = dynamic_cast<idx_scalar_rep *> (rep); |
658 dest[0] = src[r->get_data ()]; | 659 dest[0] = src[r->get_data ()]; |
659 } | 660 } |
660 break; | 661 break; |
661 | 662 |
662 case class_vector: | 663 case class_vector: |
663 { | 664 { |
664 idx_vector_rep * r = dynamic_cast<idx_vector_rep *> (rep); | 665 idx_vector_rep * r = dynamic_cast<idx_vector_rep *> (rep); |
665 const octave_idx_type *data = r->get_data (); | 666 const octave_idx_type *data = r->get_data (); |
666 for (octave_idx_type i = 0; i < len; i++) | 667 for (octave_idx_type i = 0; i < len; i++) |
667 dest[i] = src[data[i]]; | 668 dest[i] = src[data[i]]; |
668 } | 669 } |
669 break; | 670 break; |
670 | 671 |
671 case class_mask: | 672 case class_mask: |
672 { | 673 { |
673 idx_mask_rep * r = dynamic_cast<idx_mask_rep *> (rep); | 674 idx_mask_rep * r = dynamic_cast<idx_mask_rep *> (rep); |
674 const bool *data = r->get_data (); | 675 const bool *data = r->get_data (); |
675 octave_idx_type ext = r->extent (0); | 676 octave_idx_type ext = r->extent (0); |
676 for (octave_idx_type i = 0; i < ext; i++) | 677 for (octave_idx_type i = 0; i < ext; i++) |
677 if (data[i]) *dest++ = src[i]; | 678 if (data[i]) *dest++ = src[i]; |
678 } | 679 } |
679 break; | 680 break; |
680 | 681 |
681 default: | 682 default: |
682 assert (false); | 683 assert (false); |
683 break; | 684 break; |
684 } | 685 } |
685 | 686 |
686 return len; | 687 return len; |
687 } | 688 } |
688 | 689 |
689 // Slice assignment with specializations. No checking of bounds! | 690 // Slice assignment with specializations. No checking of bounds! |
690 // | 691 // |
691 // This is equivalent to the following loop (but much faster): | 692 // This is equivalent to the following loop (but much faster): |
692 // | 693 // |
695 // return i; | 696 // return i; |
696 // | 697 // |
697 template <class T> | 698 template <class T> |
698 octave_idx_type | 699 octave_idx_type |
699 assign (const T *src, octave_idx_type n, T *dest) const | 700 assign (const T *src, octave_idx_type n, T *dest) const |
700 { | 701 { |
701 octave_idx_type len = rep->length (n); | 702 octave_idx_type len = rep->length (n); |
702 | 703 |
703 switch (rep->idx_class ()) | 704 switch (rep->idx_class ()) |
704 { | 705 { |
705 case class_colon: | 706 case class_colon: |
706 copy_or_memcpy (len, src, dest); | 707 copy_or_memcpy (len, src, dest); |
707 break; | 708 break; |
708 | 709 |
709 case class_range: | 710 case class_range: |
710 { | 711 { |
711 idx_range_rep * r = dynamic_cast<idx_range_rep *> (rep); | 712 idx_range_rep * r = dynamic_cast<idx_range_rep *> (rep); |
712 octave_idx_type start = r->get_start (), step = r->get_step (); | 713 octave_idx_type start = r->get_start (), step = r->get_step (); |
713 T *sdest = dest + start; | 714 T *sdest = dest + start; |
714 if (step == 1) | 715 if (step == 1) |
715 copy_or_memcpy (len, src, sdest); | 716 copy_or_memcpy (len, src, sdest); |
716 else if (step == -1) | 717 else if (step == -1) |
717 std::reverse_copy (src, src + len, sdest - len + 1); | 718 std::reverse_copy (src, src + len, sdest - len + 1); |
718 else | 719 else |
719 { | 720 { |
720 for (octave_idx_type i = 0, j = 0; i < len; i++, j += step) | 721 for (octave_idx_type i = 0, j = 0; i < len; i++, j += step) |
721 sdest[j] = src[i]; | 722 sdest[j] = src[i]; |
722 } | 723 } |
723 } | 724 } |
724 break; | 725 break; |
725 | 726 |
726 case class_scalar: | 727 case class_scalar: |
727 { | 728 { |
728 idx_scalar_rep * r = dynamic_cast<idx_scalar_rep *> (rep); | 729 idx_scalar_rep * r = dynamic_cast<idx_scalar_rep *> (rep); |
729 dest[r->get_data ()] = src[0]; | 730 dest[r->get_data ()] = src[0]; |
730 } | 731 } |
731 break; | 732 break; |
732 | 733 |
733 case class_vector: | 734 case class_vector: |
734 { | 735 { |
735 idx_vector_rep * r = dynamic_cast<idx_vector_rep *> (rep); | 736 idx_vector_rep * r = dynamic_cast<idx_vector_rep *> (rep); |
736 const octave_idx_type *data = r->get_data (); | 737 const octave_idx_type *data = r->get_data (); |
737 for (octave_idx_type i = 0; i < len; i++) | 738 for (octave_idx_type i = 0; i < len; i++) |
738 dest[data[i]] = src[i]; | 739 dest[data[i]] = src[i]; |
739 } | 740 } |
740 break; | 741 break; |
741 | 742 |
742 case class_mask: | 743 case class_mask: |
743 { | 744 { |
744 idx_mask_rep * r = dynamic_cast<idx_mask_rep *> (rep); | 745 idx_mask_rep * r = dynamic_cast<idx_mask_rep *> (rep); |
745 const bool *data = r->get_data (); | 746 const bool *data = r->get_data (); |
746 octave_idx_type ext = r->extent (0); | 747 octave_idx_type ext = r->extent (0); |
747 for (octave_idx_type i = 0; i < ext; i++) | 748 for (octave_idx_type i = 0; i < ext; i++) |
748 if (data[i]) dest[i] = *src++; | 749 if (data[i]) dest[i] = *src++; |
749 } | 750 } |
750 break; | 751 break; |
751 | 752 |
752 default: | 753 default: |
753 assert (false); | 754 assert (false); |
754 break; | 755 break; |
755 } | 756 } |
756 | 757 |
757 return len; | 758 return len; |
758 } | 759 } |
759 | 760 |
760 // Slice fill with specializations. No checking of bounds! | 761 // Slice fill with specializations. No checking of bounds! |
761 // | 762 // |
762 // This is equivalent to the following loop (but much faster): | 763 // This is equivalent to the following loop (but much faster): |
763 // | 764 // |
766 // return i; | 767 // return i; |
767 // | 768 // |
768 template <class T> | 769 template <class T> |
769 octave_idx_type | 770 octave_idx_type |
770 fill (const T& val, octave_idx_type n, T *dest) const | 771 fill (const T& val, octave_idx_type n, T *dest) const |
771 { | 772 { |
772 octave_idx_type len = rep->length (n); | 773 octave_idx_type len = rep->length (n); |
773 | 774 |
774 switch (rep->idx_class ()) | 775 switch (rep->idx_class ()) |
775 { | 776 { |
776 case class_colon: | 777 case class_colon: |
777 std::fill (dest, dest + len, val); | 778 std::fill (dest, dest + len, val); |
778 break; | 779 break; |
779 | 780 |
780 case class_range: | 781 case class_range: |
781 { | 782 { |
782 idx_range_rep * r = dynamic_cast<idx_range_rep *> (rep); | 783 idx_range_rep * r = dynamic_cast<idx_range_rep *> (rep); |
783 octave_idx_type start = r->get_start (), step = r->get_step (); | 784 octave_idx_type start = r->get_start (), step = r->get_step (); |
784 T *sdest = dest + start; | 785 T *sdest = dest + start; |
785 if (step == 1) | 786 if (step == 1) |
786 std::fill (sdest, sdest + len, val); | 787 std::fill (sdest, sdest + len, val); |
787 else if (step == -1) | 788 else if (step == -1) |
788 std::fill (sdest - len + 1, sdest + 1, val); | 789 std::fill (sdest - len + 1, sdest + 1, val); |
789 else | 790 else |
790 { | 791 { |
791 for (octave_idx_type i = 0, j = 0; i < len; i++, j += step) | 792 for (octave_idx_type i = 0, j = 0; i < len; i++, j += step) |
792 sdest[j] = val; | 793 sdest[j] = val; |
793 } | 794 } |
794 } | 795 } |
795 break; | 796 break; |
796 | 797 |
797 case class_scalar: | 798 case class_scalar: |
798 { | 799 { |
799 idx_scalar_rep * r = dynamic_cast<idx_scalar_rep *> (rep); | 800 idx_scalar_rep * r = dynamic_cast<idx_scalar_rep *> (rep); |
800 dest[r->get_data ()] = val; | 801 dest[r->get_data ()] = val; |
801 } | 802 } |
802 break; | 803 break; |
803 | 804 |
804 case class_vector: | 805 case class_vector: |
805 { | 806 { |
806 idx_vector_rep * r = dynamic_cast<idx_vector_rep *> (rep); | 807 idx_vector_rep * r = dynamic_cast<idx_vector_rep *> (rep); |
807 const octave_idx_type *data = r->get_data (); | 808 const octave_idx_type *data = r->get_data (); |
808 for (octave_idx_type i = 0; i < len; i++) | 809 for (octave_idx_type i = 0; i < len; i++) |
809 dest[data[i]] = val; | 810 dest[data[i]] = val; |
810 } | 811 } |
811 break; | 812 break; |
812 | 813 |
813 case class_mask: | 814 case class_mask: |
814 { | 815 { |
815 idx_mask_rep * r = dynamic_cast<idx_mask_rep *> (rep); | 816 idx_mask_rep * r = dynamic_cast<idx_mask_rep *> (rep); |
816 const bool *data = r->get_data (); | 817 const bool *data = r->get_data (); |
817 octave_idx_type ext = r->extent (0); | 818 octave_idx_type ext = r->extent (0); |
818 for (octave_idx_type i = 0; i < ext; i++) | 819 for (octave_idx_type i = 0; i < ext; i++) |
819 if (data[i]) dest[i] = val; | 820 if (data[i]) dest[i] = val; |
820 } | 821 } |
821 break; | 822 break; |
822 | 823 |
823 default: | 824 default: |
824 assert (false); | 825 assert (false); |
825 break; | 826 break; |
826 } | 827 } |
827 | 828 |
828 return len; | 829 return len; |
829 } | 830 } |
830 | 831 |
831 // Generic non-breakable indexed loop. The loop body should be | 832 // Generic non-breakable indexed loop. The loop body should be |
832 // encapsulated in a single functor body. This is equivalent to the | 833 // encapsulated in a single functor body. This is equivalent to the |
833 // following loop (but faster, at least for simple inlined bodies): | 834 // following loop (but faster, at least for simple inlined bodies): |
834 // | 835 // |
835 // for (octave_idx_type i = 0; i < idx->length (n); i++) body (idx(i)); | 836 // for (octave_idx_type i = 0; i < idx->length (n); i++) body (idx(i)); |
836 | 837 |
837 template <class Functor> | 838 template <class Functor> |
838 void | 839 void |
839 loop (octave_idx_type n, Functor body) const | 840 loop (octave_idx_type n, Functor body) const |
840 { | 841 { |
841 octave_idx_type len = rep->length (n); | 842 octave_idx_type len = rep->length (n); |
842 | 843 |
843 switch (rep->idx_class ()) | 844 switch (rep->idx_class ()) |
844 { | 845 { |
845 case class_colon: | 846 case class_colon: |
846 for (octave_idx_type i = 0; i < len; i++) body (i); | 847 for (octave_idx_type i = 0; i < len; i++) body (i); |
847 break; | 848 break; |
848 | 849 |
849 case class_range: | 850 case class_range: |
850 { | 851 { |
851 idx_range_rep * r = dynamic_cast<idx_range_rep *> (rep); | 852 idx_range_rep * r = dynamic_cast<idx_range_rep *> (rep); |
852 octave_idx_type start = r->get_start (), step = r->get_step (); | 853 octave_idx_type start = r->get_start (), step = r->get_step (); |
853 octave_idx_type i, j; | 854 octave_idx_type i, j; |
854 if (step == 1) | 855 if (step == 1) |
855 for (i = start, j = start + len; i < j; i++) body (i); | 856 for (i = start, j = start + len; i < j; i++) body (i); |
856 else if (step == -1) | 857 else if (step == -1) |
857 for (i = start, j = start - len; i > j; i--) body (i); | 858 for (i = start, j = start - len; i > j; i--) body (i); |
858 else | 859 else |
859 for (i = 0, j = start; i < len; i++, j += step) body (j); | 860 for (i = 0, j = start; i < len; i++, j += step) body (j); |
860 } | 861 } |
861 break; | 862 break; |
862 | 863 |
863 case class_scalar: | 864 case class_scalar: |
864 { | 865 { |
865 idx_scalar_rep * r = dynamic_cast<idx_scalar_rep *> (rep); | 866 idx_scalar_rep * r = dynamic_cast<idx_scalar_rep *> (rep); |
866 body (r->get_data ()); | 867 body (r->get_data ()); |
867 } | 868 } |
868 break; | 869 break; |
869 | 870 |
870 case class_vector: | 871 case class_vector: |
871 { | 872 { |
872 idx_vector_rep * r = dynamic_cast<idx_vector_rep *> (rep); | 873 idx_vector_rep * r = dynamic_cast<idx_vector_rep *> (rep); |
873 const octave_idx_type *data = r->get_data (); | 874 const octave_idx_type *data = r->get_data (); |
874 for (octave_idx_type i = 0; i < len; i++) body (data[i]); | 875 for (octave_idx_type i = 0; i < len; i++) body (data[i]); |
875 } | 876 } |
876 break; | 877 break; |
877 | 878 |
878 case class_mask: | 879 case class_mask: |
879 { | 880 { |
880 idx_mask_rep * r = dynamic_cast<idx_mask_rep *> (rep); | 881 idx_mask_rep * r = dynamic_cast<idx_mask_rep *> (rep); |
881 const bool *data = r->get_data (); | 882 const bool *data = r->get_data (); |
882 octave_idx_type ext = r->extent (0); | 883 octave_idx_type ext = r->extent (0); |
883 for (octave_idx_type i = 0; i < ext; i++) | 884 for (octave_idx_type i = 0; i < ext; i++) |
884 if (data[i]) body (i); | 885 if (data[i]) body (i); |
885 } | 886 } |
886 break; | 887 break; |
887 | 888 |
888 default: | 889 default: |
889 assert (false); | 890 assert (false); |
890 break; | 891 break; |
891 } | 892 } |
892 | 893 |
893 } | 894 } |
894 | 895 |
895 // Generic breakable indexed loop. The loop body should be | 896 // Generic breakable indexed loop. The loop body should be |
896 // encapsulated in a single functor body. This is equivalent to the | 897 // encapsulated in a single functor body. This is equivalent to the |
897 // following loop (but faster, at least for simple inlined bodies): | 898 // following loop (but faster, at least for simple inlined bodies): |
898 // | 899 // |
902 // | 903 // |
903 | 904 |
904 template <class Functor> | 905 template <class Functor> |
905 octave_idx_type | 906 octave_idx_type |
906 bloop (octave_idx_type n, Functor body) const | 907 bloop (octave_idx_type n, Functor body) const |
907 { | 908 { |
908 octave_idx_type len = rep->length (n), ret; | 909 octave_idx_type len = rep->length (n), ret; |
909 | 910 |
910 switch (rep->idx_class ()) | 911 switch (rep->idx_class ()) |
911 { | 912 { |
912 case class_colon: | 913 case class_colon: |
913 { | 914 { |
914 octave_idx_type i; | 915 octave_idx_type i; |
915 for (i = 0; i < len && body (i); i++) ; | 916 for (i = 0; i < len && body (i); i++) ; |
916 ret = i; | 917 ret = i; |
917 } | 918 } |
918 break; | 919 break; |
919 | 920 |
920 case class_range: | 921 case class_range: |
921 { | 922 { |
922 idx_range_rep * r = dynamic_cast<idx_range_rep *> (rep); | 923 idx_range_rep * r = dynamic_cast<idx_range_rep *> (rep); |
923 octave_idx_type start = r->get_start (), step = r->get_step (); | 924 octave_idx_type start = r->get_start (), step = r->get_step (); |
924 octave_idx_type i, j; | 925 octave_idx_type i, j; |
925 if (step == 1) | 926 if (step == 1) |
926 for (i = start, j = start + len; i < j && body (i); i++) ; | 927 for (i = start, j = start + len; i < j && body (i); i++) ; |
927 else if (step == -1) | 928 else if (step == -1) |
928 for (i = start, j = start - len; i > j && body (i); i--) ; | 929 for (i = start, j = start - len; i > j && body (i); i--) ; |
929 else | 930 else |
930 for (i = 0, j = start; i < len && body (j); i++, j += step) ; | 931 for (i = 0, j = start; i < len && body (j); i++, j += step) ; |
931 ret = i; | 932 ret = i; |
932 } | 933 } |
933 break; | 934 break; |
934 | 935 |
935 case class_scalar: | 936 case class_scalar: |
936 { | 937 { |
937 idx_scalar_rep * r = dynamic_cast<idx_scalar_rep *> (rep); | 938 idx_scalar_rep * r = dynamic_cast<idx_scalar_rep *> (rep); |
938 ret = body (r->get_data ()) ? 1 : 0; | 939 ret = body (r->get_data ()) ? 1 : 0; |
939 } | 940 } |
940 break; | 941 break; |
941 | 942 |
942 case class_vector: | 943 case class_vector: |
943 { | 944 { |
944 idx_vector_rep * r = dynamic_cast<idx_vector_rep *> (rep); | 945 idx_vector_rep * r = dynamic_cast<idx_vector_rep *> (rep); |
945 const octave_idx_type *data = r->get_data (); | 946 const octave_idx_type *data = r->get_data (); |
946 octave_idx_type i; | 947 octave_idx_type i; |
947 for (i = 0; i < len && body (data[i]); i++) ; | 948 for (i = 0; i < len && body (data[i]); i++) ; |
948 ret = i; | 949 ret = i; |
949 } | 950 } |
950 break; | 951 break; |
951 | 952 |
952 case class_mask: | 953 case class_mask: |
953 { | 954 { |
954 idx_mask_rep * r = dynamic_cast<idx_mask_rep *> (rep); | 955 idx_mask_rep * r = dynamic_cast<idx_mask_rep *> (rep); |
955 const bool *data = r->get_data (); | 956 const bool *data = r->get_data (); |
956 octave_idx_type ext = r->extent (0), j = 0; | 957 octave_idx_type ext = r->extent (0), j = 0; |
957 for (octave_idx_type i = 0; i < ext; i++) | 958 for (octave_idx_type i = 0; i < ext; i++) |
958 { | 959 { |
959 if (data[i]) | 960 if (data[i]) |
960 { | 961 { |
961 if (body (i)) | 962 if (body (i)) |
962 break; | 963 break; |
963 else | 964 else |
964 j++; | 965 j++; |
965 } | 966 } |
966 } | 967 } |
967 | 968 |
968 ret = j; | 969 ret = j; |
969 } | 970 } |
970 break; | 971 break; |
971 | 972 |
972 default: | 973 default: |
973 assert (false); | 974 assert (false); |
974 break; | 975 break; |
975 } | 976 } |
976 | 977 |
977 return ret; | 978 return ret; |
978 } | 979 } |
979 | 980 |
980 // Rationale: | 981 // Rationale: |
981 // This method is the key to "smart indexing". When indexing cartesian | 982 // This method is the key to "smart indexing". When indexing cartesian |
982 // arrays, sometimes consecutive index vectors can be reduced into a | 983 // arrays, sometimes consecutive index vectors can be reduced into a |
983 // single index. If rows (A) = k and i.maybe_reduce (j) gives k, then | 984 // single index. If rows (A) = k and i.maybe_reduce (j) gives k, then |
1020 // necessary to mutate the index. | 1021 // necessary to mutate the index. |
1021 const octave_idx_type *raw (void); | 1022 const octave_idx_type *raw (void); |
1022 | 1023 |
1023 bool is_vector (void) const; | 1024 bool is_vector (void) const; |
1024 | 1025 |
1025 // FIXME -- these are here for compatibility. They should be removed | 1026 // FIXME: these are here for compatibility. They should be removed |
1026 // when no longer in use. | 1027 // when no longer in use. |
1027 | 1028 |
1028 octave_idx_type elem (octave_idx_type n) const | 1029 octave_idx_type elem (octave_idx_type n) const |
1029 { return (*this) (n); } | 1030 { return (*this) (n); } |
1030 | 1031 |
1031 bool is_colon_equiv (octave_idx_type n, int) const | 1032 bool is_colon_equiv (octave_idx_type n, int) const |
1032 { return is_colon_equiv (n); } | 1033 { return is_colon_equiv (n); } |
1033 | 1034 |
1034 octave_idx_type | 1035 octave_idx_type |
1035 freeze (octave_idx_type z_len, const char *tag, bool resize_ok = false); | 1036 freeze (octave_idx_type z_len, const char *tag, bool resize_ok = false); |
1036 | 1037 |
1037 void sort (bool uniq = false) | 1038 void sort (bool uniq = false) |
1038 { *this = sorted (uniq); } | 1039 { *this = sorted (uniq); } |
1039 | 1040 |
1040 octave_idx_type ones_count (void) const; | 1041 octave_idx_type ones_count (void) const; |
1041 | 1042 |
1042 octave_idx_type max (void) const { return extent (1) - 1; } | 1043 octave_idx_type max (void) const { return extent (1) - 1; } |
1043 | 1044 |