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