comparison liboctave/dim-vector.h @ 10645:8645b7087859

abstract scalar index checking off Array<T> (prep for struct optimizations)
author Jaroslav Hajek <highegg@gmail.com>
date Thu, 20 May 2010 15:10:34 +0200
parents ec3cec8277df
children 0ba9bd294421
comparison
equal deleted inserted replaced
10644:45b72e631cb5 10645:8645b7087859
610 return def; 610 return def;
611 } 611 }
612 612
613 // Compute a linear index from an index tuple. 613 // Compute a linear index from an index tuple.
614 614
615 octave_idx_type compute_index (const octave_idx_type *idx) 615 octave_idx_type compute_index (const octave_idx_type *idx) const
616 { 616 {
617 octave_idx_type k = 0; 617 octave_idx_type k = 0;
618 for (int i = length () - 1; i >= 0; i--) 618 for (int i = length () - 1; i >= 0; i--)
619 k = k * rep[i] + idx[i]; 619 k = k * rep[i] + idx[i];
620 620
621 return k; 621 return k;
622 } 622 }
623 623
624 // Ditto, but the tuple may be incomplete (nidx < length ()).
625
626 octave_idx_type compute_index (const octave_idx_type *idx, int nidx) const
627 {
628 octave_idx_type k = 0;
629 for (int i = nidx - 1; i >= 0; i--)
630 k = k * rep[i] + idx[i];
631
632 return k;
633 }
634
624 // Increment a multi-dimensional index tuple, optionally starting 635 // Increment a multi-dimensional index tuple, optionally starting
625 // from an offset position and return the index of the last index 636 // from an offset position and return the index of the last index
626 // position that was changed, or length () if just cycled over. 637 // position that was changed, or length () if just cycled over.
627 638
628 int increment_index (octave_idx_type *idx, int start = 0) 639 int increment_index (octave_idx_type *idx, int start = 0) const
629 { 640 {
630 int i; 641 int i;
631 for (i = start; i < length (); i++) 642 for (i = start; i < length (); i++)
632 { 643 {
633 if (++(*idx) == rep[i]) 644 if (++(*idx) == rep[i])
653 } 664 }
654 665
655 // Compute a linear index from an index tuple. Dimensions are 666 // Compute a linear index from an index tuple. Dimensions are
656 // required to be cumulative. 667 // required to be cumulative.
657 668
658 octave_idx_type cum_compute_index (const octave_idx_type *idx) 669 octave_idx_type cum_compute_index (const octave_idx_type *idx) const
659 { 670 {
660 octave_idx_type k = idx[0]; 671 octave_idx_type k = idx[0];
661 672
662 for (int i = 1; i < length (); i++) 673 for (int i = 1; i < length (); i++)
663 k += rep[i-1] * idx[i]; 674 k += rep[i-1] * idx[i];