| 1 | // | 
|---|
| 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. | 
|---|
| 6 | // | 
|---|
| 7 | // vector -- | 
|---|
| 8 | // | 
|---|
| 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 : Sat Jul 22 10:01:18 2017 | 
|---|
| 13 | // Update Count     : 3 | 
|---|
| 14 | // | 
|---|
| 15 |  | 
|---|
| 16 | #pragma once | 
|---|
| 17 |  | 
|---|
| 18 | extern "C" { | 
|---|
| 19 | #include <stdbool.h> | 
|---|
| 20 | } | 
|---|
| 21 |  | 
|---|
| 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) | 
|---|
| 32 | void ?{}(heap_allocator(T)& this); | 
|---|
| 33 |  | 
|---|
| 34 | forall(otype T) | 
|---|
| 35 | void ?{}(heap_allocator(T)& this, heap_allocator(T) rhs); | 
|---|
| 36 |  | 
|---|
| 37 | forall(otype T) | 
|---|
| 38 | heap_allocator(T) ?=?(heap_allocator(T)& this, heap_allocator(T) rhs); | 
|---|
| 39 |  | 
|---|
| 40 | forall(otype T) | 
|---|
| 41 | void ^?{}(heap_allocator(T)& this); | 
|---|
| 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 |  | 
|---|
| 52 | //------------------------------------------------------------------------------ | 
|---|
| 53 | //Declaration | 
|---|
| 54 | trait allocator_c(otype T, otype allocator_t) | 
|---|
| 55 | { | 
|---|
| 56 | void realloc_storage(allocator_t*, size_t); | 
|---|
| 57 | T* data(allocator_t*); | 
|---|
| 58 | }; | 
|---|
| 59 |  | 
|---|
| 60 | forall(otype T, otype allocator_t = heap_allocator(T) | allocator_c(T, allocator_t)) | 
|---|
| 61 | struct vector; | 
|---|
| 62 |  | 
|---|
| 63 | //------------------------------------------------------------------------------ | 
|---|
| 64 | //Initialization | 
|---|
| 65 | forall(otype T, otype allocator_t | allocator_c(T, allocator_t)) | 
|---|
| 66 | void ?{}(vector(T, allocator_t)& this); | 
|---|
| 67 |  | 
|---|
| 68 | forall(otype T, otype allocator_t | allocator_c(T, allocator_t)) | 
|---|
| 69 | void ?{}(vector(T, allocator_t)& this, vector(T, allocator_t) rhs); | 
|---|
| 70 |  | 
|---|
| 71 | forall(otype T, otype allocator_t | allocator_c(T, allocator_t)) | 
|---|
| 72 | vector(T, allocator_t) ?=?(vector(T, allocator_t)& this, vector(T, allocator_t) rhs); | 
|---|
| 73 |  | 
|---|
| 74 | forall(otype T, otype allocator_t | allocator_c(T, allocator_t)) | 
|---|
| 75 | void ^?{}(vector(T, allocator_t)& this); | 
|---|
| 76 |  | 
|---|
| 77 | forall(otype T, otype allocator_t = heap_allocator(T) | allocator_c(T, allocator_t)) | 
|---|
| 78 | struct vector | 
|---|
| 79 | { | 
|---|
| 80 | allocator_t storage; | 
|---|
| 81 | size_t size; | 
|---|
| 82 | }; | 
|---|
| 83 |  | 
|---|
| 84 | //------------------------------------------------------------------------------ | 
|---|
| 85 | //Capacity | 
|---|
| 86 | forall(otype T, otype allocator_t | allocator_c(T, allocator_t)) | 
|---|
| 87 | static inline bool empty(vector(T, allocator_t)* this) | 
|---|
| 88 | { | 
|---|
| 89 | return this->size == 0; | 
|---|
| 90 | } | 
|---|
| 91 |  | 
|---|
| 92 | forall(otype T, otype allocator_t | allocator_c(T, allocator_t)) | 
|---|
| 93 | static inline size_t size(vector(T, allocator_t)* this) | 
|---|
| 94 | { | 
|---|
| 95 | return this->size; | 
|---|
| 96 | } | 
|---|
| 97 |  | 
|---|
| 98 | forall(otype T, otype allocator_t | allocator_c(T, allocator_t)) | 
|---|
| 99 | static inline void reserve(vector(T, allocator_t)* this, size_t size) | 
|---|
| 100 | { | 
|---|
| 101 | realloc_storage(&this->storage, this->size+1); | 
|---|
| 102 | } | 
|---|
| 103 |  | 
|---|
| 104 | //------------------------------------------------------------------------------ | 
|---|
| 105 | //Element access | 
|---|
| 106 | forall(otype T, otype allocator_t | allocator_c(T, allocator_t)) | 
|---|
| 107 | static inline T at(vector(T, allocator_t)* this, size_t index) | 
|---|
| 108 | { | 
|---|
| 109 | return data(&this->storage)[index]; | 
|---|
| 110 | } | 
|---|
| 111 |  | 
|---|
| 112 | forall(otype T, otype allocator_t | allocator_c(T, allocator_t)) | 
|---|
| 113 | static inline T ?[?](vector(T, allocator_t)* this, size_t index) | 
|---|
| 114 | { | 
|---|
| 115 | return data(&this->storage)[index]; | 
|---|
| 116 | } | 
|---|
| 117 |  | 
|---|
| 118 | forall(otype T, otype allocator_t | allocator_c(T, allocator_t)) | 
|---|
| 119 | static inline T front(vector(T, allocator_t)* this) | 
|---|
| 120 | { | 
|---|
| 121 | return data(&this->storage)[0]; | 
|---|
| 122 | } | 
|---|
| 123 |  | 
|---|
| 124 | forall(otype T, otype allocator_t | allocator_c(T, allocator_t)) | 
|---|
| 125 | static inline T back(vector(T, allocator_t)* this) | 
|---|
| 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)) | 
|---|
| 133 | void push_back(vector(T, allocator_t)* this, T value); | 
|---|
| 134 |  | 
|---|
| 135 | forall(otype T, otype allocator_t | allocator_c(T, allocator_t)) | 
|---|
| 136 | void pop_back(vector(T, allocator_t)* this); | 
|---|
| 137 |  | 
|---|
| 138 | forall(otype T, otype allocator_t | allocator_c(T, allocator_t)) | 
|---|
| 139 | void clear(vector(T, allocator_t)* this); | 
|---|
| 140 |  | 
|---|
| 141 | //------------------------------------------------------------------------------ | 
|---|
| 142 | //Iterators | 
|---|
| 143 | forall(otype T, otype allocator_t | allocator_c(T, allocator_t)) | 
|---|
| 144 | static inline T* begin(vector(T, allocator_t)* this) | 
|---|
| 145 | { | 
|---|
| 146 | return data(&this->storage); | 
|---|
| 147 | } | 
|---|
| 148 |  | 
|---|
| 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 | // } | 
|---|
| 154 |  | 
|---|
| 155 | forall(otype T, otype allocator_t | allocator_c(T, allocator_t)) | 
|---|
| 156 | static inline T* end(vector(T, allocator_t)* this) | 
|---|
| 157 | { | 
|---|
| 158 | return data(&this->storage) + this->size; | 
|---|
| 159 | } | 
|---|
| 160 |  | 
|---|
| 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 | // } | 
|---|
| 166 |  | 
|---|
| 167 | // Local Variables: // | 
|---|
| 168 | // mode: c // | 
|---|
| 169 | // tab-width: 4 // | 
|---|
| 170 | // End: // | 
|---|