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
00170
00171
00175 template<typename TS, typename TG, typename TL, int N>
00176 class Selection
00177 {
00178 public:
00187 typedef TS AttributeSet;
00188
00192 typedef TG GlobalIndex;
00193
00200 typedef TL LocalIndex;
00201
00205 typedef Dune::ParallelIndexSet<GlobalIndex,LocalIndex,N> ParallelIndexSet;
00206
00210 typedef uint32_t* iterator;
00211
00215 typedef uint32_t* const_iterator;
00216
00217 Selection()
00218 : selected_()
00219 {}
00220
00221 Selection(const ParallelIndexSet& indexset)
00222 : selected_(), size_(0), built_(false)
00223 {
00224 setIndexSet(indexset);
00225 }
00226
00227 ~Selection();
00228
00233 void setIndexSet(const ParallelIndexSet& indexset);
00234
00238 void free();
00239
00243
00244
00249 const_iterator begin() const;
00250
00255 const_iterator end() const;
00256
00257
00258 private:
00259 uint32_t* selected_;
00260 size_t size_;
00261 bool built_;
00262
00263 };
00264
00265 template<typename TS, typename TG, typename TL, int N>
00266 inline void Selection<TS,TG,TL,N>::setIndexSet(const ParallelIndexSet& indexset)
00267 {
00268 if(built_)
00269 free();
00270
00271
00272 typedef typename ParallelIndexSet::const_iterator const_iterator;
00273 const const_iterator end = indexset.end();
00274 int entries = 0;
00275
00276 for(const_iterator index = indexset.begin(); index != end; ++index)
00277 if(AttributeSet::contains(index->local().attribute()))
00278 ++entries;
00279
00280 selected_ = new uint32_t[entries];
00281 built_ = true;
00282
00283 entries = 0;
00284 for(const_iterator index = indexset.begin(); index != end; ++index)
00285 if(AttributeSet::contains(index->local().attribute()))
00286 selected_[entries++]= index->local().local();
00287
00288 size_=entries;
00289 built_=true;
00290 }
00291
00292 template<typename TS, typename TG, typename TL, int N>
00293 uint32_t* Selection<TS,TG,TL,N>::begin() const
00294 {
00295 return selected_;
00296 }
00297
00298 template<typename TS, typename TG, typename TL, int N>
00299 uint32_t* Selection<TS,TG,TL,N>::end() const
00300 {
00301 return selected_+size_;
00302 }
00303
00304 template<typename TS, typename TG, typename TL, int N>
00305 inline void Selection<TS,TG,TL,N>::free()
00306 {
00307 delete[] selected_;
00308 size_=0;
00309 built_=false;
00310 }
00311
00312 template<typename TS, typename TG, typename TL, int N>
00313 inline Selection<TS,TG,TL,N>::~Selection()
00314 {
00315 if(built_)
00316 free();
00317 }
00318
00319 template<typename TS, typename TG, typename TL, int N>
00320 SelectionIterator<TS,TG,TL,N> UncachedSelection<TS,TG,TL,N>::begin() const
00321 {
00322 return SelectionIterator<TS,TG,TL,N>(indexSet_->begin(),
00323 indexSet_->end());
00324 }
00325
00326 template<typename TS, typename TG, typename TL, int N>
00327 SelectionIterator<TS,TG,TL,N> UncachedSelection<TS,TG,TL,N>::end() const
00328 {
00329 return SelectionIterator<TS,TG,TL,N>(indexSet_->end(),
00330 indexSet_->end());
00331 }
00332 template<typename TS, typename TG, typename TL, int N>
00333 void UncachedSelection<TS,TG,TL,N>::setIndexSet(const ParallelIndexSet& indexset)
00334 {
00335 indexSet_ = &indexset;
00336 }
00337
00341 }
00342 #endif