source: src/libcfa/containers/vector @ 6a1bdfd

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 6a1bdfd 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
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
[df4aea7]16#pragma once
17
[00b7cd3]18extern "C" {
[f1e42c1]19#include <stdbool.h>
[00b7cd3]20}
[c44e622]21
[eb38dd5]22//------------------------------------------------------------------------------
23//Declaration
[df4aea7]24trait allocator_c(otype T, otype allocator_t)
25{
[bd34fc87]26        void realloc_storage(allocator_t*, size_t);
27        T* data(allocator_t*);
[c44e622]28};
29
[385c130]30forall(otype T, otype allocator_t | allocator_c(T, allocator_t))
[bd34fc87]31struct vector;
[4ef8fb3]32
33//------------------------------------------------------------------------------
34//Initialization
35forall(otype T, otype allocator_t | allocator_c(T, allocator_t))
[bd34fc87]36void ?{}(vector(T, allocator_t)* this);
[4ef8fb3]37
38forall(otype T, otype allocator_t | allocator_c(T, allocator_t))
[bd34fc87]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};
[4ef8fb3]53
54//------------------------------------------------------------------------------
55//Capacity
56forall(otype T, otype allocator_t | allocator_c(T, allocator_t))
[bd34fc87]57static inline bool empty(vector(T, allocator_t)* this)
[4ef8fb3]58{
59        return this->size == 0;
60}
61
62forall(otype T, otype allocator_t | allocator_c(T, allocator_t))
[bd34fc87]63static inline size_t size(vector(T, allocator_t)* this)
[4ef8fb3]64{
65        return this->size;
66}
67
68forall(otype T, otype allocator_t | allocator_c(T, allocator_t))
[bd34fc87]69static inline void reserve(vector(T, allocator_t)* this, size_t size)
[4ef8fb3]70{
[60aa49a7]71        realloc_storage(&this->storage, this->size+1);
[4ef8fb3]72}
73
74//------------------------------------------------------------------------------
75//Element access
76forall(otype T, otype allocator_t | allocator_c(T, allocator_t))
[bd34fc87]77static inline T at(vector(T, allocator_t)* this, size_t index)
[4ef8fb3]78{
79        return data(&this->storage)[index];
80}
81
82forall(otype T, otype allocator_t | allocator_c(T, allocator_t))
[bd34fc87]83static inline T ?[?](vector(T, allocator_t)* this, size_t index)
[4ef8fb3]84{
85        return data(&this->storage)[index];
86}
87
88forall(otype T, otype allocator_t | allocator_c(T, allocator_t))
[bd34fc87]89static inline T front(vector(T, allocator_t)* this)
[4ef8fb3]90{
91        return data(&this->storage)[0];
92}
93
94forall(otype T, otype allocator_t | allocator_c(T, allocator_t))
[bd34fc87]95static inline T back(vector(T, allocator_t)* this)
[4ef8fb3]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))
[bd34fc87]103void push_back(vector(T, allocator_t)* this, T value);
[4ef8fb3]104
105forall(otype T, otype allocator_t | allocator_c(T, allocator_t))
[bd34fc87]106void pop_back(vector(T, allocator_t)* this);
[4ef8fb3]107
108forall(otype T, otype allocator_t | allocator_c(T, allocator_t))
[bd34fc87]109void clear(vector(T, allocator_t)* this);
[4ef8fb3]110
111//------------------------------------------------------------------------------
112//Iterators
113forall(otype T, otype allocator_t | allocator_c(T, allocator_t))
[bd34fc87]114static inline T* begin(vector(T, allocator_t)* this)
[4ef8fb3]115{
116        return data(&this->storage);
117}
118
119forall(otype T, otype allocator_t | allocator_c(T, allocator_t))
[bd34fc87]120static inline const T* cbegin(const vector(T, allocator_t)* this)
[4ef8fb3]121{
122        return data(&this->storage);
123}
124
125forall(otype T, otype allocator_t | allocator_c(T, allocator_t))
[bd34fc87]126static inline T* end(vector(T, allocator_t)* this)
[4ef8fb3]127{
128        return data(&this->storage) + this->size;
129}
130
131forall(otype T, otype allocator_t | allocator_c(T, allocator_t))
[bd34fc87]132static inline const T* cend(const vector(T, allocator_t)* this)
[4ef8fb3]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)
[bd34fc87]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);
[4ef8fb3]154
155forall(otype T)
[bd34fc87]156void ^?{}(heap_allocator(T)* this);
[4ef8fb3]157
158forall(otype T)
[bd34fc87]159void realloc_storage(heap_allocator(T)* this, size_t size);
[4ef8fb3]160
161forall(otype T)
[bd34fc87]162static inline T* data(heap_allocator(T)* this)
[4ef8fb3]163{
164        return this->storage;
165}
[a6151ba]166
167// Local Variables: //
168// mode: c //
169// tab-width: 4 //
170// End: //
Note: See TracBrowser for help on using the repository browser.