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>
41 template<
class TG,
class TL>
44 template<
class TG,
class TL>
47 template<
class TG,
class TL>
50 template<
class TG,
class TL>
53 template<
class TG,
class TL>
56 template<
class TG,
class TL>
59 template<
class TG,
class TL>
62 template<
class TG,
class TL>
63 bool operator<(const IndexPair<TG,TL>&,
const TG&);
65 template<
class TG,
class TL>
68 template<
class TG,
class TL>
69 bool operator<=(const IndexPair<TG,TL>&,
const TG&);
71 template<
class TG,
class TL>
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!");
302 ArrayList<IndexPair,N>::const_iterator
334 inline
void add(const
GlobalIndex& global) throw(InvalidIndexSetState);
345 throw(InvalidIndexSetState);
355 throw(InvalidIndexSetState);
369 void endResize() throw(InvalidIndexSetState);
425 inline iterator
begin();
431 inline iterator
end();
462 inline
int seqNo() const;
468 inline
size_t size() const;
480 bool deletedEntries_;
494 template<class TG, class TL,
int N>
495 std::ostream& operator<<(std::ostream& os, const
ParallelIndexSet<TG,TL,N>& indexSet);
534 GlobalLookupIndexSet(
const ParallelIndexSet& indexset, std::size_t
size);
541 GlobalLookupIndexSet(
const ParallelIndexSet& indexset);
546 ~GlobalLookupIndexSet();
557 inline const IndexPair&
563 inline const IndexPair*
564 pair(
const std::size_t& local)
const;
570 inline const_iterator
begin()
const;
576 inline const_iterator
end()
const;
584 inline int seqNo()
const;
590 inline size_t size()
const;
595 const ParallelIndexSet& indexSet_;
605 std::vector<const IndexPair*> indices_;
613 static bool compare(
const T& t1,
const T& t2){
618 template<
class TG,
class TL>
631 template<
class TG,
class TL>
632 inline std::ostream& operator<<(std::ostream& os, const IndexPair<TG,TL>& pair)
634 os<<
"{global="<<pair.global_<<
", local="<<pair.local_<<
"}";
638 template<
class TG,
class TL,
int N>
639 inline std::ostream& operator<<(std::ostream& os, const ParallelIndexSet<TG,TL,N>& indexSet)
642 Iterator
end = indexSet.end();
644 for(Iterator index = indexSet.begin(); index !=
end; ++index)
651 template<
class TG,
class TL>
654 return a.global_==b.global_;
657 template<
class TG,
class TL>
660 return a.global_!=b.global_;
663 template<
class TG,
class TL>
666 return a.global_<b.global_;
669 template<
class TG,
class TL>
672 return a.global_>b.global_;
675 template<
class TG,
class TL>
678 return a.global_<=b.global_;
681 template<
class TG,
class TL>
684 return a.global_>=b.global_;
687 template<
class TG,
class TL>
693 template<
class TG,
class TL>
699 template<
class TG,
class TL>
700 inline bool operator<(const IndexPair<TG,TL>& a,
const TG& b)
705 template<
class TG,
class TL>
711 template<
class TG,
class TL>
712 inline bool operator<=(const IndexPair<TG,TL>& a,
const TG& b)
717 template<
class TG,
class TL>
725 template<
class TG,
class TL>
727 : global_(global), local_(local){}
729 template<
class TG,
class TL>
730 IndexPair<TG,TL>::IndexPair(
const TG& global)
731 : global_(global), local_(){}
733 template<
class TG,
class TL>
734 IndexPair<TG,TL>::IndexPair()
735 : global_(), local_(){}
737 template<
class TG,
class TL>
738 inline const TG& IndexPair<TG,TL>::global()
const{
742 template<
class TG,
class TL>
743 inline TL& IndexPair<TG,TL>::local() {
747 template<
class TG,
class TL>
748 inline const TL& IndexPair<TG,TL>::local()
const{
752 template<
class TG,
class TL>
753 inline void IndexPair<TG,TL>::setLocal(
int local){
757 template<
class TG,
class TL,
int N>
758 ParallelIndexSet<TG,TL,N>::ParallelIndexSet()
759 : state_(
GROUND), seqNo_(0)
762 template<
class TG,
class TL,
int N>
763 void ParallelIndexSet<TG,TL,N>::beginResize() throw(InvalidIndexSetState)
770 "IndexSet has to be in GROUND state, when "
771 <<
"beginResize() is called!");
775 deletedEntries_ =
false;
778 template<
class TG,
class TL,
int N>
779 inline void ParallelIndexSet<TG,TL,N>::add(
const GlobalIndex& global)
780 throw(InvalidIndexSetState)
785 DUNE_THROW(InvalidIndexSetState,
"Indices can only be added "
786 <<
"while in RESIZE state!");
788 newIndices_.push_back(IndexPair(global));
791 template<
class TG,
class TL,
int N>
792 inline void ParallelIndexSet<TG,TL,N>::add(
const TG& global,
const TL& local)
793 throw(InvalidIndexSetState)
798 DUNE_THROW(InvalidIndexSetState,
"Indices can only be added "
799 <<
"while in RESIZE state!");
801 newIndices_.push_back(IndexPair(global,local));
804 template<
class TG,
class TL,
int N>
805 inline void ParallelIndexSet<TG,TL,N>::markAsDeleted(
const iterator& global)
806 throw(InvalidIndexSetState){
810 DUNE_THROW(InvalidIndexSetState,
"Indices can only be removed "
811 <<
"while in RESIZE state!");
813 deletedEntries_ =
true;
815 global.markAsDeleted();
818 template<
class TG,
class TL,
int N>
819 void ParallelIndexSet<TG,TL,N>::endResize() throw(InvalidIndexSetState){
823 DUNE_THROW(InvalidIndexSetState,
"endResize called while not "
824 <<
"in RESIZE state!");
827 std::sort(newIndices_.begin(), newIndices_.end(), IndexSetSortFunctor<TG,TL>());
834 template<
class TG,
class TL,
int N>
835 inline void ParallelIndexSet<TG,TL,N>::merge(){
836 if(localIndices_.size()==0)
838 localIndices_=newIndices_;
841 else if(newIndices_.size()>0 || deletedEntries_)
843 ArrayList<IndexPair,N> tempPairs;
844 typedef typename ArrayList<IndexPair,N>::iterator iterator;
845 typedef typename ArrayList<IndexPair,N>::const_iterator const_iterator;
847 iterator old=localIndices_.begin();
848 iterator added=newIndices_.begin();
849 const const_iterator endold=localIndices_.end();
850 const const_iterator endadded=newIndices_.end();
852 while(old != endold && added!= endadded)
854 if(old->local().state()==
DELETED){
859 if(old->global() < added->global() ||
860 (old->global() == added->global()
861 && LocalIndexComparator<TL>::compare(old->local(),added->local())))
863 tempPairs.push_back(*old);
868 tempPairs.push_back(*added);
876 if(old->local().state()!=
DELETED){
877 tempPairs.push_back(*old);
882 while(added!= endadded)
884 tempPairs.push_back(*added);
887 localIndices_ = tempPairs;
892 template<
class TG,
class TL,
int N>
893 inline const IndexPair<TG,TL>&
894 ParallelIndexSet<TG,TL,N>::at(
const TG& global)
const
897 int low=0, high=localIndices_.size()-1, probe=-1;
901 probe = (high + low) / 2;
902 if(global <= localIndices_[probe].global())
911 if( localIndices_[low].global() != global)
912 DUNE_THROW(RangeError,
"Could not find entry of "<<global);
914 return localIndices_[low];
917 template<
class TG,
class TL,
int N>
918 inline const IndexPair<TG,TL>&
919 ParallelIndexSet<TG,TL,N>::operator[](
const TG& global)
const
922 int low=0, high=localIndices_.size()-1, probe=-1;
926 probe = (high + low) / 2;
927 if(global <= localIndices_[probe].global())
933 return localIndices_[low];
935 template<
class TG,
class TL,
int N>
936 inline IndexPair<TG,TL>& ParallelIndexSet<TG,TL,N>::at(
const TG& global)
939 int low=0, high=localIndices_.size()-1, probe=-1;
943 probe = (high + low) / 2;
944 if(localIndices_[probe].global() >= global)
953 if( localIndices_[low].global() != global)
954 DUNE_THROW(RangeError,
"Could not find entry of "<<global);
956 return localIndices_[low];
959 template<
class TG,
class TL,
int N>
960 inline IndexPair<TG,TL>& ParallelIndexSet<TG,TL,N>::operator[](
const TG& global)
963 int low=0, high=localIndices_.size()-1, probe=-1;
967 probe = (high + low) / 2;
968 if(localIndices_[probe].global() >= global)
974 return localIndices_[low];
976 template<
class TG,
class TL,
int N>
977 inline typename ParallelIndexSet<TG,TL,N>::iterator
978 ParallelIndexSet<TG,TL,N>::begin()
980 return iterator(*
this, localIndices_.begin());
984 template<
class TG,
class TL,
int N>
985 inline typename ParallelIndexSet<TG,TL,N>::iterator
986 ParallelIndexSet<TG,TL,N>::end()
988 return iterator(*
this,localIndices_.end());
991 template<
class TG,
class TL,
int N>
992 inline typename ParallelIndexSet<TG,TL,N>::const_iterator
993 ParallelIndexSet<TG,TL,N>::begin()
const
995 return localIndices_.begin();
999 template<
class TG,
class TL,
int N>
1000 inline typename ParallelIndexSet<TG,TL,N>::const_iterator
1001 ParallelIndexSet<TG,TL,N>::end()
const
1003 return localIndices_.end();
1006 template<
class TG,
class TL,
int N>
1007 void ParallelIndexSet<TG,TL,N>::renumberLocal(){
1010 DUNE_THROW(InvalidIndexSetState,
"IndexSet has to be in "
1011 <<
"GROUND state for renumberLocal()");
1014 typedef typename ArrayList<IndexPair,N>::iterator iterator;
1015 const const_iterator end_ = end();
1018 for(iterator pair=begin(); pair!=end_; index++, ++pair)
1019 pair->local()=index;
1022 template<
class TG,
class TL,
int N>
1023 inline int ParallelIndexSet<TG,TL,N>::seqNo()
const
1028 template<
class TG,
class TL,
int N>
1029 inline size_t ParallelIndexSet<TG,TL,N>::size()
const
1031 return localIndices_.size();
1035 GlobalLookupIndexSet<I>::GlobalLookupIndexSet(
const I& indexset,
1037 : indexSet_(indexset), size_(size),
1038 indices_(size_, static_cast<const IndexPair*>(0))
1040 const_iterator end_ = indexSet_.end();
1042 for(const_iterator pair = indexSet_.begin(); pair!=end_; ++pair){
1043 assert(pair->local()<size_);
1044 indices_[pair->local()] = &(*pair);
1049 GlobalLookupIndexSet<I>::GlobalLookupIndexSet(
const I& indexset)
1050 : indexSet_(indexset), size_(0)
1052 const_iterator end_ = indexSet_.end();
1053 for(const_iterator pair = indexSet_.begin(); pair!=end_; ++pair)
1054 size_=std::max(size_,static_cast<std::size_t>(pair->local()));
1056 indices_.resize(++size_, 0);
1058 for(const_iterator pair = indexSet_.begin(); pair!=end_; ++pair)
1059 indices_[pair->local()] = &(*pair);
1063 GlobalLookupIndexSet<I>::~GlobalLookupIndexSet()
1067 inline const IndexPair<typename I::GlobalIndex, typename I::LocalIndex>*
1068 GlobalLookupIndexSet<I>::pair(
const std::size_t& local)
const
1070 return indices_[local];
1074 inline const IndexPair<typename I::GlobalIndex, typename I::LocalIndex>&
1075 GlobalLookupIndexSet<I>::operator[](
const GlobalIndex& global)
const
1077 return indexSet_[global];
1081 typename I::const_iterator GlobalLookupIndexSet<I>::begin()
const
1083 return indexSet_.begin();
1087 typename I::const_iterator GlobalLookupIndexSet<I>::end()
const
1089 return indexSet_.end();
1093 inline size_t GlobalLookupIndexSet<I>::size()
const
1099 inline int GlobalLookupIndexSet<I>::seqNo()
const
1101 return indexSet_.seqNo();
1104 template<
typename TG,
typename TL,
int N,
typename TG1,
typename TL1,
int N1>
1105 bool operator==(
const ParallelIndexSet<TG,TL,N>& idxset,
1106 const ParallelIndexSet<TG1,TL1,N1>& idxset1)
1108 if(idxset.size()!=idxset1.size())
1110 typedef typename ParallelIndexSet<TG,TL,N>::const_iterator Iter;
1111 typedef typename ParallelIndexSet<TG1,TL1,N1>::const_iterator Iter1;
1112 Iter iter=idxset.begin();
1113 for(Iter1 iter1=idxset1.begin(); iter1 != idxset1.end(); ++iter, ++iter1){
1114 if(iter1->global()!=iter->global())
1116 typedef typename ParallelIndexSet<TG,TL,N>::LocalIndex PI;
1117 const PI& pi=iter->local(), pi1=iter1->local();
1125 template<
typename TG,
typename TL,
int N,
typename TG1,
typename TL1,
int N1>
1126 bool operator!=(
const ParallelIndexSet<TG,TL,N>& idxset,
1127 const ParallelIndexSet<TG1,TL1,N1>& idxset1)
1129 return !(idxset==idxset1);