source: src/libcfa/containers/vector @ 4cb935e

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 4cb935e was bd34fc87, checked in by Thierry Delisle <tdelisle@…>, 8 years ago

reenabled vector tests and added proper constructor semantics to vector

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