2 #ifndef DUNE_INDEXSET_HH
3 #define DUNE_INDEXSET_HH
27 template<
class TG,
class TL>
35 template<
class TG,
class TL>
36 std::ostream& operator<<(std::ostream& os, const IndexPair<TG,TL>& pair);
38 template<
class TG,
class TL>
39 bool operator==(
const IndexPair<TG,TL>&,
const IndexPair<TG,TL>&);
41 template<
class TG,
class TL>
42 bool operator!=(
const IndexPair<TG,TL>&,
const IndexPair<TG,TL>&);
44 template<
class TG,
class TL>
45 bool operator<(const IndexPair<TG,TL>&,
const IndexPair<TG,TL>&);
47 template<
class TG,
class TL>
48 bool operator>(
const IndexPair<TG,TL>&,
const IndexPair<TG,TL>&);
50 template<
class TG,
class TL>
51 bool operator<=(const IndexPair<TG,TL>&,
const IndexPair<TG,TL>&);
53 template<
class TG,
class TL>
54 bool operator >=(
const IndexPair<TG,TL>&,
const IndexPair<TG,TL>&);
56 template<
class TG,
class TL>
57 bool operator==(
const IndexPair<TG,TL>&,
const TG&);
59 template<
class TG,
class TL>
60 bool operator!=(
const IndexPair<TG,TL>&,
const TG&);
62 template<
class TG,
class TL>
63 bool operator<(const IndexPair<TG,TL>&,
const TG&);
65 template<
class TG,
class TL>
66 bool operator>(
const IndexPair<TG,TL>&,
const TG&);
68 template<
class TG,
class TL>
69 bool operator<=(const IndexPair<TG,TL>&,
const TG&);
71 template<
class TG,
class TL>
72 bool operator >=(
const IndexPair<TG,TL>&,
const TG&);
80 template<
class TG,
class TL>
213 template<
typename TG,
typename TL,
int N=100>
262 : Father(father), indexSet_(&indexSet)
266 : Father(other), indexSet_(other.indexSet_)
272 indexSet_ = other.indexSet_;
286 if(indexSet_->state_ !=
RESIZE)
287 DUNE_THROW(InvalidIndexSetState,
"Indices can only be removed "
288 <<
"while in RESIZE state!");
331 inline
void add(const
GlobalIndex& global) throw(InvalidIndexSetState);
342 throw(InvalidIndexSetState);
352 throw(InvalidIndexSetState);
366 void endResize() throw(InvalidIndexSetState);
459 inline
int seqNo() const;
465 inline
size_t size() const;
477 bool deletedEntries_;
491 template<class TG, class TL,
int N>
492 std::ostream& operator<<(std::ostream& os, const
ParallelIndexSet<TG,TL,N>& indexSet);
531 GlobalLookupIndexSet(
const ParallelIndexSet& indexset, std::size_t
size);
538 GlobalLookupIndexSet(
const ParallelIndexSet& indexset);
543 ~GlobalLookupIndexSet();
554 inline const IndexPair&
560 inline const IndexPair*
561 pair(
const std::size_t& local)
const;
567 inline const_iterator
begin()
const;
573 inline const_iterator
end()
const;
581 inline int seqNo()
const;
587 inline size_t size()
const;
592 const ParallelIndexSet& indexSet_;
602 std::vector<const IndexPair*> indices_;
610 static bool compare(
const T& t1,
const T& t2){
615 template<
class TG,
class TL>
628 template<
class TG,
class TL>
629 inline std::ostream& operator<<(std::ostream& os, const IndexPair<TG,TL>& pair)
631 os<<
"{global="<<pair.global_<<
", local="<<pair.local_<<
"}";
635 template<
class TG,
class TL,
int N>
636 inline std::ostream& operator<<(std::ostream& os, const ParallelIndexSet<TG,TL,N>& indexSet)
639 Iterator
end = indexSet.end();
641 for(Iterator index = indexSet.begin(); index !=
end; ++index)
648 template<
class TG,
class TL>
651 return a.global_==b.global_;
654 template<
class TG,
class TL>
657 return a.global_!=b.global_;
660 template<
class TG,
class TL>
663 return a.global_<b.global_;
666 template<
class TG,
class TL>
669 return a.global_>b.global_;
672 template<
class TG,
class TL>
675 return a.global_<=b.global_;
678 template<
class TG,
class TL>
681 return a.global_>=b.global_;
684 template<
class TG,
class TL>
690 template<
class TG,
class TL>
696 template<
class TG,
class TL>
697 inline bool operator<(const IndexPair<TG,TL>& a,
const TG& b)
702 template<
class TG,
class TL>
708 template<
class TG,
class TL>
709 inline bool operator<=(const IndexPair<TG,TL>& a,
const TG& b)
714 template<
class TG,
class TL>
722 template<
class TG,
class TL>
724 : global_(global), local_(local){}
726 template<
class TG,
class TL>
727 IndexPair<TG,TL>::IndexPair(
const TG& global)
728 : global_(global), local_(){}
730 template<
class TG,
class TL>
731 IndexPair<TG,TL>::IndexPair()
732 : global_(), local_(){}
734 template<
class TG,
class TL>
735 inline const TG& IndexPair<TG,TL>::global()
const{
739 template<
class TG,
class TL>
740 inline TL& IndexPair<TG,TL>::local() {
744 template<
class TG,
class TL>
745 inline const TL& IndexPair<TG,TL>::local()
const{
749 template<
class TG,
class TL>
750 inline void IndexPair<TG,TL>::setLocal(
int local){
754 template<
class TG,
class TL,
int N>
755 ParallelIndexSet<TG,TL,N>::ParallelIndexSet()
756 : state_(
GROUND), seqNo_(0)
759 template<
class TG,
class TL,
int N>
760 void ParallelIndexSet<TG,TL,N>::beginResize() throw(InvalidIndexSetState)
767 "IndexSet has to be in GROUND state, when "
768 <<
"beginResize() is called!");
772 deletedEntries_ =
false;
775 template<
class TG,
class TL,
int N>
776 inline void ParallelIndexSet<TG,TL,N>::add(
const GlobalIndex& global)
777 throw(InvalidIndexSetState)
782 DUNE_THROW(InvalidIndexSetState,
"Indices can only be added "
783 <<
"while in RESIZE state!");
785 newIndices_.push_back(IndexPair(global));
788 template<
class TG,
class TL,
int N>
789 inline void ParallelIndexSet<TG,TL,N>::add(
const TG& global,
const TL& local)
790 throw(InvalidIndexSetState)
795 DUNE_THROW(InvalidIndexSetState,
"Indices can only be added "
796 <<
"while in RESIZE state!");
798 newIndices_.push_back(IndexPair(global,local));
801 template<
class TG,
class TL,
int N>
802 inline void ParallelIndexSet<TG,TL,N>::markAsDeleted(
const iterator& global)
803 throw(InvalidIndexSetState){
807 DUNE_THROW(InvalidIndexSetState,
"Indices can only be removed "
808 <<
"while in RESIZE state!");
810 deletedEntries_ =
true;
812 global.markAsDeleted();
815 template<
class TG,
class TL,
int N>
816 void ParallelIndexSet<TG,TL,N>::endResize() throw(InvalidIndexSetState){
820 DUNE_THROW(InvalidIndexSetState,
"endResize called while not "
821 <<
"in RESIZE state!");
824 std::sort(newIndices_.begin(), newIndices_.end(), IndexSetSortFunctor<TG,TL>());
831 template<
class TG,
class TL,
int N>
832 inline void ParallelIndexSet<TG,TL,N>::merge(){
833 if(localIndices_.size()==0)
835 localIndices_=newIndices_;
838 else if(newIndices_.size()>0 || deletedEntries_)
840 ArrayList<IndexPair,N> tempPairs;
841 typedef typename ArrayList<IndexPair,N>::iterator
iterator;
842 typedef typename ArrayList<IndexPair,N>::const_iterator const_iterator;
844 iterator old=localIndices_.begin();
845 iterator added=newIndices_.begin();
846 const const_iterator endold=localIndices_.end();
847 const const_iterator endadded=newIndices_.end();
849 while(old != endold && added!= endadded)
851 if(old->local().state()==
DELETED){
856 if(old->global() < added->global() ||
857 (old->global() == added->global()
858 && LocalIndexComparator<TL>::compare(old->local(),added->local())))
860 tempPairs.push_back(*old);
865 tempPairs.push_back(*added);
873 if(old->local().state()!=
DELETED){
874 tempPairs.push_back(*old);
879 while(added!= endadded)
881 tempPairs.push_back(*added);
884 localIndices_ = tempPairs;
889 template<
class TG,
class TL,
int N>
890 inline const IndexPair<TG,TL>&
891 ParallelIndexSet<TG,TL,N>::at(
const TG& global)
const
894 int low=0, high=localIndices_.size()-1, probe=-1;
898 probe = (high + low) / 2;
899 if(global <= localIndices_[probe].global())
908 if( localIndices_[low].global() != global)
909 DUNE_THROW(RangeError,
"Could not find entry of "<<global);
911 return localIndices_[low];
914 template<
class TG,
class TL,
int N>
915 inline const IndexPair<TG,TL>&
916 ParallelIndexSet<TG,TL,N>::operator[](
const TG& global)
const
919 int low=0, high=localIndices_.size()-1, probe=-1;
923 probe = (high + low) / 2;
924 if(global <= localIndices_[probe].global())
930 return localIndices_[low];
932 template<
class TG,
class TL,
int N>
933 inline IndexPair<TG,TL>& ParallelIndexSet<TG,TL,N>::at(
const TG& global)
936 int low=0, high=localIndices_.size()-1, probe=-1;
940 probe = (high + low) / 2;
941 if(localIndices_[probe].global() >= global)
950 if( localIndices_[low].global() != global)
951 DUNE_THROW(RangeError,
"Could not find entry of "<<global);
953 return localIndices_[low];
956 template<
class TG,
class TL,
int N>
957 inline IndexPair<TG,TL>& ParallelIndexSet<TG,TL,N>::operator[](
const TG& global)
960 int low=0, high=localIndices_.size()-1, probe=-1;
964 probe = (high + low) / 2;
965 if(localIndices_[probe].global() >= global)
971 return localIndices_[low];
973 template<
class TG,
class TL,
int N>
974 inline typename ParallelIndexSet<TG,TL,N>::iterator
975 ParallelIndexSet<TG,TL,N>::begin()
977 return iterator(*
this, localIndices_.begin());
981 template<
class TG,
class TL,
int N>
982 inline typename ParallelIndexSet<TG,TL,N>::iterator
983 ParallelIndexSet<TG,TL,N>::end()
985 return iterator(*
this,localIndices_.end());
988 template<
class TG,
class TL,
int N>
989 inline typename ParallelIndexSet<TG,TL,N>::const_iterator
990 ParallelIndexSet<TG,TL,N>::begin()
const
992 return localIndices_.begin();
996 template<
class TG,
class TL,
int N>
997 inline typename ParallelIndexSet<TG,TL,N>::const_iterator
998 ParallelIndexSet<TG,TL,N>::end()
const
1000 return localIndices_.end();
1003 template<
class TG,
class TL,
int N>
1004 void ParallelIndexSet<TG,TL,N>::renumberLocal(){
1007 DUNE_THROW(InvalidIndexSetState,
"IndexSet has to be in "
1008 <<
"GROUND state for renumberLocal()");
1011 typedef typename ArrayList<IndexPair,N>::iterator iterator;
1012 const const_iterator end_ = end();
1015 for(iterator pair=begin(); pair!=end_; index++, ++pair)
1016 pair->local()=index;
1019 template<
class TG,
class TL,
int N>
1020 inline int ParallelIndexSet<TG,TL,N>::seqNo()
const
1025 template<
class TG,
class TL,
int N>
1026 inline size_t ParallelIndexSet<TG,TL,N>::size()
const
1028 return localIndices_.size();
1032 GlobalLookupIndexSet<I>::GlobalLookupIndexSet(
const I& indexset,
1034 : indexSet_(indexset), size_(size),
1035 indices_(size_, static_cast<const IndexPair*>(0))
1037 const_iterator end_ = indexSet_.end();
1039 for(const_iterator pair = indexSet_.begin(); pair!=end_; ++pair){
1040 assert(pair->local()<size_);
1041 indices_[pair->local()] = &(*pair);
1046 GlobalLookupIndexSet<I>::GlobalLookupIndexSet(
const I& indexset)
1047 : indexSet_(indexset), size_(0)
1049 const_iterator end_ = indexSet_.end();
1050 for(const_iterator pair = indexSet_.begin(); pair!=end_; ++pair)
1051 size_=std::max(size_,static_cast<std::size_t>(pair->local()));
1053 indices_.resize(++size_, 0);
1055 for(const_iterator pair = indexSet_.begin(); pair!=end_; ++pair)
1056 indices_[pair->local()] = &(*pair);
1060 GlobalLookupIndexSet<I>::~GlobalLookupIndexSet()
1064 inline const IndexPair<typename I::GlobalIndex, typename I::LocalIndex>*
1065 GlobalLookupIndexSet<I>::pair(
const std::size_t& local)
const
1067 return indices_[local];
1071 inline const IndexPair<typename I::GlobalIndex, typename I::LocalIndex>&
1072 GlobalLookupIndexSet<I>::operator[](
const GlobalIndex& global)
const
1074 return indexSet_[global];
1078 typename I::const_iterator GlobalLookupIndexSet<I>::begin()
const
1080 return indexSet_.begin();
1084 typename I::const_iterator GlobalLookupIndexSet<I>::end()
const
1086 return indexSet_.end();
1090 inline size_t GlobalLookupIndexSet<I>::size()
const
1096 inline int GlobalLookupIndexSet<I>::seqNo()
const
1098 return indexSet_.seqNo();
1101 template<
typename TG,
typename TL,
int N,
typename TG1,
typename TL1,
int N1>
1102 bool operator==(
const ParallelIndexSet<TG,TL,N>& idxset,
1103 const ParallelIndexSet<TG1,TL1,N1>& idxset1)
1105 if(idxset.size()!=idxset1.size())
1107 typedef typename ParallelIndexSet<TG,TL,N>::const_iterator Iter;
1108 typedef typename ParallelIndexSet<TG1,TL1,N1>::const_iterator Iter1;
1109 Iter iter=idxset.begin();
1110 for(Iter1 iter1=idxset1.begin(); iter1 != idxset1.end(); ++iter, ++iter1){
1111 if(iter1->global()!=iter->global())
1113 typedef typename ParallelIndexSet<TG,TL,N>::LocalIndex PI;
1114 const PI& pi=iter->local(), pi1=iter1->local();
1122 template<
typename TG,
typename TL,
int N,
typename TG1,
typename TL1,
int N1>
1123 bool operator!=(
const ParallelIndexSet<TG,TL,N>& idxset,
1124 const ParallelIndexSet<TG1,TL1,N1>& idxset1)
1126 return !(idxset==idxset1);