[5d28a19] | 1 | #include <containers/vector>
|
---|
[385c130] | 2 |
|
---|
[16cfd8c] | 3 | #include <stdlib>
|
---|
| 4 |
|
---|
[385c130] | 5 | //------------------------------------------------------------------------------
|
---|
| 6 | //Initialization
|
---|
| 7 | forall(otype T, otype allocator_t | allocator_c(T, allocator_t))
|
---|
[16cfd8c] | 8 | void ctor(vector(T, allocator_t) *const this)
|
---|
[385c130] | 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 | {
|
---|
[16cfd8c] | 17 | clear(this);
|
---|
[385c130] | 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 | {
|
---|
[16cfd8c] | 60 | free(this->storage);
|
---|
[385c130] | 61 | }
|
---|
| 62 |
|
---|
| 63 | forall(otype T)
|
---|
| 64 | inline void realloc(heap_allocator(T) *const this, size_t size)
|
---|
| 65 | {
|
---|
[1b5c81ed] | 66 | enum { GROWTH_RATE = 2 };
|
---|
[385c130] | 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 | }
|
---|