26#if ! defined (octave_Array_h)
27#define octave_Array_h 1
29#include "octave-config.h"
126template <
typename T,
typename Alloc>
140 typedef typename T_Alloc_traits::pointer
pointer;
147 :
Alloc (), m_data (allocate (
len)), m_len (
len), m_count (1)
149 std::copy_n (
d,
len, m_data);
152 template <
typename U>
154 :
Alloc (), m_data (allocate (
len)), m_len (
len), m_count (1)
156 std::copy_n (
d,
len, m_data);
163 :
Alloc (), m_data (allocate (0)), m_len (0), m_count (1) { }
166 :
Alloc (), m_data (allocate (
len)), m_len (
len), m_count (1) { }
169 :
Alloc (), m_data (allocate (
len)), m_len (
len), m_count (1)
171 std::fill_n (m_data,
len, val);
176 :
Alloc (xallocator), m_data (ptr), m_len (dv.safe_numel ()), m_count (1)
181 :
Alloc (), m_data (allocate (a.m_len)), m_len (a.m_len),
197 pointer data = Alloc_traits::allocate (*
this,
len);
198 for (
size_t i = 0; i <
len; i++)
199 T_Alloc_traits::construct (*
this, data+i);
205 for (
size_t i = 0; i <
len; i++)
206 T_Alloc_traits::destroy (*
this, data+i);
207 Alloc_traits::deallocate (*
this, data,
len);
217 if (m_rep->m_count > 1)
221 if (--m_rep->m_count == 0)
225 m_slice_data = m_rep->
m_data;
261 : m_dimensions (dv), m_rep(a.m_rep), m_slice_data (a.m_slice_data+l), m_slice_len (u-l)
275 : m_dimensions (), m_rep (nil_rep ()), m_slice_data (m_rep->m_data),
276 m_slice_len (m_rep->m_len)
285 m_slice_data (m_rep->m_data), m_slice_len (m_rep->m_len)
294 m_slice_data (m_rep->m_data), m_slice_len (m_rep->m_len)
310 m_slice_data (m_rep->m_data), m_slice_len (m_rep->m_len)
319 template<
template <
typename...>
class Container>
323 template <
typename U,
typename A = Alloc>
325 : m_dimensions (a.dims ()),
327 m_slice_data (m_rep->m_data), m_slice_len (m_rep->m_len)
332 : m_dimensions (a.m_dimensions), m_rep (a.m_rep), m_slice_data (a.m_slice_data),
333 m_slice_len (a.m_slice_len)
339 : m_dimensions (
std::move (a.m_dimensions)), m_rep (a.m_rep),
340 m_slice_data (a.m_slice_data), m_slice_len (a.m_slice_len)
343 a.m_slice_data =
nullptr;
355 if (m_rep && --m_rep->
m_count == 0)
381 m_dimensions = std::move (a.m_dimensions);
387 if (m_rep && --m_rep->
m_count == 0)
391 m_slice_data = a.m_slice_data;
392 m_slice_len = a.m_slice_len;
395 a.m_slice_data =
nullptr;
402 OCTARRAY_API
void fill (
const T& val);
404 OCTARRAY_API
void clear (
void);
405 OCTARRAY_API
void clear (
const dim_vector& dv);
418 if (m_dimensions.
ndims () != 2 || m_dimensions(1) != 1)
428 if (m_dimensions.
ndims () != 2 || m_dimensions(0) != 1)
438 if (m_dimensions.
ndims () != 2)
480 return d >= ndims () ? 1 : m_dimensions(
d);
484 {
return static_cast<std::size_t
> (
numel ()) *
sizeof (T); }
508 {
return xelem (dim1 ()*j+i); }
510 {
return xelem (dim1 ()*j+i); }
513 {
return xelem (i, dim2 ()*k+j); }
515 {
return xelem (i, dim2 ()*k+j); }
518 {
return xelem (compute_index_unchecked (
ra_idx)); }
521 {
return xelem (compute_index_unchecked (
ra_idx)); }
543 {
return elem (i, dim2 ()*k+j); }
551 {
return elem (i, j, k); }
567 {
return xelem (i, j); }
570 {
return xelem (i, j, k); }
577 {
return elem (i, j); }
580 {
return elem (i, j, k); }
603 {
return permute (vec,
true); }
605 bool issquare (
void)
const {
return (dim1 () == dim2 ()); }
614 OCTARRAY_API
Array<T, Alloc> hermitian (T (*fcn) (
const T&) =
nullptr)
const;
616 const T *
data (
void)
const {
return m_slice_data; }
618#if defined (OCTAVE_PROVIDE_DEPRECATED_SYMBOLS)
619 OCTAVE_DEPRECATED (7,
"for read-only access, use 'data' method instead")
620 const T * fortran_vec (
void)
const {
return data (); }
623 OCTARRAY_API T * fortran_vec (
void);
638 virtual OCTARRAY_API T resize_fill_value (
void)
const;
645 resize2 (nr, nc, resize_fill_value ());
651 OCTARRAY_API
void resize (
const dim_vector& dv,
const T& rfv);
664 return index (i, resize_ok, resize_fill_value ());
671 bool resize_ok)
const
673 return index (i, j, resize_ok, resize_fill_value ());
680 return index (ia, resize_ok, resize_fill_value ());
689 assign (i, rhs, resize_fill_value ());
697 assign (i, j, rhs, resize_fill_value ());
703 assign (ia, rhs, resize_fill_value ());
736 m_slice_data = m_rep->
m_data;
740 OCTARRAY_API
void print_info (std::ostream& os,
const std::string& prefix)
const;
770 bool backward =
false)
const;
792#if defined (OCTAVE_HAVE_STD_PMR_POLYMORPHIC_ALLOCATOR)
793 template <
typename U,
typename F,
794 typename A = std::pmr::polymorphic_allocator<U>>
796 template <
typename U,
typename F,
typename A = std::allocator<U>>
803 const T *m = data ();
809 for (i = 0; i <
len - 3; i += 4)
814 p[i+1] = fcn (m[i+1]);
815 p[i+2] = fcn (m[i+2]);
816 p[i+3] = fcn (m[i+3]);
829#if defined (OCTAVE_HAVE_STD_PMR_POLYMORPHIC_ALLOCATOR)
830 template <
typename U,
typename A = std::pmr::polymorphic_allocator<U>>
832 template <
typename U,
typename A = std::allocator<U>>
836 {
return map<U, U (&) (T), A> (fcn); }
838#if defined (OCTAVE_HAVE_STD_PMR_POLYMORPHIC_ALLOCATOR)
839 template <
typename U,
typename A = std::pmr::polymorphic_allocator<U>>
841 template <
typename U,
typename A = std::allocator<U>>
844 map (U (&fcn) (
const T&))
const
845 {
return map<U, U (&) (const T&), A> (fcn); }
849 template <
typename F,
bool zero>
852 return octave::any_all_test<F, T, zero> (fcn, data (),
numel ());
857 template <
typename F>
859 {
return test<F, false> (fcn); }
861 template <
typename F>
863 {
return test<F, true> (fcn); }
869 {
return test<bool (&) (T), false> (fcn); }
872 {
return test<bool (&) (const T&), false> (fcn); }
875 {
return test<bool (&) (T), true> (fcn); }
878 {
return test<bool (&) (const T&), true> (fcn); }
881 template <
typename U,
typename A>
friend class Array;
895template<
typename T,
typename Alloc>
896template<
template <
typename...>
class Container>
905 (*current_liboctave_error_handler)
906 (
"reshape: can't reshape %zi elements into %s array",
907 a.size (), new_dims_str.c_str ());
917template <
typename T,
typename Alloc>
918OCTARRAY_API std::ostream&
octave_idx_type compute_index(octave_idx_type n, const dim_vector &dims)
OCTARRAY_API std::ostream & operator<<(std::ostream &os, const Array< T, Alloc > &a)
octave_idx_type lookup(const T *x, octave_idx_type n, T y)
The real representation of all arrays.
T_Alloc_traits::pointer pointer
pointer allocate(size_t len)
ArrayRep(U *d, octave_idx_type len)
octave_idx_type numel(void) const
ArrayRep(pointer d, octave_idx_type len)
octave::refcount< octave_idx_type > m_count
ArrayRep(octave_idx_type len)
ArrayRep(octave_idx_type len, const T &val)
ArrayRep(pointer ptr, const dim_vector &dv, const Alloc &xallocator=Alloc())
std::allocator_traits< Alloc > Alloc_traits
ArrayRep(const ArrayRep &a)
void deallocate(pointer data, size_t len)
Alloc_traits::template rebind_traits< T > T_Alloc_traits
N Dimensional Array with copy-on-write semantics.
bool issquare(void) const
Size of the specified dimension.
Array< T, Alloc >::ArrayRep * m_rep
bool test_all(bool(&fcn)(const T &)) const
Size of the specified dimension.
T & xelem(octave_idx_type n)
Size of the specified dimension.
void resize2(octave_idx_type nr, octave_idx_type nc)
Size of the specified dimension.
crefT xelem(octave_idx_type n) const
Size of the specified dimension.
octave_idx_type numel(void) const
Number of elements in the array.
size_type size(const size_type d) const
Size of the specified dimension.
Array< T, Alloc > index(const Array< octave::idx_vector > &ia, bool resize_ok) const
Size of the specified dimension.
T & xelem(octave_idx_type i, octave_idx_type j)
Size of the specified dimension.
friend class Array
Size of the specified dimension.
crefT xelem(octave_idx_type i, octave_idx_type j) const
Size of the specified dimension.
octave_idx_type size_type
Used for operator(), and returned by numel() and size() (beware: signed integer)
Array< T, Alloc > ipermute(const Array< octave_idx_type > &vec) const
Size of the specified dimension.
void resize(const dim_vector &dv)
Size of the specified dimension.
crefT elem(octave_idx_type n) const
Size of the specified dimension.
bool test_all(bool(&fcn)(T)) const
Size of the specified dimension.
T & elem(octave_idx_type i, octave_idx_type j)
Size of the specified dimension.
Array(T *ptr, const dim_vector &dv, const Alloc &xallocator=Alloc())
octave_idx_type cols(void) const
bool test_all(F fcn) const
Size of the specified dimension.
const dim_vector & dims(void) const
Return a const-reference so that dims ()(i) works efficiently.
Array(const Array< U, A > &a)
Type conversion case.
octave_idx_type compute_index_unchecked(const Array< octave_idx_type > &ra_idx) const
Size of the specified dimension.
Array(void)
Empty ctor (0 by 0).
void resize1(octave_idx_type n)
Size of the specified dimension.
bool is_nd_vector(void) const
Size of the specified dimension.
octave_idx_type dim2(void) const
T & elem(octave_idx_type n)
Size of the specified dimension.
octave_idx_type m_slice_len
ref_param< T >::type crefT
crefT elem(octave_idx_type i, octave_idx_type j) const
Size of the specified dimension.
octave_idx_type rows(void) const
static OCTARRAY_API void instantiation_guard()
Size of the specified dimension.
T & xelem(const Array< octave_idx_type > &ra_idx)
Size of the specified dimension.
crefT xelem(octave_idx_type i, octave_idx_type j, octave_idx_type k) const
Size of the specified dimension.
Array< U, A > map(U(&fcn)(const T &)) const
Size of the specified dimension.
bool isvector(void) const
Size of the specified dimension.
bool isempty(void) const
Size of the specified dimension.
Array< U, A > map(F fcn) const
Apply function fcn to each element of the Array<T, Alloc>.
OCTARRAY_API bool optimize_dimensions(const dim_vector &dv)
Returns true if this->dims () == dv, and if so, replaces this->m_dimensions by a shallow copy of dv.
Array< T, Alloc > as_row(void) const
Return the array as a row vector.
void clear(octave_idx_type r, octave_idx_type c)
Array(const dim_vector &dv, const T &val)
nD initialized ctor.
Array(const dim_vector &dv)
nD uninitialized ctor.
bool test_any(F fcn) const
Simpler calls.
octave_idx_type columns(void) const
const T * data(void) const
Size of the specified dimension.
Array< T, Alloc > index(const octave::idx_vector &i, bool resize_ok) const
Size of the specified dimension.
Array< U, A > map(U(&fcn)(T)) const
Overloads for function references.
void assign(const octave::idx_vector &i, const Array< T, Alloc > &rhs)
Size of the specified dimension.
Array< T, Alloc > reshape(const dim_vector &new_dims) const
Size of the specified dimension.
void assign(const Array< octave::idx_vector > &ia, const Array< T, Alloc > &rhs)
Size of the specified dimension.
octave_idx_type dim1(void) const
OCTARRAY_API T * fortran_vec(void)
Size of the specified dimension.
void assign(const octave::idx_vector &i, const octave::idx_vector &j, const Array< T, Alloc > &rhs)
Size of the specified dimension.
Array< T, Alloc > as_matrix(void) const
Return the array as a matrix.
std::size_t byte_size(void) const
Size of the specified dimension.
Array(Array< T, Alloc > &&a)
Array(const Array< T, Alloc > &a, const dim_vector &dv, octave_idx_type l, octave_idx_type u)
slice constructor
Array< T, Alloc > as_column(void) const
Return the array as a column vector.
Array< T, Alloc > reshape(octave_idx_type nr, octave_idx_type nc) const
Size of the specified dimension.
bool test_any(bool(&fcn)(const T &)) const
Size of the specified dimension.
int ndims(void) const
Size of the specified dimension.
bool test(F fcn) const
Generic any/all test functionality with arbitrary predicate.
Array(const Container< T > &a, const dim_vector &dv)
Constructor from standard library sequence containers.
T & xelem(octave_idx_type i, octave_idx_type j, octave_idx_type k)
Size of the specified dimension.
octave_idx_type dim3(void) const
Size of the specified dimension.
octave_idx_type pages(void) const
Size of the specified dimension.
crefT elem(octave_idx_type i, octave_idx_type j, octave_idx_type k) const
Size of the specified dimension.
crefT elem(const Array< octave_idx_type > &ra_idx) const
Size of the specified dimension.
T & elem(octave_idx_type i, octave_idx_type j, octave_idx_type k)
Size of the specified dimension.
T & elem(const Array< octave_idx_type > &ra_idx)
Size of the specified dimension.
void maybe_economize(void)
Size of the specified dimension.
bool test_any(bool(&fcn)(T)) const
Overloads for function references.
Array< T, Alloc > index(const octave::idx_vector &i, const octave::idx_vector &j, bool resize_ok) const
Size of the specified dimension.
crefT xelem(const Array< octave_idx_type > &ra_idx) const
Size of the specified dimension.
bool is_shared(void)
Size of the specified dimension.
Array(const Array< T, Alloc > &a)
No type conversion case.
Vector representing the dimensions (size) of an Array.
octave_idx_type compute_index(const octave_idx_type *idx) const
Linear index from an index tuple.
OCTAVE_API std::string str(char sep='x') const
bool isvector(void) const
void chop_trailing_singletons(void)
octave_idx_type ndims(void) const
Number of dimensions.
bool is_nd_vector(void) const
OCTAVE_API dim_vector redim(int n) const
Force certain dimensionality, preserving numel ().
OCTAVE_API octave_idx_type safe_numel(void) const
The following function will throw a std::bad_alloc () exception if the requested size is larger than ...
virtual octave_idx_type numel(void) const
if_then_else< is_class_type< T >::no, T, Tconst & >::result type
static octave_idx_type find(octave_idx_type i, octave_idx_type *pp)
F77_RET_T const F77_INT F77_CMPLX * A
F77_RET_T const F77_DBLE const F77_DBLE F77_DBLE * d
F77_RET_T const F77_DBLE * x
void F(const TSRC *v, TRES *r, octave_idx_type m, octave_idx_type n)
T::size_type numel(const T &str)
const octave_base_value const Array< octave_idx_type > & ra_idx
static void transpose(octave_idx_type N, const octave_idx_type *ridx, const octave_idx_type *cidx, octave_idx_type *ridx2, octave_idx_type *cidx2)