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>
|
---|
17 |
|
---|
18 | #include <stdlib>
|
---|
19 |
|
---|
20 | //------------------------------------------------------------------------------
|
---|
21 | //Initialization
|
---|
22 | forall(otype T, otype allocator_t | allocator_c(T, allocator_t))
|
---|
23 | void ctor(vector(T, allocator_t) *const this)
|
---|
24 | {
|
---|
25 | ctor(&this->storage);
|
---|
26 | this->size = 0;
|
---|
27 | }
|
---|
28 |
|
---|
29 | forall(otype T, otype allocator_t | allocator_c(T, allocator_t))
|
---|
30 | void dtor(vector(T, allocator_t) *const this)
|
---|
31 | {
|
---|
32 | clear(this);
|
---|
33 | dtor(&this->storage);
|
---|
34 | }
|
---|
35 |
|
---|
36 | //------------------------------------------------------------------------------
|
---|
37 | //Modifiers
|
---|
38 | forall(otype T, otype allocator_t | allocator_c(T, allocator_t))
|
---|
39 | void push_back(vector(T, allocator_t) *const this, T value)
|
---|
40 | {
|
---|
41 | realloc(&this->storage, this->size+1);
|
---|
42 | data(&this->storage)[this->size] = value;
|
---|
43 | this->size++;
|
---|
44 | }
|
---|
45 |
|
---|
46 | forall(otype T, otype allocator_t | allocator_c(T, allocator_t))
|
---|
47 | void pop_back(vector(T, allocator_t) *const this)
|
---|
48 | {
|
---|
49 | this->size--;
|
---|
50 | DESTROY(data(&this->storage)[this->size]);
|
---|
51 | }
|
---|
52 |
|
---|
53 | forall(otype T, otype allocator_t | allocator_c(T, allocator_t))
|
---|
54 | void clear(vector(T, allocator_t) *const this)
|
---|
55 | {
|
---|
56 | for(size_t i = 0; i < this->size; i++)
|
---|
57 | {
|
---|
58 | DESTROY(data(&this->storage)[this->size]);
|
---|
59 | }
|
---|
60 | this->size = 0;
|
---|
61 | }
|
---|
62 |
|
---|
63 | //------------------------------------------------------------------------------
|
---|
64 | //Allocator
|
---|
65 | forall(otype T)
|
---|
66 | void ctor(heap_allocator(T) *const this)
|
---|
67 | {
|
---|
68 | this->storage = 0;
|
---|
69 | this->capacity = 0;
|
---|
70 | }
|
---|
71 |
|
---|
72 | forall(otype T)
|
---|
73 | void dtor(heap_allocator(T) *const this)
|
---|
74 | {
|
---|
75 | free(this->storage);
|
---|
76 | }
|
---|
77 |
|
---|
78 | forall(otype T)
|
---|
79 | inline void realloc(heap_allocator(T) *const this, size_t size)
|
---|
80 | {
|
---|
81 | enum { GROWTH_RATE = 2 };
|
---|
82 | if(size > this->capacity)
|
---|
83 | {
|
---|
84 | this->capacity = GROWTH_RATE * size;
|
---|
85 | this->storage = (T*)realloc((void*)this->storage, this->capacity * sizeof(T));
|
---|
86 | }
|
---|
87 | }
|
---|
88 |
|
---|
89 | // Local Variables: //
|
---|
90 | // mode: c //
|
---|
91 | // tab-width: 4 //
|
---|
92 | // End: //
|
---|