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