DUNE PDELab (2.8)

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 {
36 typedef DynamicVector< K, Allocator > derived_type;
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 {
45 typedef typename FieldTraits< K >::field_type field_type;
46 typedef typename FieldTraits< K >::real_type real_type;
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 std::vector< K, Allocator > container_type;
66
67 typedef Allocator allocator_type;
68
70 explicit DynamicVector(const allocator_type &a = allocator_type() ) :
71 _data( a )
72 {}
73
74 explicit DynamicVector(size_type n, const allocator_type &a = allocator_type() ) :
75 _data( n, value_type(), a )
76 {}
77
79 DynamicVector( size_type n, value_type c, const allocator_type &a = allocator_type() ) :
80 _data( n, c, a )
81 {}
82
84 DynamicVector (std::initializer_list<K> const &l) :
85 _data(l)
86 {}
87
90 Base(), _data(x._data)
91 {}
92
95 _data(std::move(x._data))
96 {}
97
98 template< class T >
100 _data(x.begin(), x.end(), x.get_allocator())
101 {}
102
104 template< class X >
105 DynamicVector(const DenseVector< X > & x, const allocator_type &a = allocator_type() ) :
106 _data(a)
107 {
108 const size_type n = x.size();
109 _data.reserve(n);
110 for( size_type i =0; i<n ;++i)
111 _data.push_back( x[ i ] );
112 }
113
114 using Base::operator=;
115
118 {
119 _data = other._data;
120 return *this;
121 }
122
125 {
126 _data = std::move(other._data);
127 return *this;
128 }
129
130 //==== forward some methods of std::vector
135 size_type capacity() const
136 {
137 return _data.capacity();
138 }
139 void resize (size_type n, value_type c = value_type() )
140 {
141 _data.resize(n,c);
142 }
143 void reserve (size_type n)
144 {
145 _data.reserve(n);
146 }
147
148 //==== make this thing a vector
149 size_type size() const { return _data.size(); }
150 K & operator[](size_type i) {
151 DUNE_ASSERT_BOUNDS(i < size());
152 return _data[i];
153 }
154 const K & operator[](size_type i) const {
155 DUNE_ASSERT_BOUNDS(i < size());
156 return _data[i];
157 }
158
160 K* data() noexcept
161 {
162 return _data.data();
163 }
164
166 const K* data() const noexcept
167 {
168 return _data.data();
169 }
170
171 const container_type &container () const { return _data; }
172 container_type &container () { return _data; }
173 };
174
186 template< class K, class Allocator >
187 inline std::istream &operator>> ( std::istream &in,
189 {
191 for( typename DynamicVector< K, Allocator >::size_type i = 0; i < w.size(); ++i )
192 in >> w[ i ];
193 if(in)
194 v = std::move(w);
195 return in;
196 }
197
200} // end namespace
201
202#endif
Macro for wrapping boundary checks.
Interface for a class of dense vectors over a given field.
Definition: densevector.hh:227
Traits::value_type value_type
export the type representing the field
Definition: densevector.hh:248
Iterator begin()
begin iterator
Definition: densevector.hh:348
size_type size() const
size method
Definition: densevector.hh:337
Iterator end()
end iterator
Definition: densevector.hh:354
Traits::size_type size_type
The type used for the index access and size operation.
Definition: densevector.hh:257
Construct a vector with a dynamic size.
Definition: dynvector.hh:57
DynamicVector(const DynamicVector &x)
Constructor making vector with identical coordinates.
Definition: dynvector.hh:89
const K * data() const noexcept
return pointer to underlying array
Definition: dynvector.hh:166
DynamicVector(const allocator_type &a=allocator_type())
Constructor making uninitialized vector.
Definition: dynvector.hh:70
DynamicVector(DynamicVector &&x)
Move constructor.
Definition: dynvector.hh:94
K * data() noexcept
return pointer to underlying array
Definition: dynvector.hh:160
DynamicVector & operator=(DynamicVector &&other)
Move assignment operator.
Definition: dynvector.hh:124
DynamicVector & operator=(const DynamicVector &other)
Copy assignment operator.
Definition: dynvector.hh:117
size_type capacity() const
Number of elements for which memory has been allocated.
Definition: dynvector.hh:135
DynamicVector(std::initializer_list< K > const &l)
Construct from a std::initializer_list.
Definition: dynvector.hh:84
DynamicVector(size_type n, value_type c, const allocator_type &a=allocator_type())
Constructor making vector with identical coordinates.
Definition: dynvector.hh:79
DynamicVector(const DenseVector< X > &x, const allocator_type &a=allocator_type())
Copy constructor from another DenseVector.
Definition: dynvector.hh:105
Implements the dense vector interface, with an exchangeable storage class.
TypeTree-specific exceptions.
Implements a generic iterator class for writing stl conformant iterators.
Stream & operator>>(Stream &stream, std::tuple< Ts... > &t)
Read a std::tuple.
Definition: streamoperators.hh:41
#define DUNE_ASSERT_BOUNDS(cond)
If DUNE_CHECK_BOUNDS is defined: check if condition cond holds; otherwise, do nothing.
Definition: boundschecking.hh:28
Dune namespace.
Definition: alignedallocator.hh:11
STL namespace.
Creative Commons License   |  Legal Statements / Impressum  |  Hosted by TU Dresden  |  generated with Hugo v0.111.3 (Dec 21, 23:30, 2024)