Dune Core Modules (unstable)

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// SPDX-FileCopyrightInfo: Copyright © DUNE Project contributors, see file LICENSE.md in module root
4// SPDX-License-Identifier: LicenseRef-GPL-2.0-only-with-DUNE-exception
5#ifndef DUNE_DYNVECTOR_HH
6#define DUNE_DYNVECTOR_HH
7
8#include <cmath>
9#include <cstddef>
10#include <cstdlib>
11#include <complex>
12#include <cstring>
13#include <initializer_list>
14#include <limits>
15#include <utility>
16
17#include "boundschecking.hh"
18#include "exceptions.hh"
19#include "genericiterator.hh"
20
21#include <vector>
22#include "densevector.hh"
23
24namespace Dune {
25
34 template< class K, class Allocator > class DynamicVector;
35 template< class K, class Allocator >
36 struct DenseMatVecTraits< DynamicVector< K, Allocator > >
37 {
38 typedef DynamicVector< K, Allocator > derived_type;
39 typedef std::vector< K, Allocator > container_type;
40 typedef K value_type;
41 typedef typename container_type::size_type size_type;
42 };
43
44 template< class K, class Allocator >
45 struct FieldTraits< DynamicVector< K, Allocator > >
46 {
47 typedef typename FieldTraits< K >::field_type field_type;
48 typedef typename FieldTraits< K >::real_type real_type;
49 };
50
57 template< class K, class Allocator = std::allocator< K > >
58 class DynamicVector : public DenseVector< DynamicVector< K, Allocator > >
59 {
60 std::vector< K, Allocator > _data;
61
63 public:
64 typedef typename Base::size_type size_type;
65 typedef typename Base::value_type value_type;
66
67 typedef std::vector< K, Allocator > container_type;
68
69 typedef Allocator allocator_type;
70
72 explicit DynamicVector(const allocator_type &a = allocator_type() ) :
73 _data( a )
74 {}
75
76 explicit DynamicVector(size_type n, const allocator_type &a = allocator_type() ) :
77 _data( n, value_type(), a )
78 {}
79
81 DynamicVector( size_type n, value_type c, const allocator_type &a = allocator_type() ) :
82 _data( n, c, a )
83 {}
84
86 DynamicVector (std::initializer_list<K> const &l) :
87 _data(l)
88 {}
89
92 Base(), _data(x._data)
93 {}
94
97 _data(std::move(x._data))
98 {}
99
100 template< class T >
102 _data(x.begin(), x.end(), x.get_allocator())
103 {}
104
106 template< class X >
107 DynamicVector(const DenseVector< X > & x, const allocator_type &a = allocator_type() ) :
108 _data(a)
109 {
110 const size_type n = x.size();
111 _data.reserve(n);
112 for( size_type i =0; i<n ;++i)
113 _data.push_back( x[ i ] );
114 }
115
116 using Base::operator=;
117
120 {
121 _data = other._data;
122 return *this;
123 }
124
127 {
128 _data = std::move(other._data);
129 return *this;
130 }
131
132 //==== forward some methods of std::vector
137 size_type capacity() const
138 {
139 return _data.capacity();
140 }
141 void resize (size_type n, value_type c = value_type() )
142 {
143 _data.resize(n,c);
144 }
145 void reserve (size_type n)
146 {
147 _data.reserve(n);
148 }
149
150 //==== make this thing a vector
151 size_type size() const { return _data.size(); }
152 K & operator[](size_type i) {
153 DUNE_ASSERT_BOUNDS(i < size());
154 return _data[i];
155 }
156 const K & operator[](size_type i) const {
157 DUNE_ASSERT_BOUNDS(i < size());
158 return _data[i];
159 }
160
162 K* data() noexcept
163 {
164 return _data.data();
165 }
166
168 const K* data() const noexcept
169 {
170 return _data.data();
171 }
172
173 const container_type &container () const { return _data; }
174 container_type &container () { return _data; }
175 };
176
188 template< class K, class Allocator >
189 inline std::istream &operator>> ( std::istream &in,
191 {
193 for( typename DynamicVector< K, Allocator >::size_type i = 0; i < w.size(); ++i )
194 in >> w[ i ];
195 if(in)
196 v = std::move(w);
197 return in;
198 }
199
202} // end namespace
203
204#endif
Macro for wrapping boundary checks.
Interface for a class of dense vectors over a given field.
Definition: densevector.hh:229
Traits::value_type value_type
export the type representing the field
Definition: densevector.hh:250
Iterator begin()
begin iterator
Definition: densevector.hh:347
size_type size() const
size method
Definition: densevector.hh:336
Iterator end()
end iterator
Definition: densevector.hh:353
Traits::size_type size_type
The type used for the index access and size operation.
Definition: densevector.hh:259
Construct a vector with a dynamic size.
Definition: dynvector.hh:59
DynamicVector(const DynamicVector &x)
Constructor making vector with identical coordinates.
Definition: dynvector.hh:91
const K * data() const noexcept
return pointer to underlying array
Definition: dynvector.hh:168
DynamicVector(const allocator_type &a=allocator_type())
Constructor making uninitialized vector.
Definition: dynvector.hh:72
DynamicVector(DynamicVector &&x)
Move constructor.
Definition: dynvector.hh:96
K * data() noexcept
return pointer to underlying array
Definition: dynvector.hh:162
DynamicVector & operator=(DynamicVector &&other)
Move assignment operator.
Definition: dynvector.hh:126
DynamicVector & operator=(const DynamicVector &other)
Copy assignment operator.
Definition: dynvector.hh:119
size_type capacity() const
Number of elements for which memory has been allocated.
Definition: dynvector.hh:137
DynamicVector(std::initializer_list< K > const &l)
Construct from a std::initializer_list.
Definition: dynvector.hh:86
DynamicVector(size_type n, value_type c, const allocator_type &a=allocator_type())
Constructor making vector with identical coordinates.
Definition: dynvector.hh:81
DynamicVector(const DenseVector< X > &x, const allocator_type &a=allocator_type())
Copy constructor from another DenseVector.
Definition: dynvector.hh:107
Implements the dense vector interface, with an exchangeable storage class.
Implements a generic iterator class for writing stl conformant iterators.
std::istream & operator>>(std::istream &stream, std::tuple< Ts... > &t)
Read a std::tuple.
Definition: streamoperators.hh:43
#define DUNE_ASSERT_BOUNDS(cond)
If DUNE_CHECK_BOUNDS is defined: check if condition cond holds; otherwise, do nothing.
Definition: boundschecking.hh:30
Dune namespace.
Definition: alignedallocator.hh:13
STL namespace.
Creative Commons License   |  Legal Statements / Impressum  |  Hosted by TU Dresden  |  generated with Hugo v0.111.3 (Dec 21, 23:30, 2024)