source: src/libcfa/containers/vector @ a6151ba

aaron-thesisarm-ehcleanup-dtorsctordeferred_resndemanglerenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxmemorynew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumresolv-newwith_gc
Last change on this file since a6151ba was a6151ba, checked in by Peter A. Buhr <pabuhr@…>, 7 years ago

update comments in code and tests, and add test searchsort

  • Property mode set to 100644
File size: 4.2 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#define DESTROY(x)
23
24//------------------------------------------------------------------------------
25//Declaration
26trait allocator_c(otype T, otype allocator_t)
27{
28        void ctor(allocator_t* const);
29        void dtor(allocator_t* const);
30        void realloc(allocator_t* const, size_t);
31        T* data(allocator_t* const);
32};
33
34forall(otype T, otype allocator_t | allocator_c(T, allocator_t))
35struct vector
36{
37        allocator_t storage;
38        size_t size;
39};
40
41//------------------------------------------------------------------------------
42//Initialization
43forall(otype T, otype allocator_t | allocator_c(T, allocator_t))
44void ctor(vector(T, allocator_t) *const this);
45
46forall(otype T, otype allocator_t | allocator_c(T, allocator_t))
47void dtor(vector(T, allocator_t) *const this);
48
49//------------------------------------------------------------------------------
50//Capacity
51forall(otype T, otype allocator_t | allocator_c(T, allocator_t))
52static inline bool empty(vector(T, allocator_t) *const this)
53{
54        return this->size == 0;
55}
56
57forall(otype T, otype allocator_t | allocator_c(T, allocator_t))
58static inline size_t size(vector(T, allocator_t) *const this)
59{
60        return this->size;
61}
62
63forall(otype T, otype allocator_t | allocator_c(T, allocator_t))
64static inline void reserve(vector(T, allocator_t) *const this, size_t size)
65{
66        realloc(&this->storage, this->size+1);
67}
68
69//------------------------------------------------------------------------------
70//Element access
71forall(otype T, otype allocator_t | allocator_c(T, allocator_t))
72static inline T at(vector(T, allocator_t) *const this, size_t index)
73{
74        return data(&this->storage)[index];
75}
76
77forall(otype T, otype allocator_t | allocator_c(T, allocator_t))
78static inline T ?[?](vector(T, allocator_t) *const this, size_t index)
79{
80        return data(&this->storage)[index];
81}
82
83forall(otype T, otype allocator_t | allocator_c(T, allocator_t))
84static inline T front(vector(T, allocator_t) *const this)
85{
86        return data(&this->storage)[0];
87}
88
89forall(otype T, otype allocator_t | allocator_c(T, allocator_t))
90static inline T back(vector(T, allocator_t) *const this)
91{
92        return data(&this->storage)[this->size - 1];
93}
94
95//------------------------------------------------------------------------------
96//Modifiers
97forall(otype T, otype allocator_t | allocator_c(T, allocator_t))
98void push_back(vector(T, allocator_t) *const this, T value);
99
100forall(otype T, otype allocator_t | allocator_c(T, allocator_t))
101void pop_back(vector(T, allocator_t) *const this);
102
103forall(otype T, otype allocator_t | allocator_c(T, allocator_t))
104void clear(vector(T, allocator_t) *const this);
105
106//------------------------------------------------------------------------------
107//Iterators
108forall(otype T, otype allocator_t | allocator_c(T, allocator_t))
109static inline T* begin(vector(T, allocator_t) *const this)
110{
111        return data(&this->storage);
112}
113
114forall(otype T, otype allocator_t | allocator_c(T, allocator_t))
115static inline const T* cbegin(const vector(T, allocator_t) *const this)
116{
117        return data(&this->storage);
118}
119
120forall(otype T, otype allocator_t | allocator_c(T, allocator_t))
121static inline T* end(vector(T, allocator_t) *const this)
122{
123        return data(&this->storage) + this->size;
124}
125
126forall(otype T, otype allocator_t | allocator_c(T, allocator_t))
127static inline const T* cend(const vector(T, allocator_t) *const this)
128{
129        return data(&this->storage) + this->size;
130}
131
132//------------------------------------------------------------------------------
133//Allocator
134forall(otype T)
135struct heap_allocator
136{
137        T* storage;
138        size_t capacity;
139};
140
141forall(otype T)
142void ctor(heap_allocator(T) *const this);
143
144forall(otype T)
145void dtor(heap_allocator(T) *const this);
146
147forall(otype T)
148void realloc(heap_allocator(T) *const this, size_t size);
149
150forall(otype T)
151static inline T* data(heap_allocator(T) *const this)
152{
153        return this->storage;
154}
155
156// Local Variables: //
157// mode: c //
158// tab-width: 4 //
159// End: //
Note: See TracBrowser for help on using the repository browser.