4 #ifndef DUNE_INDEXSET_HH
5 #define DUNE_INDEXSET_HH
30 template<
class TG,
class TL>
38 template<
class TG,
class TL>
39 std::ostream& operator<<(std::ostream& os, const IndexPair<TG,TL>& pair);
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 IndexPair<TG,TL>&);
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&);
74 template<
class TG,
class TL>
75 bool operator >=(
const IndexPair<TG,TL>&,
const TG&);
83 template<
class TG,
class TL>
216 template<
typename TG,
typename TL,
int N=100>
265 : Father(father), indexSet_(&indexSet)
269 : Father(other), indexSet_(other.indexSet_)
275 indexSet_ = other.indexSet_;
289 if(indexSet_->state_ !=
RESIZE)
290 DUNE_THROW(InvalidIndexSetState,
"Indices can only be removed "
291 <<
"while in RESIZE state!");
337 inline
void add(const
GlobalIndex& global) throw(InvalidIndexSetState);
348 throw(InvalidIndexSetState);
358 throw(InvalidIndexSetState);
372 void endResize() throw(InvalidIndexSetState);
465 inline
int seqNo() const;
471 inline
size_t size() const;
483 bool deletedEntries_;
497 template<class TG, class TL,
int N>
498 std::ostream& operator<<(std::ostream& os, const
ParallelIndexSet<TG,TL,N>& indexSet);
537 GlobalLookupIndexSet(
const ParallelIndexSet& indexset, std::size_t
size);
544 GlobalLookupIndexSet(
const ParallelIndexSet& indexset);
549 ~GlobalLookupIndexSet();
560 inline const IndexPair&
566 inline const IndexPair*
567 pair(
const std::size_t& local)
const;
573 inline const_iterator
begin()
const;
579 inline const_iterator
end()
const;
587 inline int seqNo()
const;
593 inline size_t size()
const;
598 const ParallelIndexSet& indexSet_;
608 std::vector<const IndexPair*> indices_;
616 static bool compare(
const T& t1,
const T& t2){
623 template<
class TG,
class TL>
636 template<
class TG,
class TL>
637 inline std::ostream& operator<<(std::ostream& os, const IndexPair<TG,TL>& pair)
639 os<<
"{global="<<pair.global_<<
", local="<<pair.local_<<
"}";
643 template<
class TG,
class TL,
int N>
644 inline std::ostream& operator<<(std::ostream& os, const ParallelIndexSet<TG,TL,N>& indexSet)
647 Iterator
end = indexSet.end();
649 for(Iterator index = indexSet.begin(); index !=
end; ++index)
656 template<
class TG,
class TL>
659 return a.global_==b.global_;
662 template<
class TG,
class TL>
665 return a.global_!=b.global_;
668 template<
class TG,
class TL>
671 return a.global_<b.global_;
674 template<
class TG,
class TL>
677 return a.global_>b.global_;
680 template<
class TG,
class TL>
683 return a.global_<=b.global_;
686 template<
class TG,
class TL>
689 return a.global_>=b.global_;
692 template<
class TG,
class TL>
698 template<
class TG,
class TL>
704 template<
class TG,
class TL>
705 inline bool operator<(const IndexPair<TG,TL>& a,
const TG& b)
710 template<
class TG,
class TL>
716 template<
class TG,
class TL>
717 inline bool operator<=(const IndexPair<TG,TL>& a,
const TG& b)
722 template<
class TG,
class TL>
730 template<
class TG,
class TL>
732 : global_(global), local_(local){}
734 template<
class TG,
class TL>
735 IndexPair<TG,TL>::IndexPair(
const TG& global)
736 : global_(global), local_(){}
738 template<
class TG,
class TL>
739 IndexPair<TG,TL>::IndexPair()
740 : global_(), local_(){}
742 template<
class TG,
class TL>
743 inline const TG& IndexPair<TG,TL>::global()
const {
747 template<
class TG,
class TL>
748 inline TL& IndexPair<TG,TL>::local() {
752 template<
class TG,
class TL>
753 inline const TL& IndexPair<TG,TL>::local()
const {
757 template<
class TG,
class TL>
758 inline void IndexPair<TG,TL>::setLocal(
int local){
762 template<
class TG,
class TL,
int N>
763 ParallelIndexSet<TG,TL,N>::ParallelIndexSet()
764 : state_(
GROUND), seqNo_(0)
767 template<
class TG,
class TL,
int N>
768 void ParallelIndexSet<TG,TL,N>::beginResize() throw(InvalidIndexSetState)
775 "IndexSet has to be in GROUND state, when "
776 <<
"beginResize() is called!");
780 deletedEntries_ =
false;
783 template<
class TG,
class TL,
int N>
784 inline void ParallelIndexSet<TG,TL,N>::add(
const GlobalIndex& global)
785 throw(InvalidIndexSetState)
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)
798 throw(InvalidIndexSetState)
803 DUNE_THROW(InvalidIndexSetState,
"Indices can only be added "
804 <<
"while in RESIZE state!");
806 newIndices_.push_back(IndexPair(global,local));
809 template<
class TG,
class TL,
int N>
810 inline void ParallelIndexSet<TG,TL,N>::markAsDeleted(
const iterator& global)
811 throw(InvalidIndexSetState){
815 DUNE_THROW(InvalidIndexSetState,
"Indices can only be removed "
816 <<
"while in RESIZE state!");
818 deletedEntries_ =
true;
820 global.markAsDeleted();
823 template<
class TG,
class TL,
int N>
824 void ParallelIndexSet<TG,TL,N>::endResize() throw(InvalidIndexSetState){
828 DUNE_THROW(InvalidIndexSetState,
"endResize called while not "
829 <<
"in RESIZE state!");
832 std::sort(newIndices_.begin(), newIndices_.end(), IndexSetSortFunctor<TG,TL>());
839 template<
class TG,
class TL,
int N>
840 inline void ParallelIndexSet<TG,TL,N>::merge(){
841 if(localIndices_.size()==0)
843 localIndices_=newIndices_;
846 else if(newIndices_.size()>0 || deletedEntries_)
848 ArrayList<IndexPair,N> tempPairs;
849 typedef typename ArrayList<IndexPair,N>::iterator
iterator;
850 typedef typename ArrayList<IndexPair,N>::const_iterator const_iterator;
852 iterator old=localIndices_.begin();
853 iterator added=newIndices_.begin();
854 const const_iterator endold=localIndices_.end();
855 const const_iterator endadded=newIndices_.end();
857 while(old != endold && added!= endadded)
859 if(old->local().state()==
DELETED) {
864 if(old->global() < added->global() ||
865 (old->global() == added->global()
866 && LocalIndexComparator<TL>::compare(old->local(),added->local())))
868 tempPairs.push_back(*old);
873 tempPairs.push_back(*added);
881 if(old->local().state()!=
DELETED) {
882 tempPairs.push_back(*old);
887 while(added!= endadded)
889 tempPairs.push_back(*added);
892 localIndices_ = tempPairs;
897 template<
class TG,
class TL,
int N>
898 inline const IndexPair<TG,TL>&
899 ParallelIndexSet<TG,TL,N>::at(
const TG& global)
const
902 int low=0, high=localIndices_.size()-1, probe=-1;
906 probe = (high + low) / 2;
907 if(global <= localIndices_[probe].global())
916 if( localIndices_[low].global() != global)
917 DUNE_THROW(RangeError,
"Could not find entry of "<<global);
919 return localIndices_[low];
922 template<
class TG,
class TL,
int N>
923 inline const IndexPair<TG,TL>&
924 ParallelIndexSet<TG,TL,N>::operator[](
const TG& global)
const
927 int low=0, high=localIndices_.size()-1, probe=-1;
931 probe = (high + low) / 2;
932 if(global <= localIndices_[probe].global())
938 return localIndices_[low];
940 template<
class TG,
class TL,
int N>
941 inline IndexPair<TG,TL>& ParallelIndexSet<TG,TL,N>::at(
const TG& global)
944 int low=0, high=localIndices_.size()-1, probe=-1;
948 probe = (high + low) / 2;
949 if(localIndices_[probe].global() >= global)
958 if( localIndices_[low].global() != global)
959 DUNE_THROW(RangeError,
"Could not find entry of "<<global);
961 return localIndices_[low];
964 template<
class TG,
class TL,
int N>
965 inline IndexPair<TG,TL>& ParallelIndexSet<TG,TL,N>::operator[](
const TG& global)
968 int low=0, high=localIndices_.size()-1, probe=-1;
972 probe = (high + low) / 2;
973 if(localIndices_[probe].global() >= global)
979 return localIndices_[low];
981 template<
class TG,
class TL,
int N>
982 inline typename ParallelIndexSet<TG,TL,N>::iterator
983 ParallelIndexSet<TG,TL,N>::begin()
985 return iterator(*
this, localIndices_.begin());
989 template<
class TG,
class TL,
int N>
990 inline typename ParallelIndexSet<TG,TL,N>::iterator
991 ParallelIndexSet<TG,TL,N>::end()
993 return iterator(*
this,localIndices_.end());
996 template<
class TG,
class TL,
int N>
997 inline typename ParallelIndexSet<TG,TL,N>::const_iterator
998 ParallelIndexSet<TG,TL,N>::begin()
const
1000 return localIndices_.begin();
1004 template<
class TG,
class TL,
int N>
1005 inline typename ParallelIndexSet<TG,TL,N>::const_iterator
1006 ParallelIndexSet<TG,TL,N>::end()
const
1008 return localIndices_.end();
1011 template<
class TG,
class TL,
int N>
1012 void ParallelIndexSet<TG,TL,N>::renumberLocal(){
1015 DUNE_THROW(InvalidIndexSetState,
"IndexSet has to be in "
1016 <<
"GROUND state for renumberLocal()");
1019 typedef typename ArrayList<IndexPair,N>::iterator iterator;
1020 const const_iterator end_ = end();
1023 for(iterator pair=begin(); pair!=end_; index++, ++pair)
1024 pair->local()=index;
1027 template<
class TG,
class TL,
int N>
1028 inline int ParallelIndexSet<TG,TL,N>::seqNo()
const
1033 template<
class TG,
class TL,
int N>
1034 inline size_t ParallelIndexSet<TG,TL,N>::size()
const
1036 return localIndices_.size();
1040 GlobalLookupIndexSet<I>::GlobalLookupIndexSet(
const I& indexset,
1042 : indexSet_(indexset),
size_(size),
1043 indices_(
size_, static_cast<const IndexPair*>(0))
1045 const_iterator end_ = indexSet_.end();
1047 for(const_iterator pair = indexSet_.begin(); pair!=end_; ++pair) {
1048 assert(pair->local()<
size_);
1049 indices_[pair->local()] = &(*pair);
1054 GlobalLookupIndexSet<I>::GlobalLookupIndexSet(
const I& indexset)
1055 : indexSet_(indexset),
size_(0)
1057 const_iterator end_ = indexSet_.end();
1058 for(const_iterator pair = indexSet_.begin(); pair!=end_; ++pair)
1059 size_=std::max(
size_,static_cast<std::size_t>(pair->local()));
1061 indices_.resize(++
size_, 0);
1063 for(const_iterator pair = indexSet_.begin(); pair!=end_; ++pair)
1064 indices_[pair->local()] = &(*pair);
1068 GlobalLookupIndexSet<I>::~GlobalLookupIndexSet()
1072 inline const IndexPair<typename I::GlobalIndex, typename I::LocalIndex>*
1073 GlobalLookupIndexSet<I>::pair(
const std::size_t& local)
const
1075 return indices_[local];
1079 inline const IndexPair<typename I::GlobalIndex, typename I::LocalIndex>&
1080 GlobalLookupIndexSet<I>::operator[](
const GlobalIndex& global)
const
1082 return indexSet_[global];
1086 typename I::const_iterator GlobalLookupIndexSet<I>::begin()
const
1088 return indexSet_.begin();
1092 typename I::const_iterator GlobalLookupIndexSet<I>::end()
const
1094 return indexSet_.end();
1098 inline size_t GlobalLookupIndexSet<I>::size()
const
1104 inline int GlobalLookupIndexSet<I>::seqNo()
const
1106 return indexSet_.seqNo();
1109 template<
typename TG,
typename TL,
int N,
typename TG1,
typename TL1,
int N1>
1110 bool operator==(
const ParallelIndexSet<TG,TL,N>& idxset,
1111 const ParallelIndexSet<TG1,TL1,N1>& idxset1)
1113 if(idxset.size()!=idxset1.size())
1115 typedef typename ParallelIndexSet<TG,TL,N>::const_iterator Iter;
1116 typedef typename ParallelIndexSet<TG1,TL1,N1>::const_iterator Iter1;
1117 Iter iter=idxset.begin();
1118 for(Iter1 iter1=idxset1.begin(); iter1 != idxset1.end(); ++iter, ++iter1) {
1119 if(iter1->global()!=iter->global())
1121 typedef typename ParallelIndexSet<TG,TL,N>::LocalIndex PI;
1122 const PI& pi=iter->local(), pi1=iter1->local();
1130 template<
typename TG,
typename TL,
int N,
typename TG1,
typename TL1,
int N1>
1131 bool operator!=(
const ParallelIndexSet<TG,TL,N>& idxset,
1132 const ParallelIndexSet<TG1,TL1,N1>& idxset1)
1134 return !(idxset==idxset1);