source: libcfa/src/containers/vector.cfa @ 789f279

ADTast-experimentalpthread-emulationqualifiedEnum
Last change on this file since 789f279 was accc9df9, checked in by Thierry Delisle <tdelisle@…>, 2 years ago

Visibility containers lib

  • Property mode set to 100644
File size: 3.4 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.hfa>
17
18#include <stdlib.hfa>
19
20#pragma GCC visibility push(default)
21
22forall(T, allocator_t | allocator_c(T, allocator_t))
23static void copy_internal(vector(T, allocator_t)* this, vector(T, allocator_t)* other);
24
25//------------------------------------------------------------------------------
26//Initialization
27forall(T, allocator_t | allocator_c(T, allocator_t))
28void ?{}(vector(T, allocator_t)& this)
29{
30        (this.storage){};
31        this.size = 0;
32}
33
34forall(T, allocator_t | allocator_c(T, allocator_t))
35void ?{}(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
49forall(T, allocator_t | allocator_c(T, allocator_t))
50void ^?{}(vector(T, allocator_t)& this)
51{
52        clear(&this);
53        ^(this.storage){};
54}
55
56//------------------------------------------------------------------------------
57//Modifiers
58forall(T, allocator_t | allocator_c(T, allocator_t))
59void 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
66forall(T, allocator_t | allocator_c(T, allocator_t))
67void pop_back(vector(T, allocator_t)* this)
68{
69        this->size--;
70        ^(data(&this->storage)[this->size]){};
71}
72
73forall(T, allocator_t | allocator_c(T, allocator_t))
74void 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
86forall(T, allocator_t | allocator_c(T, allocator_t))
87static 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
97forall(T)
98void ?{}(heap_allocator(T)& this)
99{
100        this.storage = 0;
101        this.capacity = 0;
102}
103
104forall(T)
105void ?{}(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
111forall(T)
112heap_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
119forall(T)
120void ^?{}(heap_allocator(T)& this)
121{
122        free(this.storage);
123}
124
125forall(T)
126inline 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: //
Note: See TracBrowser for help on using the repository browser.