[60aa49a7] | 1 | // |
---|
[a6151ba] | 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. |
---|
[60aa49a7] | 6 | // |
---|
| 7 | // vector.c -- |
---|
| 8 | // |
---|
[a6151ba] | 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 |
---|
[60aa49a7] | 14 | // |
---|
[a6151ba] | 15 | |
---|
[58b6d1b] | 16 | #include <containers/vector.hfa> |
---|
[385c130] | 17 | |
---|
[73abe95] | 18 | #include <stdlib.hfa> |
---|
[16cfd8c] | 19 | |
---|
[accc9df9] | 20 | #pragma GCC visibility push(default) |
---|
| 21 | |
---|
[fd54fef] | 22 | forall(T, allocator_t | allocator_c(T, allocator_t)) |
---|
[accc9df9] | 23 | static void copy_internal(vector(T, allocator_t)* this, vector(T, allocator_t)* other); |
---|
[bd34fc87] | 24 | |
---|
[385c130] | 25 | //------------------------------------------------------------------------------ |
---|
| 26 | //Initialization |
---|
[fd54fef] | 27 | forall(T, allocator_t | allocator_c(T, allocator_t)) |
---|
[aca65621] | 28 | void ?{}(vector(T, allocator_t)& this) |
---|
[385c130] | 29 | { |
---|
[aca65621] | 30 | (this.storage){}; |
---|
| 31 | this.size = 0; |
---|
[385c130] | 32 | } |
---|
| 33 | |
---|
[fd54fef] | 34 | forall(T, allocator_t | allocator_c(T, allocator_t)) |
---|
[aca65621] | 35 | void ?{}(vector(T, allocator_t)& this, vector(T, allocator_t) rhs) |
---|
[bd34fc87] | 36 | { |
---|
[aca65621] | 37 | (this.storage){ rhs.storage }; |
---|
| 38 | copy_internal(&this, &rhs); |
---|
[bd34fc87] | 39 | } |
---|
| 40 | |
---|
[fd54fef] | 41 | // forall(T, allocator_t | allocator_c(T, allocator_t)) |
---|
[bd34fc87] | 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 | |
---|
[fd54fef] | 49 | forall(T, allocator_t | allocator_c(T, allocator_t)) |
---|
[aca65621] | 50 | void ^?{}(vector(T, allocator_t)& this) |
---|
[385c130] | 51 | { |
---|
[aca65621] | 52 | clear(&this); |
---|
| 53 | ^(this.storage){}; |
---|
[385c130] | 54 | } |
---|
| 55 | |
---|
| 56 | //------------------------------------------------------------------------------ |
---|
| 57 | //Modifiers |
---|
[fd54fef] | 58 | forall(T, allocator_t | allocator_c(T, allocator_t)) |
---|
[bd34fc87] | 59 | void push_back(vector(T, allocator_t)* this, T value) |
---|
[385c130] | 60 | { |
---|
[60aa49a7] | 61 | realloc_storage(&this->storage, this->size+1); |
---|
[385c130] | 62 | data(&this->storage)[this->size] = value; |
---|
| 63 | this->size++; |
---|
| 64 | } |
---|
| 65 | |
---|
[fd54fef] | 66 | forall(T, allocator_t | allocator_c(T, allocator_t)) |
---|
[bd34fc87] | 67 | void pop_back(vector(T, allocator_t)* this) |
---|
[385c130] | 68 | { |
---|
| 69 | this->size--; |
---|
[aca65621] | 70 | ^(data(&this->storage)[this->size]){}; |
---|
[385c130] | 71 | } |
---|
| 72 | |
---|
[fd54fef] | 73 | forall(T, allocator_t | allocator_c(T, allocator_t)) |
---|
[bd34fc87] | 74 | void clear(vector(T, allocator_t)* this) |
---|
[385c130] | 75 | { |
---|
| 76 | for(size_t i = 0; i < this->size; i++) |
---|
| 77 | { |
---|
[aca65621] | 78 | ^(data(&this->storage)[this->size]){}; |
---|
[385c130] | 79 | } |
---|
| 80 | this->size = 0; |
---|
| 81 | } |
---|
| 82 | |
---|
[bd34fc87] | 83 | //------------------------------------------------------------------------------ |
---|
| 84 | //Internal Helpers |
---|
| 85 | |
---|
[fd54fef] | 86 | forall(T, allocator_t | allocator_c(T, allocator_t)) |
---|
[accc9df9] | 87 | static void copy_internal(vector(T, allocator_t)* this, vector(T, allocator_t)* other) |
---|
[bd34fc87] | 88 | { |
---|
| 89 | this->size = other->size; |
---|
| 90 | for(size_t i = 0; i < this->size; i++) { |
---|
[aca65621] | 91 | (data(&this->storage)[this->size]){ data(&other->storage)[other->size] }; |
---|
[bd34fc87] | 92 | } |
---|
| 93 | } |
---|
| 94 | |
---|
[385c130] | 95 | //------------------------------------------------------------------------------ |
---|
| 96 | //Allocator |
---|
[fd54fef] | 97 | forall(T) |
---|
[aca65621] | 98 | void ?{}(heap_allocator(T)& this) |
---|
[385c130] | 99 | { |
---|
[aca65621] | 100 | this.storage = 0; |
---|
| 101 | this.capacity = 0; |
---|
[385c130] | 102 | } |
---|
| 103 | |
---|
[fd54fef] | 104 | forall(T) |
---|
[aca65621] | 105 | void ?{}(heap_allocator(T)& this, heap_allocator(T) rhs) |
---|
[bd34fc87] | 106 | { |
---|
[aca65621] | 107 | this.capacity = rhs.capacity; |
---|
| 108 | this.storage = (T*)realloc((void*)this.storage, this.capacity * sizeof(T)); |
---|
[bd34fc87] | 109 | } |
---|
| 110 | |
---|
[fd54fef] | 111 | forall(T) |
---|
[aca65621] | 112 | heap_allocator(T) ?=?(heap_allocator(T)& this, heap_allocator(T) rhs) |
---|
[bd34fc87] | 113 | { |
---|
[aca65621] | 114 | this.capacity = rhs.capacity; |
---|
| 115 | this.storage = (T*)realloc((void*)this.storage, this.capacity * sizeof(T)); |
---|
| 116 | return this; |
---|
[bd34fc87] | 117 | } |
---|
| 118 | |
---|
[fd54fef] | 119 | forall(T) |
---|
[aca65621] | 120 | void ^?{}(heap_allocator(T)& this) |
---|
[385c130] | 121 | { |
---|
[aca65621] | 122 | free(this.storage); |
---|
[385c130] | 123 | } |
---|
| 124 | |
---|
[fd54fef] | 125 | forall(T) |
---|
[bd34fc87] | 126 | inline void realloc_storage(heap_allocator(T)* this, size_t size) |
---|
[385c130] | 127 | { |
---|
[1b5c81ed] | 128 | enum { GROWTH_RATE = 2 }; |
---|
[385c130] | 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 | } |
---|
[a6151ba] | 135 | |
---|
| 136 | // Local Variables: // |
---|
| 137 | // mode: c // |
---|
| 138 | // tab-width: 4 // |
---|
| 139 | // End: // |
---|