source: src/libcfa/containers/vector @ ea29e73

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsctordeferred_resndemanglerenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxmemorynew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumresolv-newwith_gc
Last change on this file since ea29e73 was ea29e73, checked in by Thierry Delisle <tdelisle@…>, 8 years ago

adding a generic vector implementation to libcfa

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