Changeset a6fe3de for src/libcfa/containers/vector.c
- Timestamp:
- Sep 19, 2016, 5:23:45 PM (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:
- db46512
- Parents:
- 89e6ffc (diff), bd34fc87 (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the(diff)
links above to see all the changes relative to each parent. - File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
src/libcfa/containers/vector.c
r89e6ffc ra6fe3de 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 };
Note: See TracChangeset
for help on using the changeset viewer.