Mercurial > hg > octave-shane
changeset 14792:8483286c0a13
Update Sparse sources to GNU standards (whitespace changes only)
* Sparse.cc: Fix whitespace
* Sparse.h: Ditto
author | Jordi Gutiérrez Hermoso <jordigh@octave.org> |
---|---|
date | Thu, 21 Jun 2012 14:29:21 -0400 |
parents | 90c131272396 |
children | cda76da34693 |
files | liboctave/Sparse.cc liboctave/Sparse.h |
diffstat | 2 files changed, 206 insertions(+), 138 deletions(-) [+] |
line wrap: on
line diff
--- a/liboctave/Sparse.cc +++ b/liboctave/Sparse.cc @@ -1241,7 +1241,9 @@ else if (idx_j.is_cont_range (nc, lb, ub)) { const Sparse<T> tmp = *this; - octave_idx_type lbi = tmp.cidx(lb), ubi = tmp.cidx(ub), new_nz = nz - (ubi - lbi); + octave_idx_type lbi = tmp.cidx(lb), ubi = tmp.cidx(ub), + new_nz = nz - (ubi - lbi); + *this = Sparse<T> (nr, nc - (ub - lb), new_nz); copy_or_memcpy (lbi, tmp.data (), data ()); copy_or_memcpy (lbi, tmp.ridx (), ridx ());
--- a/liboctave/Sparse.h +++ b/liboctave/Sparse.h @@ -71,10 +71,15 @@ octave_idx_type ncols; octave_refcount<int> count; - SparseRep (void) : d (0), r (0), c (new octave_idx_type [1]), nzmx (0), nrows (0), - ncols (0), count (1) { c[0] = 0; } + SparseRep (void) + : d (0), r (0), c (new octave_idx_type [1]), nzmx (0), nrows (0), + ncols (0), count (1) + { + c[0] = 0; + } - SparseRep (octave_idx_type n) : d (0), r (0), c (new octave_idx_type [n+1]), nzmx (0), nrows (n), + SparseRep (octave_idx_type n) + : d (0), r (0), c (new octave_idx_type [n+1]), nzmx (0), nrows (n), ncols (n), count (1) { for (octave_idx_type i = 0; i < n + 1; i++) @@ -97,7 +102,8 @@ } SparseRep (const SparseRep& a) - : d (new T [a.nzmx]), r (new octave_idx_type [a.nzmx]), c (new octave_idx_type [a.ncols + 1]), + : d (new T [a.nzmx]), r (new octave_idx_type [a.nzmx]), + c (new octave_idx_type [a.ncols + 1]), nzmx (a.nzmx), nrows (a.nrows), ncols (a.ncols), count (1) { octave_idx_type nz = a.nnz (); @@ -144,17 +150,17 @@ //-------------------------------------------------------------------- void make_unique (void) - { - if (rep->count > 1) - { - SparseRep *r = new SparseRep (*rep); + { + if (rep->count > 1) + { + SparseRep *r = new SparseRep (*rep); - if (--rep->count == 0) - delete rep; + if (--rep->count == 0) + delete rep; - rep = r; - } - } + rep = r; + } + } public: @@ -168,18 +174,18 @@ private: typename Sparse<T>::SparseRep *nil_rep (void) const - { - static typename Sparse<T>::SparseRep nr; - return &nr; - } + { + static typename Sparse<T>::SparseRep nr; + return &nr; + } public: Sparse (void) : rep (nil_rep ()), dimensions (dim_vector(0,0)) - { - rep->count++; - } + { + rep->count++; + } explicit Sparse (octave_idx_type n) : rep (new typename Sparse<T>::SparseRep (n)), @@ -193,7 +199,7 @@ Sparse (const dim_vector& dv, octave_idx_type nz) : rep (new typename Sparse<T>::SparseRep (dv(0), dv(1), nz)), - dimensions (dv) { } + dimensions (dv) { } Sparse (octave_idx_type nr, octave_idx_type nc, octave_idx_type nz) : rep (new typename Sparse<T>::SparseRep (nr, nc, nz)), @@ -207,20 +213,20 @@ template <class U> Sparse (const Sparse<U>& a) : rep (new typename Sparse<T>::SparseRep (a.rep->nrows, a.rep->ncols, a.rep->nzmx)), - dimensions (a.dimensions) - { - octave_idx_type nz = a.nnz (); - std::copy (a.rep->d, a.rep->d + nz, rep->d); - copy_or_memcpy (nz, a.rep->r, rep->r); - copy_or_memcpy (rep->ncols + 1, a.rep->c, rep->c); - } + dimensions (a.dimensions) + { + octave_idx_type nz = a.nnz (); + std::copy (a.rep->d, a.rep->d + nz, rep->d); + copy_or_memcpy (nz, a.rep->r, rep->r); + copy_or_memcpy (rep->ncols + 1, a.rep->c, rep->c); + } // No type conversion case. Sparse (const Sparse<T>& a) : rep (a.rep), dimensions (a.dimensions) - { - rep->count++; - } + { + rep->count++; + } public: @@ -249,9 +255,9 @@ // Querying the number of elements (incl. zeros) may overflow the index type, // so don't do it unless you really need it. octave_idx_type numel (void) const - { - return dimensions.safe_numel (); - } + { + return dimensions.safe_numel (); + } octave_idx_type nelem (void) const { return capacity (); } octave_idx_type length (void) const { return numel (); } @@ -265,18 +271,19 @@ octave_idx_type get_row_index (octave_idx_type k) { return ridx (k); } octave_idx_type get_col_index (octave_idx_type k) - { - octave_idx_type ret = 0; - while (cidx(ret+1) < k) - ret++; - return ret; - } + { + octave_idx_type ret = 0; + while (cidx(ret+1) < k) + ret++; + return ret; + } size_t byte_size (void) const - { - return (static_cast<size_t>(cols () + 1) * sizeof (octave_idx_type) - + static_cast<size_t> (capacity ()) * (sizeof (T) + sizeof (octave_idx_type))); - } + { + return (static_cast<size_t>(cols () + 1) * sizeof (octave_idx_type) + + static_cast<size_t> (capacity ()) + * (sizeof (T) + sizeof (octave_idx_type))); + } dim_vector dims (void) const { return dimensions; } @@ -296,145 +303,189 @@ // No checking, even for multiple references, ever. T& xelem (octave_idx_type n) - { - octave_idx_type i = n % rows (), j = n / rows(); - return xelem (i, j); - } + { + octave_idx_type i = n % rows (), j = n / rows(); + return xelem (i, j); + } T xelem (octave_idx_type n) const - { - octave_idx_type i = n % rows (), j = n / rows(); - return xelem (i, j); - } + { + octave_idx_type i = n % rows (), j = n / rows(); + return xelem (i, j); + } T& xelem (octave_idx_type i, octave_idx_type j) { return rep->elem (i, j); } - T xelem (octave_idx_type i, octave_idx_type j) const { return rep->celem (i, j); } + T xelem (octave_idx_type i, octave_idx_type j) const + { + return rep->celem (i, j); + } T& xelem (const Array<octave_idx_type>& ra_idx) - { return xelem (compute_index (ra_idx)); } + { return xelem (compute_index (ra_idx)); } T xelem (const Array<octave_idx_type>& ra_idx) const - { return xelem (compute_index (ra_idx)); } + { return xelem (compute_index (ra_idx)); } // FIXME -- would be nice to fix this so that we don't // unnecessarily force a copy, but that is not so easy, and I see no // clean way to do it. T& checkelem (octave_idx_type n) - { - if (n < 0 || n >= numel ()) - return range_error ("T& Sparse<T>::checkelem", n); - else - { - make_unique (); - return xelem (n); - } - } + { + if (n < 0 || n >= numel ()) + return range_error ("T& Sparse<T>::checkelem", n); + else + { + make_unique (); + return xelem (n); + } + } T& checkelem (octave_idx_type i, octave_idx_type j) - { - if (i < 0 || j < 0 || i >= dim1 () || j >= dim2 ()) - return range_error ("T& Sparse<T>::checkelem", i, j); - else - { - make_unique (); - return xelem (i, j); - } - } + { + if (i < 0 || j < 0 || i >= dim1 () || j >= dim2 ()) + return range_error ("T& Sparse<T>::checkelem", i, j); + else + { + make_unique (); + return xelem (i, j); + } + } T& checkelem (const Array<octave_idx_type>& ra_idx) - { - octave_idx_type i = compute_index (ra_idx); + { + octave_idx_type i = compute_index (ra_idx); - if (i < 0) - return range_error ("T& Sparse<T>::checkelem", ra_idx); - else - return elem (i); - } + if (i < 0) + return range_error ("T& Sparse<T>::checkelem", ra_idx); + else + return elem (i); + } T& elem (octave_idx_type n) - { - make_unique (); - return xelem (n); - } + { + make_unique (); + return xelem (n); + } T& elem (octave_idx_type i, octave_idx_type j) - { - make_unique (); - return xelem (i, j); - } + { + make_unique (); + return xelem (i, j); + } T& elem (const Array<octave_idx_type>& ra_idx) - { return Sparse<T>::elem (compute_index (ra_idx)); } + { return Sparse<T>::elem (compute_index (ra_idx)); } #if defined (BOUNDS_CHECKING) - T& operator () (octave_idx_type n) { return checkelem (n); } - T& operator () (octave_idx_type i, octave_idx_type j) { return checkelem (i, j); } - T& operator () (const Array<octave_idx_type>& ra_idx) { return checkelem (ra_idx); } + T& operator () (octave_idx_type n) + { + return checkelem (n); + } + + T& operator () (octave_idx_type i, octave_idx_type j) + { + return checkelem (i, j); + } + + T& operator () (const Array<octave_idx_type>& ra_idx) + { + return checkelem (ra_idx); + } + #else - T& operator () (octave_idx_type n) { return elem (n); } - T& operator () (octave_idx_type i, octave_idx_type j) { return elem (i, j); } - T& operator () (const Array<octave_idx_type>& ra_idx) { return elem (ra_idx); } + T& operator () (octave_idx_type n) + { + return elem (n); + } + + T& operator () (octave_idx_type i, octave_idx_type j) + { + return elem (i, j); + } + + T& operator () (const Array<octave_idx_type>& ra_idx) + { + return elem (ra_idx); + } + #endif T checkelem (octave_idx_type n) const - { - if (n < 0 || n >= numel ()) - return range_error ("T Sparse<T>::checkelem", n); - else - return xelem (n); - } + { + if (n < 0 || n >= numel ()) + return range_error ("T Sparse<T>::checkelem", n); + else + return xelem (n); + } T checkelem (octave_idx_type i, octave_idx_type j) const - { - if (i < 0 || j < 0 || i >= dim1 () || j >= dim2 ()) - return range_error ("T Sparse<T>::checkelem", i, j); - else - return xelem (i, j); - } + { + if (i < 0 || j < 0 || i >= dim1 () || j >= dim2 ()) + return range_error ("T Sparse<T>::checkelem", i, j); + else + return xelem (i, j); + } T checkelem (const Array<octave_idx_type>& ra_idx) const - { - octave_idx_type i = compute_index (ra_idx); + { + octave_idx_type i = compute_index (ra_idx); - if (i < 0) - return range_error ("T Sparse<T>::checkelem", ra_idx); - else - return Sparse<T>::elem (i); - } + if (i < 0) + return range_error ("T Sparse<T>::checkelem", ra_idx); + else + return Sparse<T>::elem (i); + } T elem (octave_idx_type n) const { return xelem (n); } T elem (octave_idx_type i, octave_idx_type j) const { return xelem (i, j); } T elem (const Array<octave_idx_type>& ra_idx) const - { return Sparse<T>::elem (compute_index (ra_idx)); } + { return Sparse<T>::elem (compute_index (ra_idx)); } #if defined (BOUNDS_CHECKING) T operator () (octave_idx_type n) const { return checkelem (n); } - T operator () (octave_idx_type i, octave_idx_type j) const { return checkelem (i, j); } - T operator () (const Array<octave_idx_type>& ra_idx) const { return checkelem (ra_idx); } + T operator () (octave_idx_type i, octave_idx_type j) const + { + return checkelem (i, j); + } + + T operator () (const Array<octave_idx_type>& ra_idx) const + { + return checkelem (ra_idx); + } + #else T operator () (octave_idx_type n) const { return elem (n); } - T operator () (octave_idx_type i, octave_idx_type j) const { return elem (i, j); } - T operator () (const Array<octave_idx_type>& ra_idx) const { return elem (ra_idx); } + T operator () (octave_idx_type i, octave_idx_type j) const + { + return elem (i, j); + } + + T operator () (const Array<octave_idx_type>& ra_idx) const + { + return elem (ra_idx); + } #endif Sparse<T> maybe_compress (bool remove_zeros = false) - { - if (remove_zeros) - make_unique (); // Needs to unshare because elements are removed. + { + if (remove_zeros) + make_unique (); // Needs to unshare because elements are removed. - rep->maybe_compress (remove_zeros); - return (*this); - } + rep->maybe_compress (remove_zeros); + return (*this); + } Sparse<T> reshape (const dim_vector& new_dims) const; Sparse<T> permute (const Array<octave_idx_type>& vec, bool inv = false) const; Sparse<T> ipermute (const Array<octave_idx_type>& vec) const - { return permute (vec, true); } + { + return permute (vec, true); + } void resize1 (octave_idx_type n); @@ -443,11 +494,11 @@ void resize (const dim_vector& dv); void change_capacity (octave_idx_type nz) - { - if (nz < nnz ()) - make_unique (); // Unshare now because elements will be truncated. - rep->change_length (nz); - } + { + if (nz < nnz ()) + make_unique (); // Unshare now because elements will be truncated. + rep->change_length (nz); + } Sparse<T>& insert (const Sparse<T>& a, octave_idx_type r, octave_idx_type c); Sparse<T>& insert (const Sparse<T>& a, const Array<octave_idx_type>& idx); @@ -468,7 +519,11 @@ T* data (void) const { return rep->d; } octave_idx_type* ridx (void) { make_unique (); return rep->r; } - octave_idx_type& ridx (octave_idx_type i) { make_unique (); return rep->ridx (i); } + octave_idx_type& ridx (octave_idx_type i) + { + make_unique (); return rep->ridx (i); + } + octave_idx_type* xridx (void) { return rep->r; } octave_idx_type& xridx (octave_idx_type i) { return rep->ridx (i); } @@ -477,7 +532,11 @@ octave_idx_type* ridx (void) const { return rep->r; } octave_idx_type* cidx (void) { make_unique (); return rep->c; } - octave_idx_type& cidx (octave_idx_type i) { make_unique (); return rep->cidx (i); } + octave_idx_type& cidx (octave_idx_type i) + { + make_unique (); return rep->cidx (i); + } + octave_idx_type* xcidx (void) { return rep->c; } octave_idx_type& xcidx (octave_idx_type i) { return rep->cidx (i); } @@ -495,7 +554,8 @@ Sparse<T> index (const idx_vector& i, bool resize_ok = false) const; - Sparse<T> index (const idx_vector& i, const idx_vector& j, bool resize_ok = false) const; + Sparse<T> index (const idx_vector& i, const idx_vector& j, + bool resize_ok = false) const; void assign (const idx_vector& i, const Sparse<T>& rhs); @@ -507,13 +567,19 @@ // You should not use them anywhere else. void *mex_get_data (void) const { return const_cast<T *> (data ()); } - octave_idx_type *mex_get_ir (void) const { return const_cast<octave_idx_type *> (ridx ()); } + octave_idx_type *mex_get_ir (void) const + { + return const_cast<octave_idx_type *> (ridx ()); + } - octave_idx_type *mex_get_jc (void) const { return const_cast<octave_idx_type *> (cidx ()); } + octave_idx_type *mex_get_jc (void) const + { + return const_cast<octave_idx_type *> (cidx ()); + } Sparse<T> sort (octave_idx_type dim = 0, sortmode mode = ASCENDING) const; Sparse<T> sort (Array<octave_idx_type> &sidx, octave_idx_type dim = 0, - sortmode mode = ASCENDING) const; + sortmode mode = ASCENDING) const; Sparse<T> diag (octave_idx_type k = 0) const;