1 | //
|
---|
2 | // Cforall Version 1.0.0 Copyright (C) 2016 University of Waterloo
|
---|
3 | //
|
---|
4 | // The contents of this file are covered under the licence agreement in the
|
---|
5 | // file "LICENCE" distributed with Cforall.
|
---|
6 | //
|
---|
7 | // vector --
|
---|
8 | //
|
---|
9 | // Author : Thierry Delisle
|
---|
10 | // Created On : Tue Jul 5 18:00:07 2016
|
---|
11 | // Last Modified By : Peter A. Buhr
|
---|
12 | // Last Modified On : Tue Jul 5 18:01:35 2016
|
---|
13 | // Update Count : 2
|
---|
14 | //
|
---|
15 |
|
---|
16 | #ifndef VECTOR_H
|
---|
17 | #define VECTOR_H
|
---|
18 |
|
---|
19 | extern "C" {
|
---|
20 | #include <stdbool.h>
|
---|
21 | }
|
---|
22 |
|
---|
23 | //------------------------------------------------------------------------------
|
---|
24 | //Allocator
|
---|
25 | forall(otype T)
|
---|
26 | struct heap_allocator
|
---|
27 | {
|
---|
28 | T* storage;
|
---|
29 | size_t capacity;
|
---|
30 | };
|
---|
31 |
|
---|
32 | forall(otype T)
|
---|
33 | void ?{}(heap_allocator(T)* this);
|
---|
34 |
|
---|
35 | forall(otype T)
|
---|
36 | void ?{}(heap_allocator(T)* this, heap_allocator(T) rhs);
|
---|
37 |
|
---|
38 | forall(otype T)
|
---|
39 | heap_allocator(T) ?=?(heap_allocator(T)* this, heap_allocator(T) rhs);
|
---|
40 |
|
---|
41 | forall(otype T)
|
---|
42 | void ^?{}(heap_allocator(T)* this);
|
---|
43 |
|
---|
44 | forall(otype T)
|
---|
45 | void realloc_storage(heap_allocator(T)* this, size_t size);
|
---|
46 |
|
---|
47 | forall(otype T)
|
---|
48 | static inline T* data(heap_allocator(T)* this)
|
---|
49 | {
|
---|
50 | return this->storage;
|
---|
51 | }
|
---|
52 |
|
---|
53 | //------------------------------------------------------------------------------
|
---|
54 | //Declaration
|
---|
55 | trait allocator_c(otype T, otype allocator_t)
|
---|
56 | {
|
---|
57 | void realloc_storage(allocator_t*, size_t);
|
---|
58 | T* data(allocator_t*);
|
---|
59 | };
|
---|
60 |
|
---|
61 | forall(otype T, otype allocator_t = heap_allocator(T) | allocator_c(T, allocator_t))
|
---|
62 | struct vector;
|
---|
63 |
|
---|
64 | //------------------------------------------------------------------------------
|
---|
65 | //Initialization
|
---|
66 | forall(otype T, otype allocator_t | allocator_c(T, allocator_t))
|
---|
67 | void ?{}(vector(T, allocator_t)* this);
|
---|
68 |
|
---|
69 | forall(otype T, otype allocator_t | allocator_c(T, allocator_t))
|
---|
70 | void ?{}(vector(T, allocator_t)* this, vector(T, allocator_t) rhs);
|
---|
71 |
|
---|
72 | forall(otype T, otype allocator_t | allocator_c(T, allocator_t))
|
---|
73 | vector(T, allocator_t) ?=?(vector(T, allocator_t)* this, vector(T, allocator_t) rhs);
|
---|
74 |
|
---|
75 | forall(otype T, otype allocator_t | allocator_c(T, allocator_t))
|
---|
76 | void ^?{}(vector(T, allocator_t)* this);
|
---|
77 |
|
---|
78 | forall(otype T, otype allocator_t = heap_allocator(T) | allocator_c(T, allocator_t))
|
---|
79 | struct vector
|
---|
80 | {
|
---|
81 | allocator_t storage;
|
---|
82 | size_t size;
|
---|
83 | };
|
---|
84 |
|
---|
85 | //------------------------------------------------------------------------------
|
---|
86 | //Capacity
|
---|
87 | forall(otype T, otype allocator_t | allocator_c(T, allocator_t))
|
---|
88 | static inline bool empty(vector(T, allocator_t)* this)
|
---|
89 | {
|
---|
90 | return this->size == 0;
|
---|
91 | }
|
---|
92 |
|
---|
93 | forall(otype T, otype allocator_t | allocator_c(T, allocator_t))
|
---|
94 | static inline size_t size(vector(T, allocator_t)* this)
|
---|
95 | {
|
---|
96 | return this->size;
|
---|
97 | }
|
---|
98 |
|
---|
99 | forall(otype T, otype allocator_t | allocator_c(T, allocator_t))
|
---|
100 | static inline void reserve(vector(T, allocator_t)* this, size_t size)
|
---|
101 | {
|
---|
102 | realloc_storage(&this->storage, this->size+1);
|
---|
103 | }
|
---|
104 |
|
---|
105 | //------------------------------------------------------------------------------
|
---|
106 | //Element access
|
---|
107 | forall(otype T, otype allocator_t | allocator_c(T, allocator_t))
|
---|
108 | static inline T at(vector(T, allocator_t)* this, size_t index)
|
---|
109 | {
|
---|
110 | return data(&this->storage)[index];
|
---|
111 | }
|
---|
112 |
|
---|
113 | forall(otype T, otype allocator_t | allocator_c(T, allocator_t))
|
---|
114 | static inline T ?[?](vector(T, allocator_t)* this, size_t index)
|
---|
115 | {
|
---|
116 | return data(&this->storage)[index];
|
---|
117 | }
|
---|
118 |
|
---|
119 | forall(otype T, otype allocator_t | allocator_c(T, allocator_t))
|
---|
120 | static inline T front(vector(T, allocator_t)* this)
|
---|
121 | {
|
---|
122 | return data(&this->storage)[0];
|
---|
123 | }
|
---|
124 |
|
---|
125 | forall(otype T, otype allocator_t | allocator_c(T, allocator_t))
|
---|
126 | static inline T back(vector(T, allocator_t)* this)
|
---|
127 | {
|
---|
128 | return data(&this->storage)[this->size - 1];
|
---|
129 | }
|
---|
130 |
|
---|
131 | //------------------------------------------------------------------------------
|
---|
132 | //Modifiers
|
---|
133 | forall(otype T, otype allocator_t | allocator_c(T, allocator_t))
|
---|
134 | void push_back(vector(T, allocator_t)* this, T value);
|
---|
135 |
|
---|
136 | forall(otype T, otype allocator_t | allocator_c(T, allocator_t))
|
---|
137 | void pop_back(vector(T, allocator_t)* this);
|
---|
138 |
|
---|
139 | forall(otype T, otype allocator_t | allocator_c(T, allocator_t))
|
---|
140 | void clear(vector(T, allocator_t)* this);
|
---|
141 |
|
---|
142 | //------------------------------------------------------------------------------
|
---|
143 | //Iterators
|
---|
144 | forall(otype T, otype allocator_t | allocator_c(T, allocator_t))
|
---|
145 | static inline T* begin(vector(T, allocator_t)* this)
|
---|
146 | {
|
---|
147 | return data(&this->storage);
|
---|
148 | }
|
---|
149 |
|
---|
150 | // forall(otype T, otype allocator_t | allocator_c(T, allocator_t))
|
---|
151 | // static inline const T* cbegin(const vector(T, allocator_t)* this)
|
---|
152 | // {
|
---|
153 | // return data(&this->storage);
|
---|
154 | // }
|
---|
155 |
|
---|
156 | forall(otype T, otype allocator_t | allocator_c(T, allocator_t))
|
---|
157 | static inline T* end(vector(T, allocator_t)* this)
|
---|
158 | {
|
---|
159 | return data(&this->storage) + this->size;
|
---|
160 | }
|
---|
161 |
|
---|
162 | // forall(otype T, otype allocator_t | allocator_c(T, allocator_t))
|
---|
163 | // static inline const T* cend(const vector(T, allocator_t)* this)
|
---|
164 | // {
|
---|
165 | // return data(&this->storage) + this->size;
|
---|
166 | // }
|
---|
167 |
|
---|
168 | #endif // VECTOR_H
|
---|
169 |
|
---|
170 | // Local Variables: //
|
---|
171 | // mode: c //
|
---|
172 | // tab-width: 4 //
|
---|
173 | // End: //
|
---|