00001
00002
00003 #ifndef DUNE_DYNVECTOR_HH
00004 #define DUNE_DYNVECTOR_HH
00005
00006 #include <cmath>
00007 #include <cstddef>
00008 #include <cstdlib>
00009 #include <complex>
00010 #include <cstring>
00011 #include <initializer_list>
00012 #include <limits>
00013 #include <utility>
00014
00015 #include "boundschecking.hh"
00016 #include "exceptions.hh"
00017 #include "genericiterator.hh"
00018
00019 #include <vector>
00020 #include "densevector.hh"
00021
00022 namespace Dune {
00023
00032 template< class K, class Allocator > class DynamicVector;
00033 template< class K, class Allocator >
00034 struct DenseMatVecTraits< DynamicVector< K, Allocator > >
00035 {
00036 typedef DynamicVector< K, Allocator > derived_type;
00037 typedef std::vector< K, Allocator > container_type;
00038 typedef K value_type;
00039 typedef typename container_type::size_type size_type;
00040 };
00041
00042 template< class K, class Allocator >
00043 struct FieldTraits< DynamicVector< K, Allocator > >
00044 {
00045 typedef typename FieldTraits< K >::field_type field_type;
00046 typedef typename FieldTraits< K >::real_type real_type;
00047 };
00048
00055 template< class K, class Allocator = std::allocator< K > >
00056 class DynamicVector : public DenseVector< DynamicVector< K, Allocator > >
00057 {
00058 std::vector< K, Allocator > _data;
00059
00060 typedef DenseVector< DynamicVector< K, Allocator > > Base;
00061 public:
00062 typedef typename Base::size_type size_type;
00063 typedef typename Base::value_type value_type;
00064
00065 typedef Allocator allocator_type;
00066
00068 explicit DynamicVector(const allocator_type &a = allocator_type() ) :
00069 _data( a )
00070 {}
00071
00072 explicit DynamicVector(size_type n, const allocator_type &a = allocator_type() ) :
00073 _data( n, value_type(), a )
00074 {}
00075
00077 DynamicVector( size_type n, value_type c, const allocator_type &a = allocator_type() ) :
00078 _data( n, c, a )
00079 {}
00080
00082 DynamicVector (std::initializer_list<K> const &l) :
00083 _data(l)
00084 {}
00085
00087 DynamicVector(const DynamicVector & x) :
00088 Base(), _data(x._data)
00089 {}
00090
00092 DynamicVector(DynamicVector && x) :
00093 _data(std::move(x._data))
00094 {}
00095
00096 template< class T >
00097 DynamicVector(const DynamicVector< T, Allocator > & x) :
00098 _data(x.begin(), x.end(), x.get_allocator())
00099 {}
00100
00102 template< class X >
00103 DynamicVector(const DenseVector< X > & x, const allocator_type &a = allocator_type() ) :
00104 _data(a)
00105 {
00106 const size_type n = x.size();
00107 _data.reserve(n);
00108 for( size_type i =0; i<n ;++i)
00109 _data.push_back( x[ i ] );
00110 }
00111
00112 using Base::operator=;
00113
00115 DynamicVector &operator=(const DynamicVector &other)
00116 {
00117 _data = other._data;
00118 return *this;
00119 }
00120
00122 DynamicVector &operator=(DynamicVector &&other)
00123 {
00124 _data = std::move(other._data);
00125 return *this;
00126 }
00127
00128
00133 size_type capacity() const
00134 {
00135 return _data.capacity();
00136 }
00137 void resize (size_type n, value_type c = value_type() )
00138 {
00139 _data.resize(n,c);
00140 }
00141 void reserve (size_type n)
00142 {
00143 _data.reserve(n);
00144 }
00145
00146
00147 size_type size() const { return _data.size(); }
00148 K & operator[](size_type i) {
00149 DUNE_ASSERT_BOUNDS(i < size());
00150 return _data[i];
00151 }
00152 const K & operator[](size_type i) const {
00153 DUNE_ASSERT_BOUNDS(i < size());
00154 return _data[i];
00155 }
00156 };
00157
00169 template< class K, class Allocator >
00170 inline std::istream &operator>> ( std::istream &in,
00171 DynamicVector< K, Allocator > &v )
00172 {
00173 DynamicVector< K, Allocator > w(v);
00174 for( typename DynamicVector< K, Allocator >::size_type i = 0; i < w.size(); ++i )
00175 in >> w[ i ];
00176 if(in)
00177 v = std::move(w);
00178 return in;
00179 }
00180
00183 }
00184
00185 #endif