12 #ifndef SMALLVEC_TYPE_HPP 13 #define SMALLVEC_TYPE_HPP 28 template <
typename T, u
int S>
36 SmallVector() : data(NULL), items(0), capacity(0) { }
89 if ((
const void *)&other == (
void *)
this)
return;
122 uint capacity =
Align(this->items, S);
123 if (capacity >= this->capacity)
return;
126 this->data =
ReallocT(this->data, this->capacity);
136 uint begin = this->
items;
137 this->items += to_add;
139 if (this->items > this->capacity) {
140 this->capacity =
Align(this->items, S);
141 this->data =
ReallocT(this->data, this->capacity);
144 return &this->data[begin];
153 this->items = num_items;
155 if (this->items > this->capacity) {
156 this->capacity =
Align(this->items, S);
157 this->data =
ReallocT(this->data, this->capacity);
168 assert(item >= this->
Begin() && item <= this->
End());
170 size_t to_move = this->
End() - item;
171 size_t start = item - this->
Begin();
175 return this->
Begin() + start;
184 inline const T *
Find(
const T &item)
const 186 const T *pos = this->
Begin();
187 const T *end = this->
End();
188 while (pos != end && *pos != item) pos++;
200 T *pos = this->
Begin();
201 const T *end = this->
End();
202 while (pos != end && *pos != item) pos++;
215 const T *pos = this->
Begin();
216 const T *end = this->
End();
217 while (pos != end && *pos != item) {
221 return pos == end ? -1 : index;
232 return this->
Find(item) != this->
End();
242 assert(item >= this->
Begin() && item < this->
End());
243 *item = this->data[--this->
items];
263 if (count == 0)
return;
264 assert(item >= this->
Begin());
265 assert(item + count <= this->
End());
267 this->items -= count;
268 ptrdiff_t to_move = this->
End() - item;
269 if (to_move > 0)
MemMoveT(item, item + count, to_move);
280 bool is_member = this->
Contains(item);
281 if (!is_member) *this->
Append() = item;
320 inline const T *
End()
const 322 return &this->data[this->
items];
332 return &this->data[this->
items];
341 inline const T *
Get(uint index)
const 344 assert(index <= this->items);
345 return &this->data[index];
357 assert(index <= this->items);
358 return &this->data[index];
369 assert(index < this->items);
370 return this->data[index];
381 assert(index < this->items);
382 return this->data[index];
397 template <
typename T, u
int S>
410 for (uint i = 0; i < this->
items; i++) {
428 template <
typename T, u
int S>
441 for (uint i = 0; i < this->
items; i++) {
442 delete this->
data[i];
void ErasePreservingOrder(uint pos, uint count=1)
Remove items from the vector while preserving the order of other items.
void Reset()
Remove all items from the list and free allocated memory.
void Clear()
Remove all items from the list.
void Compact()
Compact the list down to the smallest block size boundary.
const T * Begin() const
Get the pointer to the first item (const)
Simple vector template class.
const T & operator[](uint index) const
Get item "number" (const)
const T * End() const
Get the pointer behind the last valid item (const)
T * Append(uint to_add=1)
Append an item and return it.
static void MemMoveT(T *destination, const T *source, size_t num=1)
Type-safe version of memmove().
void Resize(uint num_items)
Set the size of the vector, effectively truncating items from the end or appending uninitialised ones...
SmallVector & operator=(const SmallVector< T, X > &other)
Generic assignment.
uint capacity
The available space for storing items.
uint Length() const
Get the number of items in the list.
static T Align(const T x, uint n)
Return the smallest multiple of n equal or greater than x.
bool Contains(const T &item) const
Tests whether a item is present in the vector.
Functions related to the allocation of memory.
SmallVector(const SmallVector &other)
Copy constructor.
Simple vector template class, with automatic delete.
T * data
The pointer to the first item.
static T * ReallocT(T *t_ptr, size_t num_elements)
Simplified reallocation function that allocates the specified number of elements of the given type...
void Assign(const SmallVector< T, X > &other)
Assign items from other vector.
const T * Find(const T &item) const
Search for the first occurrence of an item.
SmallVector(const SmallVector< T, X > &other)
Generic copy constructor.
AutoFreeSmallVector< char *, 4 > StringList
Type for a list of strings.
Simple vector template class, with automatic free.
void Clear()
Remove all items from the list.
int FindIndex(const T &item) const
Search for the first occurrence of an item.
bool Include(const T &item)
Tests whether a item is present in the vector, and appends it to the end if not.
void Erase(T *item)
Removes given item from this vector.
void Clear()
Remove all items from the list.
void ErasePreservingOrder(T *item, uint count=1)
Remove items from the vector while preserving the order of other items.
static void free(const void *ptr)
Version of the standard free that accepts const pointers.
const T * Get(uint index) const
Get the pointer to item "number" (const)
T * Insert(T *item)
Insert a new item at a specific position into the vector, moving all following items.
Functions related to memory operations.
SmallVector & operator=(const SmallVector &other)
Assignment.
uint items
The number of items stored.