26 #if ! defined (octave_Sparse_h)
27 #define octave_Sparse_h 1
29 #include "octave-config.h"
74 nzmx (1), nrows (0), ncols (0), count (1)
80 nzmx (1), nrows (
n), ncols (
n), count (1)
84 :
d (nz > 0 ? new T [nz] : new T [1]),
87 nzmx (nz > 0 ? nz : 1), nrows (nr), ncols (nc), count (1)
93 nzmx (a.nzmx), nrows (a.nrows), ncols (a.ncols), count (1)
96 std::copy_n (a.
d, nz,
d);
97 std::copy_n (a.
r, nz,
r);
98 std::copy_n (a.
c, ncols + 1, c);
123 void maybe_compress (
bool remove_zeros);
127 bool indices_ok (
void)
const;
129 bool any_element_is_nan (
void)
const;
146 if (--rep->count == 0)
169 : rep (nil_rep ()), dimensions (
dim_vector (0,0))
197 template <
typename U>
201 dimensions (a.dimensions)
204 std::copy_n (a.
rep->
d, nz, rep->
d);
205 std::copy_n (a.
rep->
r, nz, rep->
r);
206 std::copy_n (a.
rep->
c, rep->
ncols + 1, rep->
c);
211 : rep (a.rep), dimensions (a.dimensions)
258 while (cidx (ret+1) < k)
266 +
static_cast<size_t> (nzmax ())
281 OCTAVE_NORETURN T range_error (
const char *fcn,
283 OCTAVE_NORETURN T& range_error (
const char *fcn,
286 OCTAVE_NORETURN T range_error (
const char *fcn,
288 OCTAVE_NORETURN T& range_error (
const char *fcn,
310 return rep->
celem (i, j);
324 if (n < 0 || n >=
numel ())
326 return range_error (
"T& Sparse<T>::checkelem",
n);
336 if (i < 0 || j < 0 || i >= dim1 () || j >= dim2 ())
337 return range_error (
"T& Sparse<T>::checkelem", i, j);
350 return range_error (
"T& Sparse<T>::checkelem",
ra_idx);
387 if (n < 0 || n >=
numel ())
388 return range_error (
"T Sparse<T>::checkelem",
n);
395 if (i < 0 || j < 0 || i >= dim1 () || j >= dim2 ())
396 return range_error (
"T Sparse<T>::checkelem", i, j);
406 return range_error (
"T Sparse<T>::checkelem",
ra_idx);
445 return permute (vec,
true);
464 bool issquare (
void)
const {
return (dim1 () == dim2 ()); }
466 bool isempty (
void)
const {
return (rows () < 1 || cols () < 1); }
470 T *
data (
void) { make_unique ();
return rep->
d; }
477 T *
data (
void)
const {
return rep->
d; }
482 make_unique ();
return rep->
ridx (i);
495 make_unique ();
return rep->
cidx (i);
509 void delete_elements (
int dim,
const idx_vector& i);
516 bool resize_ok =
false)
const;
522 void print_info (std::ostream& os,
const std::string& prefix)
const;
551 template <
typename F,
bool zero>
554 return any_all_test<F, T, zero> (fcn, data (), nnz ());
558 template <
typename F>
560 {
return test<F, false> (fcn); }
562 template <
typename F>
564 {
return test<F, true> (fcn); }
568 {
return test<bool (&) (T), false> (fcn); }
571 {
return test<bool (&) (const T&), false> (fcn); }
574 {
return test<bool (&) (T), true> (fcn); }
577 {
return test<bool (&) (const T&), true> (fcn); }
579 template <
typename U,
typename F>
584 U f_zero = fcn (0.0);
598 result.
data (ridx (i) + j * nr) = fcn (data (i));
611 result.
cidx (ii) = 0;
617 U val = fcn (data (i));
620 result.
data (ii) = val;
621 result.
ridx (ii++) = ridx (i);
625 result.
cidx (j+1) = ii;
635 template <
typename U>
638 {
return map<U, U (&) (T)> (fcn); }
640 template <
typename U>
642 map (U (&fcn) (
const T&))
const
643 {
return map<U, U (&) (const T&)> (fcn); }
651 template <
typename T>
654 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 &))
N Dimensional Array with copy-on-write semantics.
octave::refcount< octave_idx_type > count
bool indices_ok(void) const
octave_idx_type & cidx(octave_idx_type i)
void change_length(octave_idx_type nz)
SparseRep(const SparseRep &a)
void maybe_compress(bool remove_zeros)
octave_idx_type & ridx(octave_idx_type i)
T celem(octave_idx_type _r, octave_idx_type _c) const
T & elem(octave_idx_type _r, octave_idx_type _c)
octave_idx_type length(void) const
T cdata(octave_idx_type i) const
SparseRep(octave_idx_type nr, octave_idx_type nc, octave_idx_type nz=1)
SparseRep(octave_idx_type n)
octave_idx_type ccidx(octave_idx_type i) const
T & data(octave_idx_type i)
octave_idx_type nnz(void) const
bool any_element_is_nan(void) const
octave_idx_type cridx(octave_idx_type i) const
bool any_element_is_nan(void) const
octave_idx_type ndims(void) const
T & elem(const Array< octave_idx_type > &ra_idx)
T & checkelem(octave_idx_type i, octave_idx_type j)
T & checkelem(octave_idx_type n)
T & checkelem(const Array< octave_idx_type > &ra_idx)
T & elem(octave_idx_type i, octave_idx_type j)
void change_capacity(octave_idx_type nz)
T checkelem(octave_idx_type n) const
bool test_any(bool(&fcn)(const T &)) const
Sparse(octave_idx_type n)
T elem(const Array< octave_idx_type > &ra_idx) const
Sparse(const dim_vector &dv, octave_idx_type nz)
T checkelem(octave_idx_type i, octave_idx_type j) const
Sparse< T > squeeze(void) const
octave_idx_type numel(void) const
bool test_all(bool(&fcn)(T)) const
bool test_all(bool(&fcn)(const T &)) const
Sparse< U > map(U(&fcn)(T)) const
T & xelem(const Array< octave_idx_type > &ra_idx)
octave_idx_type get_col_index(octave_idx_type k)
octave_idx_type cols(void) const
T checkelem(const Array< octave_idx_type > &ra_idx) const
T & xelem(octave_idx_type i, octave_idx_type j)
octave_idx_type * xridx(void)
T & xelem(octave_idx_type n)
octave_idx_type ridx(octave_idx_type i) const
T xelem(const Array< octave_idx_type > &ra_idx) const
Sparse(octave_idx_type nr, octave_idx_type nc)
T elem(octave_idx_type i, octave_idx_type j) const
T xelem(octave_idx_type n) const
bool test_any(F fcn) const
octave_idx_type * mex_get_ir(void) const
octave_idx_type * cidx(void)
octave_idx_type dim1(void) const
Sparse< T > ipermute(const Array< octave_idx_type > &vec) const
Sparse(const Sparse< U > &a)
T data(octave_idx_type i) const
octave_idx_type nnz(void) const
Actual number of nonzero terms.
octave_idx_type nzmax(void) const
Amount of storage for nonzero elements.
Sparse< T >::SparseRep * rep
octave_idx_type cidx(octave_idx_type i) const
octave_idx_type get_row_index(octave_idx_type k)
octave_idx_type * ridx(void) const
octave_idx_type rows(void) const
size_t byte_size(void) const
T & xdata(octave_idx_type i)
T xelem(octave_idx_type i, octave_idx_type j) const
octave_idx_type dim2(void) const
octave_idx_type columns(void) const
T elem(octave_idx_type n) const
octave_idx_type & ridx(octave_idx_type i)
dim_vector dims(void) const
void * mex_get_data(void) const
octave_idx_type * xcidx(void)
octave_idx_type * cidx(void) const
Sparse< U > map(U(&fcn)(const T &)) const
T & elem(octave_idx_type n)
octave_idx_type * mex_get_jc(void) const
Sparse< U > map(F fcn) const
Sparse(const Sparse< T > &a)
T & data(octave_idx_type i)
bool test_all(F fcn) const
bool test_any(bool(&fcn)(T)) const
Sparse(octave_idx_type nr, octave_idx_type nc, octave_idx_type nz)
bool indices_ok(void) const
octave_idx_type & cidx(octave_idx_type i)
Sparse< T > maybe_compress(bool remove_zeros=false)
bool issquare(void) const
octave_idx_type & xcidx(octave_idx_type i)
octave_idx_type & xridx(octave_idx_type i)
octave_idx_type * ridx(void)
Vector representing the dimensions (size) of an Array.
octave_idx_type ndims(void) const
Number of dimensions.
octave_idx_type safe_numel(void) const
The following function will throw a std::bad_alloc () exception if the requested size is larger than ...
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
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)