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