[a772d8ab] | 1 | #pragma once |
---|
| 2 | |
---|
| 3 | #include <stdlib> |
---|
| 4 | |
---|
| 5 | #define DESTROY(x) |
---|
| 6 | |
---|
| 7 | //------------------------------------------------------------------------------ |
---|
| 8 | //Declaration |
---|
| 9 | trait allocator_c(otype T, otype allocator_t) |
---|
| 10 | { |
---|
| 11 | void ctor(allocator_t* const); |
---|
| 12 | void dtor(allocator_t* const); |
---|
| 13 | void realloc(allocator_t* const, size_t); |
---|
| 14 | T* data(allocator_t* const); |
---|
| 15 | }; |
---|
| 16 | |
---|
| 17 | forall(otype T, otype allocator_t | allocator_c(T, allocator_t)) |
---|
| 18 | struct vector |
---|
| 19 | { |
---|
| 20 | allocator_t storage; |
---|
| 21 | size_t size; |
---|
| 22 | }; |
---|
| 23 | |
---|
| 24 | //------------------------------------------------------------------------------ |
---|
| 25 | //Initialization |
---|
| 26 | forall(otype T, otype allocator_t | allocator_c(T, allocator_t)) |
---|
| 27 | void ctor(vector(T, allocator_t) *const this); |
---|
| 28 | |
---|
| 29 | forall(otype T, otype allocator_t | allocator_c(T, allocator_t)) |
---|
| 30 | void dtor(vector(T, allocator_t) *const this); |
---|
| 31 | |
---|
| 32 | //------------------------------------------------------------------------------ |
---|
| 33 | //Capacity |
---|
| 34 | forall(otype T, otype allocator_t | allocator_c(T, allocator_t)) |
---|
| 35 | static inline bool empty(vector(T, allocator_t) *const this) |
---|
| 36 | { |
---|
| 37 | return this->size == 0; |
---|
| 38 | } |
---|
| 39 | |
---|
| 40 | forall(otype T, otype allocator_t | allocator_c(T, allocator_t)) |
---|
| 41 | static inline bool size(vector(T, allocator_t) *const this) |
---|
| 42 | { |
---|
| 43 | return this->size; |
---|
| 44 | } |
---|
| 45 | |
---|
| 46 | forall(otype T, otype allocator_t | allocator_c(T, allocator_t)) |
---|
| 47 | static inline void reserve(vector(T, allocator_t) *const this, size_t size) |
---|
| 48 | { |
---|
| 49 | realloc(&this->storage, this->size+1); |
---|
| 50 | } |
---|
| 51 | |
---|
| 52 | //------------------------------------------------------------------------------ |
---|
| 53 | //Element access |
---|
| 54 | forall(otype T, otype allocator_t | allocator_c(T, allocator_t)) |
---|
| 55 | static inline T at(vector(T, allocator_t) *const this, size_t index) |
---|
| 56 | { |
---|
| 57 | return data(&this->storage)[index]; |
---|
| 58 | } |
---|
| 59 | |
---|
| 60 | forall(otype T, otype allocator_t | allocator_c(T, allocator_t)) |
---|
| 61 | static inline T ?[?](vector(T, allocator_t) *const this, size_t index) |
---|
| 62 | { |
---|
| 63 | return data(&this->storage)[index]; |
---|
| 64 | } |
---|
| 65 | |
---|
| 66 | forall(otype T, otype allocator_t | allocator_c(T, allocator_t)) |
---|
| 67 | static inline T front(vector(T, allocator_t) *const this) |
---|
| 68 | { |
---|
| 69 | return data(&this->storage)[0]; |
---|
| 70 | } |
---|
| 71 | |
---|
| 72 | forall(otype T, otype allocator_t | allocator_c(T, allocator_t)) |
---|
| 73 | static inline T back(vector(T, allocator_t) *const this) |
---|
| 74 | { |
---|
| 75 | return data(&this->storage)[this->size - 1]; |
---|
| 76 | } |
---|
| 77 | |
---|
| 78 | //------------------------------------------------------------------------------ |
---|
| 79 | //Modifiers |
---|
| 80 | forall(otype T, otype allocator_t | allocator_c(T, allocator_t)) |
---|
| 81 | void push_back(vector(T, allocator_t) *const this, T value); |
---|
| 82 | |
---|
| 83 | forall(otype T, otype allocator_t | allocator_c(T, allocator_t)) |
---|
| 84 | void pop_back(vector(T, allocator_t) *const this); |
---|
| 85 | |
---|
| 86 | forall(otype T, otype allocator_t | allocator_c(T, allocator_t)) |
---|
| 87 | void clear(vector(T, allocator_t) *const this); |
---|
| 88 | |
---|
| 89 | //------------------------------------------------------------------------------ |
---|
| 90 | //Iterators |
---|
| 91 | forall(otype T, otype allocator_t | allocator_c(T, allocator_t)) |
---|
| 92 | static inline T* begin(vector(T, allocator_t) *const this) |
---|
| 93 | { |
---|
| 94 | return data(&this->storage); |
---|
| 95 | } |
---|
| 96 | |
---|
| 97 | forall(otype T, otype allocator_t | allocator_c(T, allocator_t)) |
---|
| 98 | static inline const T* cbegin(const vector(T, allocator_t) *const this) |
---|
| 99 | { |
---|
| 100 | return data(&this->storage); |
---|
| 101 | } |
---|
| 102 | |
---|
| 103 | forall(otype T, otype allocator_t | allocator_c(T, allocator_t)) |
---|
| 104 | static inline T* end(vector(T, allocator_t) *const this) |
---|
| 105 | { |
---|
| 106 | return data(&this->storage) + this->size; |
---|
| 107 | } |
---|
| 108 | |
---|
| 109 | forall(otype T, otype allocator_t | allocator_c(T, allocator_t)) |
---|
| 110 | static inline const T* cend(const vector(T, allocator_t) *const this) |
---|
| 111 | { |
---|
| 112 | return data(&this->storage) + this->size; |
---|
| 113 | } |
---|
| 114 | |
---|
| 115 | //------------------------------------------------------------------------------ |
---|
| 116 | //Allocator |
---|
| 117 | forall(otype T) |
---|
| 118 | struct heap_allocator |
---|
| 119 | { |
---|
| 120 | T* storage; |
---|
| 121 | size_t capacity; |
---|
| 122 | }; |
---|
| 123 | |
---|
| 124 | forall(otype T) |
---|
| 125 | void ctor(heap_allocator(T) *const this); |
---|
| 126 | |
---|
| 127 | forall(otype T) |
---|
| 128 | void dtor(heap_allocator(T) *const this); |
---|
| 129 | |
---|
| 130 | forall(otype T) |
---|
| 131 | void realloc(heap_allocator(T) *const this, size_t size); |
---|
| 132 | |
---|
| 133 | forall(otype T) |
---|
| 134 | static inline T* data(heap_allocator(T) *const this) |
---|
| 135 | { |
---|
| 136 | return this->storage; |
---|
| 137 | } |
---|