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