| 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 : Tue Jul  5 18:01:35 2016
 | 
|---|
| 13 | // Update Count     : 2
 | 
|---|
| 14 | //
 | 
|---|
| 15 | 
 | 
|---|
| 16 | #ifndef VECTOR_H
 | 
|---|
| 17 | #define VECTOR_H
 | 
|---|
| 18 | 
 | 
|---|
| 19 | extern "C" {
 | 
|---|
| 20 | #include <stdbool.h>
 | 
|---|
| 21 | }
 | 
|---|
| 22 | 
 | 
|---|
| 23 | //------------------------------------------------------------------------------
 | 
|---|
| 24 | //Declaration
 | 
|---|
| 25 | trait allocator_c(otype T, otype allocator_t)
 | 
|---|
| 26 | {
 | 
|---|
| 27 |         void realloc_storage(allocator_t*, size_t);
 | 
|---|
| 28 |         T* data(allocator_t*);
 | 
|---|
| 29 | };
 | 
|---|
| 30 | 
 | 
|---|
| 31 | forall(otype T, otype allocator_t | allocator_c(T, allocator_t))
 | 
|---|
| 32 | struct vector;
 | 
|---|
| 33 | 
 | 
|---|
| 34 | //------------------------------------------------------------------------------
 | 
|---|
| 35 | //Initialization
 | 
|---|
| 36 | forall(otype T, otype allocator_t | allocator_c(T, allocator_t))
 | 
|---|
| 37 | void ?{}(vector(T, allocator_t)* this);
 | 
|---|
| 38 | 
 | 
|---|
| 39 | forall(otype T, otype allocator_t | allocator_c(T, allocator_t))
 | 
|---|
| 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 | };
 | 
|---|
| 54 | 
 | 
|---|
| 55 | //------------------------------------------------------------------------------
 | 
|---|
| 56 | //Capacity
 | 
|---|
| 57 | forall(otype T, otype allocator_t | allocator_c(T, allocator_t))
 | 
|---|
| 58 | static inline bool empty(vector(T, allocator_t)* this)
 | 
|---|
| 59 | {
 | 
|---|
| 60 |         return this->size == 0;
 | 
|---|
| 61 | }
 | 
|---|
| 62 | 
 | 
|---|
| 63 | forall(otype T, otype allocator_t | allocator_c(T, allocator_t))
 | 
|---|
| 64 | static inline size_t size(vector(T, allocator_t)* this)
 | 
|---|
| 65 | {
 | 
|---|
| 66 |         return this->size;
 | 
|---|
| 67 | }
 | 
|---|
| 68 | 
 | 
|---|
| 69 | forall(otype T, otype allocator_t | allocator_c(T, allocator_t))
 | 
|---|
| 70 | static inline void reserve(vector(T, allocator_t)* this, size_t size)
 | 
|---|
| 71 | {
 | 
|---|
| 72 |         realloc_storage(&this->storage, this->size+1);
 | 
|---|
| 73 | }
 | 
|---|
| 74 | 
 | 
|---|
| 75 | //------------------------------------------------------------------------------
 | 
|---|
| 76 | //Element access
 | 
|---|
| 77 | forall(otype T, otype allocator_t | allocator_c(T, allocator_t))
 | 
|---|
| 78 | static inline T at(vector(T, allocator_t)* this, size_t index)
 | 
|---|
| 79 | {
 | 
|---|
| 80 |         return data(&this->storage)[index];
 | 
|---|
| 81 | }
 | 
|---|
| 82 | 
 | 
|---|
| 83 | forall(otype T, otype allocator_t | allocator_c(T, allocator_t))
 | 
|---|
| 84 | static inline T ?[?](vector(T, allocator_t)* this, size_t index)
 | 
|---|
| 85 | {
 | 
|---|
| 86 |         return data(&this->storage)[index];
 | 
|---|
| 87 | }
 | 
|---|
| 88 | 
 | 
|---|
| 89 | forall(otype T, otype allocator_t | allocator_c(T, allocator_t))
 | 
|---|
| 90 | static inline T front(vector(T, allocator_t)* this)
 | 
|---|
| 91 | {
 | 
|---|
| 92 |         return data(&this->storage)[0];
 | 
|---|
| 93 | }
 | 
|---|
| 94 | 
 | 
|---|
| 95 | forall(otype T, otype allocator_t | allocator_c(T, allocator_t))
 | 
|---|
| 96 | static inline T back(vector(T, allocator_t)* this)
 | 
|---|
| 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))
 | 
|---|
| 104 | void push_back(vector(T, allocator_t)* this, T value);
 | 
|---|
| 105 | 
 | 
|---|
| 106 | forall(otype T, otype allocator_t | allocator_c(T, allocator_t))
 | 
|---|
| 107 | void pop_back(vector(T, allocator_t)* this);
 | 
|---|
| 108 | 
 | 
|---|
| 109 | forall(otype T, otype allocator_t | allocator_c(T, allocator_t))
 | 
|---|
| 110 | void clear(vector(T, allocator_t)* this);
 | 
|---|
| 111 | 
 | 
|---|
| 112 | //------------------------------------------------------------------------------
 | 
|---|
| 113 | //Iterators
 | 
|---|
| 114 | forall(otype T, otype allocator_t | allocator_c(T, allocator_t))
 | 
|---|
| 115 | static inline T* begin(vector(T, allocator_t)* this)
 | 
|---|
| 116 | {
 | 
|---|
| 117 |         return data(&this->storage);
 | 
|---|
| 118 | }
 | 
|---|
| 119 | 
 | 
|---|
| 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 | }
 | 
|---|
| 125 | 
 | 
|---|
| 126 | forall(otype T, otype allocator_t | allocator_c(T, allocator_t))
 | 
|---|
| 127 | static inline T* end(vector(T, allocator_t)* this)
 | 
|---|
| 128 | {
 | 
|---|
| 129 |         return data(&this->storage) + this->size;
 | 
|---|
| 130 | }
 | 
|---|
| 131 | 
 | 
|---|
| 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 | }
 | 
|---|
| 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)
 | 
|---|
| 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);
 | 
|---|
| 155 | 
 | 
|---|
| 156 | forall(otype T)
 | 
|---|
| 157 | void ^?{}(heap_allocator(T)* this);
 | 
|---|
| 158 | 
 | 
|---|
| 159 | forall(otype T)
 | 
|---|
| 160 | void realloc_storage(heap_allocator(T)* this, size_t size);
 | 
|---|
| 161 | 
 | 
|---|
| 162 | forall(otype T)
 | 
|---|
| 163 | static inline T* data(heap_allocator(T)* this)
 | 
|---|
| 164 | {
 | 
|---|
| 165 |         return this->storage;
 | 
|---|
| 166 | }
 | 
|---|
| 167 | 
 | 
|---|
| 168 | #endif // VECTOR_H
 | 
|---|
| 169 | 
 | 
|---|
| 170 | // Local Variables: //
 | 
|---|
| 171 | // mode: c //
 | 
|---|
| 172 | // tab-width: 4 //
 | 
|---|
| 173 | // End: //
 | 
|---|