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 | #pragma once
|
---|
17 |
|
---|
18 | extern "C" {
|
---|
19 | #include <stdbool.h>
|
---|
20 | }
|
---|
21 |
|
---|
22 | //------------------------------------------------------------------------------
|
---|
23 | //Declaration
|
---|
24 | trait allocator_c(otype T, otype allocator_t)
|
---|
25 | {
|
---|
26 | void realloc_storage(allocator_t*, size_t);
|
---|
27 | T* data(allocator_t*);
|
---|
28 | };
|
---|
29 |
|
---|
30 | forall(otype T, otype allocator_t | allocator_c(T, allocator_t))
|
---|
31 | struct vector;
|
---|
32 |
|
---|
33 | //------------------------------------------------------------------------------
|
---|
34 | //Initialization
|
---|
35 | forall(otype T, otype allocator_t | allocator_c(T, allocator_t))
|
---|
36 | void ?{}(vector(T, allocator_t)* this);
|
---|
37 |
|
---|
38 | forall(otype T, otype allocator_t | allocator_c(T, allocator_t))
|
---|
39 | void ?{}(vector(T, allocator_t)* this, vector(T, allocator_t) rhs);
|
---|
40 |
|
---|
41 | forall(otype T, otype allocator_t | allocator_c(T, allocator_t))
|
---|
42 | vector(T, allocator_t) ?=?(vector(T, allocator_t)* this, vector(T, allocator_t) rhs);
|
---|
43 |
|
---|
44 | forall(otype T, otype allocator_t | allocator_c(T, allocator_t))
|
---|
45 | void ^?{}(vector(T, allocator_t)* this);
|
---|
46 |
|
---|
47 | forall(otype T, otype allocator_t | allocator_c(T, allocator_t))
|
---|
48 | struct vector
|
---|
49 | {
|
---|
50 | allocator_t storage;
|
---|
51 | size_t size;
|
---|
52 | };
|
---|
53 |
|
---|
54 | //------------------------------------------------------------------------------
|
---|
55 | //Capacity
|
---|
56 | forall(otype T, otype allocator_t | allocator_c(T, allocator_t))
|
---|
57 | static inline bool empty(vector(T, allocator_t)* this)
|
---|
58 | {
|
---|
59 | return this->size == 0;
|
---|
60 | }
|
---|
61 |
|
---|
62 | forall(otype T, otype allocator_t | allocator_c(T, allocator_t))
|
---|
63 | static inline size_t size(vector(T, allocator_t)* this)
|
---|
64 | {
|
---|
65 | return this->size;
|
---|
66 | }
|
---|
67 |
|
---|
68 | forall(otype T, otype allocator_t | allocator_c(T, allocator_t))
|
---|
69 | static inline void reserve(vector(T, allocator_t)* this, size_t size)
|
---|
70 | {
|
---|
71 | realloc_storage(&this->storage, this->size+1);
|
---|
72 | }
|
---|
73 |
|
---|
74 | //------------------------------------------------------------------------------
|
---|
75 | //Element access
|
---|
76 | forall(otype T, otype allocator_t | allocator_c(T, allocator_t))
|
---|
77 | static inline T at(vector(T, allocator_t)* this, size_t index)
|
---|
78 | {
|
---|
79 | return data(&this->storage)[index];
|
---|
80 | }
|
---|
81 |
|
---|
82 | forall(otype T, otype allocator_t | allocator_c(T, allocator_t))
|
---|
83 | static inline T ?[?](vector(T, allocator_t)* this, size_t index)
|
---|
84 | {
|
---|
85 | return data(&this->storage)[index];
|
---|
86 | }
|
---|
87 |
|
---|
88 | forall(otype T, otype allocator_t | allocator_c(T, allocator_t))
|
---|
89 | static inline T front(vector(T, allocator_t)* this)
|
---|
90 | {
|
---|
91 | return data(&this->storage)[0];
|
---|
92 | }
|
---|
93 |
|
---|
94 | forall(otype T, otype allocator_t | allocator_c(T, allocator_t))
|
---|
95 | static inline T back(vector(T, allocator_t)* this)
|
---|
96 | {
|
---|
97 | return data(&this->storage)[this->size - 1];
|
---|
98 | }
|
---|
99 |
|
---|
100 | //------------------------------------------------------------------------------
|
---|
101 | //Modifiers
|
---|
102 | forall(otype T, otype allocator_t | allocator_c(T, allocator_t))
|
---|
103 | void push_back(vector(T, allocator_t)* this, T value);
|
---|
104 |
|
---|
105 | forall(otype T, otype allocator_t | allocator_c(T, allocator_t))
|
---|
106 | void pop_back(vector(T, allocator_t)* this);
|
---|
107 |
|
---|
108 | forall(otype T, otype allocator_t | allocator_c(T, allocator_t))
|
---|
109 | void clear(vector(T, allocator_t)* this);
|
---|
110 |
|
---|
111 | //------------------------------------------------------------------------------
|
---|
112 | //Iterators
|
---|
113 | forall(otype T, otype allocator_t | allocator_c(T, allocator_t))
|
---|
114 | static inline T* begin(vector(T, allocator_t)* this)
|
---|
115 | {
|
---|
116 | return data(&this->storage);
|
---|
117 | }
|
---|
118 |
|
---|
119 | forall(otype T, otype allocator_t | allocator_c(T, allocator_t))
|
---|
120 | static inline const T* cbegin(const vector(T, allocator_t)* this)
|
---|
121 | {
|
---|
122 | return data(&this->storage);
|
---|
123 | }
|
---|
124 |
|
---|
125 | forall(otype T, otype allocator_t | allocator_c(T, allocator_t))
|
---|
126 | static inline T* end(vector(T, allocator_t)* this)
|
---|
127 | {
|
---|
128 | return data(&this->storage) + this->size;
|
---|
129 | }
|
---|
130 |
|
---|
131 | forall(otype T, otype allocator_t | allocator_c(T, allocator_t))
|
---|
132 | static inline const T* cend(const vector(T, allocator_t)* this)
|
---|
133 | {
|
---|
134 | return data(&this->storage) + this->size;
|
---|
135 | }
|
---|
136 |
|
---|
137 | //------------------------------------------------------------------------------
|
---|
138 | //Allocator
|
---|
139 | forall(otype T)
|
---|
140 | struct heap_allocator
|
---|
141 | {
|
---|
142 | T* storage;
|
---|
143 | size_t capacity;
|
---|
144 | };
|
---|
145 |
|
---|
146 | forall(otype T)
|
---|
147 | void ?{}(heap_allocator(T)* this);
|
---|
148 |
|
---|
149 | forall(otype T)
|
---|
150 | void ?{}(heap_allocator(T)* this, heap_allocator(T) rhs);
|
---|
151 |
|
---|
152 | forall(otype T)
|
---|
153 | heap_allocator(T) ?=?(heap_allocator(T)* this, heap_allocator(T) rhs);
|
---|
154 |
|
---|
155 | forall(otype T)
|
---|
156 | void ^?{}(heap_allocator(T)* this);
|
---|
157 |
|
---|
158 | forall(otype T)
|
---|
159 | void realloc_storage(heap_allocator(T)* this, size_t size);
|
---|
160 |
|
---|
161 | forall(otype T)
|
---|
162 | static inline T* data(heap_allocator(T)* this)
|
---|
163 | {
|
---|
164 | return this->storage;
|
---|
165 | }
|
---|
166 |
|
---|
167 | // Local Variables: //
|
---|
168 | // mode: c //
|
---|
169 | // tab-width: 4 //
|
---|
170 | // End: //
|
---|