Ignore:
File:
1 edited

Legend:

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

    r92e7631 r2233ad4  
    1010// Created On       : Tue Oct 31 16:38:50 2017
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Wed Jan 15 07:42:35 2020
    13 // Update Count     : 28
     12// Last Modified On : Wed Jun 26 08:52:20 2019
     13// Update Count     : 4
    1414
    1515#pragma once
     
    4444
    4545        forall(dtype T | sized(T))
    46         static inline T & ?[?]( __small_array(T) & this, __lock_size_t idx ) {
     46        static inline T& ?[?]( __small_array(T) & this, __lock_size_t idx) {
    4747                return ((typeof(this.data))this.data)[idx];
    4848        }
    4949
    5050        forall(dtype T | sized(T))
    51         static inline T & ?[?]( const __small_array(T) & this, __lock_size_t idx ) {
     51        static inline T& ?[?]( const __small_array(T) & this, __lock_size_t idx) {
    5252                return ((typeof(this.data))this.data)[idx];
    5353        }
    5454
    55         forall(dtype T)
    56         static inline T * begin( const __small_array(T) & this ) {
     55        forall(dtype T | sized(T))
     56        static inline T* begin( const __small_array(T) & this ) {
    5757                return ((typeof(this.data))this.data);
    5858        }
    5959
    6060        forall(dtype T | sized(T))
    61         static inline T * end( const __small_array(T) & this ) {
     61        static inline T* end( const __small_array(T) & this ) {
    6262                return ((typeof(this.data))this.data) + this.size;
    6363        }
     
    7070#ifdef __cforall
    7171        trait is_node(dtype T) {
    72                 T *& get_next( T & );
     72                T*& get_next( T& );
    7373        };
    7474#endif
     
    9797        forall(dtype T)
    9898        static inline void ?{}( __stack(T) & this ) {
    99                 (this.top){ 0p };
    100         }
    101 
    102         static inline forall( dtype T | is_node(T) ) {
    103                 void push( __stack(T) & this, T * val ) {
    104                         verify( !get_next( *val ) );
    105                         get_next( *val ) = this.top;
    106                         this.top = val;
    107                 }
    108 
    109                 T * pop( __stack(T) & this ) {
    110                         T * top = this.top;
    111                         if( top ) {
    112                                 this.top = get_next( *top );
    113                                 get_next( *top ) = 0p;
    114                         }
    115                         return top;
    116                 }
    117 
    118                 int ?!=?( const __stack(T) & this, __attribute__((unused)) zero_t zero ) {
    119                         return this.top != 0;
    120                 }
     99                (this.top){ NULL };
     100        }
     101
     102        forall(dtype T | is_node(T) | sized(T))
     103        static inline void push( __stack(T) & this, T * val ) {
     104                verify( !get_next( *val ) );
     105                get_next( *val ) = this.top;
     106                this.top = val;
     107        }
     108
     109        forall(dtype T | is_node(T) | sized(T))
     110        static inline T * pop( __stack(T) & this ) {
     111                T * top = this.top;
     112                if( top ) {
     113                        this.top = get_next( *top );
     114                        get_next( *top ) = NULL;
     115                }
     116                return top;
     117        }
     118
     119        forall(dtype T | is_node(T))
     120        static inline int ?!=?( const __stack(T) & this, __attribute__((unused)) zero_t zero ) {
     121                return this.top != 0;
    121122        }
    122123#endif
     
    144145
    145146#ifdef __cforall
    146         static inline forall( dtype T | is_node(T) ) {
    147                 void ?{}( __queue(T) & this ) with( this ) {
    148                         head{ 1p };
    149                         tail{ &head };
    150                         verify(*tail == 1p);
    151                 }
    152 
    153                 void append( __queue(T) & this, T * val ) with( this ) {
    154                         verify(tail != 0p);
    155                         verify(*tail == 1p);
    156                         *tail = val;
    157                         tail = &get_next( *val );
    158                         *tail = 1p;
    159                 }
    160 
    161                 T * pop_head( __queue(T) & this ) {
    162                         verify(*this.tail == 1p);
    163                         T * head = this.head;
    164                         if( head != 1p ) {
    165                                 this.head = get_next( *head );
    166                                 if( get_next( *head ) == 1p ) {
    167                                         this.tail = &this.head;
    168                                 }
    169                                 get_next( *head ) = 0p;
    170                                 verify(*this.tail == 1p);
    171                                 verify( get_next(*head) == 0p );
    172                                 return head;
     147
     148        forall(dtype T)
     149        static inline void ?{}( __queue(T) & this ) with( this ) {
     150                head{ NULL };
     151                tail{ &head };
     152        }
     153
     154        forall(dtype T | is_node(T) | sized(T))
     155        static inline void append( __queue(T) & this, T * val ) with( this ) {
     156                verify(tail != NULL);
     157                *tail = val;
     158                tail = &get_next( *val );
     159        }
     160
     161        forall(dtype T | is_node(T) | sized(T))
     162        static inline T * pop_head( __queue(T) & this ) {
     163                T * head = this.head;
     164                if( head ) {
     165                        this.head = get_next( *head );
     166                        if( !get_next( *head ) ) {
     167                                this.tail = &this.head;
    173168                        }
    174                         verify(*this.tail == 1p);
    175                         return 0p;
    176                 }
    177 
    178                 T * remove( __queue(T) & this, T ** it ) with( this ) {
    179                         T * val = *it;
    180                         verify( val );
    181 
    182                         (*it) = get_next( *val );
    183 
    184                         if( tail == &get_next( *val ) ) {
    185                                 tail = it;
    186                         }
    187 
    188                         get_next( *val ) = 0p;
    189 
    190                         verify( (head == 1p) == (&head == tail) );
    191                         verify( *tail == 1p );
    192                         return val;
    193                 }
    194 
    195                 int ?!=?( const __queue(T) & this, __attribute__((unused)) zero_t zero ) {
    196                         return this.head != 0;
    197                 }
     169                        get_next( *head ) = NULL;
     170                }
     171                return head;
     172        }
     173
     174        forall(dtype T | is_node(T) | sized(T))
     175        static inline T * remove( __queue(T) & this, T ** it ) with( this ) {
     176                T * val = *it;
     177                verify( val );
     178
     179                (*it) = get_next( *val );
     180
     181                if( tail == &get_next( *val ) ) {
     182                        tail = it;
     183                }
     184
     185                get_next( *val ) = NULL;
     186
     187                verify( (head == NULL) == (&head == tail) );
     188                verify( *tail == NULL );
     189                return val;
     190        }
     191
     192        forall(dtype T | is_node(T))
     193        static inline int ?!=?( const __queue(T) & this, __attribute__((unused)) zero_t zero ) {
     194                return this.head != 0;
    198195        }
    199196#endif
     
    226223
    227224#ifdef __cforall
    228         forall(dtype T )
     225
     226        forall(dtype T | sized(T))
    229227        static inline [void] ?{}( __dllist(T) & this, * [T * & next, T * & prev] ( T & ) __get ) {
    230                 this.head{ 0p };
     228                this.head{ NULL };
    231229                this.__get = __get;
    232230        }
     
    234232        #define next 0
    235233        #define prev 1
    236         static inline forall(dtype T) {
    237                 void push_front( __dllist(T) & this, T & node ) with( this ) {
    238                         verify(__get);
    239                         if ( head ) {
    240                                 __get( node ).next = head;
    241                                 __get( node ).prev = __get( *head ).prev;
    242                                 // inserted node must be consistent before it is seen
    243                                 // prevent code movement across barrier
    244                                 asm( "" : : : "memory" );
    245                                 __get( *head ).prev = &node;
    246                                 T & _prev = *__get( node ).prev;
    247                                 __get( _prev ).next = &node;
    248                         } else {
    249                                 __get( node ).next = &node;
    250                                 __get( node ).prev = &node;
    251                         }
    252 
     234        forall(dtype T | sized(T))
     235        static inline void push_front( __dllist(T) & this, T & node ) with( this ) {
     236                verify(__get);
     237                if ( head ) {
     238                        __get( node ).next = head;
     239                        __get( node ).prev = __get( *head ).prev;
     240                        // inserted node must be consistent before it is seen
    253241                        // prevent code movement across barrier
    254242                        asm( "" : : : "memory" );
    255                         head = &node;
    256                 }
    257 
    258                 void remove( __dllist(T) & this, T & node ) with( this ) {
    259                         verify(__get);
    260                         if ( &node == head ) {
    261                                 if ( __get( *head ).next == head ) {
    262                                         head = 0p;
    263                                 } else {
    264                                         head = __get( *head ).next;
    265                                 }
     243                        __get( *head ).prev = &node;
     244                        T & _prev = *__get( node ).prev;
     245                        __get( _prev ).next = &node;
     246                }
     247                else {
     248                        __get( node ).next = &node;
     249                        __get( node ).prev = &node;
     250                }
     251
     252                // prevent code movement across barrier
     253                asm( "" : : : "memory" );
     254                head = &node;
     255        }
     256
     257        forall(dtype T | sized(T))
     258        static inline void remove( __dllist(T) & this, T & node ) with( this ) {
     259                verify(__get);
     260                if ( &node == head ) {
     261                        if ( __get( *head ).next == head ) {
     262                                head = NULL;
    266263                        }
    267                         __get( *__get( node ).next ).prev = __get( node ).prev;
    268                         __get( *__get( node ).prev ).next = __get( node ).next;
    269                         __get( node ).next = 0p;
    270                         __get( node ).prev = 0p;
    271                 }
    272 
    273                 int ?!=?( const __dllist(T) & this, __attribute__((unused)) zero_t zero ) {
    274                         return this.head != 0;
    275                 }
    276 
    277                 void move_to_front( __dllist(T) & src, __dllist(T) & dst, T & node ) {
    278                         remove    (src, node);
    279                         push_front(dst, node);
    280                 }
     264                        else {
     265                                head = __get( *head ).next;
     266                        }
     267                }
     268                __get( *__get( node ).next ).prev = __get( node ).prev;
     269                __get( *__get( node ).prev ).next = __get( node ).next;
     270                __get( node ).next = NULL;
     271                __get( node ).prev = NULL;
     272        }
     273
     274        forall(dtype T | sized(T))
     275        static inline int ?!=?( const __dllist(T) & this, __attribute__((unused)) zero_t zero ) {
     276                return this.head != 0;
    281277        }
    282278        #undef next
     
    290286
    291287#endif
    292 
    293 // Local Variables: //
    294 // tab-width: 4 //
    295 // End: //
Note: See TracChangeset for help on using the changeset viewer.