[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 |
|
---|
[bd34fc87] | 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 |
|
---|
[385c130] | 23 | //------------------------------------------------------------------------------
|
---|
| 24 | //Initialization
|
---|
| 25 | forall(otype T, otype allocator_t | allocator_c(T, allocator_t))
|
---|
[aca65621] | 26 | void ?{}(vector(T, allocator_t)& this)
|
---|
[385c130] | 27 | {
|
---|
[aca65621] | 28 | (this.storage){};
|
---|
| 29 | this.size = 0;
|
---|
[385c130] | 30 | }
|
---|
| 31 |
|
---|
| 32 | forall(otype T, otype allocator_t | allocator_c(T, allocator_t))
|
---|
[aca65621] | 33 | void ?{}(vector(T, allocator_t)& this, vector(T, allocator_t) rhs)
|
---|
[bd34fc87] | 34 | {
|
---|
[aca65621] | 35 | (this.storage){ rhs.storage };
|
---|
| 36 | copy_internal(&this, &rhs);
|
---|
[bd34fc87] | 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))
|
---|
[aca65621] | 48 | void ^?{}(vector(T, allocator_t)& this)
|
---|
[385c130] | 49 | {
|
---|
[aca65621] | 50 | clear(&this);
|
---|
| 51 | ^(this.storage){};
|
---|
[385c130] | 52 | }
|
---|
| 53 |
|
---|
| 54 | //------------------------------------------------------------------------------
|
---|
| 55 | //Modifiers
|
---|
| 56 | forall(otype T, otype allocator_t | allocator_c(T, allocator_t))
|
---|
[bd34fc87] | 57 | void push_back(vector(T, allocator_t)* this, T value)
|
---|
[385c130] | 58 | {
|
---|
[60aa49a7] | 59 | realloc_storage(&this->storage, this->size+1);
|
---|
[385c130] | 60 | data(&this->storage)[this->size] = value;
|
---|
| 61 | this->size++;
|
---|
| 62 | }
|
---|
| 63 |
|
---|
| 64 | forall(otype T, otype allocator_t | allocator_c(T, allocator_t))
|
---|
[bd34fc87] | 65 | void pop_back(vector(T, allocator_t)* this)
|
---|
[385c130] | 66 | {
|
---|
| 67 | this->size--;
|
---|
[aca65621] | 68 | ^(data(&this->storage)[this->size]){};
|
---|
[385c130] | 69 | }
|
---|
| 70 |
|
---|
| 71 | forall(otype T, otype allocator_t | allocator_c(T, allocator_t))
|
---|
[bd34fc87] | 72 | void clear(vector(T, allocator_t)* this)
|
---|
[385c130] | 73 | {
|
---|
| 74 | for(size_t i = 0; i < this->size; i++)
|
---|
| 75 | {
|
---|
[aca65621] | 76 | ^(data(&this->storage)[this->size]){};
|
---|
[385c130] | 77 | }
|
---|
| 78 | this->size = 0;
|
---|
| 79 | }
|
---|
| 80 |
|
---|
[bd34fc87] | 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++) {
|
---|
[aca65621] | 89 | (data(&this->storage)[this->size]){ data(&other->storage)[other->size] };
|
---|
[bd34fc87] | 90 | }
|
---|
| 91 | }
|
---|
| 92 |
|
---|
[385c130] | 93 | //------------------------------------------------------------------------------
|
---|
| 94 | //Allocator
|
---|
| 95 | forall(otype T)
|
---|
[aca65621] | 96 | void ?{}(heap_allocator(T)& this)
|
---|
[385c130] | 97 | {
|
---|
[aca65621] | 98 | this.storage = 0;
|
---|
| 99 | this.capacity = 0;
|
---|
[385c130] | 100 | }
|
---|
| 101 |
|
---|
| 102 | forall(otype T)
|
---|
[aca65621] | 103 | void ?{}(heap_allocator(T)& this, heap_allocator(T) rhs)
|
---|
[bd34fc87] | 104 | {
|
---|
[aca65621] | 105 | this.capacity = rhs.capacity;
|
---|
| 106 | this.storage = (T*)realloc((void*)this.storage, this.capacity * sizeof(T));
|
---|
[bd34fc87] | 107 | }
|
---|
| 108 |
|
---|
| 109 | forall(otype T)
|
---|
[aca65621] | 110 | heap_allocator(T) ?=?(heap_allocator(T)& this, heap_allocator(T) rhs)
|
---|
[bd34fc87] | 111 | {
|
---|
[aca65621] | 112 | this.capacity = rhs.capacity;
|
---|
| 113 | this.storage = (T*)realloc((void*)this.storage, this.capacity * sizeof(T));
|
---|
| 114 | return this;
|
---|
[bd34fc87] | 115 | }
|
---|
| 116 |
|
---|
| 117 | forall(otype T)
|
---|
[aca65621] | 118 | void ^?{}(heap_allocator(T)& this)
|
---|
[385c130] | 119 | {
|
---|
[aca65621] | 120 | free(this.storage);
|
---|
[385c130] | 121 | }
|
---|
| 122 |
|
---|
| 123 | forall(otype T)
|
---|
[bd34fc87] | 124 | inline void realloc_storage(heap_allocator(T)* this, size_t size)
|
---|
[385c130] | 125 | {
|
---|
[1b5c81ed] | 126 | enum { GROWTH_RATE = 2 };
|
---|
[385c130] | 127 | if(size > this->capacity)
|
---|
| 128 | {
|
---|
| 129 | this->capacity = GROWTH_RATE * size;
|
---|
| 130 | this->storage = (T*)realloc((void*)this->storage, this->capacity * sizeof(T));
|
---|
| 131 | }
|
---|
| 132 | }
|
---|
[a6151ba] | 133 |
|
---|
| 134 | // Local Variables: //
|
---|
| 135 | // mode: c //
|
---|
| 136 | // tab-width: 4 //
|
---|
| 137 | // End: //
|
---|