1 | #include "vector.h" |
---|
2 | |
---|
3 | //------------------------------------------------------------------------------ |
---|
4 | //Initialization |
---|
5 | forall(otype T, otype allocator_t | allocator_c(T, allocator_t)) |
---|
6 | void vector_ctor(vector(T, allocator_t) *const this) |
---|
7 | { |
---|
8 | ctor(&this->storage); |
---|
9 | this->size = 0; |
---|
10 | } |
---|
11 | |
---|
12 | forall(otype T, otype allocator_t | allocator_c(T, allocator_t)) |
---|
13 | void dtor(vector(T, allocator_t) *const this) |
---|
14 | { |
---|
15 | dtor(&this->storage); |
---|
16 | } |
---|
17 | |
---|
18 | //------------------------------------------------------------------------------ |
---|
19 | //Modifiers |
---|
20 | forall(otype T, otype allocator_t | allocator_c(T, allocator_t)) |
---|
21 | void push_back(vector(T, allocator_t) *const this, T value) |
---|
22 | { |
---|
23 | realloc(&this->storage, this->size+1); |
---|
24 | data(&this->storage)[this->size] = value; |
---|
25 | this->size++; |
---|
26 | } |
---|
27 | |
---|
28 | forall(otype T, otype allocator_t | allocator_c(T, allocator_t)) |
---|
29 | void pop_back(vector(T, allocator_t) *const this) |
---|
30 | { |
---|
31 | this->size--; |
---|
32 | DESTROY(data(&this->storage)[this->size]); |
---|
33 | } |
---|
34 | |
---|
35 | forall(otype T, otype allocator_t | allocator_c(T, allocator_t)) |
---|
36 | void clear(vector(T, allocator_t) *const this) |
---|
37 | { |
---|
38 | for(size_t i = 0; i < this->size; i++) |
---|
39 | { |
---|
40 | DESTROY(data(&this->storage)[this->size]); |
---|
41 | } |
---|
42 | this->size = 0; |
---|
43 | } |
---|
44 | |
---|
45 | //------------------------------------------------------------------------------ |
---|
46 | //Allocator |
---|
47 | forall(otype T) |
---|
48 | void ctor(heap_allocator(T) *const this) |
---|
49 | { |
---|
50 | this->storage = 0; |
---|
51 | this->capacity = 0; |
---|
52 | } |
---|
53 | |
---|
54 | forall(otype T) |
---|
55 | void dtor(heap_allocator(T) *const this) |
---|
56 | { |
---|
57 | free((void*)this->storage); |
---|
58 | } |
---|
59 | |
---|
60 | forall(otype T) |
---|
61 | inline void realloc(heap_allocator(T) *const this, size_t size) |
---|
62 | { |
---|
63 | static const size_t GROWTH_RATE = 2; |
---|
64 | if(size > this->capacity) |
---|
65 | { |
---|
66 | this->capacity = GROWTH_RATE * size; |
---|
67 | this->storage = (T*)realloc((void*)this->storage, this->capacity * sizeof(T)); |
---|
68 | } |
---|
69 | } |
---|