source: src/libcfa/containers/vector @ ff2d7341

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

Added proper include guards to cfa headers so they can be added to the c++ include path without issues

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