source: src/libcfa/containers/vector.c @ 4999940

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsctordeferred_resndemanglerenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumresolv-newwith_gc
Last change on this file since 4999940 was 60aa49a7, checked in by Thierry Delisle <tdelisle@…>, 8 years ago

refactored vector trait to speed-up compilation

  • Property mode set to 100644
File size: 2.2 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
20//------------------------------------------------------------------------------
21//Initialization
22forall(otype T, otype allocator_t | allocator_c(T, allocator_t))
23void ctor(vector(T, allocator_t) *const this)
24{
25        ctor(&this->storage);
26        this->size = 0;
27}
28
29forall(otype T, otype allocator_t | allocator_c(T, allocator_t))
30void dtor(vector(T, allocator_t) *const this)
31{
32        clear(this);
33        dtor(&this->storage);
34}
35
36//------------------------------------------------------------------------------
37//Modifiers
38forall(otype T, otype allocator_t | allocator_c(T, allocator_t))
39void push_back(vector(T, allocator_t) *const this, T value)
40{
41        realloc_storage(&this->storage, this->size+1);
42        data(&this->storage)[this->size] = value;
43        this->size++;
44}
45
46forall(otype T, otype allocator_t | allocator_c(T, allocator_t))
47void pop_back(vector(T, allocator_t) *const this)
48{
49        this->size--;
50        DESTROY(data(&this->storage)[this->size]);
51}
52
53forall(otype T, otype allocator_t | allocator_c(T, allocator_t))
54void 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
65forall(otype T)
66void ctor(heap_allocator(T) *const this)
67{
68        this->storage = 0;
69        this->capacity = 0;
70}
71
72forall(otype T)
73void dtor(heap_allocator(T) *const this)
74{
75        free(this->storage);
76}
77
78forall(otype T)
79inline void realloc_storage(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: //
Note: See TracBrowser for help on using the repository browser.