Ignore:
Timestamp:
Nov 20, 2017, 12:12:34 PM (6 years ago)
Author:
Thierry Delisle <tdelisle@…>
Branches:
ADT, aaron-thesis, arm-eh, ast-experimental, cleanup-dtors, deferred_resn, demangler, enum, forall-pointer-decay, jacob/cs343-translation, jenkins-sandbox, master, new-ast, new-ast-unique-expr, new-env, no_list, persistent-indexer, pthread-emulation, qualifiedEnum, resolv-new, with_gc
Children:
fdd3786
Parents:
b7f8cb4
Message:

Added generic containers for runtime.
Moved some internal code to bits folder.
consistently used cforall instead of CFORALL define.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/libcfa/bits/containers.h

    rb7f8cb4 r0cf5b79  
    1515#pragma once
    1616
    17 #include <stddef.h>
     17#include "bits/defs.h"
     18#include "libhdr.h"
    1819
    19 #include "libhdr.h"
     20//-----------------------------------------------------------------------------
     21// Array
     22//-----------------------------------------------------------------------------
     23
     24#ifdef __cforall
     25        forall(dtype T)
     26#else
     27        #define T void
     28#endif
     29struct __small_array {
     30        T *           data;
     31        __lock_size_t size;
     32};
     33#undef T
     34
     35#ifdef __cforall
     36        #define __small_array_t(T) __small_array(T)
     37#else
     38        #define __small_array_t(T) struct __small_array
     39#endif
     40
     41#ifdef __cforall
     42        // forall(otype T | sized(T))
     43        // static inline void ?{}(__small_array(T) & this) {}
     44
     45        forall(dtype T | sized(T))
     46        static inline T& ?[?]( __small_array(T) & this, __lock_size_t idx) {
     47                return ((typeof(this.data))this.data)[idx];
     48        }
     49
     50        forall(dtype T | sized(T))
     51        static inline T& ?[?]( const __small_array(T) & this, __lock_size_t idx) {
     52                return ((typeof(this.data))this.data)[idx];
     53        }
     54
     55        forall(dtype T | sized(T))
     56        static inline T* begin( const __small_array(T) & this ) {
     57                return ((typeof(this.data))this.data);
     58        }
     59
     60        forall(dtype T | sized(T))
     61        static inline T* end( const __small_array(T) & this ) {
     62                return ((typeof(this.data))this.data) + this.size;
     63        }
     64#endif
    2065
    2166//-----------------------------------------------------------------------------
     
    2368//-----------------------------------------------------------------------------
    2469
    25 #ifdef __CFORALL__
     70#ifdef __cforall
    2671        trait is_node(dtype T) {
    2772                T*& get_next( T& );
     
    3277// Stack
    3378//-----------------------------------------------------------------------------
    34 #ifdef __CFORALL__
     79#ifdef __cforall
    3580        forall(dtype TYPE | is_node(TYPE))
    3681        #define T TYPE
     
    4186        T * top;
    4287};
     88#undef T
    4389
    44 #ifdef __CFORALL__
     90#ifdef __cforall
    4591#define __stack_t(T) __stack(T)
    4692#else
     
    4894#endif
    4995
    50 #ifdef __CFORALL__
     96#ifdef __cforall
    5197        forall(dtype T | is_node(T))
    52         void ?{}( __stack(T) & this ) {
    53                 this.top = NULL;
     98        static inline void ?{}( __stack(T) & this ) {
     99                (this.top){ NULL };
    54100        }
    55101
    56102        forall(dtype T | is_node(T) | sized(T))
    57         void push( __stack(T) & this, T * val ) {
     103        static inline void push( __stack(T) & this, T * val ) {
    58104                verify( !get_next( *val ) );
    59105                get_next( *val ) = this.top;
     
    62108
    63109        forall(dtype T | is_node(T) | sized(T))
    64         T * pop( __stack(T) & this ) {
     110        static inline T * pop( __stack(T) & this ) {
    65111                T * top = this.top;
    66112                if( top ) {
     
    75121// Queue
    76122//-----------------------------------------------------------------------------
    77 #ifdef __CFORALL__
    78         forall(dtype T | is_node(T))
     123#ifdef __cforall
     124        forall(dtype TYPE | is_node(TYPE))
    79125        #define T TYPE
    80126#else
     
    85131        T ** tail;
    86132};
     133#undef T
    87134
    88 #ifdef __CFORALL__
     135#ifdef __cforall
     136#define __queue_t(T) __queue(T)
     137#else
     138#define __queue_t(T) struct __queue
     139#endif
     140
     141#ifdef __cforall
    89142        forall(dtype T | is_node(T))
    90         void ?{}( __queue(T) & this ) {
    91                 this.head = NULL;
    92                 this.tail = &this.head;
     143        static inline void ?{}( __queue(T) & this ) {
     144                (this.head){ NULL };
     145                (this.tail){ &this.head };
    93146        }
    94147
    95148        forall(dtype T | is_node(T) | sized(T))
    96         void append( __queue(T) & this, T * val ) {
     149        static inline void append( __queue(T) & this, T * val ) {
    97150                verify(this.tail != NULL);
    98151                *this.tail = val;
     
    101154
    102155        forall(dtype T | is_node(T) | sized(T))
    103         T * pop_head( __queue(T) & this ) {
     156        static inline T * pop_head( __queue(T) & this ) {
    104157                T * head = this.head;
    105158                if( head ) {
     
    114167
    115168        forall(dtype T | is_node(T) | sized(T))
    116         T * remove( __queue(T) & this, T ** it ) {
     169        static inline T * remove( __queue(T) & this, T ** it ) {
    117170                T * val = *it;
    118171                verify( val );
Note: See TracChangeset for help on using the changeset viewer.