source: src/examples/strings/src/vector.h @ 020a349

string
Last change on this file since 020a349 was a772d8ab, checked in by Thierry Delisle <tdelisle@…>, 8 years ago

basic commit + some work on assertion to make them match normal assertions

  • Property mode set to 100644
File size: 3.7 KB
Line 
1#pragma once
2
3#include <stdlib>
4
5#define DESTROY(x)
6
7//------------------------------------------------------------------------------
8//Declaration
9trait allocator_c(otype T, otype allocator_t)
10{
11        void ctor(allocator_t* const);
12        void dtor(allocator_t* const);
13        void realloc(allocator_t* const, size_t);
14        T* data(allocator_t* const);
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 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))
35static inline 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))
41static inline 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))
47static inline 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))
55static inline T at(vector(T, allocator_t) *const this, size_t index)
56{
57        return data(&this->storage)[index];
58}
59
60forall(otype T, otype allocator_t | allocator_c(T, allocator_t))
61static inline T ?[?](vector(T, allocator_t) *const this, size_t index)
62{
63        return data(&this->storage)[index];
64}
65
66forall(otype T, otype allocator_t | allocator_c(T, allocator_t))
67static inline T front(vector(T, allocator_t) *const this)
68{
69        return data(&this->storage)[0];
70}
71
72forall(otype T, otype allocator_t | allocator_c(T, allocator_t))
73static inline T back(vector(T, allocator_t) *const this)
74{
75        return data(&this->storage)[this->size - 1];
76}
77
78//------------------------------------------------------------------------------
79//Modifiers
80forall(otype T, otype allocator_t | allocator_c(T, allocator_t))
81void push_back(vector(T, allocator_t) *const this, T value);
82
83forall(otype T, otype allocator_t | allocator_c(T, allocator_t))
84void pop_back(vector(T, allocator_t) *const this);
85
86forall(otype T, otype allocator_t | allocator_c(T, allocator_t))
87void clear(vector(T, allocator_t) *const this);
88
89//------------------------------------------------------------------------------
90//Iterators
91forall(otype T, otype allocator_t | allocator_c(T, allocator_t))
92static inline T* begin(vector(T, allocator_t) *const this)
93{
94        return data(&this->storage);
95}
96
97forall(otype T, otype allocator_t | allocator_c(T, allocator_t))
98static inline const T* cbegin(const vector(T, allocator_t) *const this)
99{
100        return data(&this->storage);
101}
102
103forall(otype T, otype allocator_t | allocator_c(T, allocator_t))
104static inline T* end(vector(T, allocator_t) *const this)
105{
106        return data(&this->storage) + this->size;
107}
108
109forall(otype T, otype allocator_t | allocator_c(T, allocator_t))
110static inline const T* cend(const vector(T, allocator_t) *const this)
111{
112        return data(&this->storage) + this->size;
113}
114
115//------------------------------------------------------------------------------
116//Allocator
117forall(otype T)
118struct heap_allocator
119{
120        T* storage;
121        size_t capacity;
122};
123
124forall(otype T)
125void ctor(heap_allocator(T) *const this);
126
127forall(otype T)
128void dtor(heap_allocator(T) *const this);
129
130forall(otype T)
131void realloc(heap_allocator(T) *const this, size_t size);
132
133forall(otype T)
134static inline T* data(heap_allocator(T) *const this)
135{
136        return this->storage;
137}
Note: See TracBrowser for help on using the repository browser.