source: src/examples/gc_no_raii/src/vector.h @ 385c130

aaron-thesisarm-ehcleanup-dtorsctordeferred_resndemanglergc_noraiijacob/cs343-translationjenkins-sandboxmemorynew-astnew-ast-unique-exprnew-envno_listpersistent-indexerresolv-newwith_gc
Last change on this file since 385c130 was 385c130, checked in by Thierry Delisle <tdelisle@…>, 6 years ago

pre merge

  • Property mode set to 100644
File size: 3.0 KB
Line 
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
10trait 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
17forall(otype T, otype allocator_t | allocator_c(T, allocator_t))
18struct vector
19{
20        allocator_t storage;
21        size_t size;
22};
23
24//------------------------------------------------------------------------------
25//Initialization
26forall(otype T, otype allocator_t | allocator_c(T, allocator_t))
27void vector_ctor(vector(T, allocator_t) *const this);
28
29forall(otype T, otype allocator_t | allocator_c(T, allocator_t))
30void dtor(vector(T, allocator_t) *const this);
31
32//------------------------------------------------------------------------------
33//Capacity
34forall(otype T, otype allocator_t | allocator_c(T, allocator_t))
35inline bool empty(vector(T, allocator_t) *const this)
36{
37        return this->size == 0;
38}
39
40forall(otype T, otype allocator_t | allocator_c(T, allocator_t))
41inline bool size(vector(T, allocator_t) *const this)
42{
43        return this->size;
44}
45
46forall(otype T, otype allocator_t | allocator_c(T, allocator_t))
47inline 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
54forall(otype T, otype allocator_t | allocator_c(T, allocator_t))
55inline 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
61forall(otype T, otype allocator_t | allocator_c(T, allocator_t))
62inline T ?[?](vector(T, allocator_t) *const this, size_t index)
63{
64        return data(&this->storage)[index];
65}
66
67forall(otype T, otype allocator_t | allocator_c(T, allocator_t))
68inline T front(vector(T, allocator_t) *const this)
69{
70        return data(&this->storage)[0];
71}
72
73forall(otype T, otype allocator_t | allocator_c(T, allocator_t))
74inline 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
81forall(otype T, otype allocator_t | allocator_c(T, allocator_t))
82void push_back(vector(T, allocator_t) *const this, T value);
83
84forall(otype T, otype allocator_t | allocator_c(T, allocator_t))
85void pop_back(vector(T, allocator_t) *const this);
86
87forall(otype T, otype allocator_t | allocator_c(T, allocator_t))
88void clear(vector(T, allocator_t) *const this);
89
90//------------------------------------------------------------------------------
91//Allocator
92forall(otype T)
93struct heap_allocator
94{
95        T* storage;
96        size_t capacity;
97};
98
99forall(otype T)
100void ctor(heap_allocator(T) *const this);
101
102forall(otype T)
103void dtor(heap_allocator(T) *const this);
104
105forall(otype T)
106void realloc(heap_allocator(T) *const this, size_t size);
107
108forall(otype T)
109inline T* data(heap_allocator(T) *const this)
110{
111        return this->storage;
112}
Note: See TracBrowser for help on using the repository browser.