00001
00002 #ifndef DUNE_SELECTION_HH
00003 #define DUNE_SELECTION_HH
00004
00005 #include"indexset.hh"
00006 #include<dune/common/iteratorfacades.hh>
00007
00008 namespace Dune
00009 {
00024 template<typename TS, typename TG, typename TL, int N>
00025 class SelectionIterator
00026 {
00027 public:
00036 typedef TS AttributeSet;
00037
00041 typedef Dune::ParallelIndexSet<TG,TL,N> ParallelIndexSet;
00042
00043
00044
00045 typedef ConstArrayListIterator<IndexPair<TG,TL>, N, std::allocator<Dune::IndexPair<TG,TL> > > ParallelIndexSetIterator;
00051 SelectionIterator(const ParallelIndexSetIterator& iter, const ParallelIndexSetIterator& end)
00052 : iter_(iter), end_(end)
00053 {
00054
00055 while(iter_!=end_ && !AttributeSet::contains(iter_->local().attribute()))
00056 ++iter_;
00057 }
00058
00059 void operator++()
00060 {
00061 assert(iter_!=end_);
00062 for(++iter_;iter_!=end_; ++iter_)
00063 if(AttributeSet::contains(iter_->local().attribute()))
00064 break;
00065 }
00066
00067
00068 uint32_t operator*() const
00069 {
00070 return iter_->local().local();
00071 }
00072
00073 bool operator==(const SelectionIterator<TS,TG,TL,N>& other) const
00074 {
00075 return iter_ == other.iter_;
00076 }
00077
00078 bool operator!=(const SelectionIterator<TS,TG,TL,N>& other) const
00079 {
00080 return iter_ != other.iter_;
00081 }
00082
00083 private:
00084 ParallelIndexSetIterator iter_;
00085 const ParallelIndexSetIterator end_;
00086 };
00087
00088
00092 template<typename TS, typename TG, typename TL, int N>
00093 class UncachedSelection
00094 {
00095 public:
00104 typedef TS AttributeSet;
00105
00109 typedef TG GlobalIndex;
00110
00117 typedef TL LocalIndex;
00118
00122 typedef Dune::ParallelIndexSet<GlobalIndex,LocalIndex,N> ParallelIndexSet;
00123
00127 typedef SelectionIterator<TS,TG,TL,N> iterator;
00128
00132 typedef iterator const_iterator;
00133
00134 UncachedSelection()
00135 : indexSet_()
00136 {}
00137
00138 UncachedSelection(const ParallelIndexSet& indexset)
00139 : indexSet_(&indexset)
00140 {}
00145 void setIndexSet(const ParallelIndexSet& indexset);
00146
00150
00151
00156 const_iterator begin() const;
00157
00162 const_iterator end() const;
00163
00164
00165 private:
00166 const ParallelIndexSet* indexSet_;
00167
00168 };
00169
00173 template<typename TS, typename TG, typename TL, int N>
00174 class Selection
00175 {
00176 public:
00185 typedef TS AttributeSet;
00186
00190 typedef TG GlobalIndex;
00191
00198 typedef TL LocalIndex;
00199
00203 typedef Dune::ParallelIndexSet<GlobalIndex,LocalIndex,N> ParallelIndexSet;
00204
00208 typedef uint32_t* iterator;
00209
00213 typedef uint32_t* const_iterator;
00214
00215 Selection()
00216 : selected_()
00217 {}
00218
00219 Selection(const ParallelIndexSet& indexset)
00220 : selected_(), size_(0), built_(false)
00221 {
00222 setIndexSet(indexset);
00223 }
00224
00225 ~Selection();
00226
00231 void setIndexSet(const ParallelIndexSet& indexset);
00232
00236 void free();
00237
00241
00242
00247 const_iterator begin() const;
00248
00253 const_iterator end() const;
00254
00255
00256 private:
00257 uint32_t* selected_;
00258 size_t size_;
00259 bool built_;
00260
00261 };
00262
00263 template<typename TS, typename TG, typename TL, int N>
00264 inline void Selection<TS,TG,TL,N>::setIndexSet(const ParallelIndexSet& indexset)
00265 {
00266 if(built_)
00267 free();
00268
00269
00270 typedef typename ParallelIndexSet::const_iterator const_iterator;
00271 const const_iterator end = indexset.end();
00272 int entries = 0;
00273
00274 for(const_iterator index = indexset.begin(); index != end; ++index)
00275 if(AttributeSet::contains(index->local().attribute()))
00276 ++entries;
00277
00278 selected_ = new uint32_t[entries];
00279 built_ = true;
00280
00281 entries = 0;
00282 for(const_iterator index = indexset.begin(); index != end; ++index)
00283 if(AttributeSet::contains(index->local().attribute()))
00284 selected_[entries++]= index->local().local();
00285
00286 size_=entries;
00287 built_=true;
00288 }
00289
00290 template<typename TS, typename TG, typename TL, int N>
00291 uint32_t* Selection<TS,TG,TL,N>::begin() const
00292 {
00293 return selected_;
00294 }
00295
00296 template<typename TS, typename TG, typename TL, int N>
00297 uint32_t* Selection<TS,TG,TL,N>::end() const
00298 {
00299 return selected_+size_;
00300 }
00301
00302 template<typename TS, typename TG, typename TL, int N>
00303 inline void Selection<TS,TG,TL,N>::free()
00304 {
00305 delete[] selected_;
00306 size_=0;
00307 built_=false;
00308 }
00309
00310 template<typename TS, typename TG, typename TL, int N>
00311 inline Selection<TS,TG,TL,N>::~Selection()
00312 {
00313 if(built_)
00314 free();
00315 }
00316
00317 template<typename TS, typename TG, typename TL, int N>
00318 SelectionIterator<TS,TG,TL,N> UncachedSelection<TS,TG,TL,N>::begin() const
00319 {
00320 return SelectionIterator<TS,TG,TL,N>(indexSet_->begin(),
00321 indexSet_->end());
00322 }
00323
00324 template<typename TS, typename TG, typename TL, int N>
00325 SelectionIterator<TS,TG,TL,N> UncachedSelection<TS,TG,TL,N>::end() const
00326 {
00327 return SelectionIterator<TS,TG,TL,N>(indexSet_->end(),
00328 indexSet_->end());
00329 }
00330 template<typename TS, typename TG, typename TL, int N>
00331 void UncachedSelection<TS,TG,TL,N>::setIndexSet(const ParallelIndexSet& indexset)
00332 {
00333 indexSet_ = &indexset;
00334 }
00335
00339 }
00340 #endif