Changeset bd34fc87


Ignore:
Timestamp:
Sep 19, 2016, 9:56:27 AM (5 years ago)
Author:
Thierry Delisle <tdelisle@…>
Branches:
aaron-thesis, arm-eh, cleanup-dtors, deferred_resn, demangler, jacob/cs343-translation, jenkins-sandbox, master, new-ast, new-ast-unique-expr, new-env, no_list, persistent-indexer, resolv-new, with_gc
Children:
4e8d8a2, 694ee7d, a6fe3de
Parents:
24bc651
Message:

reenabled vector tests and added proper constructor semantics to vector

Location:
src
Files:
1 added
3 edited

Legend:

Unmodified
Added
Removed
  • src/libcfa/containers/vector

    r24bc651 rbd34fc87  
    2020}
    2121
    22 #define DESTROY(x)
    23 
    2422//------------------------------------------------------------------------------
    2523//Declaration
    2624trait allocator_c(otype T, otype allocator_t)
    2725{
    28         void ctor(allocator_t* const);
    29         void dtor(allocator_t* const);
    30         void realloc_storage(allocator_t* const, size_t);
    31         T* data(allocator_t* const);
     26        void realloc_storage(allocator_t*, size_t);
     27        T* data(allocator_t*);
    3228};
     29
     30forall(otype T, otype allocator_t | allocator_c(T, allocator_t))
     31struct vector;
     32
     33//------------------------------------------------------------------------------
     34//Initialization
     35forall(otype T, otype allocator_t | allocator_c(T, allocator_t))
     36void ?{}(vector(T, allocator_t)* this);
     37
     38forall(otype T, otype allocator_t | allocator_c(T, allocator_t))
     39void ?{}(vector(T, allocator_t)* this, vector(T, allocator_t) rhs);
     40
     41forall(otype T, otype allocator_t | allocator_c(T, allocator_t))
     42vector(T, allocator_t) ?=?(vector(T, allocator_t)* this, vector(T, allocator_t) rhs);
     43
     44forall(otype T, otype allocator_t | allocator_c(T, allocator_t))
     45void ^?{}(vector(T, allocator_t)* this);
    3346
    3447forall(otype T, otype allocator_t | allocator_c(T, allocator_t))
     
    4053
    4154//------------------------------------------------------------------------------
    42 //Initialization
    43 forall(otype T, otype allocator_t | allocator_c(T, allocator_t))
    44 void ctor(vector(T, allocator_t) *const this);
    45 
    46 forall(otype T, otype allocator_t | allocator_c(T, allocator_t))
    47 void dtor(vector(T, allocator_t) *const this);
    48 
    49 //------------------------------------------------------------------------------
    5055//Capacity
    5156forall(otype T, otype allocator_t | allocator_c(T, allocator_t))
    52 static inline bool empty(vector(T, allocator_t) *const this)
     57static inline bool empty(vector(T, allocator_t)* this)
    5358{
    5459        return this->size == 0;
     
    5661
    5762forall(otype T, otype allocator_t | allocator_c(T, allocator_t))
    58 static inline size_t size(vector(T, allocator_t) *const this)
     63static inline size_t size(vector(T, allocator_t)* this)
    5964{
    6065        return this->size;
     
    6267
    6368forall(otype T, otype allocator_t | allocator_c(T, allocator_t))
    64 static inline void reserve(vector(T, allocator_t) *const this, size_t size)
     69static inline void reserve(vector(T, allocator_t)* this, size_t size)
    6570{
    6671        realloc_storage(&this->storage, this->size+1);
     
    7075//Element access
    7176forall(otype T, otype allocator_t | allocator_c(T, allocator_t))
    72 static inline T at(vector(T, allocator_t) *const this, size_t index)
     77static inline T at(vector(T, allocator_t)* this, size_t index)
    7378{
    7479        return data(&this->storage)[index];
     
    7681
    7782forall(otype T, otype allocator_t | allocator_c(T, allocator_t))
    78 static inline T ?[?](vector(T, allocator_t) *const this, size_t index)
     83static inline T ?[?](vector(T, allocator_t)* this, size_t index)
    7984{
    8085        return data(&this->storage)[index];
     
    8287
    8388forall(otype T, otype allocator_t | allocator_c(T, allocator_t))
    84 static inline T front(vector(T, allocator_t) *const this)
     89static inline T front(vector(T, allocator_t)* this)
    8590{
    8691        return data(&this->storage)[0];
     
    8893
    8994forall(otype T, otype allocator_t | allocator_c(T, allocator_t))
    90 static inline T back(vector(T, allocator_t) *const this)
     95static inline T back(vector(T, allocator_t)* this)
    9196{
    9297        return data(&this->storage)[this->size - 1];
     
    96101//Modifiers
    97102forall(otype T, otype allocator_t | allocator_c(T, allocator_t))
    98 void push_back(vector(T, allocator_t) *const this, T value);
     103void push_back(vector(T, allocator_t)* this, T value);
    99104
    100105forall(otype T, otype allocator_t | allocator_c(T, allocator_t))
    101 void pop_back(vector(T, allocator_t) *const this);
     106void pop_back(vector(T, allocator_t)* this);
    102107
    103108forall(otype T, otype allocator_t | allocator_c(T, allocator_t))
    104 void clear(vector(T, allocator_t) *const this);
     109void clear(vector(T, allocator_t)* this);
    105110
    106111//------------------------------------------------------------------------------
    107112//Iterators
    108113forall(otype T, otype allocator_t | allocator_c(T, allocator_t))
    109 static inline T* begin(vector(T, allocator_t) *const this)
     114static inline T* begin(vector(T, allocator_t)* this)
    110115{
    111116        return data(&this->storage);
     
    113118
    114119forall(otype T, otype allocator_t | allocator_c(T, allocator_t))
    115 static inline const T* cbegin(const vector(T, allocator_t) *const this)
     120static inline const T* cbegin(const vector(T, allocator_t)* this)
    116121{
    117122        return data(&this->storage);
     
    119124
    120125forall(otype T, otype allocator_t | allocator_c(T, allocator_t))
    121 static inline T* end(vector(T, allocator_t) *const this)
     126static inline T* end(vector(T, allocator_t)* this)
    122127{
    123128        return data(&this->storage) + this->size;
     
    125130
    126131forall(otype T, otype allocator_t | allocator_c(T, allocator_t))
    127 static inline const T* cend(const vector(T, allocator_t) *const this)
     132static inline const T* cend(const vector(T, allocator_t)* this)
    128133{
    129134        return data(&this->storage) + this->size;
     
    140145
    141146forall(otype T)
    142 void ctor(heap_allocator(T) *const this);
     147void ?{}(heap_allocator(T)* this);
    143148
    144149forall(otype T)
    145 void dtor(heap_allocator(T) *const this);
     150void ?{}(heap_allocator(T)* this, heap_allocator(T) rhs);
    146151
    147152forall(otype T)
    148 void realloc_storage(heap_allocator(T) *const this, size_t size);
     153heap_allocator(T) ?=?(heap_allocator(T)* this, heap_allocator(T) rhs);
    149154
    150155forall(otype T)
    151 static inline T* data(heap_allocator(T) *const this)
     156void ^?{}(heap_allocator(T)* this);
     157
     158forall(otype T)
     159void realloc_storage(heap_allocator(T)* this, size_t size);
     160
     161forall(otype T)
     162static inline T* data(heap_allocator(T)* this)
    152163{
    153164        return this->storage;
  • src/libcfa/containers/vector.c

    r24bc651 rbd34fc87  
    1818#include <stdlib>
    1919
     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
    2023//------------------------------------------------------------------------------
    2124//Initialization
    2225forall(otype T, otype allocator_t | allocator_c(T, allocator_t))
    23 void ctor(vector(T, allocator_t) *const this)
     26void ?{}(vector(T, allocator_t)* this)
    2427{
    25         ctor(&this->storage);
     28        (&this->storage){};
    2629        this->size = 0;
    2730}
    2831
    2932forall(otype T, otype allocator_t | allocator_c(T, allocator_t))
    30 void dtor(vector(T, allocator_t) *const this)
     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)
    3149{
    3250        clear(this);
    33         dtor(&this->storage);
     51        ^(&this->storage){};
    3452}
    3553
     
    3755//Modifiers
    3856forall(otype T, otype allocator_t | allocator_c(T, allocator_t))
    39 void push_back(vector(T, allocator_t) *const this, T value)
     57void push_back(vector(T, allocator_t)* this, T value)
    4058{
    4159        realloc_storage(&this->storage, this->size+1);
     
    4563
    4664forall(otype T, otype allocator_t | allocator_c(T, allocator_t))
    47 void pop_back(vector(T, allocator_t) *const this)
     65void pop_back(vector(T, allocator_t)* this)
    4866{
    4967        this->size--;
    50         DESTROY(data(&this->storage)[this->size]);
     68        ^(&data(&this->storage)[this->size]){};
    5169}
    5270
    5371forall(otype T, otype allocator_t | allocator_c(T, allocator_t))
    54 void clear(vector(T, allocator_t) *const this)
     72void clear(vector(T, allocator_t)* this)
    5573{
    5674        for(size_t i = 0; i < this->size; i++)
    5775        {
    58                 DESTROY(data(&this->storage)[this->size]);
     76                ^(&data(&this->storage)[this->size]){};
    5977        }
    6078        this->size = 0;
     
    6280
    6381//------------------------------------------------------------------------------
     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//------------------------------------------------------------------------------
    6494//Allocator
    6595forall(otype T)
    66 void ctor(heap_allocator(T) *const this)
     96void ?{}(heap_allocator(T)* this)
    6797{
    6898        this->storage = 0;
     
    71101
    72102forall(otype T)
    73 void dtor(heap_allocator(T) *const this)
     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)
    74119{
    75120        free(this->storage);
     
    77122
    78123forall(otype T)
    79 inline void realloc_storage(heap_allocator(T) *const this, size_t size)
     124inline void realloc_storage(heap_allocator(T)* this, size_t size)
    80125{
    81126        enum { GROWTH_RATE = 2 };
  • src/tests/libcfa_vector.c

    r24bc651 rbd34fc87  
    1 // 
     1//
    22// Cforall Version 1.0.0 Copyright (C) 2016 University of Waterloo
    33//
    44// The contents of this file are covered under the licence agreement in the
    55// file "LICENCE" distributed with Cforall.
    6 // 
    7 // libcfa_vector.c -- 
    8 // 
     6//
     7// libcfa_vector.c --
     8//
    99// Author           : Thierry Delisle
    1010// Created On       : Mon Jul  4 23:36:19 2016
     
    1212// Last Modified On : Tue Jul  5 15:08:05 2016
    1313// Update Count     : 26
    14 // 
     14//
    1515
    1616#include <fstream>
     
    2828int main() {
    2929        vector( int, heap_allocator(int) ) iv;
    30         ctor( &iv );
    3130
    3231        assert( empty( &iv ) );
Note: See TracChangeset for help on using the changeset viewer.