source: libcfa/src/collections/list.hfa@ a17f496

Last change on this file since a17f496 was 6b33e89, checked in by Peter A. Buhr <pabuhr@…>, 6 months ago

change backquote call to regular call

  • Property mode set to 100644
File size: 11.9 KB
RevLine 
[6091b88a]1//
2// Cforall Version 1.0.0 Copyright (C) 2020 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// list -- lets a user-defined stuct form intrusive linked lists
8//
9// Author : Michael Brooks
10// Created On : Wed Apr 22 18:00:00 2020
[8a97248]11// Last Modified By : Peter A. Buhr
[6b33e89]12// Last Modified On : Thu Apr 24 18:12:59 2025
13// Update Count : 72
[6091b88a]14//
15
[e857743]16#pragma once
17
[6091b88a]18#include <assert.h>
19
[69914cbc]20forall( Decorator &, T & )
21struct tytagref {
[9dd1dd6]22 inline T &;
[6091b88a]23};
24
[8a97248]25forall( tOuter &, tMid &, tInner & )
26trait embedded {
[9dd1dd6]27 tytagref( tMid, tInner ) ?`inner( tOuter & );
[69914cbc]28};
[6091b88a]29
[69914cbc]30// embedded is reflexive, with no info (void) as the type tag
[0eacfd4]31forall( T & )
[69914cbc]32static inline tytagref(void, T) ?`inner ( T & this ) { tytagref( void, T ) ret = {this}; return ret; }
33
[1fd3d85]34
35//
36// P9_EMBEDDED: Use on every case of plan-9 inheritance, to make "implements embedded" be a closure of plan-9 inheritance.
37//
38// struct foo {
[9dd1dd6]39// int a, b, c;
40// inline (bar);
[1fd3d85]41// };
42// P9_EMBEDDED( foo, bar )
43//
44
45// usual version, for structs that are top-level declarations
[9dd1dd6]46#define P9_EMBEDDED( derived, immedBase ) P9_EMBEDDED_DECL_( derived, immedBase, static ) P9_EMBEDDED_BDY_( immedBase )
[1fd3d85]47
48// special version, for structs that are declared in functions
[9dd1dd6]49#define P9_EMBEDDED_INFUNC( derived, immedBase ) P9_EMBEDDED_DECL_( derived, immedBase, ) P9_EMBEDDED_BDY_( immedBase )
[1fd3d85]50
51// forward declarations of both the above; generally not needed
52// may help you control where the P9_EMBEEDED cruft goes, in case "right after the stuct" isn't where you want it
[9dd1dd6]53#define P9_EMBEDDED_FWD( derived, immedBase ) P9_EMBEDDED_DECL_( derived, immedBase, static ) ;
54#define P9_EMBEDDED_FWD_INFUNC( derived, immedBase ) auto P9_EMBEDDED_DECL_( derived, immedBase, ) ;
[1fd3d85]55
56// private helpers
57#define P9_EMBEDDED_DECL_( derived, immedBase, STORAGE ) \
[9dd1dd6]58 forall( Tbase &, TdiscardPath & | { tytagref( TdiscardPath, Tbase ) ?`inner( immedBase & ); } ) \
[65b0402]59 STORAGE inline tytagref( immedBase, Tbase ) ?`inner( derived & this )
[9dd1dd6]60
[1fd3d85]61#define P9_EMBEDDED_BDY_( immedBase ) { \
[9dd1dd6]62 immedBase & ib = this; \
63 Tbase & b = ib`inner; \
[65b0402]64 tytagref( immedBase, Tbase ) result = { b }; \
[9dd1dd6]65 return result; \
66 }
[69914cbc]67
[65b0402]68#define EMBEDDED_VIA( OUTER, MID, INNER ) (struct { tytagref( MID, INNER ) ( * ?`inner ) ( OUTER & ); }){ ?`inner }
[0eacfd4]69
[65b0402]70#define DLINK_VIA( TE, TLINK ) EMBEDDED_VIA( TE, TLINK, dlink( TE ) )
[0eacfd4]71
72
73// The origin is the position encountered at the start of iteration, signifying, "need to advance to the first element,"
[6b33e89]74// and at the end of iteration, signifying, "no more elements." Normal comsumption of an iterator runs "advance" as
75// the first step, and uses the return of "advance" as a guard, before dereferencing the iterator. So normal
[0eacfd4]76// consumption of an iterator does not dereference an iterator in origin position. The value of a pointer (underlying a
77// refence) that is exposed publicly as an iteraor, and also a pointer stored internally in a link field, is tagged, to
78// indicate "is the origin" (internally, is the list-head sentinel node), or untagged, to indicate "is a regular node."
79// Intent is to help a user who dereferences an iterator in origin position (which would be an API-use error on their
[69914cbc]80// part), by failing fast.
81
82#if defined( __x86_64 )
[0eacfd4]83 // Preferred case: tag in the most-significant bit. Dereference has been shown to segfault consistently.
84 // Maintenance should list more architectures as "ok" here, to let them use the preferred case, when valid.
[9dd1dd6]85 #define ORIGIN_TAG_BITNO ( 8 * sizeof( size_t ) - 1 )
[69914cbc]86#else
[0eacfd4]87 // Fallback case: tag in the least-significant bit. Dereference will often give an alignment error, but may not,
88 // e.g. if accessing a char-typed member. 32-bit x86 uses the most- significant bit for real room on the heap.
[9dd1dd6]89 #define ORIGIN_TAG_BITNO 0
[69914cbc]90#endif
91#define ORIGIN_TAG_MASK (((size_t)1) << ORIGIN_TAG_BITNO)
92
[65b0402]93#define ORIGIN_TAG_SET( p ) ((p) | ORIGIN_TAG_MASK)
94#define ORIGIN_TAG_CLEAR( p ) ((p) & ~ORIGIN_TAG_MASK)
95#define ORIGIN_TAG_QUERY( p ) ((p) & ORIGIN_TAG_MASK)
[6091b88a]96
[69914cbc]97forall( tE & ) {
[9dd1dd6]98 struct dlink{
[0eacfd4]99 tE * next;
100 tE * prev;
[9dd1dd6]101 };
102
[65b0402]103 static inline void ?{}( dlink( tE ) & this ) {
[0eacfd4]104 this.next = this.prev = 0p;
[9dd1dd6]105 }
106
[65b0402]107 forall( tLinks & = dlink( tE ) )
[9dd1dd6]108 struct dlist {
[65b0402]109 inline dlink( tE );
[9dd1dd6]110 };
111
[65b0402]112 forall( tLinks & | embedded( tE, tLinks, dlink( tE ) ) ) {
113 static inline tE * $get_list_origin_addr( dlist( tE, tLinks ) & list ) {
114 dlink( tE ) & link_from_null = (*(tE *)0p)`inner;
115 ptrdiff_t link_offset = (ptrdiff_t)&link_from_null;
116 size_t origin_addr = ((size_t)&list) - link_offset;
[9dd1dd6]117 size_t preResult = ORIGIN_TAG_SET( origin_addr );
118 return (tE *)preResult;
119 }
120
[65b0402]121 static inline void ?{}( dlist( tE, tLinks ) & this ) {
[9dd1dd6]122 tE * listOrigin = $get_list_origin_addr( this );
[65b0402]123 ((dlink( tE ) &)this){ listOrigin, listOrigin };
[9dd1dd6]124 }
125 }
[69914cbc]126}
[4d741e9]127
128
[65b0402]129static inline forall( tE &, tLinks & | embedded( tE, tLinks, dlink( tE ) ) ) {
[6b33e89]130 bool isListed( tE & node ) {
131 verify( &node != 0p );
132 dlink( tE ) & node_links = node`inner;
133 return (node_links.prev != 0p) || (node_links.next != 0p);
134 }
135
136 bool isEmpty( dlist( tE, tLinks ) & list ) {
137 tE * firstPtr = list.next;
138 if ( ORIGIN_TAG_QUERY(( size_t)firstPtr) ) firstPtr = 0p;
139 return firstPtr == 0p;
140 }
141
142 tE & first( dlist( tE, tLinks ) & list ) {
143 tE * firstPtr = list.next;
144 if ( ORIGIN_TAG_QUERY( (size_t)firstPtr ) ) firstPtr = 0p;
145 return *firstPtr;
146 }
147
148 tE & last( dlist( tE, tLinks ) & list ) {
149 tE * lastPtr = list.prev;
150 if ( ORIGIN_TAG_QUERY( (size_t)lastPtr) ) lastPtr = 0p;
151 return *lastPtr;
152 }
153
[65b0402]154 tE & insert_before( tE & before, tE & node ) {
155 verify( &before != 0p );
156 verify( &node != 0p );
[4d741e9]157
[65b0402]158 dlink( tE ) & linkToInsert = node`inner;
[0eacfd4]159
160 verify( linkToInsert.next == 0p );
[65b0402]161 verify( linkToInsert.prev == 0p );
[0eacfd4]162
[65b0402]163 tE & list_pos_elem = *(tE *)ORIGIN_TAG_CLEAR( (size_t)&before );
164 dlink( tE ) & list_pos_links = list_pos_elem`inner;
[9dd1dd6]165 asm( "" : : : "memory" );
[65b0402]166 tE & before_raw = *list_pos_links.prev;
167 tE & before_elem = *(tE *) ORIGIN_TAG_CLEAR( (size_t)&before_raw );
168 linkToInsert.next = &before;
169 linkToInsert.prev = &before_raw;
170 dlink( tE ) & beforeLinks = before_elem`inner;
171 beforeLinks.next = &node;
172 list_pos_links.prev = &node;
[9dd1dd6]173 asm( "" : : : "memory" );
[65b0402]174 return node;
[0eacfd4]175 }
176
[65b0402]177 tE & insert_after( tE & after, tE & node ) {
178 verify( &after != 0p );
179 verify( &node != 0p );
[6091b88a]180
[65b0402]181 dlink( tE ) & linkToInsert = node`inner;
[0eacfd4]182
183 verify( linkToInsert.prev == 0p );
[65b0402]184 verify( linkToInsert.next == 0p );
[0eacfd4]185
[65b0402]186 tE & list_pos_elem = *(tE *)ORIGIN_TAG_CLEAR( (size_t)&after );
187 dlink( tE ) & list_pos_links = list_pos_elem`inner;
[9dd1dd6]188 asm( "" : : : "memory" );
[65b0402]189 tE & after_raw = *list_pos_links.next;
190 tE & after_elem = *(tE *)ORIGIN_TAG_CLEAR( (size_t)&after_raw );
191 linkToInsert.prev = &after;
192 linkToInsert.next = &after_raw;
193 dlink( tE ) & afterLinks = after_elem`inner;
194 afterLinks.prev = &node;
195 list_pos_links.next = &node;
[9dd1dd6]196 asm( "" : : : "memory" );
[65b0402]197 return node;
[6091b88a]198 }
199
[65b0402]200 tE & remove( tE & node ) {
201 verify( &node != 0p );
202 verify( ! ORIGIN_TAG_QUERY( (size_t)&node) );
203
204 dlink( tE ) & list_pos_links = node`inner;
205 tE & before_raw = *list_pos_links.prev;
206 tE & before_elem = *(tE *)ORIGIN_TAG_CLEAR( (size_t)&before_raw );
207 dlink( tE ) & before_links = before_elem`inner;
208 tE & after_raw = *list_pos_links.next;
209 tE & after_elem = *(tE *)ORIGIN_TAG_CLEAR( (size_t)&after_raw );
210 dlink( tE ) & after_links = after_elem`inner;
[9dd1dd6]211 before_links.next = &after_raw;
212 after_links.prev = &before_raw;
213 asm( "" : : : "memory" );
[69914cbc]214 list_pos_links.prev = 0p;
215 list_pos_links.next = 0p;
[9dd1dd6]216 asm( "" : : : "memory" );
[65b0402]217 return node;
[9dd1dd6]218 }
[65b0402]219
[6b33e89]220 tE & iter( dlist( tE, tLinks ) & list ) {
[65b0402]221 tE * origin = $get_list_origin_addr( list );
[9dd1dd6]222 return *origin;
223 }
224
[6b33e89]225 bool recede( tE && refx ) {
[9dd1dd6]226 tE && ref_inner = refx;
[65b0402]227 tE & oldReferent = *(tE*)ORIGIN_TAG_CLEAR( (size_t)&ref_inner );
[6b33e89]228 &ref_inner = oldReferent`inner.prev;
[65b0402]229 return &ref_inner != 0p && ! ORIGIN_TAG_QUERY( (size_t)&ref_inner );
[0eacfd4]230 }
[9dd1dd6]231
[6b33e89]232 bool advance( tE && refx ) {
[9dd1dd6]233 tE && ref_inner = refx;
[65b0402]234 tE & oldReferent = *(tE*)ORIGIN_TAG_CLEAR( (size_t)&ref_inner );
[6b33e89]235 &ref_inner = oldReferent`inner.next;
[65b0402]236 return &ref_inner != 0p && ! ORIGIN_TAG_QUERY( (size_t)&ref_inner );
[0eacfd4]237 }
[9dd1dd6]238
[6b33e89]239 bool isFirst( tE & node ) {
240 return recede( node );
241 }
[9dd1dd6]242
[6b33e89]243 bool isLast( tE & node ) {
244 return advance( node );
245 }
[9dd1dd6]246
[6b33e89]247 tE & prev( tE & node ) {
248 if ( recede( node ) ) return node;
[0eacfd4]249 return *0p;
[9dd1dd6]250 }
251
[6b33e89]252 tE & next( tE & node ) {
253 if ( advance( node ) ) return node;
[0eacfd4]254 return *0p;
[9dd1dd6]255 }
256
[65b0402]257 tE & insert_first( dlist( tE, tLinks ) & list, tE & node ) {
[6b33e89]258 insert_after( iter( list ), node );
[65b0402]259 return node;
[9dd1dd6]260 }
261
[65b0402]262 tE & insert_last( dlist( tE, tLinks ) & list, tE & node ) {
[6b33e89]263 insert_before( iter( list ), node );
[65b0402]264 return node;
265 }
[6b33e89]266 tE & insert( dlist( tE, tLinks ) & list, tE & node ) { // synonym for insert_last
[65b0402]267 insert_last( list, node );
268 return node;
[0eacfd4]269 }
[65b0402]270
271 tE & remove_first( dlist( tE, tLinks ) & list ) {
[6b33e89]272 tE & first_node = first( list );
273 if ( &first_node ) return remove( first_node );
274 return first_node;
[65b0402]275 }
276
277 tE & remove_last( dlist( tE, tLinks ) & list ) {
[6b33e89]278 tE & last_node = last( list );
279 if ( &last_node ) return remove( last_node );
280 return last_node;
[9dd1dd6]281 }
282
[65b0402]283 // Transfer the "from" list to the end of this sequence; the "from" list is empty after the transfer.
284// void transfer( dlist( tE, tLinks ) & to, dlist( tE, tLinks ) & from ) {
285// if ( isEmpty( from ) ) return; // "from" list empty ?
286// if ( isEmpty( to ) ) { // "to" list empty ?
287// root = from.root;
288// } else { // "to" list not empty
289// T * toEnd = (T *)uBack( root );
290// T * fromEnd = (T *)from.uBack( from.root );
291// uBack( root ) = fromEnd;
292// from.uNext( fromEnd ) = root;
293// from.uBack( from.root ) = toEnd;
294// uNext( toEnd ) = from.root;
295// } // if
296// from.root = nullptr; // mark "from" list empty
297// }
298
299 // Transfer the "from" list up to node "n" to the end of this list; the "from" list becomes the sequence after node "n".
300 // Node "n" must be in the "from" list.
301// void split( dlist( tE, tLinks ) & to, dlist( tE, tLinks ) & from, tE & node ) {
302// #ifdef __U_DEBUG__
303// if ( ! n->listed() ) abort( "(uSequence &)%p.split( %p ) : Node is not on a list.", this, n );
304// #endif // __U_DEBUG__
305// uSequence<T> to;
306// to.root = from.root; // start of "to" list
307// from.root = (T *)uNext( n ); // start of "from" list
308// if ( to.root == from.root ) { // last node in list ?
309// from.root = nullptr; // mark "from" list empty
310// } else {
311// uBack( from.root ) = (T *)uBack( to.root ); // fix "from" list
312// uNext( uBack( to.root ) ) = from.root;
313// uNext( n ) = to.root; // fix "to" list
314// uBack( to.root ) = n;
315// } // if
316// transfer( to );
317// }
318
[0eacfd4]319 #if ! defined(NDEBUG) && (defined(__CFA_DEBUG__) || defined(__CFA_VERIFY__))
[65b0402]320 bool $validate_fwd( dlist( tE, tLinks ) & this ) {
[6b33e89]321 if ( ! & first( this ) ) return &last( this ) == 0p;
[69914cbc]322
[9dd1dd6]323 tE & lagElem = *0p;
[6b33e89]324 while ( tE & it = iter( this ); advance( it ) ) {
325 if ( & lagElem == 0p && &it != & first( this ) ) return false;
[0eacfd4]326 &lagElem = &it;
[9dd1dd6]327 }
[69914cbc]328
[6b33e89]329 if ( &lagElem != &last( this ) ) return false;
[69914cbc]330
[6b33e89]331 // TODO: verify that it is back at iter( this );
[9dd1dd6]332 return true;
[6091b88a]333 }
334
[65b0402]335 bool $validate_rev( dlist( tE, tLinks ) & this ) {
[6b33e89]336 if ( ! & last( this ) ) return &first( this ) == 0p;
[f2d05e9]337
[0eacfd4]338 tE & lagElem = *0p;
[6b33e89]339 while ( tE & it = iter( this ); recede( it ) ) {
340 if ( &lagElem == 0p && &it != & last( this ) ) return false;
[0eacfd4]341 &lagElem = &it;
[9dd1dd6]342 }
[f2d05e9]343
[6b33e89]344 if ( &lagElem != &first( this ) ) return false;
[f2d05e9]345
[6b33e89]346 // TODO: verify that it is back at iter( this );
[9dd1dd6]347 return true;
[69914cbc]348 }
[0eacfd4]349
[65b0402]350 bool validate( dlist( tE, tLinks ) & this ) {
351 return $validate_fwd( this ) && $validate_rev( this );
[f2d05e9]352 }
[0eacfd4]353 #endif
[6091b88a]354}
355
[0eacfd4]356// TEMPORARY, until foreach statement created.
[6b33e89]357#define FOREACH( list, index ) for ( typeof(iter( list )) & (index) = iter( list ); advance( index ); )
358#define FOREACH_REV( list, index ) for ( typeof(iter( list )) & (index) = iter( list ); recede( index ); )
359#define FOREACH_COND( list, index, expr ) for ( typeof(iter( list )) & (index) = iter( list ); advance( index ) && !(expr); )
360#define FOREACH_REV_COND( list, index, expr ) for ( typeof(iter( list )) & (index) = iter( list ); recede( index ) && !(expr); )
Note: See TracBrowser for help on using the repository browser.