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