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