From 748df0b21a38c41bbcbd75c77bd10441f680274d Mon Sep 17 00:00:00 2001 From: Dennis Luxen Date: Tue, 15 Oct 2013 11:56:27 +0200 Subject: [PATCH] fix iterator concept implementation --- DataStructures/DeallocatingVector.h | 65 +++++++++++------------------ 1 file changed, 24 insertions(+), 41 deletions(-) diff --git a/DataStructures/DeallocatingVector.h b/DataStructures/DeallocatingVector.h index c3bce4dd8..6acec9b27 100644 --- a/DataStructures/DeallocatingVector.h +++ b/DataStructures/DeallocatingVector.h @@ -40,39 +40,18 @@ protected: //make constructors explicit, so we do not mix random access and deallocation iterators. DeallocatingVectorIteratorState(); public: - explicit DeallocatingVectorIteratorState(const DeallocatingVectorIteratorState &r) : mData(r.mData), mIndex(r.mIndex), mBucketList(r.mBucketList) {} - //explicit DeallocatingVectorIteratorState(const ElementT * ptr, const std::size_t idx, const std::vector & input_list) : mData(ptr), mIndex(idx), mBucketList(input_list) {} - explicit DeallocatingVectorIteratorState(const std::size_t idx, std::vector & input_list) : mData(DEALLOCATION_VECTOR_NULL_PTR), mIndex(idx), mBucketList(input_list) { - setPointerForIndex(); + explicit DeallocatingVectorIteratorState(const DeallocatingVectorIteratorState &r) : /*mData(r.mData),*/ mIndex(r.mIndex), mBucketList(r.mBucketList) {} + explicit DeallocatingVectorIteratorState(const std::size_t idx, std::vector & input_list) : /*mData(DEALLOCATION_VECTOR_NULL_PTR),*/ mIndex(idx), mBucketList(input_list) { } - ElementT * mData; std::size_t mIndex; std::vector & mBucketList; - inline void setPointerForIndex() { - if(bucketSizeC*mBucketList.size() <= mIndex) { - mData = DEALLOCATION_VECTOR_NULL_PTR; - return; - } - std::size_t _bucket = mIndex/bucketSizeC; - std::size_t _index = mIndex%bucketSizeC; - mData = &(mBucketList[_bucket][_index]); - - if(DeallocateC) { - //if we hopped over the border of the previous bucket, then delete that bucket. - if(0 == _index && _bucket) { - delete[] mBucketList[_bucket-1]; - mBucketList[_bucket-1] = DEALLOCATION_VECTOR_NULL_PTR; - } - } - - } inline bool operator!=(const DeallocatingVectorIteratorState &other) { - return (mData != other.mData) || (mIndex != other.mIndex) || (mBucketList != other.mBucketList); + return mIndex != other.mIndex; } inline bool operator==(const DeallocatingVectorIteratorState &other) { - return (mData == other.mData) && (mIndex == other.mIndex) && (mBucketList == other.mBucketList); + return mIndex == other.mIndex; } bool operator<(const DeallocatingVectorIteratorState &other) const { @@ -112,7 +91,6 @@ public: DeallocatingVectorIterator(const DeallocatingVectorIterator & r) : mState(r.mState) {} DeallocatingVectorIterator(std::size_t idx, std::vector & input_list) : mState(idx, input_list) {} - //DeallocatingVectorIterator(std::size_t idx, const std::vector & input_list) : mState(idx, input_list) {} DeallocatingVectorIterator(const DeallocatingVectorIteratorState & r) : mState(r) {} template @@ -122,30 +100,32 @@ public: } inline DeallocatingVectorIterator& operator++() { //prefix -// if(DeallocateC) assert(false); - ++mState.mIndex; mState.setPointerForIndex(); return *this; + if(DeallocateC) assert(false); + ++mState.mIndex; + return *this; } inline DeallocatingVectorIterator& operator--() { //prefix if(DeallocateC) assert(false); - --mState.mIndex; mState.setPointerForIndex(); return *this; + --mState.mIndex; + return *this; } inline DeallocatingVectorIterator operator++(int) { //postfix DeallocatingVectorIteratorState _myState(mState); - mState.mIndex++; mState.setPointerForIndex(); + mState.mIndex++; return DeallocatingVectorIterator(_myState); } inline DeallocatingVectorIterator operator--(int) { //postfix if(DeallocateC) assert(false); DeallocatingVectorIteratorState _myState(mState); - mState.mIndex--; mState.setPointerForIndex(); + mState.mIndex--; return DeallocatingVectorIterator(_myState); } inline DeallocatingVectorIterator operator+(const difference_type& n) const { DeallocatingVectorIteratorState _myState(mState); - _myState.mIndex+=n; _myState.setPointerForIndex(); + _myState.mIndex+=n; return DeallocatingVectorIterator(_myState); } @@ -156,7 +136,7 @@ public: inline DeallocatingVectorIterator operator-(const difference_type& n) const { if(DeallocateC) assert(false); DeallocatingVectorIteratorState _myState(mState); - _myState.mIndex-=n; _myState.setPointerForIndex(); + _myState.mIndex-=n; return DeallocatingVectorIterator(_myState); } @@ -164,14 +144,17 @@ public: if(DeallocateC) assert(false); mState.mIndex-=n; return *this; } - inline reference operator*() const { return *mState.mData; } - inline pointer operator->() const { return mState.mData; } - inline reference operator[](const difference_type &n) const { - if(DeallocateC) assert(false); - DeallocatingVectorIteratorState _myState(mState); - _myState.mIndex += n; - _myState.setPointerForIndex; - return _myState.mData; + + inline reference operator*() const { + std::size_t _bucket = mState.mIndex/bucketSizeC; + std::size_t _index = mState.mIndex%bucketSizeC; + return (mState.mBucketList[_bucket][_index]); + } + + inline pointer operator->() const { + std::size_t _bucket = mState.mIndex/bucketSizeC; + std::size_t _index = mState.mIndex%bucketSizeC; + return &(mState.mBucketList[_bucket][_index]); } inline bool operator!=(const DeallocatingVectorIterator & other) {