diff --git a/common/sllist.hh b/common/sllist.hh index 6f07c82b600d4c133347328ad62f453f57371168..a9f9055638d70dc93f0ae8049b088edd85396aed 100644 --- a/common/sllist.hh +++ b/common/sllist.hh @@ -245,6 +245,7 @@ namespace Dune class SLListIterator : public Dune::ForwardIteratorFacade<SLListIterator<T,A>, T, T&, std::size_t> { friend class SLListConstIterator<T,A>; + friend class SLListModifyIterator<T,A>; friend class SLList<T,A>; public: @@ -258,7 +259,7 @@ namespace Dune {} inline SLListIterator(const SLListModifyIterator<T,A>& other) - : current_(other.iterator_.current_) + : current_(other.iterator_.current_), list_(other.iterator_.list_) {} /** @@ -280,7 +281,6 @@ namespace Dune return current_==other.current_; } - /** * @brief Equality test for the iterator facade. * @param other The other iterator to check. @@ -291,6 +291,16 @@ namespace Dune return current_==other.current_; } + /** + * @brief Equality test for the iterator facade. + * @param other The other iterator to check. + * @return true If the other iterator is at the same position. + */ + inline bool equals(const SLListModifyIterator<T,A>& other) const + { + return current_==other.iterator_.current_; + } + /** * @brief Increment function for the iterator facade. */ @@ -367,6 +377,10 @@ namespace Dune : current_(other.current_) {} + inline SLListConstIterator(const SLListConstIterator<T,A>& other) + : current_(other.current_) + {} + inline SLListConstIterator(const SLListModifyIterator<T,A>& other) : current_(other.iterator_.current_) {} @@ -390,27 +404,6 @@ namespace Dune return current_==other.current_; } - /** - * @brief Equality test for the iterator facade. - * @param other The other iterator to check. - * @return true If the other iterator is at the same position. - */ - inline bool equals(const SLListModifyIterator<T,A>& other) const - { - return current_==other.iterator_.current_; - } - - - /** - * @brief Equality test for the iterator facade. - * @param other The other iterator to check. - * @return true If the other iterator is at the same position. - */ - inline bool equals(const SLListIterator<T,A>& other) const - { - return current_==other.current_; - } - /** * @brief Increment function for the iterator facade. */ @@ -428,16 +421,20 @@ namespace Dune * @brief A mutable iterator for the SLList. */ template<typename T, class A> - class SLListModifyIterator : public Dune::ForwardIteratorFacade<SLListIterator<T,A>, T, T&, std::size_t> + class SLListModifyIterator : public Dune::ForwardIteratorFacade<SLListModifyIterator<T,A>, T, T&, std::size_t> { friend class SLListConstIterator<T,A>; friend class SLListIterator<T,A>; public: inline SLListModifyIterator(SLListIterator<T,A> beforeIterator, - SLListConstIterator<T,A> _iterator) + SLListIterator<T,A> _iterator) : beforeIterator_(beforeIterator), iterator_(_iterator) {} + inline SLListModifyIterator(const SLListModifyIterator<T,A>& other) + : beforeIterator_(other.beforeIterator_), iterator_(other.iterator_) + {} + inline SLListModifyIterator() : beforeIterator_(), iterator_() {} @@ -448,7 +445,39 @@ namespace Dune */ inline T& dereference() const { - return iterator_.dereference(); + return *iterator_; + } + + /** + * @brief Test whether another iterator is equal. + * @return true if the other iterator is at the same position as + * this one. + */ + inline bool equals(const SLListConstIterator<T,A>& other) const + { + return iterator_== other; + } + + + /** + * @brief Test whether another iterator is equal. + * @return true if the other iterator is at the same position as + * this one. + */ + inline bool equals(const SLListIterator<T,A>& other) const + { + return iterator_== other; + } + + + /** + * @brief Test whether another iterator is equal. + * @return true if the other iterator is at the same position as + * this one. + */ + inline bool equals(const SLListModifyIterator<T,A>& other) const + { + return iterator_== other.iterator_; } /** @@ -465,7 +494,10 @@ namespace Dune * * Starting from the element at the current position all * elements will be shifted by one position to the back. - * The iterator will point to the same element after the insertion. + * The iterator will point to the same element as before + * after the insertion, i.e the number of increments to + * reach the same position from a begin iterator increases + * by one. * This means the inserted element is the one before the one * the iterator points to. * @param v The value to insert. @@ -493,7 +525,7 @@ namespace Dune /** @brief Iterator positioned at the position before the current. */ SLListIterator<T,A> beforeIterator_; /** @brief Iterator positioned at the current position. */ - SLListConstIterator<T,A> iterator_; + SLListIterator<T,A> iterator_; }; template<typename T, class A> SLList<T,A>::Element::Element(const T& item) @@ -626,7 +658,7 @@ namespace Dune template<typename T, class A> inline SLListModifyIterator<T,A> SLList<T,A>::endModify() { - return SLListModifyIterator<T,A>(tail(),end()); + return SLListModifyIterator<T,A>(iterator(tail_, this),iterator()); } diff --git a/common/test/sllisttest.cc b/common/test/sllisttest.cc index b9f1c877f4403e1a997007703121de3630cc1e5a..ae60bc746a75e706942acaf3158727d8bda311c7 100644 --- a/common/test/sllisttest.cc +++ b/common/test/sllisttest.cc @@ -50,7 +50,7 @@ void randomizeListFront(Dune::SLList<T,A>& alist){ alist.push_back((range*(rand()/(RAND_MAX+1.0)))); } -int testDeleteNext() +int testDelete() { typedef Dune::SLList<int,Dune::PoolAllocator<int,8*1024-16> > List; List alist; @@ -59,82 +59,86 @@ int testDeleteNext() alist.push_back(4); alist.push_back(5); - List::iterator iter=alist.oneBeforeBegin(); - iter.deleteNext(); - List::iterator iter1=iter; - ++iter1; + List::ModifyIterator iter = alist.beginModify(); + iter.remove(); if(*(alist.begin())!=4) { - std::cerr<<"delete next on position before head failed!"<<std::endl; + std::cerr<<"delete next on position before head failed! "<<__FILE__<<":"<<__LINE__<<std::endl; return 1; } - if(*iter1!=4) { - std::cerr<<"delete next failed"<<std::endl; + if(*iter!=4) { + std::cerr<<"delete next failed! "<<__FILE__<<":"<<__LINE__<<std::endl; return 1; } ++iter; - iter.deleteNext(); - ++iter; + iter.remove(); if(iter!=alist.end()) { - std::cerr<<"delete next faild"<<std::endl; + std::cerr<<"delete next faild! "<<__FILE__<<":"<<__LINE__<<std::endl; return 1; } if(*(alist.tail())!=4) { - std::cerr<<"delete before tail did not change tail!"<<std::endl; + std::cerr<<"delete before tail did not change tail! "<<__FILE__<<":"<<__LINE__<<std::endl; } return 0; } -int testInsertAfter() +int testInsert() { typedef Dune::SLList<int,Dune::PoolAllocator<int,8*1024-16> > List; List alist; alist.push_back(3); - List::iterator iter=alist.begin(); - iter.insertAfter(5); + List::ModifyIterator iter=alist.beginModify(); + iter.insert(7); int ret=0; if(*iter!=3) { - std::cerr<<"Value at current position changed due to insertAfter"<<std::endl; + std::cerr<<"Value at current position changed due to insert! "<<__FILE__<<":"<<__LINE__<<std::endl; ret++; } - ++iter; - if(iter==alist.end() || *iter!=5) { - std::cerr<<"Insertion failed!"<<std::endl; - ++ret; + + if(*alist.begin()!=7) { + std::cerr<<"Insert did not change first element! "<<__FILE__<<":"<<__LINE__<<std::endl; + ret++; } - iter=alist.oneBeforeBegin(); - iter.insertAfter(5); - ++iter; - if(iter==alist.end() || *iter!=5) { - std::cerr<<"Insertion failed!"<<std::endl; + iter=alist.beginModify(); + iter.insert(5); + + if(iter==alist.end() || *iter!=7) { + std::cerr<<"Insertion failed.! "<<__FILE__<<":"<<__LINE__<<std::endl; ++ret; } + if(*(alist.begin())!=5) { - std::cerr<<"Insert after at onebeforeBegin did not change head!"<<std::endl; + std::cerr<<"Insert after at onebeforeBegin did not change head! "<<__FILE__<<":"<<__LINE__<<std::endl; ++ret; } - iter = alist.tail(); - iter.insertAfter(20); - ++iter; - if(iter == alist.end() || *iter != 20) { - std::cerr<<"Insertion failed!"<<std::endl; + iter = alist.endModify(); + + if(iter!=alist.end()) { + std::cerr <<" Iterator got by endModify does not equal that got by end()! "<<__FILE__<<":"<<__LINE__<<std::endl; + ++ret; + } + + + iter.insert(20); + + if(iter != alist.end()) { + std::cerr<<"Insertion changed end iterator! "<<__FILE__<<":"<<__LINE__<<std::endl; ++ret; } if(*(alist.tail())!=20) { - std::cerr<<"tail was not changed!!"<<std::endl; + std::cerr<<"tail was not changed!! "<<__FILE__<<":"<<__LINE__<<std::endl; ++ret; } alist.clear(); - iter=alist.oneBeforeBegin(); - iter.insertAfter(5); - ++iter; - if(iter==alist.end() || *iter!=5) { - std::cerr<<"Insertion failed!"<<std::endl; + iter=alist.beginModify(); + iter.insert(5); + if(iter!=alist.end()) { + std::cerr<<"Insertion failed! "<<__FILE__<<":"<<__LINE__<<std::endl; ++ret; } return ret; @@ -152,11 +156,11 @@ int testOneBeforeBegin(T& alist) ++citerBefore; if(iterBefore!=iter || &(*iterBefore) != &(*iter)) { - std::cerr<<"one before iterator incremented once should point to begin()"<<std::endl; + std::cerr<<"one before iterator incremented once should point to begin()! "<<__FILE__<<":"<<__LINE__<<std::endl; ret++; } if(citerBefore!=iter || &(*citerBefore) != &(*iter)) { - std::cerr<<"one before iterator incremented once should point to begin()"<<std::endl; + std::cerr<<"one before iterator incremented once should point to begin()! "<<__FILE__<<":"<<__LINE__<<std::endl; ret++; } return ret; @@ -212,13 +216,29 @@ int main() randomizeListFront(list); randomizeListFront(list2); + Printer<std::iterator_traits<Dune::SLList<double>::ModifyIterator>::value_type> print; + + Dune::SLList<double>::ModifyIterator lbegin = list.beginModify(), lend = list.endModify(); + + double& d = lbegin.dereference(); + + d= 2.0; + + double& d1 = lbegin.dereference(); + + lbegin.dereference()=5.0; + + lbegin.operator*()=5.0; + + *lbegin=5.0; + ret+=testConstIterator(lbegin, lend, print); ret+=testIterator(list); ret+=testIterator(list1); ret+=testPushPop(); ret+=testOneBeforeBegin(list1); - ret+=testInsertAfter(); - ret+=testDeleteNext(); + ret+=testInsert(); + ret+=testDelete(); list.clear(); list1.clear();