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