3 #ifndef DUNE_INDEXSET_HH
4 #define DUNE_INDEXSET_HH
29 template<
class TG,
class TL>
37 template<
class TG,
class TL>
40 template<
class TG,
class TL>
43 template<
class TG,
class TL>
46 template<
class TG,
class TL>
49 template<
class TG,
class TL>
52 template<
class TG,
class TL>
55 template<
class TG,
class TL>
58 template<
class TG,
class TL>
61 template<
class TG,
class TL>
64 template<
class TG,
class TL>
67 template<
class TG,
class TL>
70 template<
class TG,
class TL>
73 template<
class TG,
class TL>
82 template<
class TG,
class TL>
215 template<
typename TG,
typename TL,
int N=100>
264 :
Father(father), indexSet_(&indexSet)
277 inline void markAsDeleted()
const
280 if(indexSet_->state_ !=
RESIZE)
282 <<
"while in RESIZE state!");
429 inline iterator
begin();
435 inline iterator
end();
466 inline int seqNo()
const;
472 inline size_t size()
const;
484 bool deletedEntries_;
498 template<
class TG,
class TL,
int N>
499 std::ostream&
operator<<(std::ostream& os,
const ParallelIndexSet<TG,TL,N>& indexSet);
507 class GlobalLookupIndexSet
568 pair(
const std::size_t& local)
const;
588 inline int seqNo()
const;
594 inline size_t size()
const;
609 std::vector<const IndexPair*> indices_;
617 static bool compare(
const T& t1,
const T& t2){
624 template<
class TG,
class TL>
637 template<
class TG,
class TL>
640 os<<
"{global="<<pair.global_<<
", local="<<pair.local_<<
"}";
644 template<
class TG,
class TL,
int N>
648 Iterator end = indexSet.
end();
650 for(Iterator index = indexSet.
begin(); index != end; ++index)
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>
690 return a.global_>=b.global_;
693 template<
class TG,
class TL>
699 template<
class TG,
class TL>
705 template<
class TG,
class TL>
711 template<
class TG,
class TL>
717 template<
class TG,
class TL>
723 template<
class TG,
class TL>
731 template<
class TG,
class TL>
733 : global_(global), local_(local){}
735 template<
class TG,
class TL>
736 IndexPair<TG,TL>::IndexPair(
const TG& global)
737 : global_(global), local_(){}
739 template<
class TG,
class TL>
740 IndexPair<TG,TL>::IndexPair()
741 : global_(), local_(){}
743 template<
class TG,
class TL>
744 inline const TG& IndexPair<TG,TL>::global()
const {
748 template<
class TG,
class TL>
749 inline TL& IndexPair<TG,TL>::local() {
753 template<
class TG,
class TL>
754 inline const TL& IndexPair<TG,TL>::local()
const {
758 template<
class TG,
class TL>
759 inline void IndexPair<TG,TL>::setLocal(
int local){
763 template<
class TG,
class TL,
int N>
764 ParallelIndexSet<TG,TL,N>::ParallelIndexSet()
765 : state_(
GROUND), seqNo_(0)
768 template<
class TG,
class TL,
int N>
769 void ParallelIndexSet<TG,TL,N>::beginResize()
776 "IndexSet has to be in GROUND state, when "
777 <<
"beginResize() is called!");
781 deletedEntries_ =
false;
784 template<
class TG,
class TL,
int N>
785 inline void ParallelIndexSet<TG,TL,N>::add(
const GlobalIndex& global)
790 DUNE_THROW(InvalidIndexSetState,
"Indices can only be added "
791 <<
"while in RESIZE state!");
793 newIndices_.push_back(IndexPair(global));
796 template<
class TG,
class TL,
int N>
797 inline void ParallelIndexSet<TG,TL,N>::add(
const TG& global,
const TL& local)
802 DUNE_THROW(InvalidIndexSetState,
"Indices can only be added "
803 <<
"while in RESIZE state!");
805 newIndices_.push_back(IndexPair(global,local));
808 template<
class TG,
class TL,
int N>
809 inline void ParallelIndexSet<TG,TL,N>::markAsDeleted(
const iterator& global)
814 DUNE_THROW(InvalidIndexSetState,
"Indices can only be removed "
815 <<
"while in RESIZE state!");
817 deletedEntries_ =
true;
819 global.markAsDeleted();
822 template<
class TG,
class TL,
int N>
823 void ParallelIndexSet<TG,TL,N>::endResize() {
827 DUNE_THROW(InvalidIndexSetState,
"endResize called while not "
828 <<
"in RESIZE state!");
831 std::sort(newIndices_.begin(), newIndices_.end(), IndexSetSortFunctor<TG,TL>());
838 template<
class TG,
class TL,
int N>
839 inline void ParallelIndexSet<TG,TL,N>::merge(){
840 if(localIndices_.size()==0)
842 localIndices_=newIndices_;
845 else if(newIndices_.size()>0 || deletedEntries_)
847 ArrayList<IndexPair,N> tempPairs;
848 typedef typename ArrayList<IndexPair,N>::iterator iterator;
849 typedef typename ArrayList<IndexPair,N>::const_iterator const_iterator;
851 iterator old=localIndices_.begin();
852 iterator added=newIndices_.begin();
853 const const_iterator endold=localIndices_.end();
854 const const_iterator endadded=newIndices_.end();
856 while(old != endold && added!= endadded)
858 if(old->local().state()==
DELETED) {
863 if(old->global() < added->global() ||
864 (old->global() == added->global()
865 && LocalIndexComparator<TL>::compare(old->local(),added->local())))
867 tempPairs.push_back(*old);
872 tempPairs.push_back(*added);
880 if(old->local().state()!=
DELETED) {
881 tempPairs.push_back(*old);
886 while(added!= endadded)
888 tempPairs.push_back(*added);
891 localIndices_ = tempPairs;
896 template<
class TG,
class TL,
int N>
897 inline const IndexPair<TG,TL>&
898 ParallelIndexSet<TG,TL,N>::at(
const TG& global)
const
901 int low=0, high=localIndices_.size()-1, probe=-1;
905 probe = (high + low) / 2;
906 if(global <= localIndices_[probe].global())
915 if( localIndices_[low].global() != global)
916 DUNE_THROW(RangeError,
"Could not find entry of "<<global);
918 return localIndices_[low];
921 template<
class TG,
class TL,
int N>
922 inline const IndexPair<TG,TL>&
923 ParallelIndexSet<TG,TL,N>::operator[](
const TG& global)
const
926 int low=0, high=localIndices_.size()-1, probe=-1;
930 probe = (high + low) / 2;
931 if(global <= localIndices_[probe].global())
937 return localIndices_[low];
939 template<
class TG,
class TL,
int N>
940 inline IndexPair<TG,TL>& ParallelIndexSet<TG,TL,N>::at(
const TG& global)
943 int low=0, high=localIndices_.size()-1, probe=-1;
947 probe = (high + low) / 2;
948 if(localIndices_[probe].global() >= global)
957 if( localIndices_[low].global() != global)
958 DUNE_THROW(RangeError,
"Could not find entry of "<<global);
960 return localIndices_[low];
963 template<
class TG,
class TL,
int N>
964 inline bool ParallelIndexSet<TG,TL,N>::exists (
const TG& global)
const
967 int low=0, high=localIndices_.size()-1, probe=-1;
971 probe = (high + low) / 2;
972 if(localIndices_[probe].global() >= global)
981 if( localIndices_[low].global() != global)
986 template<
class TG,
class TL,
int N>
987 inline IndexPair<TG,TL>& ParallelIndexSet<TG,TL,N>::operator[](
const TG& global)
990 int low=0, high=localIndices_.size()-1, probe=-1;
994 probe = (high + low) / 2;
995 if(localIndices_[probe].global() >= global)
1001 return localIndices_[low];
1003 template<
class TG,
class TL,
int N>
1004 inline typename ParallelIndexSet<TG,TL,N>::iterator
1005 ParallelIndexSet<TG,TL,N>::begin()
1007 return iterator(*
this, localIndices_.begin());
1011 template<
class TG,
class TL,
int N>
1012 inline typename ParallelIndexSet<TG,TL,N>::iterator
1013 ParallelIndexSet<TG,TL,N>::end()
1015 return iterator(*
this,localIndices_.end());
1018 template<
class TG,
class TL,
int N>
1019 inline typename ParallelIndexSet<TG,TL,N>::const_iterator
1020 ParallelIndexSet<TG,TL,N>::begin()
const
1022 return localIndices_.begin();
1026 template<
class TG,
class TL,
int N>
1027 inline typename ParallelIndexSet<TG,TL,N>::const_iterator
1028 ParallelIndexSet<TG,TL,N>::end()
const
1030 return localIndices_.end();
1033 template<
class TG,
class TL,
int N>
1034 void ParallelIndexSet<TG,TL,N>::renumberLocal(){
1037 DUNE_THROW(InvalidIndexSetState,
"IndexSet has to be in "
1038 <<
"GROUND state for renumberLocal()");
1041 typedef typename ArrayList<IndexPair,N>::iterator iterator;
1042 const const_iterator end_ = end();
1045 for(iterator pair=begin(); pair!=end_; index++, ++pair)
1046 pair->local()=index;
1049 template<
class TG,
class TL,
int N>
1050 inline int ParallelIndexSet<TG,TL,N>::seqNo()
const
1055 template<
class TG,
class TL,
int N>
1056 inline size_t ParallelIndexSet<TG,TL,N>::size()
const
1058 return localIndices_.size();
1062 GlobalLookupIndexSet<I>::GlobalLookupIndexSet(
const I& indexset,
1064 : indexSet_(indexset), size_(size),
1065 indices_(size_, static_cast<const IndexPair*>(0))
1067 const_iterator end_ = indexSet_.end();
1069 for(const_iterator pair = indexSet_.begin(); pair!=end_; ++pair) {
1070 assert(pair->local()<size_);
1071 indices_[pair->local()] = &(*pair);
1076 GlobalLookupIndexSet<I>::GlobalLookupIndexSet(
const I& indexset)
1077 : indexSet_(indexset), size_(0)
1079 const_iterator end_ = indexSet_.end();
1080 for(const_iterator pair = indexSet_.begin(); pair!=end_; ++pair)
1081 size_=
std::max(size_,
static_cast<std::size_t
>(pair->local()));
1083 indices_.resize(++size_, 0);
1085 for(const_iterator pair = indexSet_.begin(); pair!=end_; ++pair)
1086 indices_[pair->local()] = &(*pair);
1090 GlobalLookupIndexSet<I>::~GlobalLookupIndexSet()
1094 inline const IndexPair<typename I::GlobalIndex, typename I::LocalIndex>*
1095 GlobalLookupIndexSet<I>::pair(
const std::size_t& local)
const
1097 return indices_[local];
1101 inline const IndexPair<typename I::GlobalIndex, typename I::LocalIndex>&
1102 GlobalLookupIndexSet<I>::operator[](
const GlobalIndex& global)
const
1104 return indexSet_[global];
1108 typename I::const_iterator GlobalLookupIndexSet<I>::begin()
const
1110 return indexSet_.begin();
1114 typename I::const_iterator GlobalLookupIndexSet<I>::end()
const
1116 return indexSet_.end();
1120 inline size_t GlobalLookupIndexSet<I>::size()
const
1126 inline int GlobalLookupIndexSet<I>::seqNo()
const
1128 return indexSet_.seqNo();
1131 template<
typename TG,
typename TL,
int N,
typename TG1,
typename TL1,
int N1>
1132 bool operator==(
const ParallelIndexSet<TG,TL,N>& idxset,
1133 const ParallelIndexSet<TG1,TL1,N1>& idxset1)
1135 if(idxset.size()!=idxset1.size())
1137 typedef typename ParallelIndexSet<TG,TL,N>::const_iterator Iter;
1138 typedef typename ParallelIndexSet<TG1,TL1,N1>::const_iterator Iter1;
1139 Iter iter=idxset.begin();
1140 for(Iter1 iter1=idxset1.begin(); iter1 != idxset1.end(); ++iter, ++iter1) {
1141 if(iter1->global()!=iter->global())
1143 typedef typename ParallelIndexSet<TG,TL,N>::LocalIndex PI;
1144 const PI& pi=iter->local(), pi1=iter1->local();
1152 template<
typename TG,
typename TL,
int N,
typename TG1,
typename TL1,
int N1>
1153 bool operator!=(
const ParallelIndexSet<TG,TL,N>& idxset,
1154 const ParallelIndexSet<TG1,TL1,N1>& idxset1)
1156 return !(idxset==idxset1);