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