source: libcfa/src/collections/vector.cfa @ 9476549

Last change on this file since 9476549 was 55b060d, checked in by Peter A. Buhr <pabuhr@…>, 15 months ago

rename directories containers to collections

  • Property mode set to 100644
File size: 3.4 KB
RevLine 
[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
[55b060d]12// Last Modified On : Wed Aug 30 21:27:31 2023
13// Update Count     : 3
[60aa49a7]14//
[a6151ba]15
[55b060d]16#include <collections/vector.hfa>
[385c130]17
[73abe95]18#include <stdlib.hfa>
[16cfd8c]19
[accc9df9]20#pragma GCC visibility push(default)
21
[fd54fef]22forall(T, allocator_t | allocator_c(T, allocator_t))
[accc9df9]23static void copy_internal(vector(T, allocator_t)* this, vector(T, allocator_t)* other);
[bd34fc87]24
[385c130]25//------------------------------------------------------------------------------
26//Initialization
[fd54fef]27forall(T, allocator_t | allocator_c(T, allocator_t))
[aca65621]28void ?{}(vector(T, allocator_t)& this)
[385c130]29{
[aca65621]30        (this.storage){};
31        this.size = 0;
[385c130]32}
33
[fd54fef]34forall(T, allocator_t | allocator_c(T, allocator_t))
[aca65621]35void ?{}(vector(T, allocator_t)& this, vector(T, allocator_t) rhs)
[bd34fc87]36{
[aca65621]37        (this.storage){ rhs.storage };
38        copy_internal(&this, &rhs);
[bd34fc87]39}
40
[fd54fef]41// forall(T, allocator_t | allocator_c(T, allocator_t))
[bd34fc87]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
[fd54fef]49forall(T, allocator_t | allocator_c(T, allocator_t))
[aca65621]50void ^?{}(vector(T, allocator_t)& this)
[385c130]51{
[aca65621]52        clear(&this);
53        ^(this.storage){};
[385c130]54}
55
56//------------------------------------------------------------------------------
57//Modifiers
[fd54fef]58forall(T, allocator_t | allocator_c(T, allocator_t))
[bd34fc87]59void push_back(vector(T, allocator_t)* this, T value)
[385c130]60{
[60aa49a7]61        realloc_storage(&this->storage, this->size+1);
[385c130]62        data(&this->storage)[this->size] = value;
63        this->size++;
64}
65
[fd54fef]66forall(T, allocator_t | allocator_c(T, allocator_t))
[bd34fc87]67void pop_back(vector(T, allocator_t)* this)
[385c130]68{
69        this->size--;
[aca65621]70        ^(data(&this->storage)[this->size]){};
[385c130]71}
72
[fd54fef]73forall(T, allocator_t | allocator_c(T, allocator_t))
[bd34fc87]74void clear(vector(T, allocator_t)* this)
[385c130]75{
76        for(size_t i = 0; i < this->size; i++)
77        {
[aca65621]78                ^(data(&this->storage)[this->size]){};
[385c130]79        }
80        this->size = 0;
81}
82
[bd34fc87]83//------------------------------------------------------------------------------
84//Internal Helpers
85
[fd54fef]86forall(T, allocator_t | allocator_c(T, allocator_t))
[accc9df9]87static void copy_internal(vector(T, allocator_t)* this, vector(T, allocator_t)* other)
[bd34fc87]88{
89        this->size = other->size;
90        for(size_t i = 0; i < this->size; i++) {
[aca65621]91                (data(&this->storage)[this->size]){ data(&other->storage)[other->size] };
[bd34fc87]92        }
93}
94
[385c130]95//------------------------------------------------------------------------------
96//Allocator
[fd54fef]97forall(T)
[aca65621]98void ?{}(heap_allocator(T)& this)
[385c130]99{
[aca65621]100        this.storage = 0;
101        this.capacity = 0;
[385c130]102}
103
[fd54fef]104forall(T)
[aca65621]105void ?{}(heap_allocator(T)& this, heap_allocator(T) rhs)
[bd34fc87]106{
[aca65621]107        this.capacity = rhs.capacity;
108        this.storage = (T*)realloc((void*)this.storage, this.capacity * sizeof(T));
[bd34fc87]109}
110
[fd54fef]111forall(T)
[aca65621]112heap_allocator(T) ?=?(heap_allocator(T)& this, heap_allocator(T) rhs)
[bd34fc87]113{
[aca65621]114        this.capacity = rhs.capacity;
115        this.storage = (T*)realloc((void*)this.storage, this.capacity * sizeof(T));
116        return this;
[bd34fc87]117}
118
[fd54fef]119forall(T)
[aca65621]120void ^?{}(heap_allocator(T)& this)
[385c130]121{
[aca65621]122        free(this.storage);
[385c130]123}
124
[fd54fef]125forall(T)
[bd34fc87]126inline void realloc_storage(heap_allocator(T)* this, size_t size)
[385c130]127{
[1b5c81ed]128        enum { GROWTH_RATE = 2 };
[385c130]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}
[a6151ba]135
136// Local Variables: //
137// mode: c //
138// tab-width: 4 //
139// End: //
Note: See TracBrowser for help on using the repository browser.