| 1 | #include <containers/vector> 
 | 
|---|
| 2 | 
 | 
|---|
| 3 | #include <stdlib>
 | 
|---|
| 4 | 
 | 
|---|
| 5 | //------------------------------------------------------------------------------
 | 
|---|
| 6 | //Initialization
 | 
|---|
| 7 | forall(otype T, otype allocator_t | allocator_c(T, allocator_t))
 | 
|---|
| 8 | void ctor(vector(T, allocator_t) *const this)
 | 
|---|
| 9 | {
 | 
|---|
| 10 |         ctor(&this->storage);
 | 
|---|
| 11 |         this->size = 0;
 | 
|---|
| 12 | }
 | 
|---|
| 13 | 
 | 
|---|
| 14 | forall(otype T, otype allocator_t | allocator_c(T, allocator_t))
 | 
|---|
| 15 | void dtor(vector(T, allocator_t) *const this)
 | 
|---|
| 16 | {
 | 
|---|
| 17 |         clear(this);
 | 
|---|
| 18 |         dtor(&this->storage);
 | 
|---|
| 19 | }
 | 
|---|
| 20 | 
 | 
|---|
| 21 | //------------------------------------------------------------------------------
 | 
|---|
| 22 | //Modifiers
 | 
|---|
| 23 | forall(otype T, otype allocator_t | allocator_c(T, allocator_t))
 | 
|---|
| 24 | void push_back(vector(T, allocator_t) *const this, T value)
 | 
|---|
| 25 | {
 | 
|---|
| 26 |         realloc(&this->storage, this->size+1);
 | 
|---|
| 27 |         data(&this->storage)[this->size] = value;
 | 
|---|
| 28 |         this->size++;
 | 
|---|
| 29 | }
 | 
|---|
| 30 | 
 | 
|---|
| 31 | forall(otype T, otype allocator_t | allocator_c(T, allocator_t))
 | 
|---|
| 32 | void pop_back(vector(T, allocator_t) *const this)
 | 
|---|
| 33 | {
 | 
|---|
| 34 |         this->size--;
 | 
|---|
| 35 |         DESTROY(data(&this->storage)[this->size]);
 | 
|---|
| 36 | }
 | 
|---|
| 37 | 
 | 
|---|
| 38 | forall(otype T, otype allocator_t | allocator_c(T, allocator_t))
 | 
|---|
| 39 | void clear(vector(T, allocator_t) *const this)
 | 
|---|
| 40 | {
 | 
|---|
| 41 |         for(size_t i = 0; i < this->size; i++)
 | 
|---|
| 42 |         {
 | 
|---|
| 43 |                 DESTROY(data(&this->storage)[this->size]);
 | 
|---|
| 44 |         }
 | 
|---|
| 45 |         this->size = 0;
 | 
|---|
| 46 | }
 | 
|---|
| 47 | 
 | 
|---|
| 48 | //------------------------------------------------------------------------------
 | 
|---|
| 49 | //Allocator
 | 
|---|
| 50 | forall(otype T)
 | 
|---|
| 51 | void ctor(heap_allocator(T) *const this)
 | 
|---|
| 52 | {
 | 
|---|
| 53 |         this->storage = 0;
 | 
|---|
| 54 |         this->capacity = 0;
 | 
|---|
| 55 | }
 | 
|---|
| 56 | 
 | 
|---|
| 57 | forall(otype T)
 | 
|---|
| 58 | void dtor(heap_allocator(T) *const this)
 | 
|---|
| 59 | {
 | 
|---|
| 60 |         free(this->storage);
 | 
|---|
| 61 | }
 | 
|---|
| 62 | 
 | 
|---|
| 63 | forall(otype T)
 | 
|---|
| 64 | inline void realloc(heap_allocator(T) *const this, size_t size)
 | 
|---|
| 65 | {
 | 
|---|
| 66 |         enum { GROWTH_RATE = 2 };
 | 
|---|
| 67 |         if(size > this->capacity)
 | 
|---|
| 68 |         {
 | 
|---|
| 69 |                 this->capacity = GROWTH_RATE * size;
 | 
|---|
| 70 |                 this->storage = (T*)realloc((void*)this->storage, this->capacity * sizeof(T));
 | 
|---|
| 71 |         }
 | 
|---|
| 72 | }
 | 
|---|