dune-common 3.0-git
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
22namespace 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
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(); }
150 return _data[i];
151 }
152 const K & operator[](size_type i) const {
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
Implements a generic iterator class for writing stl conformant iterators.
#define DUNE_ASSERT_BOUNDS(cond)
Definition boundschecking.hh:20
A few common exception classes.
Implements the dense vector interface, with an exchangeable storage class.
std::istream & operator>>(std::istream &is, tuple< Ts... > &t)
Read a tuple.
Definition tuples.hh:136
STL namespace.
Dune namespace.
Definition alignment.hh:11
Interface for a class of dense vectors over a given field.
Definition densevector.hh:235
Traits::value_type value_type
export the type representing the field
Definition densevector.hh:257
Iterator begin()
begin iterator
Definition densevector.hh:308
size_type size() const
size method
Definition densevector.hh:297
Iterator end()
end iterator
Definition densevector.hh:314
Traits::size_type size_type
The type used for the index access and size operation.
Definition densevector.hh:266
Construct a vector with a dynamic size.
Definition dynvector.hh:57
void resize(size_type n, value_type c=value_type())
Definition dynvector.hh:137
DynamicVector(const DynamicVector &x)
Constructor making vector with identical coordinates.
Definition dynvector.hh:87
Base::size_type size_type
Definition dynvector.hh:62
size_type size() const
Definition dynvector.hh:147
K & operator[](size_type i)
Definition dynvector.hh:148
Base::value_type value_type
Definition dynvector.hh:63
Allocator allocator_type
Definition dynvector.hh:65
DynamicVector(const allocator_type &a=allocator_type())
Constructor making uninitialized vector.
Definition dynvector.hh:68
DynamicVector(DynamicVector &&x)
Move constructor.
Definition dynvector.hh:92
DynamicVector & operator=(DynamicVector &&other)
Move assignment operator.
Definition dynvector.hh:122
DynamicVector & operator=(const DynamicVector &other)
Copy assignment operator.
Definition dynvector.hh:115
size_type capacity() const
Number of elements for which memory has been allocated.
Definition dynvector.hh:133
DynamicVector(size_type n, const allocator_type &a=allocator_type())
Definition dynvector.hh:72
DynamicVector(std::initializer_list< K > const &l)
Construct from a std::initializer_list.
Definition dynvector.hh:82
DynamicVector(const DynamicVector< T, Allocator > &x)
Definition dynvector.hh:97
const K & operator[](size_type i) const
Definition dynvector.hh:152
void reserve(size_type n)
Definition dynvector.hh:141
DynamicVector(size_type n, value_type c, const allocator_type &a=allocator_type())
Constructor making vector with identical coordinates.
Definition dynvector.hh:77
DynamicVector(const DenseVector< X > &x, const allocator_type &a=allocator_type())
Copy constructor from another DenseVector.
Definition dynvector.hh:103
DynamicVector< K, Allocator > derived_type
Definition dynvector.hh:36
std::vector< K, Allocator > container_type
Definition dynvector.hh:37
container_type::size_type size_type
Definition dynvector.hh:39
FieldTraits< K >::real_type real_type
Definition dynvector.hh:46
FieldTraits< K >::field_type field_type
Definition dynvector.hh:45
Definition ftraits.hh:24
T field_type
export the type representing the field
Definition ftraits.hh:26
T real_type
export the type representing the real type of the field
Definition ftraits.hh:28
Definition matvectraits.hh:29