47 typedef uint32_t UniqueIdType;
86 : container_(container), index_(index) {
87 unique_id_ = container->GetElement(index)->unique_id;
97 return container_ == other.container_ && index_ == other.index_;
116 return container_ !=
nullptr &&
117 (container_->GetElement(index_)->unique_id == unique_id_);
142 return &(element->
data);
169 return element->
data;
196 return IsValid() ? &(container_->GetElement(index_)->data) :
nullptr;
225 size_t index()
const {
return index_; }
235 UniqueIdType unique_id_;
248 template <
bool is_const>
249 class IteratorTemplate {
254 typedef typename std::conditional<is_const, const T&, T&>::type reference;
260 typedef typename std::conditional<is_const, const T*, T*>::type pointer;
271 : container_(container), index_(index) {}
286 return container_ == other.container_ && index_ == other.index_;
307 index_ = container_->elements_[index_].next;
326 index_ = container_->elements_[index_].prev;
344 reference
operator*() {
return *(container_->GetElementData(index_)); }
350 pointer
operator->() {
return container_->GetElementData(index_); }
365 size_t index()
const {
return index_; }
400 next = std::move(src.next);
401 prev = std::move(src.prev);
403 data = std::move(src.data);
413 next = std::move(src.next);
414 prev = std::move(src.prev);
416 data = std::move(src.data);
499 assert(index < elements_.size());
500 return (&elements_[index].data);
517 assert(index < elements_.size());
518 return (&elements_[index].data);
534 RemoveFromList(index);
536 index = elements_.size();
537 elements_.push_back(VectorPoolElement());
539 switch (alloc_location) {
552 elements_[index].data.~T();
553 new (&(elements_[index].data)) T;
554 elements_[index].unique_id = AllocateUniqueId();
555 return VectorPoolReference(
this, index);
567 assert(elements_[index].unique_id !=
kInvalidId);
571 elements_[index].data.~T();
572 new (&(elements_[index].data)) T;
574 RemoveFromList(index);
588 if (element.IsValid()) {
617 size_t Size()
const {
return elements_.size(); }
681 size_t current_size = elements_.size();
682 if (current_size >= new_size)
return;
684 elements_.resize(new_size);
685 for (; current_size < new_size; current_size++) {
686 elements_[current_size].unique_id =
kInvalidId;
700 void RemoveFromList(
size_t index) {
702 VectorPoolElement& element = elements_[index];
703 elements_[element.prev].next = element.next;
704 elements_[element.next].prev = element.prev;
715 void AddToListFront(
size_t index,
size_t start_index) {
717 VectorPoolElement& list_start = elements_[start_index];
718 elements_[list_start.next].prev = index;
719 elements_[index].prev = start_index;
720 elements_[index].next = list_start.next;
721 list_start.next = index;
732 void AddToListBack(
size_t index,
size_t end_index) {
734 VectorPoolElement& list_end = elements_[end_index];
735 elements_[list_end.prev].next = index;
736 elements_[index].next = end_index;
737 elements_[index].prev = list_end.prev;
738 list_end.prev = index;
752 VectorPoolElement* GetElement(
size_t index) {
753 return index < elements_.size() ? &elements_[index] :
nullptr;
767 const VectorPoolElement* GetElement(
size_t index)
const {
768 return index < elements_.size() ? &elements_[index] :
nullptr;
785 UniqueIdType AllocateUniqueId() {
787 UniqueIdType result = next_unique_id_;
789 if (next_unique_id_ ==
kInvalidId) next_unique_id_++;
793 std::vector<VectorPoolElement> elements_;
794 size_t active_count_;
795 UniqueIdType next_unique_id_;
801 #endif // VECTOR_POOL_H
VectorPoolReference(VectorPool< T > *container, size_t index)
Constructor for a VectorPoolReference given a VectorPool.
Definition: vector_pool.h:85
const T * ToPointer() const
Get a direct pointer to the element the VectorPoolReference is referring to.
Definition: vector_pool.h:210
IteratorTemplate< false > Iterator
A non-const IteratorTemplate.
Definition: vector_pool.h:56
VectorPool< T > * container() const
Gets a pointer to the underlying vector for this object.
Definition: vector_pool.h:230
Iterator begin()
Get an Iterator to the first active element in the VectorPool.
Definition: vector_pool.h:642
size_t index() const
Get the raw index into the underlying vector for this object.
Definition: vector_pool.h:225
pointer operator->()
Member access on the iterator.
Definition: vector_pool.h:350
static const size_t kLastFree
Used to demarcate the last element of our free list.
Definition: vector_pool.h:474
VectorPoolReference ToReference() const
Converts the Iterator into a VectorPoolReference, which is the preferred way for holding onto referen...
Definition: vector_pool.h:357
IteratorTemplate & operator++()
The prefix increment operator to move the iterator forward in the list.
Definition: vector_pool.h:306
IteratorTemplate & operator--()
The prefix decrement operator to move the iterator backward in the list.
Definition: vector_pool.h:325
static const size_t kFirstFree
Used to demarcate the first element of our free list.
Definition: vector_pool.h:466
VectorPoolReference GetNewElement(AllocationLocation alloc_location)
Get a VectorPoolReference to a new element.
Definition: vector_pool.h:530
IteratorTemplate(VectorPool< T > *container, size_t index)
Constructor for an IteratorTemplate to a given VectorPool index.
Definition: vector_pool.h:270
ConstIterator cbegin()
Gets a ConstIterator to the first active element in the VectorPool.
Definition: vector_pool.h:660
size_t active_count() const
Gets the total number of active elements.
Definition: vector_pool.h:623
VectorPoolElement()
The default constructor for an empty VectorPoolElement.
Definition: vector_pool.h:391
IteratorTemplate< true > ConstIterator
A const IteratorTemplate.
Definition: vector_pool.h:61
VectorPool()
The default constructor for an empty VectorPool.
Definition: vector_pool.h:483
size_t next
The index of the next element in the vector.
Definition: vector_pool.h:427
size_t index() const
Get the index into the VectorPool vector.
Definition: vector_pool.h:365
static const size_t kLastUsed
Used to demarcate the last element of our used list.
Definition: vector_pool.h:458
const T * operator->() const
Const member access operator.
Definition: vector_pool.h:154
size_t Size() const
Get the total size of the vector pool.
Definition: vector_pool.h:617
VectorPoolReference()
Default constructor for a VectorPoolReference.
Definition: vector_pool.h:79
Iterator FreeElement(Iterator iter)
Definition: vector_pool.h:604
VectorPoolElement & operator=(VectorPoolElement &&src)
The standard operator to move a referenced VectorPoolElement into this VectorPoolElement.
Definition: vector_pool.h:399
static const size_t kFirstUsed
Used to demarcate the first element of our used list.
Definition: vector_pool.h:450
bool operator!=(const VectorPoolReference &other) const
Standard inequality operator for VectorPoolReferences.
Definition: vector_pool.h:107
ConstIterator cend()
Gets a ConstIterator to the last active element in the VectorPool.
Definition: vector_pool.h:671
static const size_t kOutOfBounds
A sentinel value that represents an out-of-bounds index.
Definition: vector_pool.h:377
void FreeElement(VectorPoolReference element)
Frees a given element.
Definition: vector_pool.h:587
IteratorTemplate operator++(int)
The postfix increment operator to move the iterator forward in the list.
Definition: vector_pool.h:315
AllocationLocation
kAddToFront allocates from the front of the pool. kAddToBack allocates from the back of the pool...
Definition: vector_pool.h:34
Iterator end()
Gets an Iterator to the last active element in the VectorPool.
Definition: vector_pool.h:650
A pool allocator, implemented as a vector-based pair of linked lists.
Definition: vector_pool.h:43
UniqueIdType unique_id
The unique ID of this VectorPoolElement.
Definition: vector_pool.h:437
T data
Holds the data within a VectorPoolElement.
Definition: vector_pool.h:422
Iterator ToIterator() const
Get an iterator that points to the element referenced by the VectorPoolReference. ...
Definition: vector_pool.h:219
bool operator!=(const IteratorTemplate &other) const
The standard inequality operator to compare two IteratorTemplates.
Definition: vector_pool.h:298
static const size_t kTotalReserved
Used to indicate the number of reserved elements. (e.g. kFirstUsed, kLastUsed, kFirstFree, kLastFree).
Definition: vector_pool.h:480
const T * GetElementData(size_t index) const
Get the data at the given element index.
Definition: vector_pool.h:516
VectorPoolElement(VectorPoolElement &&src)
A copy constructor to create a VectorPoolElement from an existing VectorPoolElement.
Definition: vector_pool.h:412
A reference object for pointing into the vector pool. It acts as a pointer for vector pool elements a...
Definition: vector_pool.h:72
T * ToPointer()
Get a direct pointer to the element the VectorPoolReference is referring to.
Definition: vector_pool.h:195
T * operator->()
The member access operator.
Definition: vector_pool.h:139
~IteratorTemplate()
Destructor for an IteratorTemplate.
Definition: vector_pool.h:274
IteratorTemplate operator--(int)
The postfix decrement operator to move the iterator backward in the list.
Definition: vector_pool.h:334
bool operator==(const VectorPoolReference &other) const
Standard equality operator for VectorPoolReferences.
Definition: vector_pool.h:96
bool operator==(const IteratorTemplate &other) const
The standard equality operator to compare two IteratorTemplates.
Definition: vector_pool.h:285
void FreeElement(size_t index)
Frees an element at a given index.
Definition: vector_pool.h:566
static const UniqueIdType kInvalidId
A sentinel value that represents an invalid ID.
Definition: vector_pool.h:384
size_t prev
The index of the previous element in the vector.
Definition: vector_pool.h:432
An Iterator for the VectorPool.
Definition: vector_pool.h:51
void Reserve(size_t new_size)
Expands the vector until it is at least as large as new_size.
Definition: vector_pool.h:680
A struct representing an element inside of a VectorPool.
Definition: vector_pool.h:389
bool IsValid() const
Verifies that the reference is still valid.
Definition: vector_pool.h:115
T & operator*()
The dereference operator.
Definition: vector_pool.h:166
const T & operator*() const
The const dereference operator.
Definition: vector_pool.h:180
T * GetElementData(size_t index)
Get the data at the given element index.
Definition: vector_pool.h:498
reference operator*()
The dereference operator.
Definition: vector_pool.h:344
void Clear()
Clears out all of the elements in the VectorPool and resizes the underlying vector to the minimum siz...
Definition: vector_pool.h:627