26 #if ! defined (octave_Sparse_h)
27 #define octave_Sparse_h 1
29 #include "octave-config.h"
45 template <
typename T,
typename Alloc>
77 octave::refcount<octave_idx_type>
m_count;
80 :
Alloc (), m_data (T_allocate (1)), m_ridx (idx_type_allocate (1)),
81 m_cidx (idx_type_allocate (1)),
82 m_nzmax (1), m_nrows (0), m_ncols (0), m_count (1)
86 :
Alloc (), m_data (T_allocate (1)), m_ridx (idx_type_allocate (1)),
87 m_cidx (idx_type_allocate (
n+1)),
88 m_nzmax (1), m_nrows (
n), m_ncols (
n), m_count (1)
92 :
Alloc (), m_data (T_allocate (nz > 0 ? nz : 1)),
93 m_ridx (idx_type_allocate (nz > 0 ? nz : 1)),
94 m_cidx (idx_type_allocate (nc+1)),
95 m_nzmax (nz > 0 ? nz : 1), m_nrows (nr), m_ncols (nc), m_count (1)
100 :
Alloc (), m_data (T_allocate (nz)),
101 m_ridx (idx_type_allocate (nz)),
102 m_cidx (idx_type_allocate (nc+1)),
103 m_nzmax (nz), m_nrows (nr), m_ncols (nc), m_count (1)
105 std::copy_n (
d, nz, m_data);
106 std::copy_n (
r, nz, m_ridx);
107 std::copy_n (c, m_ncols + 1, m_cidx);
110 template <
typename U>
113 :
Alloc (), m_data (T_allocate (nz)),
114 m_ridx (idx_type_allocate (nz)),
115 m_cidx (idx_type_allocate (nc+1)),
116 m_nzmax (nz), m_nrows (nr), m_ncols (nc), m_count (1)
118 std::copy_n (
d, nz, m_data);
119 std::copy_n (
r, nz, m_ridx);
120 std::copy_n (c, nc + 1, m_cidx);
123 template <
typename U>
127 :
Alloc (xallocator), m_data (
d), m_ridx (
r), m_cidx (c),
128 m_nzmax (nz), m_nrows (dv(0)), m_ncols (dv(1)), m_count (1)
132 :
Alloc (), m_data (T_allocate (a.m_nzmax)),
133 m_ridx (idx_type_allocate (a.m_nzmax)),
134 m_cidx (idx_type_allocate (a.m_ncols + 1)),
135 m_nzmax (a.m_nzmax), m_nrows (a.m_nrows), m_ncols (a.m_ncols),
139 std::copy_n (a.
m_data, nz, m_data);
140 std::copy_n (a.
m_ridx, nz, m_ridx);
141 std::copy_n (a.
m_cidx, m_ncols + 1, m_cidx);
146 T_deallocate (m_data, m_nzmax);
147 idx_type_deallocate (m_ridx, m_nzmax);
148 idx_type_deallocate (m_cidx, m_ncols + 1);
174 OCTAVE_API void maybe_compress (
bool remove_zeros);
185 template <
typename U,
typename A>
friend class Sparse;
193 typename T_Alloc_traits::allocator_type& alloc = *
this;
196 for (
size_t i = 0; i <
len; i++)
197 T_Alloc_traits::construct (alloc,
data+i);
204 typename T_Alloc_traits::allocator_type& alloc = *
this;
206 for (
size_t i = 0; i <
len; i++)
207 T_Alloc_traits::destroy (alloc,
data+i);
208 T_Alloc_traits::deallocate (alloc,
data,
len);
213 typename idx_type_Alloc_traits::allocator_type alloc = *
this;
216 for (
size_t i = 0; i <
len; i++)
217 idx_type_Alloc_traits::construct (alloc, idx+i);
224 typename idx_type_Alloc_traits::allocator_type alloc = *
this;
226 for (
size_t i = 0; i <
len; i++)
227 idx_type_Alloc_traits::destroy (alloc, idx+i);
228 idx_type_Alloc_traits::deallocate (alloc, idx,
len);
302 template <
typename U>
359 while (
cidx (ret+1) < k)
367 +
static_cast<std::size_t
> (
nzmax ())
426 if (n < 0 || n >=
numel ())
437 if (i < 0 || j < 0 || i >=
dim1 () || j >=
dim2 ())
488 if (n < 0 || n >=
numel ())
496 if (i < 0 || j < 0 || i >=
dim1 () || j >=
dim2 ())
622 bool resize_ok =
false)
const;
638 print_info (std::ostream& os,
const std::string& prefix)
const;
655 template <
typename F,
bool zero>
658 return octave::any_all_test<F, T, zero> (fcn,
data (),
nnz ());
662 template <
typename F>
664 {
return test<F, false> (fcn); }
666 template <
typename F>
668 {
return test<F, true> (fcn); }
672 {
return test<bool (&) (T), false> (fcn); }
675 {
return test<bool (&) (const T&), false> (fcn); }
678 {
return test<bool (&) (T), true> (fcn); }
681 {
return test<bool (&) (const T&), true> (fcn); }
683 template <
typename U,
typename F>
688 U f_zero = fcn (0.0);
715 result.
cidx (ii) = 0;
721 U val = fcn (
data (i));
724 result.
data (ii) = val;
729 result.
cidx (j+1) = ii;
739 template <
typename U>
742 {
return map<U, U (&) (T)> (fcn); }
744 template <
typename U>
746 map (U (&fcn) (
const T&))
const
747 {
return map<U, U (&) (const T&)> (fcn); }
755 template <
typename T>
759 T (*read_fcn) (std::istream&));
octave_idx_type compute_index(octave_idx_type n, const dim_vector &dims)
std::istream & read_sparse_matrix(std::istream &is, Sparse< T > &a, T(*read_fcn)(std::istream &))
SparseRep(const dim_vector &dv, octave_idx_type nz, U *d, octave_idx_type *r, octave_idx_type *c, const Alloc &xallocator=Alloc())
octave_idx_type cols() const
bool any_element_is_nan() const
void change_length(octave_idx_type nz)
octave_idx_type nzmax() const
octave_idx_type nnz() const
void T_deallocate(T_pointer data, size_t len)
octave::refcount< octave_idx_type > m_count
Alloc_traits::template rebind_traits< T > T_Alloc_traits
SparseRep(octave_idx_type n)
SparseRep(octave_idx_type nr, octave_idx_type nc, octave_idx_type nz, const T *d, const octave_idx_type *r, const octave_idx_type *c)
octave_idx_type columns() const
octave_idx_type rows() const
T cdata(octave_idx_type i) const
T_pointer T_allocate(size_t len)
SparseRep(const SparseRep &a)
octave_idx_type & cidx(octave_idx_type i)
void maybe_compress(bool remove_zeros)
SparseRep(octave_idx_type nr, octave_idx_type nc, octave_idx_type nz=1)
T & data(octave_idx_type i)
octave_idx_type length() const
SparseRep(octave_idx_type nr, octave_idx_type nc, octave_idx_type nz, const U *d, const octave_idx_type *r, const octave_idx_type *c)
T & elem(octave_idx_type r, octave_idx_type c)
T_Alloc_traits::pointer T_pointer
idx_type_pointer idx_type_allocate(size_t len)
std::allocator_traits< Alloc > Alloc_traits
octave_idx_type cridx(octave_idx_type i) const
idx_type_Alloc_traits::pointer idx_type_pointer
octave_idx_type & ridx(octave_idx_type i)
octave_idx_type ccidx(octave_idx_type i) const
SparseRep & operator=(const SparseRep &)=delete
Alloc_traits::template rebind_traits< octave_idx_type > idx_type_Alloc_traits
T celem(octave_idx_type r, octave_idx_type c) const
void idx_type_deallocate(idx_type_pointer idx, size_t len)
octave_idx_type cols() const
Sparse< T, Alloc > diag(octave_idx_type k=0) const
T xelem(const Array< octave_idx_type > &ra_idx) const
Sparse< T, Alloc > permute(const Array< octave_idx_type > &vec, bool inv=false) const
Sparse(octave_idx_type nr, octave_idx_type nc, octave_idx_type nz)
bool test_all(F fcn) const
octave_idx_type * xcidx()
octave_idx_type nzmax() const
Amount of storage for nonzero elements.
T checkelem(octave_idx_type n) const
Sparse< T, Alloc > squeeze() const
Sparse(const dim_vector &dv, octave_idx_type nz)
T elem(const Array< octave_idx_type > &ra_idx) const
octave_idx_type get_col_index(octave_idx_type k)
Array< T > array_value() const
void delete_elements(const octave::idx_vector &i)
T & xelem(octave_idx_type i, octave_idx_type j)
Sparse< T, Alloc > index(const octave::idx_vector &i, bool resize_ok=false) const
void print_info(std::ostream &os, const std::string &prefix) const
T & elem(const Array< octave_idx_type > &ra_idx)
octave_idx_type columns() const
bool test_any(bool(&fcn)(const T &)) const
void assign(const octave::idx_vector &i, const Sparse< T, Alloc > &rhs)
T xelem(octave_idx_type n) const
octave_idx_type & cidx(octave_idx_type i)
void resize(octave_idx_type r, octave_idx_type c)
Sparse< U > map(U(&fcn)(T)) const
bool test_any(bool(&fcn)(T)) const
T & data(octave_idx_type i)
T & checkelem(octave_idx_type n)
Sparse(octave_idx_type n)
Sparse< T, Alloc >::SparseRep * m_rep
T & operator()(octave_idx_type n)
Sparse< U > map(F fcn) const
Sparse< T, Alloc > sort(octave_idx_type dim=0, sortmode mode=ASCENDING) const
octave_idx_type get_row_index(octave_idx_type k)
bool test_any(F fcn) const
octave_idx_type & xridx(octave_idx_type i)
octave_idx_type & ridx(octave_idx_type i)
Sparse< T, Alloc > & insert(const Sparse< T, Alloc > &a, octave_idx_type r, octave_idx_type c)
T checkelem(const Array< octave_idx_type > &ra_idx) const
octave_idx_type ridx(octave_idx_type i) const
octave_idx_type ndims() const
T & checkelem(octave_idx_type i, octave_idx_type j)
void resize1(octave_idx_type n)
T elem(octave_idx_type n) const
Sparse< T, Alloc > maybe_compress(bool remove_zeros=false)
bool test_all(bool(&fcn)(T)) const
Sparse(const Sparse< U > &a)
OCTAVE_NORETURN T range_error(const char *fcn, octave_idx_type n) const
T & xdata(octave_idx_type i)
T & elem(octave_idx_type n)
octave_idx_type numel() const
T & xelem(octave_idx_type n)
static Sparse< T, Alloc > cat(int dim, octave_idx_type n, const Sparse< T, Alloc > *sparse_list)
T data(octave_idx_type i) const
T & elem(octave_idx_type i, octave_idx_type j)
bool test_all(bool(&fcn)(const T &)) const
Sparse(const dim_vector &dv, octave_idx_type nz, T *ptr, octave_idx_type *ridx, octave_idx_type *cidx, const Alloc &xallocator=Alloc())
T xelem(octave_idx_type i, octave_idx_type j) const
std::size_t byte_size() const
octave_idx_type dim2() const
octave_idx_type dim1() const
T elem(octave_idx_type i, octave_idx_type j) const
Sparse< T, Alloc > & operator=(const Sparse< T, Alloc > &a)
octave_idx_type nnz() const
Actual number of nonzero terms.
Sparse(const Sparse< T, Alloc > &a)
T checkelem(octave_idx_type i, octave_idx_type j) const
octave_idx_type rows() const
void change_capacity(octave_idx_type nz)
Sparse(octave_idx_type nr, octave_idx_type nc)
octave_idx_type * cidx() const
Sparse< T, Alloc > transpose() const
T & checkelem(const Array< octave_idx_type > &ra_idx)
Sparse< T, Alloc > ipermute(const Array< octave_idx_type > &vec) const
octave_idx_type * ridx() const
T & xelem(const Array< octave_idx_type > &ra_idx)
octave_idx_type cidx(octave_idx_type i) const
bool any_element_is_nan() const
octave_idx_type & xcidx(octave_idx_type i)
Sparse< U > map(U(&fcn)(const T &)) const
Sparse< T, Alloc > reshape(const dim_vector &new_dims) const
octave_idx_type * xridx()
Vector representing the dimensions (size) of an Array.
octave_idx_type safe_numel() const
The following function will throw a std::bad_alloc () exception if the requested size is larger than ...
octave_idx_type ndims() const
Number of dimensions.
octave::idx_vector idx_vector
F77_RET_T const F77_DBLE const F77_DBLE F77_DBLE * d
T::size_type numel(const T &str)
const octave_base_value const Array< octave_idx_type > & ra_idx