Boost logo

Ublas :

From: Neal Becker (ndbecker2_at_[hidden])
Date: 2005-08-17 08:55:27


Here is a test to demonstrate use:

#include <boost/numeric/ublas/vector.hpp>
#include <complex>
#include <iostream>
#include <iterator>

typedef std::complex<double> Complex;

template<typename T>
class X : public
boost::numeric::ublas::unbounded_array<T,std::allocator<T>,false> {
public:
  typedef boost::numeric::ublas::unbounded_array<T,std::allocator<T>,false>
base;
  typedef typename std::allocator<T>::size_type size_type;
  X () : base () {}
  explicit X (size_type size) : base (size) {}
  explicit X (size_type size, const T &init) : base (size, init) {}
};

int main() {

  boost::numeric::ublas::vector<Complex> v (10);
  boost::numeric::ublas::vector<Complex, X<Complex> > w (12);
  std::copy (v.begin(), v.end(), std::ostream_iterator<Complex> (std::cout,
" "));
  std::cout << '\n';
  std::copy (w.begin(), w.end(), std::ostream_iterator<Complex> (std::cout,
" "));
  std::cout << '\n';
}

diff -u boost/numeric/ublas/fwd.hpp{.orig,}; diff -u
boost/numeric/ublas/storage.hpp{.orig,}
--- boost/numeric/ublas/fwd.hpp.orig 2005-08-17 09:47:43.000000000 -0400
+++ boost/numeric/ublas/fwd.hpp 2005-08-17 08:34:29.000000000 -0400
@@ -22,7 +22,7 @@
 namespace boost { namespace numeric { namespace ublas {
 
     // Storage types
- template<class T, class ALLOC = std::allocator<T> >
+ template<class T, class ALLOC = std::allocator<T>, bool doinit=true >
     class unbounded_array;
 
     template<class T, std::size_t N, class ALLOC = std::allocator<T> >
--- boost/numeric/ublas/storage.hpp.orig 2005-08-17 07:16:57.000000000 -0400
+++ boost/numeric/ublas/storage.hpp 2005-08-17 09:46:13.000000000 -0400
@@ -37,11 +37,11 @@
 
 
     // Unbounded array - with allocator
- template<class T, class ALLOC>
+ template<class T, class ALLOC, bool doinit>
     class unbounded_array:
- public storage_array<unbounded_array<T, ALLOC> > {
+ public storage_array<unbounded_array<T, ALLOC, doinit> > {
 
- typedef unbounded_array<T, ALLOC> self_type;
+ typedef unbounded_array<T, ALLOC, doinit> self_type;
     public:
         typedef ALLOC allocator_type;
         typedef typename ALLOC::size_type size_type;
@@ -64,15 +64,17 @@
             alloc_(a), size_ (size) {
             if (size_) {
                 data_ = alloc_.allocate (size_);
- // ISSUE some compilers may zero POD here
+ if (doinit) {
+ // ISSUE some compilers may zero POD here
 #ifdef BOOST_UBLAS_USEFUL_ARRAY_PLACEMENT_NEW
- // array form fails on some compilers due to size cookie,
is it standard conforming?
- new (data_) value_type[size_];
+ // array form fails on some compilers due to size cookie, is it
standard conforming?
+ new (data_) value_type[size_];
 #else
- for (pointer d = data_; d != data_ + size_; ++d)
+ for (pointer d = data_; d != data_ + size_; ++d)
                     new (d) value_type;
 #endif
- }
+ }
+ }
         }
         // No value initialised, but still be default constructed
         BOOST_UBLAS_INLINE
@@ -80,7 +82,9 @@
             alloc_ (a), size_ (size) {
             if (size_) {
                 data_ = alloc_.allocate (size_);
- std::uninitialized_fill (begin(), end(), init);
+ if (doinit) {
+ std::uninitialized_fill (begin(), end(), init);
+ }
             }
         }
         BOOST_UBLAS_INLINE
@@ -96,13 +100,14 @@
         }
         BOOST_UBLAS_INLINE
         ~unbounded_array () {
- if (size_) {
- const iterator i_end = end();
- for (iterator i = begin (); i != i_end; ++i) {
- iterator_destroy (i);
- }
- alloc_.deallocate (data_, size_);
- }
+ if (size_) {
+ std::_Destroy (begin(), end(), alloc_);
+// const iterator i_end = end();
+// for (iterator i = begin (); i != i_end; ++i) {
+// iterator_destroy (i);
+// }
+ alloc_.deallocate (data_, size_);
+ }
         }
 
         // Resizing