From c3d15fb5773daa04e10757720ee0b30fabdfc141 Mon Sep 17 00:00:00 2001 From: Michael Stevens Date: Tue, 21 Sep 2004 22:32:10 +0000 Subject: [PATCH] Add vector/matrix_temporary_type All construction/resize uses max_nz function --- include/boost/numeric/ublas/matrix_sparse.hpp | 53 +++++++------- include/boost/numeric/ublas/vector_sparse.hpp | 70 +++++++++++-------- 2 files changed, 69 insertions(+), 54 deletions(-) diff --git a/include/boost/numeric/ublas/matrix_sparse.hpp b/include/boost/numeric/ublas/matrix_sparse.hpp index efa48e2b..bc42649b 100644 --- a/include/boost/numeric/ublas/matrix_sparse.hpp +++ b/include/boost/numeric/ublas/matrix_sparse.hpp @@ -19,7 +19,6 @@ #include #include -#include #include // Iterators based on ideas of Jeremy Siek @@ -248,15 +247,16 @@ namespace boost { namespace numeric { namespace ublas { typedef T &true_reference; typedef T *pointer; typedef F functor_type; - typedef const sparse_matrix const_self_type; typedef sparse_matrix self_type; public: #ifndef BOOST_UBLAS_CT_REFERENCE_BASE_TYPEDEFS - typedef const matrix_const_reference const_closure_type; + typedef const matrix_const_reference const_closure_type; #else - typedef const matrix_reference const_closure_type; + typedef const matrix_reference const_closure_type; #endif typedef matrix_reference closure_type; + typedef sparse_vector vector_temporary_type; + typedef self_type matrix_temporary_type; typedef sparse_tag storage_category; typedef typename F::orientation_category orientation_category; @@ -1300,15 +1300,16 @@ namespace boost { namespace numeric { namespace ublas { typedef A array_type; typedef const A const_array_type; typedef F functor_type; - typedef const sparse_vector_of_sparse_vector const_self_type; typedef sparse_vector_of_sparse_vector self_type; public: #ifndef BOOST_UBLAS_CT_REFERENCE_BASE_TYPEDEFS - typedef const matrix_const_reference const_closure_type; + typedef const matrix_const_reference const_closure_type; #else - typedef const matrix_reference const_closure_type; + typedef const matrix_reference const_closure_type; #endif typedef matrix_reference closure_type; + typedef sparse_vector vector_temporary_type; + typedef self_type matrix_temporary_type; typedef typename A::value_type::second_type vector_data_value_type; typedef sparse_tag storage_category; typedef typename F::orientation_category orientation_category; @@ -2476,15 +2477,16 @@ namespace boost { namespace numeric { namespace ublas { typedef T &true_reference; typedef T *pointer; typedef F functor_type; - typedef const compressed_matrix const_self_type; typedef compressed_matrix self_type; public: #ifndef BOOST_UBLAS_CT_REFERENCE_BASE_TYPEDEFS - typedef const matrix_const_reference const_closure_type; + typedef const matrix_const_reference const_closure_type; #else - typedef const matrix_reference const_closure_type; + typedef const matrix_reference const_closure_type; #endif typedef matrix_reference closure_type; + typedef compressed_vector vector_temporary_type; + typedef self_type matrix_temporary_type; typedef sparse_tag storage_category; typedef typename F::orientation_category orientation_category; @@ -2492,10 +2494,10 @@ namespace boost { namespace numeric { namespace ublas { BOOST_UBLAS_INLINE compressed_matrix (): matrix_expression (), - size1_ (0), size2_ (0), non_zeros_ (0), + size1_ (0), size2_ (0), non_zeros_ (max_nz (0)), filled1_ (1), filled2_ (0), - index1_data_ (1), - index2_data_ (0), value_data_ (0) { + index1_data_ (functor_type::size1 (size1_, size2_) + 1), + index2_data_ (non_zeros_), value_data_ (non_zeros_) { index1_data_ [filled1_ - 1] = k_based (filled2_); } BOOST_UBLAS_INLINE @@ -2504,7 +2506,7 @@ namespace boost { namespace numeric { namespace ublas { size1_ (size1), size2_ (size2), non_zeros_ (max_nz (non_zeros)), filled1_ (1), filled2_ (0), index1_data_ (functor_type::size1 (size1_, size2_) + 1), - index2_data_ (non_zeros), value_data_ (non_zeros) { + index2_data_ (non_zeros_), value_data_ (non_zeros_) { index1_data_ [filled1_ - 1] = k_based (filled2_); } BOOST_UBLAS_INLINE @@ -2523,7 +2525,7 @@ namespace boost { namespace numeric { namespace ublas { size1_ (ae ().size1 ()), size2_ (ae ().size2 ()), non_zeros_ (max_nz (non_zeros)), filled1_ (1), filled2_ (0), index1_data_ (functor_type::size1 (ae ().size1 (), ae ().size2 ()) + 1), - index2_data_ (non_zeros), value_data_ (non_zeros) { + index2_data_ (non_zeros_), value_data_ (non_zeros_) { index1_data_ [filled1_ - 1] = k_based (filled2_); matrix_assign (scalar_assign (), *this, ae); } @@ -3813,15 +3815,16 @@ namespace boost { namespace numeric { namespace ublas { typedef T &true_reference; typedef T *pointer; typedef F functor_type; - typedef const coordinate_matrix const_self_type; typedef coordinate_matrix self_type; public: #ifndef BOOST_UBLAS_CT_REFERENCE_BASE_TYPEDEFS - typedef const matrix_const_reference const_closure_type; + typedef const matrix_const_reference const_closure_type; #else - typedef const matrix_reference const_closure_type; + typedef const matrix_reference const_closure_type; #endif typedef matrix_reference closure_type; + typedef coordinate_vector vector_temporary_type; + typedef self_type matrix_temporary_type; typedef sparse_tag storage_category; typedef typename F::orientation_category orientation_category; @@ -3829,17 +3832,17 @@ namespace boost { namespace numeric { namespace ublas { BOOST_UBLAS_INLINE coordinate_matrix (): matrix_expression (), - size1_ (0), size2_ (0), non_zeros_ (0), + size1_ (0), size2_ (0), non_zeros_ (max_nz (0)), filled_ (0), - sorted_ (true), index1_data_ (0), - index2_data_ (0), value_data_ (0) {} + sorted_ (true), index1_data_ (non_zeros_), + index2_data_ (non_zeros_), value_data_ (non_zeros_) {} BOOST_UBLAS_INLINE coordinate_matrix (size_type size1, size_type size2, size_type non_zeros = 0): matrix_expression (), size1_ (size1), size2_ (size2), non_zeros_ (max_nz (non_zeros)), filled_ (0), - sorted_ (true), index1_data_ (non_zeros), - index2_data_ (non_zeros), value_data_ (non_zeros) { + sorted_ (true), index1_data_ (non_zeros_), + index2_data_ (non_zeros_), value_data_ (non_zeros_) { } BOOST_UBLAS_INLINE coordinate_matrix (const coordinate_matrix &m): @@ -3855,8 +3858,8 @@ namespace boost { namespace numeric { namespace ublas { matrix_expression (), size1_ (ae ().size1 ()), size2_ (ae ().size2 ()), non_zeros_ (max_nz (non_zeros)), filled_ (0), - sorted_ (true), index1_data_ (non_zeros), - index2_data_ (non_zeros), value_data_ (non_zeros) { + sorted_ (true), index1_data_ (non_zeros_), + index2_data_ (non_zeros_), value_data_ (non_zeros_) { matrix_assign (scalar_assign (), *this, ae); } diff --git a/include/boost/numeric/ublas/vector_sparse.hpp b/include/boost/numeric/ublas/vector_sparse.hpp index 220fc124..c265b15f 100644 --- a/include/boost/numeric/ublas/vector_sparse.hpp +++ b/include/boost/numeric/ublas/vector_sparse.hpp @@ -242,15 +242,15 @@ namespace boost { namespace numeric { namespace ublas { private: typedef T &true_reference; typedef T *pointer; - typedef const sparse_vector const_self_type; typedef sparse_vector self_type; public: #ifndef BOOST_UBLAS_CT_REFERENCE_BASE_TYPEDEFS - typedef const vector_const_reference const_closure_type; + typedef const vector_const_reference const_closure_type; #else - typedef const vector_reference const_closure_type; + typedef const vector_reference const_closure_type; #endif typedef vector_reference closure_type; + typedef self_type vector_temporary_type; typedef sparse_tag storage_category; // Construction and destruction @@ -705,28 +705,28 @@ namespace boost { namespace numeric { namespace ublas { private: typedef T &true_reference; typedef T *pointer; - typedef const compressed_vector const_self_type; typedef compressed_vector self_type; public: #ifndef BOOST_UBLAS_CT_REFERENCE_BASE_TYPEDEFS - typedef const vector_const_reference const_closure_type; + typedef const vector_const_reference const_closure_type; #else - typedef const vector_reference const_closure_type; + typedef const vector_reference const_closure_type; #endif typedef vector_reference closure_type; + typedef self_type vector_temporary_type; typedef sparse_tag storage_category; // Construction and destruction BOOST_UBLAS_INLINE compressed_vector (): vector_expression (), - size_ (0), non_zeros_ (0), filled_ (0), - index_data_ (0), value_data_ (0) {} + size_ (0), non_zeros_ (max_nz (0)), filled_ (0), + index_data_ (non_zeros_), value_data_ (non_zeros_) {} explicit BOOST_UBLAS_INLINE compressed_vector (size_type size, size_type non_zeros = 0): vector_expression (), - size_ (size), non_zeros_ (non_zeros), filled_ (0), - index_data_ (non_zeros), value_data_ (non_zeros) { + size_ (size), non_zeros_ (max_nz (non_zeros)), filled_ (0), + index_data_ (non_zeros_), value_data_ (non_zeros_) { } BOOST_UBLAS_INLINE compressed_vector (const compressed_vector &v): @@ -737,8 +737,8 @@ namespace boost { namespace numeric { namespace ublas { BOOST_UBLAS_INLINE compressed_vector (const vector_expression &ae, size_type non_zeros = 0): vector_expression (), - size_ (ae ().size ()), non_zeros_ (non_zeros), filled_ (0), - index_data_ (non_zeros), value_data_ (non_zeros) { + size_ (ae ().size ()), non_zeros_ (max_nz (non_zeros)), filled_ (0), + index_data_ (non_zeros_), value_data_ (non_zeros_) { vector_assign (scalar_assign (), *this, ae); } @@ -777,13 +777,20 @@ namespace boost { namespace numeric { namespace ublas { } // Resizing + private: + BOOST_UBLAS_INLINE + size_type max_nz (size_type non_zeros) const { + non_zeros = (std::max) (non_zeros, size_type (1)); + non_zeros = (std::min) (non_zeros, size_); + return non_zeros; + } + public: BOOST_UBLAS_INLINE void resize (size_type size, bool preserve = true) { // FIXME preserve unimplemented BOOST_UBLAS_CHECK (!preserve, internal_logic ()); size_ = size; - non_zeros_ = (std::max) (non_zeros_, size_type (1)); - non_zeros_ = (std::min) (non_zeros_, size_); + non_zeros_ = max_nz (non_zeros_); index_data (). resize (non_zeros_); value_data (). resize (non_zeros_); filled_ = 0; @@ -792,8 +799,7 @@ namespace boost { namespace numeric { namespace ublas { // Reserving BOOST_UBLAS_INLINE void reserve (size_type non_zeros, bool preserve = true) { - non_zeros_ = (std::max) (non_zeros, size_type (1)); - non_zeros_ = (std::min) (non_zeros_, size_); + non_zeros_ = max_nz (non_zeros); if (preserve) { index_data (). resize (non_zeros_, size_type ()); value_data (). resize (non_zeros_, value_type ()); @@ -1259,28 +1265,28 @@ namespace boost { namespace numeric { namespace ublas { private: typedef T &true_reference; typedef T *pointer; - typedef const coordinate_vector const_self_type; typedef coordinate_vector self_type; public: #ifndef BOOST_UBLAS_CT_REFERENCE_BASE_TYPEDEFS - typedef const vector_const_reference const_closure_type; + typedef const vector_const_reference const_closure_type; #else - typedef const vector_reference const_closure_type; + typedef const vector_reference const_closure_type; #endif typedef vector_reference closure_type; + typedef self_type vector_temporary_type; typedef sparse_tag storage_category; // Construction and destruction BOOST_UBLAS_INLINE coordinate_vector (): vector_expression (), - size_ (0), non_zeros_ (0), filled_ (0), - sorted_ (true), index_data_ (0), value_data_ (0) {} + size_ (0), non_zeros_ (max_nz (0)), filled_ (0), + sorted_ (true), index_data_ (non_zeros_), value_data_ (non_zeros_) {} explicit BOOST_UBLAS_INLINE coordinate_vector (size_type size, size_type non_zeros = 0): vector_expression (), - size_ (size), non_zeros_ (non_zeros), filled_ (0), - sorted_ (true), index_data_ (non_zeros), value_data_ (non_zeros) { + size_ (size), non_zeros_ (max_nz (non_zeros)), filled_ (0), + sorted_ (true), index_data_ (non_zeros_), value_data_ (non_zeros_) { } BOOST_UBLAS_INLINE coordinate_vector (const coordinate_vector &v): @@ -1291,8 +1297,8 @@ namespace boost { namespace numeric { namespace ublas { BOOST_UBLAS_INLINE coordinate_vector (const vector_expression &ae, size_type non_zeros = 0): vector_expression (), - size_ (ae ().size ()), non_zeros_ (non_zeros), filled_ (0), - sorted_ (true), index_data_ (non_zeros), value_data_ (non_zeros) { + size_ (ae ().size ()), non_zeros_ (max_nz (non_zeros)), filled_ (0), + sorted_ (true), index_data_ (non_zeros_), value_data_ (non_zeros_) { vector_assign (scalar_assign (), *this, ae); } @@ -1331,14 +1337,21 @@ namespace boost { namespace numeric { namespace ublas { } // Resizing + private: + BOOST_UBLAS_INLINE + size_type max_nz (size_type non_zeros) const { + non_zeros = (std::max) (non_zeros, size_type (1)); + non_zeros = (std::min) (non_zeros, size_); + return non_zeros; + } + public: BOOST_UBLAS_INLINE void resize (size_type size, bool preserve = true) { // FIXME preserve unimplemented BOOST_UBLAS_CHECK (!preserve, internal_logic ()); if (preserve) sort (); // remove duplicate elements. - non_zeros_ = (std::max) (non_zeros_, size_type (1)); - non_zeros_ = (std::min) (non_zeros_, size); + non_zeros_ = max_nz (non_zeros_); if (preserve) { index_data (). resize (non_zeros_, size_type ()); value_data (). resize (non_zeros_, value_type ()); @@ -1356,8 +1369,7 @@ namespace boost { namespace numeric { namespace ublas { void reserve (size_type non_zeros, bool preserve = true) { if (preserve) sort (); // remove duplicate elements. - non_zeros_ = (std::max) (non_zeros, size_type (1)); - non_zeros_ = (std::min) (non_zeros_, size_); + non_zeros_ = max_nz (non_zeros); if (preserve) { index_data (). resize (non_zeros_, size_type ()); value_data (). resize (non_zeros_, value_type ());