Changeset bd34fc87
- Timestamp:
- Sep 19, 2016, 9:56:27 AM (8 years ago)
- Branches:
- ADT, aaron-thesis, arm-eh, ast-experimental, cleanup-dtors, deferred_resn, demangler, enum, forall-pointer-decay, jacob/cs343-translation, jenkins-sandbox, master, new-ast, new-ast-unique-expr, new-env, no_list, persistent-indexer, pthread-emulation, qualifiedEnum, resolv-new, with_gc
- Children:
- 4e8d8a23, 694ee7d, a6fe3de
- Parents:
- 24bc651
- Location:
- src
- Files:
-
- 1 added
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
src/libcfa/containers/vector
r24bc651 rbd34fc87 20 20 } 21 21 22 #define DESTROY(x)23 24 22 //------------------------------------------------------------------------------ 25 23 //Declaration 26 24 trait allocator_c(otype T, otype allocator_t) 27 25 { 28 void ctor(allocator_t* const); 29 void dtor(allocator_t* const); 30 void realloc_storage(allocator_t* const, size_t); 31 T* data(allocator_t* const); 26 void realloc_storage(allocator_t*, size_t); 27 T* data(allocator_t*); 32 28 }; 29 30 forall(otype T, otype allocator_t | allocator_c(T, allocator_t)) 31 struct vector; 32 33 //------------------------------------------------------------------------------ 34 //Initialization 35 forall(otype T, otype allocator_t | allocator_c(T, allocator_t)) 36 void ?{}(vector(T, allocator_t)* this); 37 38 forall(otype T, otype allocator_t | allocator_c(T, allocator_t)) 39 void ?{}(vector(T, allocator_t)* this, vector(T, allocator_t) rhs); 40 41 forall(otype T, otype allocator_t | allocator_c(T, allocator_t)) 42 vector(T, allocator_t) ?=?(vector(T, allocator_t)* this, vector(T, allocator_t) rhs); 43 44 forall(otype T, otype allocator_t | allocator_c(T, allocator_t)) 45 void ^?{}(vector(T, allocator_t)* this); 33 46 34 47 forall(otype T, otype allocator_t | allocator_c(T, allocator_t)) … … 40 53 41 54 //------------------------------------------------------------------------------ 42 //Initialization43 forall(otype T, otype allocator_t | allocator_c(T, allocator_t))44 void ctor(vector(T, allocator_t) *const this);45 46 forall(otype T, otype allocator_t | allocator_c(T, allocator_t))47 void dtor(vector(T, allocator_t) *const this);48 49 //------------------------------------------------------------------------------50 55 //Capacity 51 56 forall(otype T, otype allocator_t | allocator_c(T, allocator_t)) 52 static inline bool empty(vector(T, allocator_t) *constthis)57 static inline bool empty(vector(T, allocator_t)* this) 53 58 { 54 59 return this->size == 0; … … 56 61 57 62 forall(otype T, otype allocator_t | allocator_c(T, allocator_t)) 58 static inline size_t size(vector(T, allocator_t) *constthis)63 static inline size_t size(vector(T, allocator_t)* this) 59 64 { 60 65 return this->size; … … 62 67 63 68 forall(otype T, otype allocator_t | allocator_c(T, allocator_t)) 64 static inline void reserve(vector(T, allocator_t) *constthis, size_t size)69 static inline void reserve(vector(T, allocator_t)* this, size_t size) 65 70 { 66 71 realloc_storage(&this->storage, this->size+1); … … 70 75 //Element access 71 76 forall(otype T, otype allocator_t | allocator_c(T, allocator_t)) 72 static inline T at(vector(T, allocator_t) *constthis, size_t index)77 static inline T at(vector(T, allocator_t)* this, size_t index) 73 78 { 74 79 return data(&this->storage)[index]; … … 76 81 77 82 forall(otype T, otype allocator_t | allocator_c(T, allocator_t)) 78 static inline T ?[?](vector(T, allocator_t) *constthis, size_t index)83 static inline T ?[?](vector(T, allocator_t)* this, size_t index) 79 84 { 80 85 return data(&this->storage)[index]; … … 82 87 83 88 forall(otype T, otype allocator_t | allocator_c(T, allocator_t)) 84 static inline T front(vector(T, allocator_t) *constthis)89 static inline T front(vector(T, allocator_t)* this) 85 90 { 86 91 return data(&this->storage)[0]; … … 88 93 89 94 forall(otype T, otype allocator_t | allocator_c(T, allocator_t)) 90 static inline T back(vector(T, allocator_t) *constthis)95 static inline T back(vector(T, allocator_t)* this) 91 96 { 92 97 return data(&this->storage)[this->size - 1]; … … 96 101 //Modifiers 97 102 forall(otype T, otype allocator_t | allocator_c(T, allocator_t)) 98 void push_back(vector(T, allocator_t) *constthis, T value);103 void push_back(vector(T, allocator_t)* this, T value); 99 104 100 105 forall(otype T, otype allocator_t | allocator_c(T, allocator_t)) 101 void pop_back(vector(T, allocator_t) *constthis);106 void pop_back(vector(T, allocator_t)* this); 102 107 103 108 forall(otype T, otype allocator_t | allocator_c(T, allocator_t)) 104 void clear(vector(T, allocator_t) *constthis);109 void clear(vector(T, allocator_t)* this); 105 110 106 111 //------------------------------------------------------------------------------ 107 112 //Iterators 108 113 forall(otype T, otype allocator_t | allocator_c(T, allocator_t)) 109 static inline T* begin(vector(T, allocator_t) *constthis)114 static inline T* begin(vector(T, allocator_t)* this) 110 115 { 111 116 return data(&this->storage); … … 113 118 114 119 forall(otype T, otype allocator_t | allocator_c(T, allocator_t)) 115 static inline const T* cbegin(const vector(T, allocator_t) *constthis)120 static inline const T* cbegin(const vector(T, allocator_t)* this) 116 121 { 117 122 return data(&this->storage); … … 119 124 120 125 forall(otype T, otype allocator_t | allocator_c(T, allocator_t)) 121 static inline T* end(vector(T, allocator_t) *constthis)126 static inline T* end(vector(T, allocator_t)* this) 122 127 { 123 128 return data(&this->storage) + this->size; … … 125 130 126 131 forall(otype T, otype allocator_t | allocator_c(T, allocator_t)) 127 static inline const T* cend(const vector(T, allocator_t) *constthis)132 static inline const T* cend(const vector(T, allocator_t)* this) 128 133 { 129 134 return data(&this->storage) + this->size; … … 140 145 141 146 forall(otype T) 142 void ctor(heap_allocator(T) *constthis);147 void ?{}(heap_allocator(T)* this); 143 148 144 149 forall(otype T) 145 void dtor(heap_allocator(T) *const this);150 void ?{}(heap_allocator(T)* this, heap_allocator(T) rhs); 146 151 147 152 forall(otype T) 148 void realloc_storage(heap_allocator(T) *const this, size_t size);153 heap_allocator(T) ?=?(heap_allocator(T)* this, heap_allocator(T) rhs); 149 154 150 155 forall(otype T) 151 static inline T* data(heap_allocator(T) *const this) 156 void ^?{}(heap_allocator(T)* this); 157 158 forall(otype T) 159 void realloc_storage(heap_allocator(T)* this, size_t size); 160 161 forall(otype T) 162 static inline T* data(heap_allocator(T)* this) 152 163 { 153 164 return this->storage; -
src/libcfa/containers/vector.c
r24bc651 rbd34fc87 18 18 #include <stdlib> 19 19 20 forall(otype T, otype allocator_t | allocator_c(T, allocator_t)) 21 void copy_internal(vector(T, allocator_t)* this, vector(T, allocator_t)* other); 22 20 23 //------------------------------------------------------------------------------ 21 24 //Initialization 22 25 forall(otype T, otype allocator_t | allocator_c(T, allocator_t)) 23 void ctor(vector(T, allocator_t) *constthis)26 void ?{}(vector(T, allocator_t)* this) 24 27 { 25 ctor(&this->storage);28 (&this->storage){}; 26 29 this->size = 0; 27 30 } 28 31 29 32 forall(otype T, otype allocator_t | allocator_c(T, allocator_t)) 30 void dtor(vector(T, allocator_t) *const this) 33 void ?{}(vector(T, allocator_t)* this, vector(T, allocator_t) rhs) 34 { 35 (&this->storage){ rhs.storage }; 36 copy_internal(this, &rhs); 37 } 38 39 // forall(otype T, otype allocator_t | allocator_c(T, allocator_t)) 40 // vector(T, allocator_t) ?=?(vector(T, allocator_t)* this, vector(T, allocator_t) rhs) 41 // { 42 // (&this->storage){}; 43 // copy_internal(this, &rhs); 44 // return *this; 45 // } 46 47 forall(otype T, otype allocator_t | allocator_c(T, allocator_t)) 48 void ^?{}(vector(T, allocator_t)* this) 31 49 { 32 50 clear(this); 33 dtor(&this->storage);51 ^(&this->storage){}; 34 52 } 35 53 … … 37 55 //Modifiers 38 56 forall(otype T, otype allocator_t | allocator_c(T, allocator_t)) 39 void push_back(vector(T, allocator_t) *constthis, T value)57 void push_back(vector(T, allocator_t)* this, T value) 40 58 { 41 59 realloc_storage(&this->storage, this->size+1); … … 45 63 46 64 forall(otype T, otype allocator_t | allocator_c(T, allocator_t)) 47 void pop_back(vector(T, allocator_t) *constthis)65 void pop_back(vector(T, allocator_t)* this) 48 66 { 49 67 this->size--; 50 DESTROY(data(&this->storage)[this->size]);68 ^(&data(&this->storage)[this->size]){}; 51 69 } 52 70 53 71 forall(otype T, otype allocator_t | allocator_c(T, allocator_t)) 54 void clear(vector(T, allocator_t) *constthis)72 void clear(vector(T, allocator_t)* this) 55 73 { 56 74 for(size_t i = 0; i < this->size; i++) 57 75 { 58 DESTROY(data(&this->storage)[this->size]);76 ^(&data(&this->storage)[this->size]){}; 59 77 } 60 78 this->size = 0; … … 62 80 63 81 //------------------------------------------------------------------------------ 82 //Internal Helpers 83 84 forall(otype T, otype allocator_t | allocator_c(T, allocator_t)) 85 void copy_internal(vector(T, allocator_t)* this, vector(T, allocator_t)* other) 86 { 87 this->size = other->size; 88 for(size_t i = 0; i < this->size; i++) { 89 (&data(&this->storage)[this->size]){ data(&other->storage)[other->size] }; 90 } 91 } 92 93 //------------------------------------------------------------------------------ 64 94 //Allocator 65 95 forall(otype T) 66 void ctor(heap_allocator(T) *constthis)96 void ?{}(heap_allocator(T)* this) 67 97 { 68 98 this->storage = 0; … … 71 101 72 102 forall(otype T) 73 void dtor(heap_allocator(T) *const this) 103 void ?{}(heap_allocator(T)* this, heap_allocator(T) rhs) 104 { 105 this->capacity = rhs.capacity; 106 this->storage = (T*)realloc((void*)this->storage, this->capacity * sizeof(T)); 107 } 108 109 forall(otype T) 110 heap_allocator(T) ?=?(heap_allocator(T)* this, heap_allocator(T) rhs) 111 { 112 this->capacity = rhs.capacity; 113 this->storage = (T*)realloc((void*)this->storage, this->capacity * sizeof(T)); 114 return *this; 115 } 116 117 forall(otype T) 118 void ^?{}(heap_allocator(T)* this) 74 119 { 75 120 free(this->storage); … … 77 122 78 123 forall(otype T) 79 inline void realloc_storage(heap_allocator(T) *constthis, size_t size)124 inline void realloc_storage(heap_allocator(T)* this, size_t size) 80 125 { 81 126 enum { GROWTH_RATE = 2 }; -
src/tests/libcfa_vector.c
r24bc651 rbd34fc87 1 // 1 // 2 2 // Cforall Version 1.0.0 Copyright (C) 2016 University of Waterloo 3 3 // 4 4 // The contents of this file are covered under the licence agreement in the 5 5 // file "LICENCE" distributed with Cforall. 6 // 7 // libcfa_vector.c -- 8 // 6 // 7 // libcfa_vector.c -- 8 // 9 9 // Author : Thierry Delisle 10 10 // Created On : Mon Jul 4 23:36:19 2016 … … 12 12 // Last Modified On : Tue Jul 5 15:08:05 2016 13 13 // Update Count : 26 14 // 14 // 15 15 16 16 #include <fstream> … … 28 28 int main() { 29 29 vector( int, heap_allocator(int) ) iv; 30 ctor( &iv );31 30 32 31 assert( empty( &iv ) );
Note: See TracChangeset
for help on using the changeset viewer.