source: src/libcfa/containers/vector@ 00b7cd3

ADT aaron-thesis arm-eh ast-experimental cleanup-dtors ctor deferred_resn demangler enum forall-pointer-decay jacob/cs343-translation jenkins-sandbox memory new-ast new-ast-unique-expr new-env no_list persistent-indexer pthread-emulation qualifiedEnum resolv-new with_gc
Last change on this file since 00b7cd3 was 00b7cd3, checked in by Peter A. Buhr <pabuhr@…>, 9 years ago

update include files for test programs

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