source: src/libcfa/containers/vector @ c4d4ecf

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