dune-common  2.2.1
selection.hh
Go to the documentation of this file.
1 // $Id$
2 #ifndef DUNE_SELECTION_HH
3 #define DUNE_SELECTION_HH
4 
5 #include"indexset.hh"
7 
8 namespace Dune
9 {
24  template<typename TS, typename TG, typename TL, int N>
26  {
27  public:
36  typedef TS AttributeSet;
37 
42 
43  //typedef typename ParallelIndexSet::const_iterator ParallelIndexSetIterator;
44 
45  typedef ConstArrayListIterator<IndexPair<TG,TL>, N, std::allocator<Dune::IndexPair<TG,TL> > > ParallelIndexSetIterator;
52  : iter_(iter), end_(end)
53  {
54  // Step to the first valid entry
55  while(iter_!=end_ && !AttributeSet::contains(iter_->local().attribute()))
56  ++iter_;
57  }
58 
59  void operator++()
60  {
61  assert(iter_!=end_);
62  for(++iter_;iter_!=end_; ++iter_)
63  if(AttributeSet::contains(iter_->local().attribute()))
64  break;
65  }
66 
67 
68  uint32_t operator*() const
69  {
70  return iter_->local().local();
71  }
72 
73  bool operator==(const SelectionIterator<TS,TG,TL,N>& other) const
74  {
75  return iter_ == other.iter_;
76  }
77 
78  bool operator!=(const SelectionIterator<TS,TG,TL,N>& other) const
79  {
80  return iter_ != other.iter_;
81  }
82 
83  private:
85  const ParallelIndexSetIterator end_;
86  };
87 
88 
92  template<typename TS, typename TG, typename TL, int N>
94  {
95  public:
104  typedef TS AttributeSet;
105 
109  typedef TG GlobalIndex;
110 
117  typedef TL LocalIndex;
118 
123 
128 
133 
135  : indexSet_()
136  {}
137 
139  : indexSet_(&indexset)
140  {}
145  void setIndexSet(const ParallelIndexSet& indexset);
146 
150  //const ParallelIndexSet& indexSet() const;
151 
156  const_iterator begin() const;
157 
162  const_iterator end() const;
163 
164 
165  private:
166  const ParallelIndexSet* indexSet_;
167 
168  };
169 
173  template<typename TS, typename TG, typename TL, int N>
174  class Selection
175  {
176  public:
185  typedef TS AttributeSet;
186 
190  typedef TG GlobalIndex;
191 
198  typedef TL LocalIndex;
199 
204 
208  typedef uint32_t* iterator;
209 
213  typedef uint32_t* const_iterator;
214 
216  : selected_()
217  {}
218 
219  Selection(const ParallelIndexSet& indexset)
220  : selected_(), size_(0), built_(false)
221  {
222  setIndexSet(indexset);
223  }
224 
225  ~Selection();
226 
231  void setIndexSet(const ParallelIndexSet& indexset);
232 
236  void free();
237 
241  //IndexSet indexSet() const;
242 
247  const_iterator begin() const;
248 
253  const_iterator end() const;
254 
255 
256  private:
257  uint32_t* selected_;
258  size_t size_;
259  bool built_;
260 
261  };
262 
263  template<typename TS, typename TG, typename TL, int N>
265  {
266  if(built_)
267  free();
268 
269  // Count the number of entries the selection has to hold
271  const const_iterator end = indexset.end();
272  int entries = 0;
273 
274  for(const_iterator index = indexset.begin(); index != end; ++index)
275  if(AttributeSet::contains(index->local().attribute()))
276  ++entries;
277 
278  selected_ = new uint32_t[entries];
279  built_ = true;
280 
281  entries = 0;
282  for(const_iterator index = indexset.begin(); index != end; ++index)
283  if(AttributeSet::contains(index->local().attribute()))
284  selected_[entries++]= index->local().local();
285 
286  size_=entries;
287  built_=true;
288  }
289 
290  template<typename TS, typename TG, typename TL, int N>
292  {
293  return selected_;
294  }
295 
296  template<typename TS, typename TG, typename TL, int N>
297  uint32_t* Selection<TS,TG,TL,N>::end() const
298  {
299  return selected_+size_;
300  }
301 
302  template<typename TS, typename TG, typename TL, int N>
304  {
305  delete[] selected_;
306  size_=0;
307  built_=false;
308  }
309 
310  template<typename TS, typename TG, typename TL, int N>
312  {
313  if(built_)
314  free();
315  }
316 
317  template<typename TS, typename TG, typename TL, int N>
319  {
320  return SelectionIterator<TS,TG,TL,N>(indexSet_->begin(),
321  indexSet_->end());
322  }
323 
324  template<typename TS, typename TG, typename TL, int N>
326  {
327  return SelectionIterator<TS,TG,TL,N>(indexSet_->end(),
328  indexSet_->end());
329  }
330  template<typename TS, typename TG, typename TL, int N>
332  {
333  indexSet_ = &indexset;
334  }
335 
339 }
340 #endif