26 #if !defined (octave_Sparse_h)
27 #define octave_Sparse_h 1
89 : d (nz > 0 ? new T [nz] : 0),
101 nzmx (a.nzmx), nrows (a.nrows), ncols (a.ncols), count (1)
104 std::copy (a.
d, a.
d + nz, d);
105 std::copy (a.
r, a.
r + nz, r);
106 std::copy (a.
c, a.
c + ncols + 1, c);
131 void maybe_compress (
bool remove_zeros);
135 bool indices_ok (
void)
const;
150 SparseRep *r =
new SparseRep (*rep);
152 if (--rep->count == 0)
179 : rep (nil_rep ()), dimensions (
dim_vector(0,0))
185 : rep (new typename
Sparse<T>::SparseRep (n)),
189 : rep (new typename
Sparse<T>::SparseRep (nr, nc)),
195 : rep (new typename
Sparse<T>::SparseRep (dv(0), dv(1), nz)),
199 : rep (new typename
Sparse<T>::SparseRep (nr, nc, nz)),
209 : rep (new typename
Sparse<T>::SparseRep (a.rep->nrows, a.rep->ncols,
211 dimensions (a.dimensions)
214 std::copy (a.
rep->d, a.
rep->d + nz, rep->
d);
215 std::copy (a.
rep->r, a.
rep->r + nz, rep->
r);
216 std::copy (a.
rep->c, a.
rep->c + rep->
ncols + 1, rep->
c);
221 : rep (a.rep), dimensions (a.dimensions)
271 while (cidx (ret+1) < k)
279 + static_cast<size_t> (capacity ())
317 return rep->
celem (i, j);
332 if (n < 0 || n >= numel ())
333 return range_error (
"T& Sparse<T>::checkelem", n);
343 if (i < 0 || j < 0 || i >= dim1 () || j >= dim2 ())
344 return range_error (
"T& Sparse<T>::checkelem", i, j);
357 return range_error (
"T& Sparse<T>::checkelem", ra_idx);
377 #if defined (BOUNDS_CHECKING)
380 return checkelem (n);
385 return checkelem (i, j);
390 return checkelem (ra_idx);
406 return elem (ra_idx);
413 if (n < 0 || n >= numel ())
414 return range_error (
"T Sparse<T>::checkelem", n);
421 if (i < 0 || j < 0 || i >= dim1 () || j >= dim2 ())
422 return range_error (
"T Sparse<T>::checkelem", i, j);
432 return range_error (
"T Sparse<T>::checkelem", ra_idx);
444 #if defined (BOUNDS_CHECKING)
448 return checkelem (i, j);
453 return checkelem (ra_idx);
465 return elem (ra_idx);
484 return permute (vec,
true);
503 bool is_square (
void)
const {
return (dim1 () == dim2 ()); }
505 bool is_empty (
void)
const {
return (rows () < 1 && cols () < 1); }
509 T*
data (
void) { make_unique ();
return rep->
d; }
516 T*
data (
void)
const {
return rep->
d; }
521 make_unique ();
return rep->
ridx (i);
534 make_unique ();
return rep->
cidx (i);
548 void delete_elements (
int dim,
const idx_vector& i);
555 bool resize_ok =
false)
const;
561 void print_info (std::ostream& os,
const std::string& prefix)
const;
590 template <
class F,
bool zero>
593 return any_all_test<F, T, zero> (fcn, data (), nnz ());
599 {
return test<F, false> (fcn); }
603 {
return test<F, true> (fcn); }
607 {
return test<bool (&) (T), false> (fcn); }
610 {
return test<bool (&) (const T&), false> (fcn); }
613 {
return test<bool (&) (T), true> (fcn); }
616 {
return test<bool (&) (const T&), true> (fcn); }
618 template <
class U,
class F>
637 result.
data (ridx (i) + j * nr) = fcn (data (i));
650 result.
cidx (ii) = 0;
656 U val = fcn (data (i));
659 result.
data (ii) = val;
660 result.
ridx (ii++) = ridx (i);
664 result.
cidx (j+1) = ii;
677 {
return map<U, U (&) (T)> (fcn); }
681 map (U (&fcn) (
const T&))
const
682 {
return map<U, U (&) (const T&)> (fcn); }
690 T (*read_fcn) (std::istream&))
696 if (nr > 0 && nc > 0)
715 if (itmp < 0 || itmp >= nr)
717 (*current_liboctave_error_handler)
718 (
"invalid sparse matrix: row index = %d out of range",
720 is.setstate (std::ios::failbit);
724 if (jtmp < 0 || jtmp >= nc)
726 (*current_liboctave_error_handler)
727 (
"invalid sparse matrix: column index = %d out of range",
729 is.setstate (std::ios::failbit);
735 (*current_liboctave_error_handler)
736 (
"invalid sparse matrix: column indices must appear in ascending order");
737 is.setstate (std::ios::failbit);
740 else if (jtmp > jold)
745 else if (itmp < iold)
747 (*current_liboctave_error_handler)
748 (
"invalid sparse matrix: row indices must appear in ascending order in each column");
749 is.setstate (std::ios::failbit);
761 a.
ridx (ii++) = itmp;
octave_idx_type & xcidx(octave_idx_type i)
octave_idx_type compute_index(octave_idx_type n, const dim_vector &dims)
T & elem(octave_idx_type _r, octave_idx_type _c)
octave_idx_type * xridx(void)
Sparse< T > ipermute(const Array< octave_idx_type > &vec) const
T & elem(octave_idx_type i, octave_idx_type j)
octave_idx_type cols(void) const
T & xelem(const Array< octave_idx_type > &ra_idx)
T cdata(octave_idx_type i) const
T xelem(octave_idx_type i, octave_idx_type j) const
octave_idx_type rows(void) const
void change_length(octave_idx_type nz)
octave_idx_type * cidx(void) const
octave_idx_type numel(void) const
const octave_base_value const Array< octave_idx_type > & ra_idx
T & elem(octave_idx_type n)
octave_idx_type cridx(octave_idx_type i) const
dim_vector dims(void) const
std::istream & read_sparse_matrix(std::istream &is, Sparse< T > &a, T(*read_fcn)(std::istream &))
octave_idx_type & ridx(octave_idx_type i)
T & xelem(octave_idx_type n)
SparseRep(octave_idx_type nr, octave_idx_type nc, octave_idx_type nz=0)
T checkelem(octave_idx_type i, octave_idx_type j) const
T & data(octave_idx_type i)
bool test_all(F fcn) const
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)
octave_idx_type * xcidx(void)
octave_idx_type ridx(octave_idx_type i) const
bool test_all(bool(&fcn)(T)) const
octave_idx_type nelem(void) const
octave_idx_type & xridx(octave_idx_type i)
octave_idx_type & cidx(octave_idx_type i)
Sparse< T >::SparseRep * nil_rep(void) const
octave_idx_type & ridx(octave_idx_type i)
octave_idx_type ccidx(octave_idx_type i) const
octave_idx_type dim1(void) const
octave_idx_type * cidx(void)
T elem(octave_idx_type i, octave_idx_type j) const
octave_idx_type columns(void) const
T xelem(octave_idx_type n) const
T & checkelem(const Array< octave_idx_type > &ra_idx)
bool indices_ok(void) const
octave_idx_type get_row_index(octave_idx_type k)
bool indices_ok(void) const
T & elem(const Array< octave_idx_type > &ra_idx)
F77_RET_T const double const double double * d
SparseRep(const SparseRep &a)
T & data(octave_idx_type i)
octave_idx_type nnz(void) const
Sparse< T > squeeze(void) const
bool test_any(bool(&fcn)(const T &)) const
octave_idx_type nzmax(void) const
void maybe_compress(bool remove_zeros)
octave_idx_type get_col_index(octave_idx_type k)
Sparse< T >::SparseRep * rep
T celem(octave_idx_type _r, octave_idx_type _c) const
T xelem(const Array< octave_idx_type > &ra_idx) const
octave_idx_type safe_numel(void) const
T data(octave_idx_type i) const
void change_capacity(octave_idx_type nz)
T checkelem(const Array< octave_idx_type > &ra_idx) const
octave_idx_type ndims(void) const
octave_idx_type * mex_get_ir(void) const
Sparse< T > maybe_compress(bool remove_zeros=false)
bool test_all(bool(&fcn)(const T &)) const
octave_idx_type * mex_get_jc(void) const
Sparse< U > map(U(&fcn)(T)) const
size_t byte_size(void) const
Sparse< U > map(F fcn) const
octave_idx_type cidx(octave_idx_type i) const
Sparse(const Sparse< T > &a)
T & xelem(octave_idx_type i, octave_idx_type j)
Handles the reference counting for all the derived classes.
octave_idx_type nnz(void) const
bool test_any(bool(&fcn)(T)) const
octave_idx_type * ridx(void)
bool is_empty(void) const
Sparse(octave_idx_type nr, octave_idx_type nc)
octave_idx_type capacity(void) const
T & checkelem(octave_idx_type i, octave_idx_type j)
bool test_any(F fcn) const
octave_idx_type & cidx(octave_idx_type i)
Sparse< U > map(U(&fcn)(const T &)) const
T & checkelem(octave_idx_type n)
T elem(const Array< octave_idx_type > &ra_idx) const
octave_idx_type * ridx(void) const
T & xdata(octave_idx_type i)
Sparse(octave_idx_type n)
Sparse(const Sparse< U > &a)
void * mex_get_data(void) const
octave_refcount< int > count
Sparse(const dim_vector &dv, octave_idx_type nz)
octave_idx_type dim2(void) const
T checkelem(octave_idx_type n) const
octave_idx_type length(void) const
bool is_square(void) const
T elem(octave_idx_type n) const
octave_idx_type length(void) const
Sparse(octave_idx_type nr, octave_idx_type nc, octave_idx_type nz)
void F(const TSRC *v, TRES *r, octave_idx_type m, octave_idx_type n)
SparseRep(octave_idx_type n)