| 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.c -- | 
|---|
| 8 | // | 
|---|
| 9 | // Author           : Thierry Delisle | 
|---|
| 10 | // Created On       : Tue Jul  5 18:07:52 2016 | 
|---|
| 11 | // Last Modified By : Peter A. Buhr | 
|---|
| 12 | // Last Modified On : Tue Jul  5 18:08:31 2016 | 
|---|
| 13 | // Update Count     : 2 | 
|---|
| 14 | // | 
|---|
| 15 |  | 
|---|
| 16 | #include <containers/vector.hfa> | 
|---|
| 17 |  | 
|---|
| 18 | #include <stdlib.hfa> | 
|---|
| 19 |  | 
|---|
| 20 | #pragma GCC visibility push(default) | 
|---|
| 21 |  | 
|---|
| 22 | forall(T, allocator_t | allocator_c(T, allocator_t)) | 
|---|
| 23 | static void copy_internal(vector(T, allocator_t)* this, vector(T, allocator_t)* other); | 
|---|
| 24 |  | 
|---|
| 25 | //------------------------------------------------------------------------------ | 
|---|
| 26 | //Initialization | 
|---|
| 27 | forall(T, allocator_t | allocator_c(T, allocator_t)) | 
|---|
| 28 | void ?{}(vector(T, allocator_t)& this) | 
|---|
| 29 | { | 
|---|
| 30 | (this.storage){}; | 
|---|
| 31 | this.size = 0; | 
|---|
| 32 | } | 
|---|
| 33 |  | 
|---|
| 34 | forall(T, allocator_t | allocator_c(T, allocator_t)) | 
|---|
| 35 | void ?{}(vector(T, allocator_t)& this, vector(T, allocator_t) rhs) | 
|---|
| 36 | { | 
|---|
| 37 | (this.storage){ rhs.storage }; | 
|---|
| 38 | copy_internal(&this, &rhs); | 
|---|
| 39 | } | 
|---|
| 40 |  | 
|---|
| 41 | // forall(T, allocator_t | allocator_c(T, allocator_t)) | 
|---|
| 42 | // vector(T, allocator_t) ?=?(vector(T, allocator_t)* this, vector(T, allocator_t) rhs) | 
|---|
| 43 | // { | 
|---|
| 44 | //      (&this->storage){}; | 
|---|
| 45 | //      copy_internal(this, &rhs); | 
|---|
| 46 | //      return *this; | 
|---|
| 47 | // } | 
|---|
| 48 |  | 
|---|
| 49 | forall(T, allocator_t | allocator_c(T, allocator_t)) | 
|---|
| 50 | void ^?{}(vector(T, allocator_t)& this) | 
|---|
| 51 | { | 
|---|
| 52 | clear(&this); | 
|---|
| 53 | ^(this.storage){}; | 
|---|
| 54 | } | 
|---|
| 55 |  | 
|---|
| 56 | //------------------------------------------------------------------------------ | 
|---|
| 57 | //Modifiers | 
|---|
| 58 | forall(T, allocator_t | allocator_c(T, allocator_t)) | 
|---|
| 59 | void push_back(vector(T, allocator_t)* this, T value) | 
|---|
| 60 | { | 
|---|
| 61 | realloc_storage(&this->storage, this->size+1); | 
|---|
| 62 | data(&this->storage)[this->size] = value; | 
|---|
| 63 | this->size++; | 
|---|
| 64 | } | 
|---|
| 65 |  | 
|---|
| 66 | forall(T, allocator_t | allocator_c(T, allocator_t)) | 
|---|
| 67 | void pop_back(vector(T, allocator_t)* this) | 
|---|
| 68 | { | 
|---|
| 69 | this->size--; | 
|---|
| 70 | ^(data(&this->storage)[this->size]){}; | 
|---|
| 71 | } | 
|---|
| 72 |  | 
|---|
| 73 | forall(T, allocator_t | allocator_c(T, allocator_t)) | 
|---|
| 74 | void clear(vector(T, allocator_t)* this) | 
|---|
| 75 | { | 
|---|
| 76 | for(size_t i = 0; i < this->size; i++) | 
|---|
| 77 | { | 
|---|
| 78 | ^(data(&this->storage)[this->size]){}; | 
|---|
| 79 | } | 
|---|
| 80 | this->size = 0; | 
|---|
| 81 | } | 
|---|
| 82 |  | 
|---|
| 83 | //------------------------------------------------------------------------------ | 
|---|
| 84 | //Internal Helpers | 
|---|
| 85 |  | 
|---|
| 86 | forall(T, allocator_t | allocator_c(T, allocator_t)) | 
|---|
| 87 | static void copy_internal(vector(T, allocator_t)* this, vector(T, allocator_t)* other) | 
|---|
| 88 | { | 
|---|
| 89 | this->size = other->size; | 
|---|
| 90 | for(size_t i = 0; i < this->size; i++) { | 
|---|
| 91 | (data(&this->storage)[this->size]){ data(&other->storage)[other->size] }; | 
|---|
| 92 | } | 
|---|
| 93 | } | 
|---|
| 94 |  | 
|---|
| 95 | //------------------------------------------------------------------------------ | 
|---|
| 96 | //Allocator | 
|---|
| 97 | forall(T) | 
|---|
| 98 | void ?{}(heap_allocator(T)& this) | 
|---|
| 99 | { | 
|---|
| 100 | this.storage = 0; | 
|---|
| 101 | this.capacity = 0; | 
|---|
| 102 | } | 
|---|
| 103 |  | 
|---|
| 104 | forall(T) | 
|---|
| 105 | void ?{}(heap_allocator(T)& this, heap_allocator(T) rhs) | 
|---|
| 106 | { | 
|---|
| 107 | this.capacity = rhs.capacity; | 
|---|
| 108 | this.storage = (T*)realloc((void*)this.storage, this.capacity * sizeof(T)); | 
|---|
| 109 | } | 
|---|
| 110 |  | 
|---|
| 111 | forall(T) | 
|---|
| 112 | heap_allocator(T) ?=?(heap_allocator(T)& this, heap_allocator(T) rhs) | 
|---|
| 113 | { | 
|---|
| 114 | this.capacity = rhs.capacity; | 
|---|
| 115 | this.storage = (T*)realloc((void*)this.storage, this.capacity * sizeof(T)); | 
|---|
| 116 | return this; | 
|---|
| 117 | } | 
|---|
| 118 |  | 
|---|
| 119 | forall(T) | 
|---|
| 120 | void ^?{}(heap_allocator(T)& this) | 
|---|
| 121 | { | 
|---|
| 122 | free(this.storage); | 
|---|
| 123 | } | 
|---|
| 124 |  | 
|---|
| 125 | forall(T) | 
|---|
| 126 | inline void realloc_storage(heap_allocator(T)* this, size_t size) | 
|---|
| 127 | { | 
|---|
| 128 | enum { GROWTH_RATE = 2 }; | 
|---|
| 129 | if(size > this->capacity) | 
|---|
| 130 | { | 
|---|
| 131 | this->capacity = GROWTH_RATE * size; | 
|---|
| 132 | this->storage = (T*)realloc((void*)this->storage, this->capacity * sizeof(T)); | 
|---|
| 133 | } | 
|---|
| 134 | } | 
|---|
| 135 |  | 
|---|
| 136 | // Local Variables: // | 
|---|
| 137 | // mode: c // | 
|---|
| 138 | // tab-width: 4 // | 
|---|
| 139 | // End: // | 
|---|