source: src/libcfa/containers/vector @ 90cedbdd

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsdeferred_resndemanglerenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumwith_gc
Last change on this file since 90cedbdd was 9236060, checked in by Rob Schluntz <rschlunt@…>, 7 years ago

Merge branch 'master' into references

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