source: src/libcfa/containers/vector.c @ 28bc8c8

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsdeferred_resndemanglerenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumresolv-newwith_gc
Last change on this file since 28bc8c8 was aca65621, checked in by Rob Schluntz <rschlunt@…>, 7 years ago

Convert several library files to use references

  • Property mode set to 100644
File size: 3.5 KB
Line 
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
20forall(otype T, otype allocator_t | allocator_c(T, allocator_t))
21void copy_internal(vector(T, allocator_t)* this, vector(T, allocator_t)* other);
22
23//------------------------------------------------------------------------------
24//Initialization
25forall(otype T, otype allocator_t | allocator_c(T, allocator_t))
26void ?{}(vector(T, allocator_t)& this)
27{
28        (this.storage){};
29        this.size = 0;
30}
31
32forall(otype T, otype allocator_t | allocator_c(T, allocator_t))
33void ?{}(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
47forall(otype T, otype allocator_t | allocator_c(T, allocator_t))
48void ^?{}(vector(T, allocator_t)& this)
49{
50        clear(&this);
51        ^(this.storage){};
52}
53
54//------------------------------------------------------------------------------
55//Modifiers
56forall(otype T, otype allocator_t | allocator_c(T, allocator_t))
57void push_back(vector(T, allocator_t)* this, T value)
58{
59        realloc_storage(&this->storage, this->size+1);
60        data(&this->storage)[this->size] = value;
61        this->size++;
62}
63
64forall(otype T, otype allocator_t | allocator_c(T, allocator_t))
65void pop_back(vector(T, allocator_t)* this)
66{
67        this->size--;
68        ^(data(&this->storage)[this->size]){};
69}
70
71forall(otype T, otype allocator_t | allocator_c(T, allocator_t))
72void clear(vector(T, allocator_t)* this)
73{
74        for(size_t i = 0; i < this->size; i++)
75        {
76                ^(data(&this->storage)[this->size]){};
77        }
78        this->size = 0;
79}
80
81//------------------------------------------------------------------------------
82//Internal Helpers
83
84forall(otype T, otype allocator_t | allocator_c(T, allocator_t))
85void 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//------------------------------------------------------------------------------
94//Allocator
95forall(otype T)
96void ?{}(heap_allocator(T)& this)
97{
98        this.storage = 0;
99        this.capacity = 0;
100}
101
102forall(otype T)
103void ?{}(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
109forall(otype T)
110heap_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
117forall(otype T)
118void ^?{}(heap_allocator(T)& this)
119{
120        free(this.storage);
121}
122
123forall(otype T)
124inline void realloc_storage(heap_allocator(T)* this, size_t size)
125{
126        enum { GROWTH_RATE = 2 };
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}
133
134// Local Variables: //
135// mode: c //
136// tab-width: 4 //
137// End: //
Note: See TracBrowser for help on using the repository browser.