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