peter1138@10205: /* $Id$ */ peter1138@10205: peter1138@10683: /** @file smallvec.h Simple vector class that allows allocating an item without the need to copy this->data needlessly. */ peter1138@10205: peter1138@10205: #ifndef SMALLVEC_H peter1138@10205: #define SMALLVEC_H peter1138@10205: rubidium@10791: #include "../core/alloc_func.hpp" rubidium@10791: #include "../core/math_func.hpp" rubidium@10791: peter1138@10683: template peter1138@10683: struct SmallVector { peter1138@10205: T *data; peter1138@10205: uint items; peter1138@10205: uint capacity; peter1138@10205: peter1138@10205: SmallVector() : data(NULL), items(0), capacity(0) { } peter1138@10205: peter1138@10205: ~SmallVector() peter1138@10205: { peter1138@10683: free(this->data); peter1138@10205: } peter1138@10205: peter1138@10205: /** peter1138@10697: * Remove all items from the list. peter1138@10697: */ peter1138@10697: void Clear() peter1138@10697: { peter1138@10697: /* In fact we just reset the item counter avoiding the need to peter1138@10697: * probably reallocate the same amount of memory the list was peter1138@10697: * previously using. */ peter1138@10697: this->items = 0; peter1138@10697: } peter1138@10697: peter1138@10697: /** peter1138@10697: * Compact the list down to the smallest block size boundary. peter1138@10697: */ peter1138@10697: void Compact() peter1138@10697: { peter1138@10697: uint capacity = Align(this->items, S); peter1138@10697: if (capacity >= this->capacity) return; peter1138@10697: peter1138@10697: this->capacity = capacity; peter1138@10697: this->data = ReallocT(this->data, this->capacity); peter1138@10697: } peter1138@10697: peter1138@10697: /** peter1138@10205: * Append an item and return it. peter1138@10205: */ peter1138@10205: T *Append() peter1138@10205: { peter1138@10683: if (this->items == this->capacity) { peter1138@10683: this->capacity += S; peter1138@10683: this->data = ReallocT(this->data, this->capacity); peter1138@10205: } peter1138@10205: peter1138@10683: return &this->data[this->items++]; peter1138@10205: } peter1138@10205: peter1138@10697: /** peter1138@10697: * Get the number of items in the list. peter1138@10697: */ peter1138@10697: uint Length() const peter1138@10697: { peter1138@10697: return this->items; peter1138@10697: } peter1138@10697: peter1138@10205: const T *Begin() const peter1138@10205: { peter1138@10683: return this->data; peter1138@10205: } peter1138@10205: rubidium@10207: T *Begin() rubidium@10207: { peter1138@10683: return this->data; rubidium@10207: } rubidium@10207: peter1138@10205: const T *End() const peter1138@10205: { peter1138@10683: return &this->data[this->items]; peter1138@10205: } rubidium@10207: rubidium@10207: T *End() rubidium@10207: { peter1138@10683: return &this->data[this->items]; rubidium@10207: } rubidium@10208: peter1138@10683: const T *Get(uint index) const rubidium@10208: { peter1138@10683: return &this->data[index]; rubidium@10208: } rubidium@10208: peter1138@10683: T *Get(uint index) rubidium@10208: { peter1138@10683: return &this->data[index]; rubidium@10208: } peter1138@10697: peter1138@10697: const T &operator[](uint index) const peter1138@10697: { peter1138@10697: return this->data[index]; peter1138@10697: } peter1138@10697: peter1138@10697: T &operator[](uint index) peter1138@10697: { peter1138@10697: return this->data[index]; peter1138@10697: } peter1138@10205: }; peter1138@10205: peter1138@10205: #endif /* SMALLVEC_H */