# HG changeset patch # User David Bateman # Date 1205937513 14400 # Node ID 7bfaa9611558fc84e653c12fb7cd9f233f1a070b # Parent 8a939b21786329cab992c0e1136a4c7e90d0128a Rewrite sparse mappers in terms of a functor template function diff --git a/liboctave/CSparse.cc b/liboctave/CSparse.cc --- a/liboctave/CSparse.cc +++ b/liboctave/CSparse.cc @@ -39,6 +39,7 @@ #include "CSparse.h" #include "boolSparse.h" #include "dSparse.h" +#include "functor.h" #include "oct-spparms.h" #include "SparseCmplxLU.h" #include "oct-sparse.h" @@ -7445,164 +7446,19 @@ SparseMatrix SparseComplexMatrix::map (dmapper fcn) const { - SparseMatrix result; - double f_zero = fcn (0.); - - if (f_zero != 0.) - { - octave_idx_type nr = rows (); - octave_idx_type nc = cols (); - - result = SparseMatrix (nr, nc, f_zero); - - for (octave_idx_type j = 0; j < nc; j++) - for (octave_idx_type i = cidx(j); i < cidx (j+1); i++) - { - OCTAVE_QUIT; - /* Use data instead of elem for better performance. */ - result.data (ridx (i) + j * nr) = fcn (data(i)); - } - - result.maybe_compress (true); - } - else - { - octave_idx_type nz = nnz (); - octave_idx_type nr = rows (); - octave_idx_type nc = cols (); - - result = SparseMatrix (nr, nc, nz); - octave_idx_type ii = 0; - result.cidx (ii) = 0; - - for (octave_idx_type j = 0; j < nc; j++) - { - for (octave_idx_type i = cidx(j); i < cidx (j+1); i++) - { - double val = fcn (data (i)); - if (val != 0.0) - { - result.data (ii) = val; - result.ridx (ii++) = ridx (i); - } - OCTAVE_QUIT; - } - result.cidx (j+1) = ii; - } - - result.maybe_compress (false); - } - - return result; + return MSparse::map (func_ptr (fcn)); } SparseComplexMatrix SparseComplexMatrix::map (cmapper fcn) const { - SparseComplexMatrix result; - Complex f_zero = fcn (0.); - - if (f_zero != 0.) - { - octave_idx_type nr = rows (); - octave_idx_type nc = cols (); - - result = SparseComplexMatrix (nr, nc, f_zero); - - for (octave_idx_type j = 0; j < nc; j++) - for (octave_idx_type i = cidx(j); i < cidx (j+1); i++) - { - OCTAVE_QUIT; - /* Use data instead of elem for better performance. */ - result.data (ridx (i) + j * nr) = fcn (data(i)); - } - - result.maybe_compress (true); - } - else - { - octave_idx_type nz = nnz (); - octave_idx_type nr = rows (); - octave_idx_type nc = cols (); - - result = SparseComplexMatrix (nr, nc, nz); - Complex zero = Complex (0.0, 0.0); - octave_idx_type ii = 0; - result.cidx (ii) = 0; - - for (octave_idx_type j = 0; j < nc; j++) - { - for (octave_idx_type i = cidx(j); i < cidx (j+1); i++) - { - Complex val = fcn (data (i)); - if (val != zero) - { - result.data (ii) = val; - result.ridx (ii++) = ridx (i); - } - OCTAVE_QUIT; - } - result.cidx (j+1) = ii; - } - - result.maybe_compress (false); - } - - return result; + return MSparse::map (func_ptr (fcn)); } SparseBoolMatrix SparseComplexMatrix::map (bmapper fcn) const { - SparseBoolMatrix result; - bool f_zero = fcn (0.); - - if (f_zero) - { - octave_idx_type nr = rows (); - octave_idx_type nc = cols (); - - result = SparseBoolMatrix (nr, nc, f_zero); - - for (octave_idx_type j = 0; j < nc; j++) - for (octave_idx_type i = cidx(j); i < cidx (j+1); i++) - { - OCTAVE_QUIT; - /* Use data instead of elem for better performance. */ - result.data (ridx (i) + j * nr) = fcn (data(i)); - } - - result.maybe_compress (true); - } - else - { - octave_idx_type nz = nnz (); - octave_idx_type nr = rows (); - octave_idx_type nc = cols (); - - result = SparseBoolMatrix (nr, nc, nz); - octave_idx_type ii = 0; - result.cidx (ii) = 0; - - for (octave_idx_type j = 0; j < nc; j++) - { - for (octave_idx_type i = cidx(j); i < cidx (j+1); i++) - { - bool val = fcn (data (i)); - if (val) - { - result.data (ii) = val; - result.ridx (ii++) = ridx (i); - } - OCTAVE_QUIT; - } - result.cidx (j+1) = ii; - } - - result.maybe_compress (false); - } - - return result; + return MSparse::map (func_ptr (fcn)); } std::ostream& diff --git a/liboctave/ChangeLog b/liboctave/ChangeLog --- a/liboctave/ChangeLog +++ b/liboctave/ChangeLog @@ -1,3 +1,17 @@ +2008-03-19 David Bateman + + * Sparse.h (template Sparse map (F fcn) + const): New template function. + * MSparse.h (template MSparse map (F fcn) + const): ditto. + * dSparse.cc (SparseMatrix SparseMatrix::map (dmapper) const, + SparseComplexMatrix SparseMatrix::map (cmapper) const, + SparseBoolMatrix SparseMatrix::map (bmapper) const): Rewrite in + terms of the new template functor. + * CSparse.cc (SparseMatrix SparseComplexMatrix::map (dmapper) const, + SparseComplexMatrix SparseComplexMatrix::map (cmapper) const, + SparseBoolMatrix SparseComplexMatrix::map (bmapper) const): ditto. + 2008-03-18 David Bateman * lo-specfun.cc (Complex xlgamma (const Complex&)): New function. diff --git a/liboctave/MSparse.h b/liboctave/MSparse.h --- a/liboctave/MSparse.h +++ b/liboctave/MSparse.h @@ -112,6 +112,12 @@ { return Sparse::ipermute (vec); } + template + MSparse map (F fcn) const + { + return Sparse::template map (fcn); + } + // Currently, the OPS functions don't need to be friends, but that // may change. diff --git a/liboctave/Sparse.h b/liboctave/Sparse.h --- a/liboctave/Sparse.h +++ b/liboctave/Sparse.h @@ -521,6 +521,61 @@ Sparse sort (octave_idx_type dim = 0, sortmode mode = ASCENDING) const; Sparse sort (Array &sidx, octave_idx_type dim = 0, sortmode mode = ASCENDING) const; + + template + Sparse + map (F fcn) const + { + Sparse result; + U f_zero = fcn (0.); + + if (f_zero != 0.) + { + octave_idx_type nr = rows (); + octave_idx_type nc = cols (); + + result = Sparse (nr, nc, f_zero); + + for (octave_idx_type j = 0; j < nc; j++) + for (octave_idx_type i = cidx(j); i < cidx (j+1); i++) + { + OCTAVE_QUIT; + /* Use data instead of elem for better performance. */ + result.data (ridx (i) + j * nr) = fcn (data(i)); + } + + result.maybe_compress (true); + } + else + { + octave_idx_type nz = nnz (); + octave_idx_type nr = rows (); + octave_idx_type nc = cols (); + + result = Sparse (nr, nc, nz); + octave_idx_type ii = 0; + result.cidx (ii) = 0; + + for (octave_idx_type j = 0; j < nc; j++) + { + for (octave_idx_type i = cidx(j); i < cidx (j+1); i++) + { + U val = fcn (data (i)); + if (val != 0.0) + { + result.data (ii) = val; + result.ridx (ii++) = ridx (i); + } + OCTAVE_QUIT; + } + result.cidx (j+1) = ii; + } + + result.maybe_compress (false); + } + + return result; + } }; // NOTE: these functions should be friends of the Sparse class and diff --git a/liboctave/dSparse.cc b/liboctave/dSparse.cc --- a/liboctave/dSparse.cc +++ b/liboctave/dSparse.cc @@ -39,6 +39,7 @@ #include "CSparse.h" #include "boolSparse.h" #include "dSparse.h" +#include "functor.h" #include "oct-spparms.h" #include "SparsedbleLU.h" #include "MatrixType.h" @@ -7537,164 +7538,19 @@ SparseMatrix SparseMatrix::map (dmapper fcn) const { - SparseMatrix result; - double f_zero = fcn (0.); - - if (f_zero != 0.) - { - octave_idx_type nr = rows (); - octave_idx_type nc = cols (); - - result = SparseMatrix (nr, nc, f_zero); - - for (octave_idx_type j = 0; j < nc; j++) - for (octave_idx_type i = cidx(j); i < cidx (j+1); i++) - { - OCTAVE_QUIT; - /* Use data instead of elem for better performance. */ - result.data (ridx (i) + j * nr) = fcn (data(i)); - } - - result.maybe_compress (true); - } - else - { - octave_idx_type nz = nnz (); - octave_idx_type nr = rows (); - octave_idx_type nc = cols (); - - result = SparseMatrix (nr, nc, nz); - octave_idx_type ii = 0; - result.cidx (ii) = 0; - - for (octave_idx_type j = 0; j < nc; j++) - { - for (octave_idx_type i = cidx(j); i < cidx (j+1); i++) - { - double val = fcn (data (i)); - if (val != 0.0) - { - result.data (ii) = val; - result.ridx (ii++) = ridx (i); - } - OCTAVE_QUIT; - } - result.cidx (j+1) = ii; - } - - result.maybe_compress (false); - } - - return result; + return MSparse::map (func_ptr (fcn)); } SparseComplexMatrix SparseMatrix::map (cmapper fcn) const { - SparseComplexMatrix result; - Complex f_zero = fcn (0.); - - if (f_zero != 0.) - { - octave_idx_type nr = rows (); - octave_idx_type nc = cols (); - - result = SparseComplexMatrix (nr, nc, f_zero); - - for (octave_idx_type j = 0; j < nc; j++) - for (octave_idx_type i = cidx(j); i < cidx (j+1); i++) - { - OCTAVE_QUIT; - /* Use data instead of elem for better performance. */ - result.data (ridx (i) + j * nr) = fcn (Complex (data(i), 0.0)); - } - - result.maybe_compress (true); - } - else - { - octave_idx_type nz = nnz (); - octave_idx_type nr = rows (); - octave_idx_type nc = cols (); - - result = SparseComplexMatrix (nr, nc, nz); - Complex zero = Complex (0.0, 0.0); - octave_idx_type ii = 0; - result.cidx (ii) = 0; - - for (octave_idx_type j = 0; j < nc; j++) - { - for (octave_idx_type i = cidx(j); i < cidx (j+1); i++) - { - Complex val = fcn (Complex (data (i), 0.0)); - if (val != zero) - { - result.data (ii) = val; - result.ridx (ii++) = ridx (i); - } - OCTAVE_QUIT; - } - result.cidx (j+1) = ii; - } - - result.maybe_compress (false); - } - - return result; + return MSparse::map (func_ptr (fcn)); } SparseBoolMatrix SparseMatrix::map (bmapper fcn) const { - SparseBoolMatrix result; - bool f_zero = fcn (0.); - - if (f_zero) - { - octave_idx_type nr = rows (); - octave_idx_type nc = cols (); - - result = SparseBoolMatrix (nr, nc, f_zero); - - for (octave_idx_type j = 0; j < nc; j++) - for (octave_idx_type i = cidx(j); i < cidx (j+1); i++) - { - OCTAVE_QUIT; - /* Use data instead of elem for better performance. */ - result.data (ridx (i) + j * nr) = fcn (data(i)); - } - - result.maybe_compress (true); - } - else - { - octave_idx_type nz = nnz (); - octave_idx_type nr = rows (); - octave_idx_type nc = cols (); - - result = SparseBoolMatrix (nr, nc, nz); - octave_idx_type ii = 0; - result.cidx (ii) = 0; - - for (octave_idx_type j = 0; j < nc; j++) - { - for (octave_idx_type i = cidx(j); i < cidx (j+1); i++) - { - bool val = fcn (data (i)); - if (val) - { - result.data (ii) = val; - result.ridx (ii++) = ridx (i); - } - OCTAVE_QUIT; - } - result.cidx (j+1) = ii; - } - - result.maybe_compress (false); - } - - return result; + return MSparse::map (func_ptr (fcn)); } std::ostream&