dune-geometry  2.3.1
generalvertexorder.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 
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"
18 
19 namespace Dune {
20 
38  template<class InIterator, class OutIterator>
39  void reduceOrder(const InIterator& inBegin, const InIterator& inEnd,
40  OutIterator outIt)
41  {
42  typedef std::less<
43  typename std::iterator_traits<InIterator>::value_type
44  > less_t;
45  const less_t less = less_t();
46 
47  for(InIterator inIt = inBegin; inIt != inEnd; ++inIt, ++outIt)
48  *outIt = std::count_if(inBegin, inEnd, std::bind2nd(less, *inIt));
49  }
50 
52 
67  template<std::size_t dim, class Index_ = std::size_t>
71 
72  const RefElem& refelem;
73  GeometryType gt;
74  std::vector<Index_> vertexOrder;
75 
76  public:
78  typedef Index_ Index;
79 
81  class iterator;
82 
84  static const std::size_t dimension = dim;
86  const GeometryType &type() const { return gt; }
87 
89 
97  template<class InIterator>
98  GeneralVertexOrder(const GeometryType& gt_, const InIterator &inBegin,
99  const InIterator &inEnd) :
100  refelem(RefElems::general(gt_)), gt(gt_),
101  vertexOrder(refelem.size(dim))
102  { reduceOrder(inBegin, inEnd, vertexOrder.begin()); }
103 
105 
109  iterator begin(std::size_t codim, std::size_t subEntity) const
110  { return iterator(*this, codim, subEntity); }
112 
116  iterator end(std::size_t codim, std::size_t subEntity) const {
117  return iterator(*this, codim, subEntity,
118  refelem.size(subEntity, codim, dim));
119  }
120 
122 
129  void getReduced(std::size_t codim, std::size_t subEntity,
130  std::vector<Index>& order) const
131  {
132  order.resize(refelem.size(subEntity, codim, dim));
133  reduceOrder(begin(codim, subEntity), end(codim, subEntity),
134  order.begin());
135  }
136  };
137 
139 
142  template<std::size_t dim, class Index_>
143  class GeneralVertexOrder<dim, Index_>::iterator :
144  public Dune::RandomAccessIteratorFacade<iterator, const Index_>
145  {
146  const GeneralVertexOrder *order;
147  std::size_t codim;
148  std::size_t subEntity;
149  std::size_t vertex;
150 
151  iterator(const GeneralVertexOrder &order_, std::size_t codim_,
152  std::size_t subEntity_, std::size_t vertex_ = 0) :
153  order(&order_), codim(codim_), subEntity(subEntity_), vertex(vertex_)
154  { }
155 
156  public:
157  const Index &dereference() const {
158  return order->vertexOrder[order->refelem.subEntity(subEntity, codim,
159  vertex, dim)];
160  }
161  const Index &elementAt(std::ptrdiff_t n) const {
162  return order->vertexOrder[order->refelem.subEntity(subEntity, codim,
163  vertex+n, dim)];
164  }
165  bool equals(const iterator &other) const {
166  return order == other.order && codim == other.codim &&
167  subEntity == other.subEntity && vertex == other.vertex;
168  }
169  void increment() { ++vertex; }
170  void decrement() { --vertex; }
171  void advance(std::ptrdiff_t n) { vertex += n; }
172  std::ptrdiff_t distanceTo(const iterator &other) const {
173  // make sure we reference the same container
174  assert(order == other.order && codim == other.codim &&
175  subEntity == other.subEntity);
176  if(vertex < other.vertex) return other.vertex - vertex;
177  else return -static_cast<std::ptrdiff_t>(vertex - other.vertex);
178  }
179 
180  friend class GeneralVertexOrder<dim, Index>;
181 
183 
188  iterator() { }
189  };
190 } // namespace Dune
191 
192 #endif // DUNE_GEOMETRY_GENERALVERTEXORDER_HH