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