dune-common  2.3.0
selection.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 // $Id$
4 #ifndef DUNE_SELECTION_HH
5 #define DUNE_SELECTION_HH
6 
7 #include "indexset.hh"
9 
10 namespace Dune
11 {
26  template<typename TS, typename TG, typename TL, int N>
28  {
29  public:
38  typedef TS AttributeSet;
39 
44 
45  //typedef typename ParallelIndexSet::const_iterator ParallelIndexSetIterator;
46 
47  typedef ConstArrayListIterator<IndexPair<TG,TL>, N, std::allocator<Dune::IndexPair<TG,TL> > > ParallelIndexSetIterator;
54  : iter_(iter), end_(end)
55  {
56  // Step to the first valid entry
57  while(iter_!=end_ && !AttributeSet::contains(iter_->local().attribute()))
58  ++iter_;
59  }
60 
61  void operator++()
62  {
63  assert(iter_!=end_);
64  for(++iter_; iter_!=end_; ++iter_)
65  if(AttributeSet::contains(iter_->local().attribute()))
66  break;
67  }
68 
69 
70  uint32_t operator*() const
71  {
72  return iter_->local().local();
73  }
74 
75  bool operator==(const SelectionIterator<TS,TG,TL,N>& other) const
76  {
77  return iter_ == other.iter_;
78  }
79 
80  bool operator!=(const SelectionIterator<TS,TG,TL,N>& other) const
81  {
82  return iter_ != other.iter_;
83  }
84 
85  private:
87  const ParallelIndexSetIterator end_;
88  };
89 
90 
94  template<typename TS, typename TG, typename TL, int N>
96  {
97  public:
106  typedef TS AttributeSet;
107 
111  typedef TG GlobalIndex;
112 
119  typedef TL LocalIndex;
120 
125 
130 
135 
137  : indexSet_()
138  {}
139 
141  : indexSet_(&indexset)
142  {}
147  void setIndexSet(const ParallelIndexSet& indexset);
148 
152  //const ParallelIndexSet& indexSet() const;
153 
158  const_iterator begin() const;
159 
164  const_iterator end() const;
165 
166 
167  private:
168  const ParallelIndexSet* indexSet_;
169 
170  };
171 
175  template<typename TS, typename TG, typename TL, int N>
176  class Selection
177  {
178  public:
187  typedef TS AttributeSet;
188 
192  typedef TG GlobalIndex;
193 
200  typedef TL LocalIndex;
201 
206 
210  typedef uint32_t* iterator;
211 
215  typedef uint32_t* const_iterator;
216 
218  : selected_()
219  {}
220 
221  Selection(const ParallelIndexSet& indexset)
222  : selected_(), size_(0), built_(false)
223  {
224  setIndexSet(indexset);
225  }
226 
227  ~Selection();
228 
233  void setIndexSet(const ParallelIndexSet& indexset);
234 
238  void free();
239 
243  //IndexSet indexSet() const;
244 
249  const_iterator begin() const;
250 
255  const_iterator end() const;
256 
257 
258  private:
259  uint32_t* selected_;
260  size_t size_;
261  bool built_;
262 
263  };
264 
265  template<typename TS, typename TG, typename TL, int N>
267  {
268  if(built_)
269  free();
270 
271  // Count the number of entries the selection has to hold
273  const const_iterator end = indexset.end();
274  int entries = 0;
275 
276  for(const_iterator index = indexset.begin(); index != end; ++index)
277  if(AttributeSet::contains(index->local().attribute()))
278  ++entries;
279 
280  selected_ = new uint32_t[entries];
281  built_ = true;
282 
283  entries = 0;
284  for(const_iterator index = indexset.begin(); index != end; ++index)
285  if(AttributeSet::contains(index->local().attribute()))
286  selected_[entries++]= index->local().local();
287 
288  size_=entries;
289  built_=true;
290  }
291 
292  template<typename TS, typename TG, typename TL, int N>
294  {
295  return selected_;
296  }
297 
298  template<typename TS, typename TG, typename TL, int N>
299  uint32_t* Selection<TS,TG,TL,N>::end() const
300  {
301  return selected_+size_;
302  }
303 
304  template<typename TS, typename TG, typename TL, int N>
306  {
307  delete[] selected_;
308  size_=0;
309  built_=false;
310  }
311 
312  template<typename TS, typename TG, typename TL, int N>
314  {
315  if(built_)
316  free();
317  }
318 
319  template<typename TS, typename TG, typename TL, int N>
321  {
322  return SelectionIterator<TS,TG,TL,N>(indexSet_->begin(),
323  indexSet_->end());
324  }
325 
326  template<typename TS, typename TG, typename TL, int N>
328  {
329  return SelectionIterator<TS,TG,TL,N>(indexSet_->end(),
330  indexSet_->end());
331  }
332  template<typename TS, typename TG, typename TL, int N>
334  {
335  indexSet_ = &indexset;
336  }
337 
341 }
342 #endif