26 #if ! defined (octave_idx_vector_h)
27 #define octave_idx_vector_h 1
29 #include "octave-config.h"
73 template <
typename T,
typename D>
friend class std::unique_ptr;
81 idx_base_rep () : m_count (1) { }
83 OCTAVE_DISABLE_COPY_MOVE (idx_base_rep)
85 virtual ~idx_base_rep () =
default;
100 virtual idx_class_type idx_class ()
const {
return class_invalid; }
103 virtual idx_base_rep * sort_uniq_clone (
bool uniq =
false) = 0;
114 virtual std::ostream& print (std::ostream& os)
const = 0;
122 class OCTAVE_API idx_colon_rep :
public idx_base_rep
126 idx_colon_rep () =
default;
130 OCTAVE_DISABLE_COPY_MOVE (idx_colon_rep)
132 ~idx_colon_rep () =
default;
142 idx_class_type idx_class ()
const {
return class_colon; }
144 idx_base_rep * sort_uniq_clone (
bool =
false)
145 { m_count++;
return this; }
151 OCTAVE_API std::ostream& print (std::ostream& os)
const;
155 enum direct { DIRECT };
158 class OCTAVE_API idx_range_rep :
public idx_base_rep
162 idx_range_rep () =
delete;
166 : idx_base_rep (), m_start (start), m_len (
len), m_step (step) { }
175 OCTAVE_DISABLE_COPY_MOVE (idx_range_rep)
177 ~idx_range_rep () =
default;
180 {
return m_start + i * m_step; }
188 return m_len ?
std::max (
n, m_start + 1 + (m_step < 0 ? 0 : m_step * (m_len - 1))) :
n;
191 idx_class_type idx_class ()
const {
return class_range; }
193 OCTAVE_API idx_base_rep * sort_uniq_clone (
bool uniq =
false);
198 {
return m_start == 0 && m_step == 1 && m_len ==
n; }
207 OCTAVE_API std::ostream& print (std::ostream& os)
const;
219 class OCTAVE_API idx_scalar_rep :
public idx_base_rep
223 idx_scalar_rep () =
delete;
225 idx_scalar_rep (
octave_idx_type i, direct) : idx_base_rep (), m_data (i) { }
227 OCTAVE_DISABLE_COPY_MOVE (idx_scalar_rep)
229 ~idx_scalar_rep () =
default;
234 template <
typename T>
235 idx_scalar_rep (T
x);
246 idx_class_type idx_class ()
const {
return class_scalar; }
248 idx_base_rep * sort_uniq_clone (
bool =
false)
249 { m_count++;
return this; }
254 {
return n == 1 && m_data == 0; }
260 OCTAVE_API std::ostream& print (std::ostream& os)
const;
272 class OCTAVE_API idx_vector_rep :
public idx_base_rep
277 : m_data (nullptr), m_len (0), m_ext (0), m_aowner (nullptr), m_orig_dims () { }
282 : idx_base_rep (), m_data (data), m_len (
len), m_ext (ext),
283 m_aowner (nullptr), m_orig_dims (od)
292 template <
typename T>
301 OCTAVE_DISABLE_COPY_MOVE (idx_vector_rep)
314 idx_class_type idx_class ()
const {
return class_vector; }
316 idx_base_rep * sort_uniq_clone (
bool uniq =
false);
320 dim_vector orig_dimensions ()
const {
return m_orig_dims; }
324 OCTAVE_API std::ostream& print (std::ostream& os)
const;
349 class OCTAVE_API idx_mask_rep :
public idx_base_rep
353 idx_mask_rep () =
delete;
358 : idx_base_rep (), m_data (data), m_len (
len), m_ext (ext),
359 m_lsti (-1), m_lste (-1), m_aowner (nullptr), m_orig_dims (od)
366 OCTAVE_DISABLE_COPY_MOVE (idx_mask_rep)
379 idx_class_type idx_class ()
const {
return class_mask; }
381 idx_base_rep * sort_uniq_clone (
bool =
false)
382 { m_count++;
return this; }
386 dim_vector orig_dimensions ()
const {
return m_orig_dims; }
389 {
return m_len ==
n && m_ext ==
n; }
391 const bool * get_data ()
const {
return m_data; }
393 OCTAVE_API std::ostream& print (std::ostream& os)
const;
426 static OCTAVE_API idx_vector_rep * nil_rep ();
436 #if OCTAVE_SIZEOF_INT != OCTAVE_SIZEOF_IDX_TYPE
441 #if (OCTAVE_SIZEOF_F77_INT_TYPE != OCTAVE_SIZEOF_IDX_TYPE \
442 && OCTAVE_SIZEOF_F77_INT_TYPE != OCTAVE_SIZEOF_INT)
449 : m_rep (new idx_range_rep (start, limit, step)) { }
455 return idx_vector (
new idx_range_rep (start,
len, step, DIRECT));
459 : m_rep (new idx_vector_rep (inda)) { }
463 : m_rep (new idx_vector_rep (inda, ext, DIRECT)) { }
473 template <
typename T>
483 template <
typename T>
485 : m_rep (new idx_vector_rep (nda)) { }
501 if (--m_rep->m_count == 0 && m_rep != nil_rep ())
509 if (--m_rep->m_count == 0 && m_rep != nil_rep ())
521 {
return m_rep->length (
n); }
524 {
return m_rep->extent (
n); }
527 {
return m_rep->xelem (
n); }
530 {
return m_rep->xelem (
n); }
533 {
return m_rep->xelem (
n); }
537 OCTAVE_DEPRECATED (9,
"idx_vector::bool () is obsolete and always returns true")
538 operator
bool ()
const {
return true; }
541 {
return m_rep->idx_class () == class_colon; }
544 {
return m_rep->idx_class () == class_scalar; }
547 {
return m_rep->idx_class () == class_range; }
550 {
return m_rep->is_colon_equiv (
n); }
553 {
return idx_vector (m_rep->sort_uniq_clone (uniq)); }
556 {
return idx_vector (m_rep->sort_idx (sidx)); }
561 {
return orig_dimensions () (0); }
564 {
return orig_dimensions () (1); }
567 {
return (! is_colon () && orig_dimensions ().any_zero ()); }
571 std::ostream&
print (std::ostream& os)
const {
return m_rep->print (os); }
574 {
return a.
print (os); }
584 template <
typename T>
590 switch (m_rep->idx_class ())
593 std::copy_n (src,
len, dest);
598 idx_range_rep *
r =
dynamic_cast<idx_range_rep *
> (m_rep);
601 const T *ssrc = src + start;
603 std::copy_n (ssrc,
len, dest);
605 std::reverse_copy (ssrc -
len + 1, ssrc + 1, dest);
607 std::fill_n (dest,
len, *ssrc);
618 idx_scalar_rep *
r =
dynamic_cast<idx_scalar_rep *
> (m_rep);
619 dest[0] = src[
r->get_data ()];
625 idx_vector_rep *
r =
dynamic_cast<idx_vector_rep *
> (m_rep);
628 dest[i] = src[data[i]];
634 idx_mask_rep *
r =
dynamic_cast<idx_mask_rep *
> (m_rep);
635 const bool *data =
r->get_data ();
638 if (data[i]) *dest++ = src[i];
658 template <
typename T>
664 switch (m_rep->idx_class ())
667 std::copy_n (src,
len, dest);
672 idx_range_rep *
r =
dynamic_cast<idx_range_rep *
> (m_rep);
675 T *sdest = dest + start;
677 std::copy_n (src,
len, sdest);
679 std::reverse_copy (src, src +
len, sdest -
len + 1);
690 idx_scalar_rep *
r =
dynamic_cast<idx_scalar_rep *
> (m_rep);
691 dest[
r->get_data ()] = src[0];
697 idx_vector_rep *
r =
dynamic_cast<idx_vector_rep *
> (m_rep);
700 dest[data[i]] = src[i];
706 idx_mask_rep *
r =
dynamic_cast<idx_mask_rep *
> (m_rep);
707 const bool *data =
r->get_data ();
710 if (data[i]) dest[i] = *src++;
730 template <
typename T>
736 switch (m_rep->idx_class ())
739 std::fill_n (dest,
len, val);
744 idx_range_rep *
r =
dynamic_cast<idx_range_rep *
> (m_rep);
747 T *sdest = dest + start;
749 std::fill_n (sdest,
len, val);
751 std::fill (sdest -
len + 1, sdest + 1, val);
762 idx_scalar_rep *
r =
dynamic_cast<idx_scalar_rep *
> (m_rep);
763 dest[
r->get_data ()] = val;
769 idx_vector_rep *
r =
dynamic_cast<idx_vector_rep *
> (m_rep);
778 idx_mask_rep *
r =
dynamic_cast<idx_mask_rep *
> (m_rep);
779 const bool *data =
r->get_data ();
782 if (data[i]) dest[i] = val;
800 template <
typename Functor>
806 switch (m_rep->idx_class ())
814 idx_range_rep *
r =
dynamic_cast<idx_range_rep *
> (m_rep);
819 for (i = start, j = start +
len; i < j; i++) body (i);
821 for (i = start, j = start -
len; i > j; i--) body (i);
823 for (i = 0, j = start; i <
len; i++, j += step) body (j);
829 idx_scalar_rep *
r =
dynamic_cast<idx_scalar_rep *
> (m_rep);
830 body (
r->get_data ());
836 idx_vector_rep *
r =
dynamic_cast<idx_vector_rep *
> (m_rep);
844 idx_mask_rep *
r =
dynamic_cast<idx_mask_rep *
> (m_rep);
845 const bool *data =
r->get_data ();
848 if (data[i]) body (i);
868 template <
typename Functor>
874 switch (m_rep->idx_class ())
879 for (i = 0; i <
len && body (i); i++) ;
886 idx_range_rep *
r =
dynamic_cast<idx_range_rep *
> (m_rep);
891 for (i = start, j = start +
len; i < j && body (i); i++) ;
893 for (i = start, j = start -
len; i > j && body (i); i--) ;
895 for (i = 0, j = start; i <
len && body (j); i++, j += step) ;
902 idx_scalar_rep *
r =
dynamic_cast<idx_scalar_rep *
> (m_rep);
903 ret = (body (
r->get_data ()) ? 1 : 0);
909 idx_vector_rep *
r =
dynamic_cast<idx_vector_rep *
> (m_rep);
912 for (i = 0; i <
len && body (data[i]); i++) ;
919 idx_mask_rep *
r =
dynamic_cast<idx_mask_rep *
> (m_rep);
920 const bool *data =
r->get_data ();
996 {
return (*
this) (
n); }
999 {
return is_colon_equiv (
n); }
1005 { *
this = sorted (uniq); }
1013 idx_base_rep *m_rep;
1017 OCTAVE_END_NAMESPACE(
octave)
template std::ostream & operator<<(std::ostream &, const Array< bool > &)
dim_vector freeze(Array< octave::idx_vector > &ra_idx, const dim_vector &dimensions, int resize_ok)
bool isvector(const dim_vector &dim)
charNDArray max(char d, const charNDArray &m)
Vector representing the dimensions (size) of an Array.
octave_idx_type xelem(octave_idx_type n) const
octave_idx_type orig_rows() const
idx_class_type idx_class() const
idx_vector(const Array< octave_idx_type > &inda, octave_idx_type ext)
idx_vector sorted(bool uniq=false) const
octave_idx_type fill(const T &val, octave_idx_type n, T *dest) const
static const idx_vector colon
idx_vector(octave_int< T > x)
idx_vector(const range< double > &r)
dim_vector orig_dimensions() const
idx_vector(const Sparse< bool > &nda)
void loop(octave_idx_type n, Functor body) const
idx_vector(const Array< double > &nda)
octave_idx_type bloop(octave_idx_type n, Functor body) const
octave_idx_type length(octave_idx_type n=0) const
octave_idx_type index(const T *src, octave_idx_type n, T *dest) const
void sort(bool uniq=false)
idx_vector(const Array< octave_int< T >> &nda)
octave_idx_type elem(octave_idx_type n) const
octave_idx_type assign(const T *src, octave_idx_type n, T *dest) const
octave_idx_type checkelem(octave_idx_type n) const
idx_vector(const Array< float > &nda)
idx_vector sorted(Array< octave_idx_type > &sidx) const
std::ostream & print(std::ostream &os) const
static idx_vector make_range(octave_idx_type start, octave_idx_type step, octave_idx_type len)
idx_vector(const Array< octave_idx_type > &inda)
octave_idx_type max() const
idx_vector(const idx_vector &a)
idx_vector(octave_idx_type start, octave_idx_type limit, octave_idx_type step=1)
bool is_colon_equiv(octave_idx_type n, int) const
octave_idx_type extent(octave_idx_type n) const
bool is_colon_equiv(octave_idx_type n) const
idx_vector(octave_idx_type i)
octave_idx_type orig_columns() const
OCTAVE_BEGIN_NAMESPACE(octave) static octave_value daspk_fcn
octave::idx_vector idx_vector
F77_RET_T const F77_DBLE * x