dune-geometry  2.2.0
generalvertexorder.hh
Go to the documentation of this file.
1 // -*- tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 // vi: set et ts=8 sw=2 sts=2:
3 
4 #ifndef DUNE_GEOMETRY_GENERALVERTEXORDER_HH
5 #define DUNE_GEOMETRY_GENERALVERTEXORDER_HH
6 
7 #include <algorithm>
8 #include <cassert>
9 #include <cstddef>
10 #include <functional>
11 #include <iterator>
12 #include <vector>
13 
14 #include <dune/common/iteratorfacades.hh>
15 
16 #include "type.hh"
17 #include "referenceelements.hh"
18 
19 namespace Dune {
20 
22 
37  template<class InIterator, class OutIterator>
38  void reduceOrder(const InIterator& inBegin, const InIterator& inEnd,
39  OutIterator outIt)
40  {
41  typedef std::less<
42  typename std::iterator_traits<InIterator>::value_type
43  > less_t;
44  static const less_t less = less_t();
45 
46  for(InIterator inIt = inBegin; inIt != inEnd; ++inIt, ++outIt)
47  *outIt = std::count_if(inBegin, inEnd, std::bind2nd(less, *inIt));
48  }
49 
51 
66  template<std::size_t dim, class Index_ = std::size_t>
70 
71  const RefElem& refelem;
72  GeometryType gt;
73  std::vector<Index_> vertexOrder;
74 
75  public:
77  typedef Index_ Index;
78 
80  class iterator;
81 
83  static const std::size_t dimension = dim;
85  const GeometryType &type() const { return gt; }
86 
88 
96  template<class InIterator>
97  GeneralVertexOrder(const GeometryType& gt_, const InIterator &inBegin,
98  const InIterator &inEnd) :
99  refelem(RefElems::general(gt_)), gt(gt_),
100  vertexOrder(refelem.size(dim))
101  { reduceOrder(inBegin, inEnd, vertexOrder.begin()); }
102 
104 
108  iterator begin(std::size_t codim, std::size_t subEntity) const
109  { return iterator(*this, codim, subEntity); }
111 
115  iterator end(std::size_t codim, std::size_t subEntity) const {
116  return iterator(*this, codim, subEntity,
117  refelem.size(subEntity, codim, dim));
118  }
119 
121 
128  void getReduced(std::size_t codim, std::size_t subEntity,
129  std::vector<Index>& order) const
130  {
131  order.resize(refelem.size(subEntity, codim, dim));
132  reduceOrder(begin(codim, subEntity), end(codim, subEntity),
133  order.begin());
134  }
135  };
136 
138 
141  template<std::size_t dim, class Index_>
142  class GeneralVertexOrder<dim, Index_>::iterator :
143  public Dune::RandomAccessIteratorFacade<iterator, const Index_>
144  {
145  const GeneralVertexOrder *order;
146  std::size_t codim;
147  std::size_t subEntity;
148  std::size_t vertex;
149 
150  iterator(const GeneralVertexOrder &order_, std::size_t codim_,
151  std::size_t subEntity_, std::size_t vertex_ = 0) :
152  order(&order_), codim(codim_), subEntity(subEntity_), vertex(vertex_)
153  { }
154 
155  public:
156  const Index &dereference() const {
157  return order->vertexOrder[order->refelem.subEntity(subEntity, codim,
158  vertex, dim)];
159  }
160  const Index &elementAt(std::ptrdiff_t n) const {
161  return order->vertexOrder[order->refelem.subEntity(subEntity, codim,
162  vertex+n, dim)];
163  }
164  bool equals(const iterator &other) const {
165  return order == other.order && codim == other.codim &&
166  subEntity == other.subEntity && vertex == other.vertex;
167  }
168  void increment() { ++vertex; }
169  void decrement() { --vertex; }
170  void advance(std::ptrdiff_t n) { vertex += n; }
171  std::ptrdiff_t distanceTo(const iterator &other) const {
172  // make sure we reference the same container
173  assert(order == other.order && codim == other.codim &&
174  subEntity == other.subEntity);
175  if(vertex < other.vertex) return other.vertex - vertex;
176  else return -static_cast<std::ptrdiff_t>(vertex - other.vertex);
177  }
178 
179  friend class GeneralVertexOrder<dim, Index>;
180 
182 
187  iterator() { }
188  };
189 } // namespace Dune
190 
191 #endif // DUNE_GEOMETRY_GENERALVERTEXORDER_HH