dune-grid-glue  2.3.0
multivector.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_GRIDGLUE_COMMON_MULTIVECTOR_HH
4 #define DUNE_GRIDGLUE_COMMON_MULTIVECTOR_HH
5 
6 #include <dune/common/tuples.hh>
7 #include <dune/common/tupleutility.hh>
8 #include <dune/common/typetraits.hh>
9 #include <dune/common/iteratorfacades.hh>
10 #include <dune/common/typetraits.hh>
11 #include <vector>
12 
13 #include <iostream>
14 #include <cassert>
15 
16 namespace Dune {
17 
18 #ifndef DOXYGEN
19  // TMPs for the MultiVector tuples stuff
20  namespace {
21  struct MultiVectorAssertSize
22  {
23  size_t sz_;
24  MultiVectorAssertSize(size_t sz) : sz_(sz) {}
25  template <class E>
26  void visit(E& elem) { assert(elem.size() == sz_); }
27  };
28  struct MultiVectorEraser
29  {
30  size_t front, back;
31  MultiVectorEraser(size_t f, size_t b) : front(f), back(b) {}
32  template <class E>
33  void visit(E& elem)
34  {
35  elem.erase( elem.begin()+front, elem.begin()+back );
36  }
37  };
38  struct MultiVectorPrinter
39  {
40  std::ostream & s;
41  size_t pos;
42  std::string del;
43  MultiVectorPrinter(std::ostream & _s, size_t p) : s(_s), pos(p), del("") {}
44  template <class E>
45  void visit(E& elem)
46  {
47  s << del << elem[pos];
48  del = ", ";
49  }
50  };
51  struct MultiVectorAssign
52  {
53  size_t pos1, pos2;
54  MultiVectorAssign(size_t p1, size_t p2) : pos1(p1), pos2(p2) {}
55  template <class E1, class E2>
56  void visit(E1& elem1, E2& elem2)
57  {
58  elem1[pos1] = elem2[pos2];
59  }
60  };
61  } // end empty namespace
62 #endif
63 
69  template< typename T >
71 
72  template< typename T >
73  struct MultiDataProxy
74  {
77  T & _vectors;
78 
79  int pos;
80  std::string name;
81  MultiDataProxy(T & v, size_t pos, std::string _n) :
82  _vectors(v), pos(pos), name(_n) {}
83  MultiDataProxy(ConstProxy & other) :
84  _vectors(other._vectors), pos(other.pos), name(other.name) {}
85  MultiDataProxy(MutableProxy & other) :
86  _vectors(other._vectors), pos(other.pos), name(other.name) {}
87  // compare
88  bool operator == (const MultiDataProxy & other) const { return get<0>() == other.get<0>(); }
89  bool operator != (const MultiDataProxy & other) const { return get<0>() != other.get<0>(); }
90  bool operator < (const MultiDataProxy & other) const { return get<0>() < other.get<0>(); }
91  bool operator > (const MultiDataProxy & other) const { return get<0>() > other.get<0>(); }
92  // assign
93  MultiDataProxy& operator = (const ConstProxy & other) {
94  assign(other);
95  return *this;
96  }
97  MultiDataProxy& operator = (const MutableProxy & other) {
98  assign(other);
99  return *this;
100  }
101 
102  // access
103  template <size_t N>
104  typename TypeTraits<typename tuple_element<N,T>::type>::ReferredType::reference
105  get() {
106  return Dune::get<N>(_vectors)[pos];
107  }
108  template <size_t N>
109  typename TypeTraits<typename tuple_element<N,T>::type>::ReferredType::const_reference
110  get() const {
111  return Dune::get<N>(_vectors)[pos];
112  }
113  private:
114  template<typename P>
115  void assign(const P & other) {
116 #ifdef DEBUG_MULTIVEC
117  std::cerr << "Assign " << name << "," << pos
118  << "\n from " << other.name << "," << other.pos << std::endl;
119 #endif
120  MultiVectorAssign assign(pos, other.pos);
121  ForEachValuePair<T,const T> forEach(_vectors, other._vectors);
122  forEach.apply(assign);
123  }
124  };
125 
126  template< typename T >
127  std::ostream& operator<< (std::ostream & s, const MultiDataProxy<T> & i)
128  {
129  s << "(";
130  MultiVectorPrinter printer(s, i.pos);
131  ForEachValue<T> forEach(i._vectors);
132  forEach.apply(printer);
133  s << ")";
134  return s;
135  }
136 
137  template< typename T >
139  public Dune::BidirectionalIteratorFacade< MultiVectorIterator<T>,
140  MultiDataProxy<T> >
141  {
142  // friend class MultiVectorIterator<typename remove_const<C>::type, typename remove_const<T>::type >;
143  // friend class TestIterator<const typename remove_const<C>::type, const typename remove_const<T>::type >;
144  public:
146 
147  // constructors
148  MultiVectorIterator(T & v, size_t n, std::string i) :
149  data(v,n,i) {}
151  data(other.data)
152  {
153 #ifdef DEBUG_MULTIVEC
154  std::cerr << "Copy Iterator " << data.name << "," << data.pos << std::endl;
155 #endif
156  }
157 
158  size_t pos() const { return data.pos; }
159 
161  {
162 #ifdef DEBUG_MULTIVEC
163  // std::cerr << "Assign Iterator " << data.name << "," << data.pos
164  // << "\n from " << other.data.name << "," << other.data.pos << std::endl;
165 #endif
166  assert(other.data._vectors == data._vectors);
167  data.pos = other.data.pos;
168  return *this;
169  }
170 
171  // operators
172  bool equals (const MultiVectorIterator & other) const
173  {
174 #ifdef DEBUG_MULTIVEC
175  // std::cerr << "Compare " << data.name << "," << data.pos
176  // << " with " << other.data.name << "," << other.data.pos << "\n";
177 #endif
178  assert(other.data._vectors == data._vectors);
179  return other.data.pos == data.pos;
180  }
181  // in-/decrement
182  void increment()
183  {
184 #ifdef DEBUG_MULTIVEC
185  // std::cerr << "Increment " << data.name << "," << data.pos << std::endl;
186 #endif
187  data.pos++;
188  }
189  void decrement()
190  {
191 #ifdef DEBUG_MULTIVEC
192  std::cerr << "Decrement " << data.name << "," << data.pos << std::endl;
193 #endif
194  data.pos--;
195  }
196  // dereference
198  {
199 #ifdef DEBUG_MULTIVEC
200  std::cerr << "dereference " << data.name << "," << data.pos << std::endl;
201 #endif
202  return data;
203  }
204  };
205 
206  template<typename A, typename B, typename C, typename D>
208  {
210 
211  typedef tuple<A&, B&, C&, D&> T;
212  T _vectors;
213  std::string _name;
214 
215  public:
218 
221 
224 
226  typedef value_type reference;
227 
229  typedef const_value_type const_reference;
230 
232  typedef reference* pointer;
233 
235  typedef const_reference* const_pointer;
236 
238  typedef size_t size_type;
239 
241 
247 
248  MultiVector(A & a, B & b, C & c, D & d, std::string n = "?") :
249  _vectors(tie(a,b,c,d))
250  {
251  assertSize();
252  _name = n;
253  }
254 
255  iterator begin()
256  {
257  return iterator(_vectors, 0, _name);
258  }
259 
260  iterator end()
261  {
262  assertSize();
263  return iterator(_vectors, get<0>().size(), _name);
264  }
265 
266  void erase(iterator front, iterator back)
267  {
268  assertSize();
269 
270  MultiVectorEraser erase(front.pos(), back.pos());
271  ForEachValue<T> forEach(_vectors);
272  forEach.apply(erase);
273  }
274  size_t size() const {
275  assertSize();
276  return get<0>().size();
277  }
278 
279  template <size_t N>
280  typename tuple_element<N,T>::type &
281  get() {
282  return Dune::get<N>(_vectors);
283  }
284  template <size_t N>
285  typename tuple_element<N,T>::type
286  get() const {
287  return Dune::get<N>(_vectors);
288  }
289  private:
290  void assertSize() const {
291  MultiVectorAssertSize check(get<0>().size());
292  ForEachValue<const T> forEach(_vectors);
293  forEach.apply(check);
294  }
295  };
296 
297 } // end namespace Dune
298 
299 #endif // DUNE_GRIDGLUE_COMMON_MULTIVECTOR_HH
Definition: multivector.hh:207
MultiDataProxy(T &v, size_t pos, std::string _n)
Definition: multivector.hh:81
MultiDataProxy< T > & dereference() const
Definition: multivector.hh:197
bool operator>(const MultiDataProxy &other) const
Definition: multivector.hh:91
MultiVectorIterator< T > iterator
Definition: multivector.hh:244
std::string name
Definition: multivector.hh:80
MultiDataProxy(MutableProxy &other)
Definition: multivector.hh:85
T & _vectors
Definition: multivector.hh:77
iterator end()
Definition: multivector.hh:260
bool operator<(const MultiDataProxy &other) const
Definition: multivector.hh:90
Definition: gridglue.hh:34
size_t size() const
Definition: multivector.hh:274
bool operator==(const MultiDataProxy &other) const
Definition: multivector.hh:88
bool equals(const MultiVectorIterator &other) const
Definition: multivector.hh:172
MultiDataProxy< const typename remove_const< T >::type > ConstProxy
Definition: multivector.hh:76
reference * pointer
Pointer to a small block of bits.
Definition: multivector.hh:232
Definition: multivector.hh:70
const_reference * const_pointer
Const pointer to a small block of bits.
Definition: multivector.hh:235
void increment()
Definition: multivector.hh:182
size_t pos() const
Definition: multivector.hh:158
size_t size_type
size type
Definition: multivector.hh:238
int pos
Definition: multivector.hh:79
MultiVector(A &a, B &b, C &c, D &d, std::string n="?")
Definition: multivector.hh:248
value_type reference
Reference to a small block of bits.
Definition: multivector.hh:226
MultiDataProxy< typename remove_const< T >::type > MutableProxy
Definition: multivector.hh:75
void decrement()
Definition: multivector.hh:189
Definition: multivector.hh:138
MultiVectorIterator(const MultiVectorIterator &other)
Definition: multivector.hh:150
MultiDataProxy< T > value_type
Type of the values stored by the container.
Definition: multivector.hh:220
MultiVectorIterator(T &v, size_t n, std::string i)
Definition: multivector.hh:148
MultiVectorIterator< const T > const_iterator
Definition: multivector.hh:245
MultiVectorIterator operator=(const MultiVectorIterator &other)
Definition: multivector.hh:160
void erase(iterator front, iterator back)
Definition: multivector.hh:266
iterator begin()
Definition: multivector.hh:255
MultiDataProxy(ConstProxy &other)
Definition: multivector.hh:83
MultiDataProxy< T > const_value_type
Type of the const values stored by the container.
Definition: multivector.hh:223
MultiDataProxy< T > data
Definition: multivector.hh:145
const_value_type const_reference
Const reference to a small block of bits.
Definition: multivector.hh:229
bool operator!=(const MultiDataProxy &other) const
Definition: multivector.hh:89
TypeTraits< typename tuple_element< N, T >::type >::ReferredType::reference get()
Definition: multivector.hh:105
MultiDataProxy & operator=(const ConstProxy &other)
Definition: multivector.hh:93