26 #if !defined (octave_Sparse_h)
27 #define octave_Sparse_h 1
90 :
d (nz > 0 ? new T [nz] : 0),
102 nzmx (a.nzmx), nrows (a.nrows), ncols (a.ncols), count (1)
132 void maybe_compress (
bool remove_zeros);
136 bool indices_ok (
void)
const;
147 void make_unique (
void)
151 SparseRep *r =
new SparseRep (*rep);
153 if (--rep->count == 0)
180 : rep (nil_rep ()), dimensions (
dim_vector(0,0))
186 : rep (new typename
Sparse<T>::SparseRep (n)),
190 : rep (new typename
Sparse<T>::SparseRep (nr, nc)),
196 : rep (new typename
Sparse<T>::SparseRep (dv(0), dv(1), nz)),
200 : rep (new typename
Sparse<T>::SparseRep (nr, nc, nz)),
210 : rep (new typename
Sparse<T>::SparseRep (a.rep->nrows, a.rep->ncols,
212 dimensions (a.dimensions)
215 std::copy (a.
rep->d, a.
rep->d + nz, rep->
d);
222 : rep (a.rep), dimensions (a.dimensions)
272 while (cidx (ret+1) < k)
277 size_t byte_size (
void)
const
280 + static_cast<size_t> (capacity ())
316 return rep->
celem (i, j);
331 if (n < 0 || n >= numel ())
332 return range_error (
"T& Sparse<T>::checkelem", n);
342 if (i < 0 || j < 0 || i >= dim1 () || j >= dim2 ())
343 return range_error (
"T& Sparse<T>::checkelem", i, j);
356 return range_error (
"T& Sparse<T>::checkelem", ra_idx);
376 #if defined (BOUNDS_CHECKING)
379 return checkelem (n);
384 return checkelem (i, j);
389 return checkelem (ra_idx);
405 return elem (ra_idx);
412 if (n < 0 || n >= numel ())
413 return range_error (
"T Sparse<T>::checkelem", n);
420 if (i < 0 || j < 0 || i >= dim1 () || j >= dim2 ())
421 return range_error (
"T Sparse<T>::checkelem", i, j);
431 return range_error (
"T Sparse<T>::checkelem", ra_idx);
443 #if defined (BOUNDS_CHECKING)
447 return checkelem (i, j);
452 return checkelem (ra_idx);
464 return elem (ra_idx);
483 return permute (vec,
true);
502 bool is_square (
void)
const {
return (dim1 () == dim2 ()); }
504 bool is_empty (
void)
const {
return (rows () < 1 && cols () < 1); }
508 T*
data (
void) { make_unique ();
return rep->
d; }
515 T*
data (
void)
const {
return rep->
d; }
520 make_unique ();
return rep->
ridx (i);
533 make_unique ();
return rep->
cidx (i);
547 void delete_elements (
int dim,
const idx_vector& i);
554 bool resize_ok =
false)
const;
560 void print_info (std::ostream& os,
const std::string& prefix)
const;
589 template <
class F,
bool zero>
590 bool test (
F fcn)
const
592 return any_all_test<F, T, zero> (fcn, data (), nnz ());
597 bool test_any (
F fcn)
const
598 {
return test<F, false> (fcn); }
601 bool test_all (
F fcn)
const
602 {
return test<F, true> (fcn); }
605 bool test_any (
bool (&fcn) (T))
const
606 {
return test<bool (&) (T), false> (fcn); }
608 bool test_any (
bool (&fcn) (
const T&))
const
609 {
return test<bool (&) (const T&), false> (fcn); }
611 bool test_all (
bool (&fcn) (T))
const
612 {
return test<bool (&) (T), true> (fcn); }
614 bool test_all (
bool (&fcn) (
const T&))
const
615 {
return test<bool (&) (const T&), true> (fcn); }
617 template <
class U,
class F>
636 result.
data (ridx (i) + j * nr) = fcn (data (i));
649 result.
cidx (ii) = 0;
655 U val = fcn (data (i));
658 result.
data (ii) = val;
659 result.
ridx (ii++) = ridx (i);
663 result.
cidx (j+1) = ii;
675 map (U (&fcn) (T))
const
676 {
return map<U, U (&) (T)> (fcn); }
680 map (U (&fcn) (
const T&))
const
681 {
return map<U, U (&) (const T&)> (fcn); }
689 T (*read_fcn) (std::istream&))
695 if (nr > 0 && nc > 0)
714 if (itmp < 0 || itmp >= nr)
716 (*current_liboctave_error_handler)
717 (
"invalid sparse matrix: row index = %d out of range",
719 is.setstate (std::ios::failbit);
723 if (jtmp < 0 || jtmp >= nc)
725 (*current_liboctave_error_handler)
726 (
"invalid sparse matrix: column index = %d out of range",
728 is.setstate (std::ios::failbit);
734 (*current_liboctave_error_handler)
735 (
"invalid sparse matrix: column indices must appear in ascending order");
736 is.setstate (std::ios::failbit);
739 else if (jtmp > jold)
744 else if (itmp < iold)
746 (*current_liboctave_error_handler)
747 (
"invalid sparse matrix: row indices must appear in ascending order in each column");
748 is.setstate (std::ios::failbit);
760 a.
ridx (ii++) = itmp;