1 | #include <assert.h> |
---|
2 | #include <stdbool.h> |
---|
3 | #include <stddef.h> |
---|
4 | #include <stdlib.h> |
---|
5 | |
---|
6 | #define DESTROY(x) |
---|
7 | |
---|
8 | //------------------------------------------------------------------------------ |
---|
9 | //Declaration |
---|
10 | trait allocator_c(otype T, otype allocator_t) { |
---|
11 | void ctor(allocator_t*); |
---|
12 | void dtor(allocator_t*); |
---|
13 | void realloc(allocator_t*, size_t); |
---|
14 | T* data(allocator_t*); |
---|
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 vector_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 | 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 | 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 | 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 | inline T at(vector(T, allocator_t) *const this, size_t index) |
---|
56 | { |
---|
57 | // assert(index < this->size); |
---|
58 | return data(&this->storage)[index]; |
---|
59 | } |
---|
60 | |
---|
61 | forall(otype T, otype allocator_t | allocator_c(T, allocator_t)) |
---|
62 | 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 | 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 | inline T back(vector(T, allocator_t) *const this, size_t index) |
---|
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 | //Allocator |
---|
92 | forall(otype T) |
---|
93 | struct heap_allocator |
---|
94 | { |
---|
95 | T* storage; |
---|
96 | size_t capacity; |
---|
97 | }; |
---|
98 | |
---|
99 | forall(otype T) |
---|
100 | void ctor(heap_allocator(T) *const this); |
---|
101 | |
---|
102 | forall(otype T) |
---|
103 | void dtor(heap_allocator(T) *const this); |
---|
104 | |
---|
105 | forall(otype T) |
---|
106 | void realloc(heap_allocator(T) *const this, size_t size); |
---|
107 | |
---|
108 | forall(otype T) |
---|
109 | inline T* data(heap_allocator(T) *const this) |
---|
110 | { |
---|
111 | return this->storage; |
---|
112 | } |
---|