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