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