Dune Core Modules (2.5.0)

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 Allocator allocator_type;
66
68 explicit DynamicVector(const allocator_type &a = allocator_type() ) :
69 _data( a )
70 {}
71
72 explicit DynamicVector(size_type n, const allocator_type &a = allocator_type() ) :
73 _data( n, value_type(), a )
74 {}
75
77 DynamicVector( size_type n, value_type c, const allocator_type &a = allocator_type() ) :
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 >
103 DynamicVector(const DenseVector< X > & x, const allocator_type &a = allocator_type() ) :
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
133 size_type capacity() const
134 {
135 return _data.capacity();
136 }
137 void resize (size_type n, value_type c = value_type() )
138 {
139 _data.resize(n,c);
140 }
141 void reserve (size_type n)
142 {
143 _data.reserve(n);
144 }
145
146 //==== make this thing a vector
147 size_type size() const { return _data.size(); }
148 K & operator[](size_type i) {
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
Macro for wrapping boundary checks.
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
DynamicVector(const DynamicVector &x)
Constructor making vector with identical coordinates.
Definition: dynvector.hh:87
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(std::initializer_list< K > const &l)
Construct from a std::initializer_list.
Definition: dynvector.hh:82
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
Implements the dense vector interface, with an exchangeable storage class.
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: alignment.hh:11
STL namespace.
Creative Commons License   |  Legal Statements / Impressum  |  Hosted by TU Dresden  |  generated with Hugo v0.111.3 (Jul 15, 22:36, 2024)