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