diff --git a/include/boost/numeric/ublas/banded.hpp b/include/boost/numeric/ublas/banded.hpp index 7109984d..a7d45354 100644 --- a/include/boost/numeric/ublas/banded.hpp +++ b/include/boost/numeric/ublas/banded.hpp @@ -64,8 +64,7 @@ namespace boost { namespace numeric { namespace ublas { banded_matrix (size_type size1, size_type size2, size_type lower = 0, size_type upper = 0): matrix_expression (), size1_ (size1), size2_ (size2), - lower_ (lower), upper_ (upper), data_ (0) { - resize (size1, size2, lower, upper); + lower_ (lower), upper_ (upper), data_ ((std::max) (size1, size2) * (lower + 1 + upper)) { } BOOST_UBLAS_INLINE banded_matrix (size_type size1, size_type size2, size_type lower, size_type upper, const array_type &data): diff --git a/include/boost/numeric/ublas/config.hpp b/include/boost/numeric/ublas/config.hpp index 25ac9a64..1b8bebbd 100644 --- a/include/boost/numeric/ublas/config.hpp +++ b/include/boost/numeric/ublas/config.hpp @@ -17,6 +17,7 @@ #ifndef BOOST_UBLAS_CONFIG_H #define BOOST_UBLAS_CONFIG_H +#include #include #include diff --git a/include/boost/numeric/ublas/fwd.hpp b/include/boost/numeric/ublas/fwd.hpp index 13b5fdaa..dabfb264 100644 --- a/include/boost/numeric/ublas/fwd.hpp +++ b/include/boost/numeric/ublas/fwd.hpp @@ -39,9 +39,12 @@ namespace boost { namespace numeric { namespace ublas { struct concrete_tag {}; struct abstract_tag {}; - template + template > class unbounded_array; + template > + class bounded_array; + class range; class slice; template > diff --git a/include/boost/numeric/ublas/hermitian.hpp b/include/boost/numeric/ublas/hermitian.hpp index 23eb9e8f..97695a15 100644 --- a/include/boost/numeric/ublas/hermitian.hpp +++ b/include/boost/numeric/ublas/hermitian.hpp @@ -322,14 +322,12 @@ namespace boost { namespace numeric { namespace ublas { BOOST_UBLAS_INLINE hermitian_matrix (size_type size): matrix_expression (), - size_ (BOOST_UBLAS_SAME (size, size)), data_ (0) { - resize (size); + size_ (BOOST_UBLAS_SAME (size, size)), data_ (functor1_type::packed_size (size, size)) { } BOOST_UBLAS_INLINE hermitian_matrix (size_type size1, size_type size2): matrix_expression (), - size_ (BOOST_UBLAS_SAME (size1, size2)), data_ (0) { - resize (size1, size2); + size_ (BOOST_UBLAS_SAME (size1, size2)), data_ (functor1_type::packed_size (size1, size2)) { } BOOST_UBLAS_INLINE hermitian_matrix (size_type size, const array_type &data): diff --git a/include/boost/numeric/ublas/storage.hpp b/include/boost/numeric/ublas/storage.hpp index 3d223a70..5ca671e1 100644 --- a/include/boost/numeric/ublas/storage.hpp +++ b/include/boost/numeric/ublas/storage.hpp @@ -63,57 +63,72 @@ namespace boost { namespace numeric { namespace ublas { struct no_init {}; - // Unbounded array - template + // Unbounded array - with allocator + template class unbounded_array { public: - typedef std::size_t size_type; - typedef std::ptrdiff_t difference_type; + typedef typename A::size_type size_type; + typedef typename A::difference_type difference_type; typedef T value_type; typedef const T &const_reference; typedef T &reference; typedef const T *const_pointer; typedef T *pointer; - // Construction and destruction - BOOST_UBLAS_INLINE - unbounded_array (): - size_ (0), data_ (new value_type [0]) { - std::fill (data_, data_ + size_, value_type ()); - } + // Construction and destruction, no default constructor explicit BOOST_UBLAS_INLINE - unbounded_array (no_init): - size_ (0), data_ (new value_type [0]) { - } - explicit BOOST_UBLAS_INLINE - unbounded_array (size_type size): - size_ (size), data_ (new value_type [size]) { - std::fill (data_, data_ + size_, value_type ()); + unbounded_array (size_type size, const A&a = A()): + size_ (size), data_ (alloc.allocate (size)) { + value_type v = value_type(); + for (iterator i = begin(); i != end(); ++i) { + alloc.construct (&(*i), v); + } } BOOST_UBLAS_INLINE unbounded_array (size_type size, no_init): - size_ (size), data_ (new value_type [size]) { + size_ (size), data_ (alloc.allocate (size)) { } BOOST_UBLAS_INLINE unbounded_array (const unbounded_array &a): - size_ (a.size_), data_ (new value_type [a.size_]) { - *this = a; + size_ (a.size_), data_ (alloc.allocate (a.size_)) { + const_iterator ai = a.begin(); + for (iterator i = begin(); i != end(); ++i) { + alloc.construct (&(*i), *ai); + ++ai; + } } BOOST_UBLAS_INLINE ~unbounded_array () { - delete [] data_; + alloc.deallocate (data_, size_); } // Resizing BOOST_UBLAS_INLINE void resize (size_type size, bool preserve = true) { if (size != size_) { - pointer data = new value_type [size]; + pointer data = alloc.allocate (size); if (preserve) { - std::copy (data_, data_ + (std::min) (size, size_), data); - std::fill (data + (std::min) (size, size_), data + size, value_type ()); + const_iterator si = begin(); + pointer di = data; + if (size < size_) { + for (; di != data + size; ++di) { + alloc.construct (&(*di), *si); + ++si; + } + } + else { + for (; si != end(); ++si) { + alloc.construct (&(*di), *si); + ++di; + } + value_type v = value_type(); + for (; di != data + size; ++di) { + alloc.construct (&(*di), v); + } + } + } - delete [] data_; + alloc.deallocate (data_, size_); size_ = size; data_ = data; } @@ -258,16 +273,17 @@ namespace boost { namespace numeric { namespace ublas { } private: + A alloc; size_type size_; pointer data_; }; - // Bounded array - template + // Bounded array - with allocator for size_type and difference_type + template class bounded_array { public: - typedef std::size_t size_type; - typedef std::ptrdiff_t difference_type; + typedef typename A::size_type size_type; + typedef typename A::difference_type difference_type; typedef T value_type; typedef const T &const_reference; typedef T &reference; diff --git a/include/boost/numeric/ublas/symmetric.hpp b/include/boost/numeric/ublas/symmetric.hpp index 6512e18d..837aa928 100644 --- a/include/boost/numeric/ublas/symmetric.hpp +++ b/include/boost/numeric/ublas/symmetric.hpp @@ -81,14 +81,12 @@ namespace boost { namespace numeric { namespace ublas { BOOST_UBLAS_INLINE symmetric_matrix (size_type size): matrix_expression (), - size_ (BOOST_UBLAS_SAME (size, size)), data_ (0) { - resize (size); + size_ (BOOST_UBLAS_SAME (size, size)), data_ (functor1_type::packed_size (size, size)) { } BOOST_UBLAS_INLINE symmetric_matrix (size_type size1, size_type size2): matrix_expression (), - size_ (BOOST_UBLAS_SAME (size1, size2)), data_ (0) { - resize (size1, size2); + size_ (BOOST_UBLAS_SAME (size1, size2)), data_ (functor1_type::packed_size (size1, size2)) { } BOOST_UBLAS_INLINE symmetric_matrix (size_type size, const array_type &data): diff --git a/include/boost/numeric/ublas/triangular.hpp b/include/boost/numeric/ublas/triangular.hpp index 54205154..be2d61b3 100644 --- a/include/boost/numeric/ublas/triangular.hpp +++ b/include/boost/numeric/ublas/triangular.hpp @@ -64,8 +64,7 @@ namespace boost { namespace numeric { namespace ublas { BOOST_UBLAS_INLINE triangular_matrix (size_type size1, size_type size2): matrix_expression (), - size1_ (size1), size2_ (size2), data_ (0) { - resize (size1, size2); + size1_ (size1), size2_ (size2), data_ (functor1_type::packed_size (size1, size2)) { } BOOST_UBLAS_INLINE triangular_matrix (size_type size1, size_type size2, const array_type &data):