2012-05-23 15:22:33 -04:00
|
|
|
/*
|
|
|
|
open source routing machine
|
|
|
|
Copyright (C) Dennis Luxen, others 2010
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU AFFERO General Public License as published by
|
|
|
|
the Free Software Foundation; either version 3 of the License, or
|
|
|
|
any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Affero General Public License
|
|
|
|
along with this program; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
or see http://www.gnu.org/licenses/agpl.txt.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef DEALLOCATINGVECTOR_H_
|
|
|
|
#define DEALLOCATINGVECTOR_H_
|
|
|
|
|
|
|
|
#include <cassert>
|
|
|
|
#include <cstdlib>
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
#if __cplusplus > 199711L
|
|
|
|
#define DEALLOCATION_VECTOR_NULL_PTR nullptr
|
|
|
|
#else
|
|
|
|
#define DEALLOCATION_VECTOR_NULL_PTR NULL
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2013-02-12 09:24:35 -05:00
|
|
|
template<typename ElementT, size_t bucketSizeC = 8388608/sizeof(ElementT), bool DeallocateC = false>
|
2012-05-23 15:22:33 -04:00
|
|
|
class DeallocatingVectorIterator : public std::iterator<std::random_access_iterator_tag, ElementT> {
|
|
|
|
protected:
|
|
|
|
|
2012-07-23 10:12:22 -04:00
|
|
|
class DeallocatingVectorIteratorState {
|
|
|
|
private:
|
2012-05-23 15:22:33 -04:00
|
|
|
//make constructors explicit, so we do not mix random access and deallocation iterators.
|
2012-07-23 10:12:22 -04:00
|
|
|
DeallocatingVectorIteratorState();
|
|
|
|
public:
|
2012-05-23 15:22:33 -04:00
|
|
|
explicit DeallocatingVectorIteratorState(const DeallocatingVectorIteratorState &r) : mData(r.mData), mIndex(r.mIndex), mBucketList(r.mBucketList) {}
|
|
|
|
//explicit DeallocatingVectorIteratorState(const ElementT * ptr, const size_t idx, const std::vector<ElementT *> & input_list) : mData(ptr), mIndex(idx), mBucketList(input_list) {}
|
|
|
|
explicit DeallocatingVectorIteratorState(const size_t idx, std::vector<ElementT *> & input_list) : mData(DEALLOCATION_VECTOR_NULL_PTR), mIndex(idx), mBucketList(input_list) {
|
|
|
|
setPointerForIndex();
|
|
|
|
}
|
|
|
|
ElementT * mData;
|
|
|
|
size_t mIndex;
|
|
|
|
std::vector<ElementT *> & mBucketList;
|
2012-08-27 10:16:59 -04:00
|
|
|
|
2012-05-23 15:22:33 -04:00
|
|
|
inline void setPointerForIndex() {
|
|
|
|
if(bucketSizeC*mBucketList.size() <= mIndex) {
|
|
|
|
mData = DEALLOCATION_VECTOR_NULL_PTR;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
size_t _bucket = mIndex/bucketSizeC;
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool operator==(const DeallocatingVectorIteratorState &other) {
|
|
|
|
return (mData == other.mData) && (mIndex == other.mIndex) && (mBucketList == other.mBucketList);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool operator<(const DeallocatingVectorIteratorState &other) {
|
|
|
|
return mIndex < other.mIndex;
|
|
|
|
}
|
|
|
|
|
|
|
|
//This is a hack to make assignment operator possible with reference member
|
|
|
|
inline DeallocatingVectorIteratorState& operator= (const DeallocatingVectorIteratorState &a) {
|
|
|
|
if (this != &a) {
|
|
|
|
this->DeallocatingVectorIteratorState::~DeallocatingVectorIteratorState(); // explicit non-virtual destructor
|
|
|
|
new (this) DeallocatingVectorIteratorState(a); // placement new
|
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
DeallocatingVectorIteratorState mState;
|
|
|
|
|
|
|
|
public:
|
|
|
|
typedef std::random_access_iterator_tag iterator_category;
|
|
|
|
typedef typename std::iterator<std::random_access_iterator_tag, ElementT>::value_type value_type;
|
|
|
|
typedef typename std::iterator<std::random_access_iterator_tag, ElementT>::difference_type difference_type;
|
|
|
|
typedef typename std::iterator<std::random_access_iterator_tag, ElementT>::reference reference;
|
|
|
|
typedef typename std::iterator<std::random_access_iterator_tag, ElementT>::pointer pointer;
|
|
|
|
|
|
|
|
DeallocatingVectorIterator() {}
|
|
|
|
|
|
|
|
template<typename T2>
|
|
|
|
DeallocatingVectorIterator(const DeallocatingVectorIterator<T2> & r) : mState(r.mState) {}
|
2012-08-27 10:16:59 -04:00
|
|
|
|
2012-05-23 15:22:33 -04:00
|
|
|
DeallocatingVectorIterator(size_t idx, std::vector<ElementT *> & input_list) : mState(idx, input_list) {}
|
2012-08-27 10:16:59 -04:00
|
|
|
//DeallocatingVectorIterator(size_t idx, const std::vector<ElementT *> & input_list) : mState(idx, input_list) {}
|
2012-05-23 15:22:33 -04:00
|
|
|
DeallocatingVectorIterator(const DeallocatingVectorIteratorState & r) : mState(r) {}
|
|
|
|
|
|
|
|
template<typename T2>
|
|
|
|
DeallocatingVectorIterator& operator=(const DeallocatingVectorIterator<T2> &r) {
|
|
|
|
if(DeallocateC) assert(false);
|
|
|
|
mState = r.mState; return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline DeallocatingVectorIterator& operator++() { //prefix
|
|
|
|
// if(DeallocateC) assert(false);
|
|
|
|
++mState.mIndex; mState.setPointerForIndex(); return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline DeallocatingVectorIterator& operator--() { //prefix
|
|
|
|
if(DeallocateC) assert(false);
|
|
|
|
--mState.mIndex; mState.setPointerForIndex(); return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline DeallocatingVectorIterator operator++(int) { //postfix
|
|
|
|
DeallocatingVectorIteratorState _myState(mState);
|
2012-09-18 17:36:57 -04:00
|
|
|
mState.mIndex++; mState.setPointerForIndex();
|
2012-05-23 15:22:33 -04:00
|
|
|
return DeallocatingVectorIterator(_myState);
|
|
|
|
}
|
|
|
|
inline DeallocatingVectorIterator operator --(int) { //postfix
|
|
|
|
if(DeallocateC) assert(false);
|
|
|
|
DeallocatingVectorIteratorState _myState(mState);
|
2012-09-18 17:36:57 -04:00
|
|
|
mState.mIndex--; mState.setPointerForIndex();
|
2012-05-23 15:22:33 -04:00
|
|
|
return DeallocatingVectorIterator(_myState);
|
|
|
|
}
|
2012-11-22 09:41:29 -05:00
|
|
|
|
2012-05-23 15:22:33 -04:00
|
|
|
inline DeallocatingVectorIterator operator+(const difference_type& n) const {
|
|
|
|
DeallocatingVectorIteratorState _myState(mState);
|
|
|
|
_myState.mIndex+=n; _myState.setPointerForIndex();
|
|
|
|
return DeallocatingVectorIterator(_myState);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline DeallocatingVectorIterator& operator+=(const difference_type& n) const {
|
|
|
|
mState.mIndex+=n; return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline DeallocatingVectorIterator operator-(const difference_type& n) const {
|
|
|
|
if(DeallocateC) assert(false);
|
|
|
|
DeallocatingVectorIteratorState _myState(mState);
|
|
|
|
_myState.mIndex-=n; _myState.setPointerForIndex();
|
|
|
|
return DeallocatingVectorIterator(_myState);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline DeallocatingVectorIterator& operator-=(const difference_type &n) const {
|
|
|
|
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 bool operator!=(const DeallocatingVectorIterator & other) {
|
|
|
|
return mState != other.mState;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool operator==(const DeallocatingVectorIterator & other) {
|
|
|
|
return mState == other.mState;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool operator<(const DeallocatingVectorIterator & other) {
|
|
|
|
return mState < other.mState;
|
|
|
|
}
|
|
|
|
|
|
|
|
difference_type operator-(const DeallocatingVectorIterator & other) {
|
|
|
|
if(DeallocateC) assert(false);
|
|
|
|
return mState.mIndex-other.mState.mIndex;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-02-12 09:24:35 -05:00
|
|
|
template<typename ElementT, size_t bucketSizeC = 8388608/sizeof(ElementT) >
|
2012-05-23 15:22:33 -04:00
|
|
|
class DeallocatingVector {
|
|
|
|
private:
|
|
|
|
size_t mCurrentSize;
|
|
|
|
std::vector<ElementT *> mBucketList;
|
|
|
|
|
|
|
|
public:
|
2012-09-17 09:38:02 -04:00
|
|
|
typedef ElementT value_type;
|
2012-05-23 15:22:33 -04:00
|
|
|
typedef DeallocatingVectorIterator<ElementT, bucketSizeC, false> iterator;
|
|
|
|
typedef DeallocatingVectorIterator<ElementT, bucketSizeC, false> const_iterator;
|
|
|
|
|
|
|
|
//this iterator deallocates all buckets that have been visited. Iterators to visited objects become invalid.
|
|
|
|
typedef DeallocatingVectorIterator<ElementT, bucketSizeC, true> deallocation_iterator;
|
|
|
|
|
|
|
|
DeallocatingVector() : mCurrentSize(0) {
|
|
|
|
//initial bucket
|
|
|
|
mBucketList.push_back(new ElementT[bucketSizeC]);
|
|
|
|
}
|
|
|
|
|
|
|
|
~DeallocatingVector() {
|
|
|
|
clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void swap(DeallocatingVector<ElementT, bucketSizeC> & other) {
|
|
|
|
std::swap(mCurrentSize, other.mCurrentSize);
|
|
|
|
mBucketList.swap(other.mBucketList);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void clear() {
|
|
|
|
//Delete[]'ing ptr's to all Buckets
|
|
|
|
for(unsigned i = 0; i < mBucketList.size(); ++i) {
|
|
|
|
if(DEALLOCATION_VECTOR_NULL_PTR != mBucketList[i]) {
|
2012-11-22 09:41:29 -05:00
|
|
|
delete[] mBucketList[i];
|
|
|
|
mBucketList[i] = DEALLOCATION_VECTOR_NULL_PTR;
|
2012-05-23 15:22:33 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
//Removing all ptrs from vector
|
|
|
|
std::vector<ElementT *>().swap(mBucketList);
|
2012-11-22 09:41:29 -05:00
|
|
|
mCurrentSize = 0;
|
2012-05-23 15:22:33 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
inline void push_back(const ElementT & element) {
|
|
|
|
size_t _capacity = capacity();
|
|
|
|
if(mCurrentSize == _capacity) {
|
|
|
|
mBucketList.push_back(new ElementT[bucketSizeC]);
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t _index = size()%bucketSizeC;
|
|
|
|
mBucketList.back()[_index] = element;
|
|
|
|
++mCurrentSize;
|
|
|
|
}
|
|
|
|
|
2012-09-19 05:46:41 -04:00
|
|
|
inline void reserve(const size_t) const {
|
2012-08-27 10:16:59 -04:00
|
|
|
//don't do anything
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void resize(const size_t new_size) {
|
|
|
|
if(new_size > mCurrentSize) {
|
|
|
|
while(capacity() < new_size) {
|
|
|
|
mBucketList.push_back(new ElementT[bucketSizeC]);
|
|
|
|
}
|
|
|
|
mCurrentSize = new_size;
|
|
|
|
}
|
|
|
|
if(new_size < mCurrentSize) {
|
|
|
|
size_t number_of_necessary_buckets = 1+(new_size / bucketSizeC);
|
|
|
|
|
|
|
|
for(unsigned i = number_of_necessary_buckets; i < mBucketList.size(); ++i) {
|
|
|
|
delete[] mBucketList[i];
|
|
|
|
}
|
|
|
|
mBucketList.resize(number_of_necessary_buckets);
|
|
|
|
mCurrentSize = new_size;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-05-23 15:22:33 -04:00
|
|
|
inline size_t size() const {
|
|
|
|
return mCurrentSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline size_t capacity() const {
|
|
|
|
return mBucketList.size() * bucketSizeC;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline iterator begin() {
|
2012-09-19 05:46:41 -04:00
|
|
|
return iterator(static_cast<size_t>(0), mBucketList);
|
2012-05-23 15:22:33 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
inline iterator end() {
|
|
|
|
return iterator(size(), mBucketList);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline deallocation_iterator dbegin() {
|
2012-09-19 05:46:41 -04:00
|
|
|
return deallocation_iterator(static_cast<size_t>(0), mBucketList);
|
2012-05-23 15:22:33 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
inline deallocation_iterator dend() {
|
|
|
|
return deallocation_iterator(size(), mBucketList);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline const_iterator begin() const {
|
2012-09-19 05:46:41 -04:00
|
|
|
return const_iterator(static_cast<size_t>(0), mBucketList);
|
2012-05-23 15:22:33 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
inline const_iterator end() const {
|
|
|
|
return const_iterator(size(), mBucketList);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline ElementT & operator[](const size_t index) {
|
|
|
|
size_t _bucket = index / bucketSizeC;
|
|
|
|
size_t _index = index % bucketSizeC;
|
|
|
|
return (mBucketList[_bucket][_index]);
|
|
|
|
}
|
|
|
|
|
|
|
|
const inline ElementT & operator[](const size_t index) const {
|
|
|
|
size_t _bucket = index / bucketSizeC;
|
|
|
|
size_t _index = index % bucketSizeC;
|
|
|
|
return (mBucketList[_bucket][_index]);
|
|
|
|
}
|
2013-01-27 08:18:54 -05:00
|
|
|
|
|
|
|
inline ElementT & back() {
|
|
|
|
size_t _bucket = mCurrentSize / bucketSizeC;
|
|
|
|
size_t _index = mCurrentSize % bucketSizeC;
|
|
|
|
return (mBucketList[_bucket][_index]);
|
|
|
|
}
|
|
|
|
|
|
|
|
const inline ElementT & back() const {
|
|
|
|
size_t _bucket = mCurrentSize / bucketSizeC;
|
|
|
|
size_t _index = mCurrentSize % bucketSizeC;
|
|
|
|
return (mBucketList[_bucket][_index]);
|
|
|
|
}
|
2012-05-23 15:22:33 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif /* DEALLOCATINGVECTOR_H_ */
|