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 : Wed Jun 17 11:02:46 2020
|
---|
13 | // Update Count : 4
|
---|
14 | //
|
---|
15 |
|
---|
16 | #pragma once
|
---|
17 |
|
---|
18 | #include <stdbool.h>
|
---|
19 |
|
---|
20 | //------------------------------------------------------------------------------
|
---|
21 | //Allocator
|
---|
22 | forall(otype T)
|
---|
23 | struct heap_allocator
|
---|
24 | {
|
---|
25 | T* storage;
|
---|
26 | size_t capacity;
|
---|
27 | };
|
---|
28 |
|
---|
29 | forall(otype T)
|
---|
30 | void ?{}(heap_allocator(T)& this);
|
---|
31 |
|
---|
32 | forall(otype T)
|
---|
33 | void ?{}(heap_allocator(T)& this, heap_allocator(T) rhs);
|
---|
34 |
|
---|
35 | forall(otype T)
|
---|
36 | heap_allocator(T) ?=?(heap_allocator(T)& this, heap_allocator(T) rhs);
|
---|
37 |
|
---|
38 | forall(otype T)
|
---|
39 | void ^?{}(heap_allocator(T)& this);
|
---|
40 |
|
---|
41 | forall(otype T)
|
---|
42 | void realloc_storage(heap_allocator(T)* this, size_t size);
|
---|
43 |
|
---|
44 | forall(otype T)
|
---|
45 | static inline T* data(heap_allocator(T)* this)
|
---|
46 | {
|
---|
47 | return this->storage;
|
---|
48 | }
|
---|
49 |
|
---|
50 | //------------------------------------------------------------------------------
|
---|
51 | //Declaration
|
---|
52 | trait allocator_c(otype T, otype allocator_t)
|
---|
53 | {
|
---|
54 | void realloc_storage(allocator_t*, size_t);
|
---|
55 | T* data(allocator_t*);
|
---|
56 | };
|
---|
57 |
|
---|
58 | forall(otype T, otype allocator_t = heap_allocator(T) | allocator_c(T, allocator_t))
|
---|
59 | struct vector;
|
---|
60 |
|
---|
61 | //------------------------------------------------------------------------------
|
---|
62 | //Initialization
|
---|
63 | forall(otype T, otype allocator_t | allocator_c(T, allocator_t))
|
---|
64 | void ?{}(vector(T, allocator_t)& this);
|
---|
65 |
|
---|
66 | forall(otype T, otype allocator_t | allocator_c(T, allocator_t))
|
---|
67 | void ?{}(vector(T, allocator_t)& this, vector(T, allocator_t) rhs);
|
---|
68 |
|
---|
69 | forall(otype T, otype allocator_t | allocator_c(T, allocator_t))
|
---|
70 | vector(T, allocator_t) ?=?(vector(T, allocator_t)& this, vector(T, allocator_t) rhs);
|
---|
71 |
|
---|
72 | forall(otype T, otype allocator_t | allocator_c(T, allocator_t))
|
---|
73 | void ^?{}(vector(T, allocator_t)& this);
|
---|
74 |
|
---|
75 | forall(otype T, otype allocator_t = heap_allocator(T) | allocator_c(T, allocator_t))
|
---|
76 | struct vector
|
---|
77 | {
|
---|
78 | allocator_t storage;
|
---|
79 | size_t size;
|
---|
80 | };
|
---|
81 |
|
---|
82 | //------------------------------------------------------------------------------
|
---|
83 | //Capacity
|
---|
84 | forall(otype T, otype allocator_t | allocator_c(T, allocator_t))
|
---|
85 | static inline bool empty(vector(T, allocator_t)* this)
|
---|
86 | {
|
---|
87 | return this->size == 0;
|
---|
88 | }
|
---|
89 |
|
---|
90 | forall(otype T, otype allocator_t | allocator_c(T, allocator_t))
|
---|
91 | static inline size_t size(vector(T, allocator_t)* this)
|
---|
92 | {
|
---|
93 | return this->size;
|
---|
94 | }
|
---|
95 |
|
---|
96 | forall(otype T, otype allocator_t | allocator_c(T, allocator_t))
|
---|
97 | static inline void reserve(vector(T, allocator_t)* this, size_t size)
|
---|
98 | {
|
---|
99 | realloc_storage(&this->storage, this->size+1);
|
---|
100 | }
|
---|
101 |
|
---|
102 | //------------------------------------------------------------------------------
|
---|
103 | //Element access
|
---|
104 | forall(otype T, otype allocator_t | allocator_c(T, allocator_t))
|
---|
105 | static inline T at(vector(T, allocator_t)* this, size_t index)
|
---|
106 | {
|
---|
107 | return data(&this->storage)[index];
|
---|
108 | }
|
---|
109 |
|
---|
110 | forall(otype T, otype allocator_t | allocator_c(T, allocator_t))
|
---|
111 | static inline T ?[?](vector(T, allocator_t)* this, size_t index)
|
---|
112 | {
|
---|
113 | return data(&this->storage)[index];
|
---|
114 | }
|
---|
115 |
|
---|
116 | forall(otype T, otype allocator_t | allocator_c(T, allocator_t))
|
---|
117 | static inline T front(vector(T, allocator_t)* this)
|
---|
118 | {
|
---|
119 | return data(&this->storage)[0];
|
---|
120 | }
|
---|
121 |
|
---|
122 | forall(otype T, otype allocator_t | allocator_c(T, allocator_t))
|
---|
123 | static inline T back(vector(T, allocator_t)* this)
|
---|
124 | {
|
---|
125 | return data(&this->storage)[this->size - 1];
|
---|
126 | }
|
---|
127 |
|
---|
128 | //------------------------------------------------------------------------------
|
---|
129 | //Modifiers
|
---|
130 | forall(otype T, otype allocator_t | allocator_c(T, allocator_t))
|
---|
131 | void push_back(vector(T, allocator_t)* this, T value);
|
---|
132 |
|
---|
133 | forall(otype T, otype allocator_t | allocator_c(T, allocator_t))
|
---|
134 | void pop_back(vector(T, allocator_t)* this);
|
---|
135 |
|
---|
136 | forall(otype T, otype allocator_t | allocator_c(T, allocator_t))
|
---|
137 | void clear(vector(T, allocator_t)* this);
|
---|
138 |
|
---|
139 | //------------------------------------------------------------------------------
|
---|
140 | //Iterators
|
---|
141 | forall(otype T, otype allocator_t | allocator_c(T, allocator_t))
|
---|
142 | static inline T* begin(vector(T, allocator_t)* this)
|
---|
143 | {
|
---|
144 | return data(&this->storage);
|
---|
145 | }
|
---|
146 |
|
---|
147 | // forall(otype T, otype allocator_t | allocator_c(T, allocator_t))
|
---|
148 | // static inline const T* cbegin(const vector(T, allocator_t)* this)
|
---|
149 | // {
|
---|
150 | // return data(&this->storage);
|
---|
151 | // }
|
---|
152 |
|
---|
153 | forall(otype T, otype allocator_t | allocator_c(T, allocator_t))
|
---|
154 | static inline T* end(vector(T, allocator_t)* this)
|
---|
155 | {
|
---|
156 | return data(&this->storage) + this->size;
|
---|
157 | }
|
---|
158 |
|
---|
159 | // forall(otype T, otype allocator_t | allocator_c(T, allocator_t))
|
---|
160 | // static inline const T* cend(const vector(T, allocator_t)* this)
|
---|
161 | // {
|
---|
162 | // return data(&this->storage) + this->size;
|
---|
163 | // }
|
---|
164 |
|
---|
165 | // Local Variables: //
|
---|
166 | // mode: c //
|
---|
167 | // tab-width: 4 //
|
---|
168 | // End: //
|
---|