source: src/libcfa/containers/vector @ 2c37f34

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 2c37f34 was 67cf18c, checked in by Rob Schluntz <rschlunt@…>, 7 years ago

implement default type arguments for generic types [closes #13]

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