dune-common  2.5.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
dynvector.hh
Go to the documentation of this file.
1 // -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 // vi: set et ts=4 sw=2 sts=2:
3 #ifndef DUNE_DYNVECTOR_HH
4 #define DUNE_DYNVECTOR_HH
5 
6 #include <cmath>
7 #include <cstddef>
8 #include <cstdlib>
9 #include <complex>
10 #include <cstring>
11 #include <initializer_list>
12 #include <limits>
13 #include <utility>
14 
15 #include "boundschecking.hh"
16 #include "exceptions.hh"
17 #include "genericiterator.hh"
18 
19 #include <vector>
20 #include "densevector.hh"
21 
22 namespace Dune {
23 
32  template< class K, class Allocator > class DynamicVector;
33  template< class K, class Allocator >
34  struct DenseMatVecTraits< DynamicVector< K, Allocator > >
35  {
37  typedef std::vector< K, Allocator > container_type;
38  typedef K value_type;
39  typedef typename container_type::size_type size_type;
40  };
41 
42  template< class K, class Allocator >
43  struct FieldTraits< DynamicVector< K, Allocator > >
44  {
47  };
48 
55  template< class K, class Allocator = std::allocator< K > >
56  class DynamicVector : public DenseVector< DynamicVector< K, Allocator > >
57  {
58  std::vector< K, Allocator > _data;
59 
61  public:
62  typedef typename Base::size_type size_type;
63  typedef typename Base::value_type value_type;
64 
65  typedef Allocator allocator_type;
66 
68  explicit DynamicVector(const allocator_type &a = allocator_type() ) :
69  _data( a )
70  {}
71 
73  _data( n, value_type(), a )
74  {}
75 
78  _data( n, c, a )
79  {}
80 
82  DynamicVector (std::initializer_list<K> const &l) :
83  _data(l)
84  {}
85 
88  Base(), _data(x._data)
89  {}
90 
93  _data(std::move(x._data))
94  {}
95 
96  template< class T >
98  _data(x.begin(), x.end(), x.get_allocator())
99  {}
100 
102  template< class X >
104  _data(a)
105  {
106  const size_type n = x.size();
107  _data.reserve(n);
108  for( size_type i =0; i<n ;++i)
109  _data.push_back( x[ i ] );
110  }
111 
112  using Base::operator=;
113 
116  {
117  _data = other._data;
118  return *this;
119  }
120 
123  {
124  _data = std::move(other._data);
125  return *this;
126  }
127 
128  //==== forward some methods of std::vector
134  {
135  return _data.capacity();
136  }
138  {
139  _data.resize(n,c);
140  }
142  {
143  _data.reserve(n);
144  }
145 
146  //==== make this thing a vector
147  size_type size() const { return _data.size(); }
149  DUNE_ASSERT_BOUNDS(i < size());
150  return _data[i];
151  }
152  const K & operator[](size_type i) const {
153  DUNE_ASSERT_BOUNDS(i < size());
154  return _data[i];
155  }
156  };
157 
169  template< class K, class Allocator >
170  inline std::istream &operator>> ( std::istream &in,
172  {
174  for( typename DynamicVector< K, Allocator >::size_type i = 0; i < w.size(); ++i )
175  in >> w[ i ];
176  if(in)
177  v = std::move(w);
178  return in;
179  }
180 
183 } // end namespace
184 
185 #endif
FieldTraits< K >::real_type real_type
Definition: dynvector.hh:46
T field_type
export the type representing the field
Definition: ftraits.hh:26
DynamicVector & operator=(const DynamicVector &other)
Copy assignment operator.
Definition: dynvector.hh:115
DynamicVector & operator=(DynamicVector &&other)
Move assignment operator.
Definition: dynvector.hh:122
const K & operator[](size_type i) const
Definition: dynvector.hh:152
Base::size_type size_type
Definition: dynvector.hh:62
Iterator end()
end iterator
Definition: densevector.hh:314
Definition: matvectraits.hh:29
A few common exception classes.
Iterator begin()
begin iterator
Definition: densevector.hh:308
size_type size() const
size method
Definition: densevector.hh:297
std::vector< K, Allocator > container_type
Definition: dynvector.hh:37
DynamicVector(const DenseVector< X > &x, const allocator_type &a=allocator_type())
Copy constructor from another DenseVector.
Definition: dynvector.hh:103
Interface for a class of dense vectors over a given field.
Definition: densevector.hh:19
#define DUNE_ASSERT_BOUNDS(cond)
If DUNE_CHECK_BOUNDS is defined: check if condition cond holds; otherwise, do nothing.
Definition: boundschecking.hh:28
FieldTraits< K >::field_type field_type
Definition: dynvector.hh:45
void resize(size_type n, value_type c=value_type())
Definition: dynvector.hh:137
Macro for wrapping boundary checks.
DynamicVector(const allocator_type &a=allocator_type())
Constructor making uninitialized vector.
Definition: dynvector.hh:68
container_type::size_type size_type
Definition: dynvector.hh:39
DynamicVector(size_type n, const allocator_type &a=allocator_type())
Definition: dynvector.hh:72
DynamicVector(size_type n, value_type c, const allocator_type &a=allocator_type())
Constructor making vector with identical coordinates.
Definition: dynvector.hh:77
K & operator[](size_type i)
Definition: dynvector.hh:148
Traits::value_type value_type
export the type representing the field
Definition: densevector.hh:257
Stream & operator>>(Stream &stream, std::tuple< Ts...> &t)
Read a std::tuple.
Definition: streamoperators.hh:41
Traits::size_type size_type
The type used for the index access and size operation.
Definition: densevector.hh:266
void reserve(size_type n)
Definition: dynvector.hh:141
DynamicVector(DynamicVector &&x)
Move constructor.
Definition: dynvector.hh:92
size_type size() const
Definition: dynvector.hh:147
Definition: ftraits.hh:23
DynamicVector(std::initializer_list< K > const &l)
Construct from a std::initializer_list.
Definition: dynvector.hh:82
Allocator allocator_type
Definition: dynvector.hh:65
Base::value_type value_type
Definition: dynvector.hh:63
Construct a vector with a dynamic size.
Definition: dynvector.hh:32
DynamicVector< K, Allocator > derived_type
Definition: dynvector.hh:36
DynamicVector(const DynamicVector< T, Allocator > &x)
Definition: dynvector.hh:97
Implements a generic iterator class for writing stl conformant iterators.
T real_type
export the type representing the real type of the field
Definition: ftraits.hh:28
DynamicVector(const DynamicVector &x)
Constructor making vector with identical coordinates.
Definition: dynvector.hh:87
Implements the dense vector interface, with an exchangeable storage class.
size_type capacity() const
Number of elements for which memory has been allocated.
Definition: dynvector.hh:133