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 | context allocator_c(type T, type allocator_t) { |
---|
11 | void realloc(allocator_t*, size_t); |
---|
12 | T* data(allocator_t*); |
---|
13 | }; |
---|
14 | |
---|
15 | forall(type T, type allocator_t | allocator_c(T, allocator_t)) |
---|
16 | struct vector |
---|
17 | { |
---|
18 | allocator_t storage; |
---|
19 | size_t size; |
---|
20 | }; |
---|
21 | |
---|
22 | //------------------------------------------------------------------------------ |
---|
23 | //Capacity |
---|
24 | forall(type T, type allocator_t | allocator_c(T, allocator_t)) |
---|
25 | bool empty(vector(T, allocator_t)* this) |
---|
26 | { |
---|
27 | return this->size == 0; |
---|
28 | } |
---|
29 | |
---|
30 | forall(type T, type allocator_t | allocator_c(T, allocator_t)) |
---|
31 | bool size(vector(T, allocator_t)* this) |
---|
32 | { |
---|
33 | return this->size; |
---|
34 | } |
---|
35 | |
---|
36 | forall(type T, type allocator_t | allocator_c(T, allocator_t)) |
---|
37 | void reserve(vector(T, allocator_t)* this, size_t size) |
---|
38 | { |
---|
39 | realloc(&this->storage, this->size+1); |
---|
40 | } |
---|
41 | |
---|
42 | //------------------------------------------------------------------------------ |
---|
43 | //Element access |
---|
44 | forall(type T, type allocator_t | allocator_c(T, allocator_t)) |
---|
45 | T at(vector(T, allocator_t)* this, size_t index) |
---|
46 | { |
---|
47 | //assert(index < this->size); |
---|
48 | return data(&this->storage)[index]; |
---|
49 | } |
---|
50 | |
---|
51 | forall(type T, type allocator_t | allocator_c(T, allocator_t)) |
---|
52 | T ?[?](vector(T, allocator_t)* this, size_t index) |
---|
53 | { |
---|
54 | return data(&this->storage)[index]; |
---|
55 | } |
---|
56 | |
---|
57 | forall(type T, type allocator_t | allocator_c(T, allocator_t)) |
---|
58 | T front(vector(T, allocator_t)* this) |
---|
59 | { |
---|
60 | return data(&this->storage)[0]; |
---|
61 | } |
---|
62 | |
---|
63 | forall(type T, type allocator_t | allocator_c(T, allocator_t)) |
---|
64 | T back(vector(T, allocator_t)* this, size_t index) |
---|
65 | { |
---|
66 | return data(&this->storage)[this->size - 1]; |
---|
67 | } |
---|
68 | |
---|
69 | //------------------------------------------------------------------------------ |
---|
70 | //Modifiers |
---|
71 | forall(type T, type allocator_t | allocator_c(T, allocator_t)) |
---|
72 | void push_back(vector(T, allocator_t)* this, T value) |
---|
73 | { |
---|
74 | realloc(&this->storage, this->size+1); |
---|
75 | data(&this->storage)[this->size] = value; |
---|
76 | this->size++; |
---|
77 | } |
---|
78 | |
---|
79 | forall(type T, type allocator_t | allocator_c(T, allocator_t)) |
---|
80 | void pop_back(vector(T, allocator_t)* this) |
---|
81 | { |
---|
82 | this->size--; |
---|
83 | DESTROY(data(&this->storage)[this->size]); |
---|
84 | } |
---|
85 | |
---|
86 | forall(type T, type allocator_t | allocator_c(T, allocator_t)) |
---|
87 | void clear(vector(T, allocator_t)* this) |
---|
88 | { |
---|
89 | for(size_t i = 0; i < this->size; i++) |
---|
90 | { |
---|
91 | DESTROY(data(&this->storage)[this->size]); |
---|
92 | } |
---|
93 | this->size = 0; |
---|
94 | } |
---|
95 | |
---|
96 | //------------------------------------------------------------------------------ |
---|
97 | //Allocator |
---|
98 | forall(type T) |
---|
99 | struct heap_allocator |
---|
100 | { |
---|
101 | T* storage; |
---|
102 | size_t capacity; |
---|
103 | }; |
---|
104 | |
---|
105 | forall(type T) |
---|
106 | void realloc(heap_allocator(T)* this, size_t size) |
---|
107 | { |
---|
108 | static const size_t GROWTH_RATE = 2; |
---|
109 | if(size > this->capacity) |
---|
110 | { |
---|
111 | this->capacity = GROWTH_RATE * size; |
---|
112 | this->storage = (T*)realloc((void*)this->storage, this->capacity * sizeof(T)); |
---|
113 | } |
---|
114 | } |
---|
115 | |
---|
116 | forall(type T) |
---|
117 | T* data(heap_allocator(T)* this) |
---|
118 | { |
---|
119 | return this->storage; |
---|
120 | } |
---|