dune-fem  2.8-git
storage/referencevector.hh
Go to the documentation of this file.
1 #ifndef DUNE_FEM_REFERENCEVECTOR_HH
2 #define DUNE_FEM_REFERENCEVECTOR_HH
3 
4 #include <algorithm>
5 #include <cassert>
6 #include <memory>
7 #include <utility>
8 #include <vector>
9 
10 #include <dune/common/densevector.hh>
11 #include <dune/common/ftraits.hh>
12 
14 
15 namespace Dune
16 {
17 
18  namespace Fem
19  {
20  // forward declaration
21  template< class K, class A = std::allocator< K* > >
22  class DynamicReferenceVector;
23  }
24 
25  // specialization of DenseMatVecTraits for DynamicReferenceVector
26  template< class K, class A >
27  struct DenseMatVecTraits< Fem::DynamicReferenceVector< K, A > >
28  {
30  typedef std::vector< K*, typename A::template rebind< K* >::other > container_type;
31 
32  typedef K value_type;
33  typedef typename container_type::size_type size_type;
34  };
35 
36  template< class K, class A >
37  struct FieldTraits< Fem::DynamicReferenceVector< K, A > >
38  {
39  typedef typename FieldTraits< K >::field_type field_type;
40  typedef typename FieldTraits< K >::real_type real_type;
41  };
42 
43  namespace Fem
44  {
45 
51  template< class K, class A >
52  class DynamicReferenceVector : public DenseVector< DynamicReferenceVector< K, A > >
53  {
55  typedef DenseVector< ThisType > BaseType;
56 
57  public:
58  typedef typename BaseType::size_type size_type;
59  typedef typename BaseType::value_type value_type;
61  typedef typename DenseMatVecTraits< ThisType >::container_type DofStorageType;
62 
64  explicit DynamicReferenceVector ( const A &a = A() )
65  : data_( a )
66  {}
67 
69  explicit DynamicReferenceVector ( size_type n, const A &a = A() )
70  : data_( n, nullptr, a )
71  {}
72 
75  : BaseType(), // DenseVector stores no data anyway
76  data_( other.data_ )
77  {}
78 
81  : data_( std::move( other.data_ ) )
82  {}
83 
84  // issue with gcc 5.5 and ambiguity of
85  // operator= ( const DenseVector< V > &other )
86  // using BaseType::operator=;
87  // to avoid this adding to missing operator= overload:
89  {
90  for (size_type i=0; i<size(); i++)
91  data_[i] = k;
92  return *this;
93  }
94 
95  template< class V >
96  ThisType & operator= ( const DenseVector< V > &other )
97  {
98  assert( data_.size() == other.size() );
99  std::copy( other.begin(), other.end(), BaseType::begin() );
100  return *this;
101  }
102 
103  ThisType & operator= ( const ThisType &other )
104  {
105  assert( data_.size() == other.size() );
106  std::copy( other.begin(), other.end(), BaseType::begin() );
107  return *this;
108  }
109 
111  {
112  data_ = std::move( other.data_ );
113  return *this;
114  }
115 
117  {
118  return data_.capacity();
119  }
120 
121  void resize ( size_type n )
122  {
123  data_.resize( n, nullptr );
124  }
125 
126  void reserve ( size_type n )
127  {
128  data_.reserve( n );
129  }
130 
132  void bind ( size_type i, K& u )
133  {
134  assert( i < data_.size() );
135  data_[ i ] = &u;
136  }
137 
139  void unbind ( size_type i )
140  {
141  asssert( i < data_.size() );
142  data_[ i ] = nullptr;
143  }
144 
145  size_type size () const
146  {
147  return data_.size();
148  }
149 
151  {
152  return *data_[ i ];
153  }
154 
155  const value_type &operator[] ( size_type i ) const
156  {
157  return *data_[ i ];
158  }
159 
160  void clear()
161  {
162  for (std::size_t i=0;i<size();++i)
163  (*this)[i] = 0;
164  }
165 
166  private:
167  DofStorageType data_;
168  };
169 
170  } // namespace Fem
171 
172 
173 } // namespace Dune
174 
175 #endif //#ifndef DUNE_FEM_REFERENCEVECTOR_HH
Definition: bindguard.hh:11
An implementation of DenseVector which uses a std::vector of references as storage.
Definition: storage/referencevector.hh:53
size_type size() const
Definition: storage/referencevector.hh:145
DynamicReferenceVector(ThisType &&other)
Move constructor.
Definition: storage/referencevector.hh:80
void unbind(size_type i)
Unbind i-th entry.
Definition: storage/referencevector.hh:139
void reserve(size_type n)
Definition: storage/referencevector.hh:126
ThisType & operator=(const value_type &k)
Definition: storage/referencevector.hh:88
void resize(size_type n)
Definition: storage/referencevector.hh:121
BaseType::size_type size_type
Definition: storage/referencevector.hh:58
BaseType::value_type value_type
Definition: storage/referencevector.hh:59
value_type FieldType
Definition: storage/referencevector.hh:60
value_type & operator[](size_type i)
Definition: storage/referencevector.hh:150
DynamicReferenceVector(const ThisType &other)
Copy constructor.
Definition: storage/referencevector.hh:74
void clear()
Definition: storage/referencevector.hh:160
DenseMatVecTraits< ThisType >::container_type DofStorageType
Definition: storage/referencevector.hh:61
DynamicReferenceVector(const A &a=A())
Constructor with uninitialized vector.
Definition: storage/referencevector.hh:64
size_type capacity() const
Definition: storage/referencevector.hh:116
DynamicReferenceVector(size_type n, const A &a=A())
Constructor with uninitialized vector of size n.
Definition: storage/referencevector.hh:69
void bind(size_type i, K &u)
Bind i-th entry to a reference.
Definition: storage/referencevector.hh:132
K value_type
Definition: storage/referencevector.hh:32
container_type::size_type size_type
Definition: storage/referencevector.hh:33
std::vector< K *, typename A::template rebind< K * >::other > container_type
Definition: storage/referencevector.hh:30
Fem::DynamicReferenceVector< K, A > derived_type
Definition: storage/referencevector.hh:29
FieldTraits< K >::field_type field_type
Definition: storage/referencevector.hh:39
FieldTraits< K >::real_type real_type
Definition: storage/referencevector.hh:40