source: src/libcfa/containers/vector @ a0b3e32

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 a0b3e32 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
RevLine 
[60aa49a7]1//
[a6151ba]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.
[60aa49a7]6//
7// vector --
8//
[a6151ba]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
[60aa49a7]14//
[a6151ba]15
[17e5e2b]16#ifndef VECTOR_H
17#define VECTOR_H
[df4aea7]18
[00b7cd3]19extern "C" {
[f1e42c1]20#include <stdbool.h>
[00b7cd3]21}
[c44e622]22
[67cf18c]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
[eb38dd5]53//------------------------------------------------------------------------------
54//Declaration
[df4aea7]55trait allocator_c(otype T, otype allocator_t)
56{
[bd34fc87]57        void realloc_storage(allocator_t*, size_t);
58        T* data(allocator_t*);
[c44e622]59};
60
[67cf18c]61forall(otype T, otype allocator_t = heap_allocator(T) | allocator_c(T, allocator_t))
[bd34fc87]62struct vector;
[4ef8fb3]63
64//------------------------------------------------------------------------------
65//Initialization
66forall(otype T, otype allocator_t | allocator_c(T, allocator_t))
[bd34fc87]67void ?{}(vector(T, allocator_t)* this);
[4ef8fb3]68
69forall(otype T, otype allocator_t | allocator_c(T, allocator_t))
[bd34fc87]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
[67cf18c]78forall(otype T, otype allocator_t = heap_allocator(T) | allocator_c(T, allocator_t))
[bd34fc87]79struct vector
80{
81        allocator_t storage;
82        size_t size;
83};
[4ef8fb3]84
85//------------------------------------------------------------------------------
86//Capacity
87forall(otype T, otype allocator_t | allocator_c(T, allocator_t))
[bd34fc87]88static inline bool empty(vector(T, allocator_t)* this)
[4ef8fb3]89{
90        return this->size == 0;
91}
92
93forall(otype T, otype allocator_t | allocator_c(T, allocator_t))
[bd34fc87]94static inline size_t size(vector(T, allocator_t)* this)
[4ef8fb3]95{
96        return this->size;
97}
98
99forall(otype T, otype allocator_t | allocator_c(T, allocator_t))
[bd34fc87]100static inline void reserve(vector(T, allocator_t)* this, size_t size)
[4ef8fb3]101{
[60aa49a7]102        realloc_storage(&this->storage, this->size+1);
[4ef8fb3]103}
104
105//------------------------------------------------------------------------------
106//Element access
107forall(otype T, otype allocator_t | allocator_c(T, allocator_t))
[bd34fc87]108static inline T at(vector(T, allocator_t)* this, size_t index)
[4ef8fb3]109{
110        return data(&this->storage)[index];
111}
112
113forall(otype T, otype allocator_t | allocator_c(T, allocator_t))
[bd34fc87]114static inline T ?[?](vector(T, allocator_t)* this, size_t index)
[4ef8fb3]115{
116        return data(&this->storage)[index];
117}
118
119forall(otype T, otype allocator_t | allocator_c(T, allocator_t))
[bd34fc87]120static inline T front(vector(T, allocator_t)* this)
[4ef8fb3]121{
122        return data(&this->storage)[0];
123}
124
125forall(otype T, otype allocator_t | allocator_c(T, allocator_t))
[bd34fc87]126static inline T back(vector(T, allocator_t)* this)
[4ef8fb3]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))
[bd34fc87]134void push_back(vector(T, allocator_t)* this, T value);
[4ef8fb3]135
136forall(otype T, otype allocator_t | allocator_c(T, allocator_t))
[bd34fc87]137void pop_back(vector(T, allocator_t)* this);
[4ef8fb3]138
139forall(otype T, otype allocator_t | allocator_c(T, allocator_t))
[bd34fc87]140void clear(vector(T, allocator_t)* this);
[4ef8fb3]141
142//------------------------------------------------------------------------------
143//Iterators
144forall(otype T, otype allocator_t | allocator_c(T, allocator_t))
[bd34fc87]145static inline T* begin(vector(T, allocator_t)* this)
[4ef8fb3]146{
147        return data(&this->storage);
148}
149
[6dc78dee]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// }
[4ef8fb3]155
156forall(otype T, otype allocator_t | allocator_c(T, allocator_t))
[bd34fc87]157static inline T* end(vector(T, allocator_t)* this)
[4ef8fb3]158{
159        return data(&this->storage) + this->size;
160}
161
[6dc78dee]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// }
[4ef8fb3]167
[17e5e2b]168#endif // VECTOR_H
169
[a6151ba]170// Local Variables: //
171// mode: c //
172// tab-width: 4 //
173// End: //
Note: See TracBrowser for help on using the repository browser.