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

Last change on this file since fc9d167 was e6e250d, checked in by Peter A. Buhr <pabuhr@…>, 5 weeks ago

3rd attempt at harmonizing isOp functions, e.g., isListed, isFirst, isLast

  • Property mode set to 100644
File size: 15.8 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
[e6e250d]12// Last Modified On : Sun Mar 29 22:59:08 2026
13// Update Count : 104
[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,"
[6cbc5a62]74// and at the end of iteration, signifying, "no more elements." Normal comsumption of an iterator runs "advance" as the
75// first step, and uses the return of "advance" as a guard, before dereferencing the iterator. So normal consumption of
76// an iterator does not dereference an iterator in origin position. The value of a pointer (underlying a refence) that
77// is exposed publicly as an iteraor, and also a pointer stored internally in a link field, is tagged, to indicate "is
78// the origin" (internally, is the list-head sentinel node), or untagged, to indicate "is a regular node." Intent is to
79// help a user who dereferences an iterator in origin position (which would be an API-use error on their part), by
80// failing fast.
[69914cbc]81
[7806f91]82#ifdef __EXPERIMENTAL_DISABLE_OTAG__ // Perf experimention alt mode
83
[6cbc5a62]84 // With origin tagging disabled, iteration never reports "no more elements."
85 // In this mode, the list API is buggy.
86 // This mode is used to quantify the cost of the normal tagging scheme.
[7806f91]87
[6cbc5a62]88 #define ORIGIN_TAG_SET(p) (p)
89 #define ORIGIN_TAG_CLEAR(p) (p)
90 #define ORIGIN_TAG_QUERY(p) 0
91 #define ORIGIN_TAG_ASGN(p, v) (p)
92 #define ORIGIN_TAG_EITHER(p, v) (p)
93 #define ORIGIN_TAG_NEQ(v1, v2) 0
[7806f91]94
95#else // Normal
96
[6cbc5a62]97 #if defined( __x86_64 )
98 // Preferred case: tag in the most-significant bit. Dereference
99 // has been shown to segfault consistently. Maintenance should
100 // list more architectures as "ok" here, to let them use the
101 // preferred case, when valid.
102 #define ORIGIN_TAG_BITNO ( 8 * sizeof( size_t ) - 1 )
103 #else
104 // Fallback case: tag in the least-significant bit. Dereference
105 // will often give an alignment error, but may not, e.g. if
106 // accessing a char-typed member. 32-bit x86 uses the most-
107 // significant bit for real room on the heap.
108 #define ORIGIN_TAG_BITNO 0
109 #endif
110
111 #define ORIGIN_TAG_MASK (((size_t)1) << ORIGIN_TAG_BITNO)
112
113 #define ORIGIN_TAG_SET(p) ((p) | ORIGIN_TAG_MASK)
114 #define ORIGIN_TAG_CLEAR(p) ((p) & ~ORIGIN_TAG_MASK)
115 #define ORIGIN_TAG_QUERY(p) ((p) & ORIGIN_TAG_MASK)
116
117 #define ORIGIN_TAG_ASGN(p, v) ( \
118 verify( ! ORIGIN_TAG_QUERY(p) && "p had no tagbit" ), \
119 ORIGIN_TAG_EITHER((p), (v)) \
120 )
121
122 #define ORIGIN_TAG_EITHER(p, v) ( \
123 verify( ! ORIGIN_TAG_CLEAR(v) && "v is a pure tagbit" ), \
124 ( (p) | (v) ) \
125 )
126
127 #define ORIGIN_TAG_NEQ(v1, v2) ( \
128 verify( ! ORIGIN_TAG_CLEAR(v1) && "v1 is a pure tagbit" ), \
129 verify( ! ORIGIN_TAG_CLEAR(v2) && "v2 is a pure tagbit" ), \
130 ( (v1) ^ (v2) ) \
131 )
[7806f91]132
133#endif
134
135
136#ifdef __EXPERIMENTAL_LOOSE_SINGLES__ // Perf experimention alt mode
137
[6cbc5a62]138 // In loose-singles mode, the ability to answer an "is listed" query is disabled, as is "to insert an element,
139 // it must not be listed already" checking. The user must know separately whether an element is listed.
140 // Other than inserting it, any list-api action on an unlisted element is undefined. Notably, list iteration
141 // starting from an unlisted element is not defined to respond "no more elements," and may instead continue
142 // iterating from a formerly occupied list position. This mode matches LQ usage.
[7806f91]143
[6cbc5a62]144 #define NOLOOSE(...)
145 #define LOOSEONLY(...) __VA_ARGS__
[7806f91]146
147#else // Normal
148
[6cbc5a62]149 #define NOLOOSE(...) __VA_ARGS__
150 #define LOOSEONLY(...)
[7806f91]151
[69914cbc]152#endif
153
[7806f91]154
[69914cbc]155forall( tE & ) {
[9dd1dd6]156 struct dlink{
[ad41cbd]157 tE * next, * prev;
[9dd1dd6]158 };
159
[65b0402]160 static inline void ?{}( dlink( tE ) & this ) {
[7806f91]161 NOLOOSE(
[0eacfd4]162 this.next = this.prev = 0p;
[7806f91]163 )
[9dd1dd6]164 }
165
[ad41cbd]166 forall( tLinks & = dlink( tE ) ) {
167 struct dlist {
168 inline dlink( tE );
169 };
170
171 forall( | embedded( tE, tLinks, dlink( tE ) ) ) {
172 static inline tE * $get_list_origin_addr( dlist( tE, tLinks ) & list ) {
173 dlink( tE ) & link_from_null = (*(tE *)0p)`inner;
174 ptrdiff_t link_offset = (ptrdiff_t)&link_from_null;
175 size_t origin_addr = ((size_t)&list) - link_offset;
176 size_t preResult = ORIGIN_TAG_SET( origin_addr );
177 return (tE *)preResult;
178 }
179
180 static inline void ?{}( dlist( tE, tLinks ) & this ) {
181 tE * listOrigin = $get_list_origin_addr( this );
182 ((dlink( tE ) &)this){ listOrigin, listOrigin };
183 }
[9dd1dd6]184 }
185 }
[69914cbc]186}
[4d741e9]187
188
[65b0402]189static inline forall( tE &, tLinks & | embedded( tE, tLinks, dlink( tE ) ) ) {
[e6e250d]190 bool listed( tE & node ) {
[6cbc5a62]191 NOLOOSE(
[6b33e89]192 verify( &node != 0p );
193 dlink( tE ) & node_links = node`inner;
194 return (node_links.prev != 0p) || (node_links.next != 0p);
[7806f91]195 )
196 LOOSEONLY(
[e6e250d]197 verify(false && "listed is undefined");
[7806f91]198 return true;
199 )
[6b33e89]200 }
201
[00675ed4]202 bool empty( dlist( tE, tLinks ) & list ) {
[6b33e89]203 tE * firstPtr = list.next;
204 if ( ORIGIN_TAG_QUERY(( size_t)firstPtr) ) firstPtr = 0p;
205 return firstPtr == 0p;
206 }
207
208 tE & first( dlist( tE, tLinks ) & list ) {
209 tE * firstPtr = list.next;
210 if ( ORIGIN_TAG_QUERY( (size_t)firstPtr ) ) firstPtr = 0p;
211 return *firstPtr;
212 }
213
214 tE & last( dlist( tE, tLinks ) & list ) {
215 tE * lastPtr = list.prev;
216 if ( ORIGIN_TAG_QUERY( (size_t)lastPtr) ) lastPtr = 0p;
217 return *lastPtr;
218 }
219
[65b0402]220 tE & insert_before( tE & before, tE & node ) {
221 verify( &before != 0p );
222 verify( &node != 0p );
[4d741e9]223
[65b0402]224 dlink( tE ) & linkToInsert = node`inner;
[6cbc5a62]225 NOLOOSE(
[0eacfd4]226 verify( linkToInsert.next == 0p );
[65b0402]227 verify( linkToInsert.prev == 0p );
[7806f91]228 )
[0eacfd4]229
[65b0402]230 tE & list_pos_elem = *(tE *)ORIGIN_TAG_CLEAR( (size_t)&before );
231 dlink( tE ) & list_pos_links = list_pos_elem`inner;
[9dd1dd6]232 asm( "" : : : "memory" );
[65b0402]233 tE & before_raw = *list_pos_links.prev;
234 tE & before_elem = *(tE *) ORIGIN_TAG_CLEAR( (size_t)&before_raw );
235 linkToInsert.next = &before;
236 linkToInsert.prev = &before_raw;
237 dlink( tE ) & beforeLinks = before_elem`inner;
238 beforeLinks.next = &node;
239 list_pos_links.prev = &node;
[9dd1dd6]240 asm( "" : : : "memory" );
[65b0402]241 return node;
[0eacfd4]242 }
[6cbc5a62]243 // FIXME: Change from pointer to reference for node, when tuple type can handle references.
244 forall( List ... | { void insert_before( tE & before, List ); } )
245 void insert_before( tE & before, tE * node, List args ) {
246 insert_before( before, *node );
247 insert_before( before, args );
248 }
249 void insert_before( tE & before, tE * node ) {
250 insert_before( before, *node );
251 }
[0eacfd4]252
[65b0402]253 tE & insert_after( tE & after, tE & node ) {
254 verify( &after != 0p );
255 verify( &node != 0p );
[6091b88a]256
[65b0402]257 dlink( tE ) & linkToInsert = node`inner;
[6cbc5a62]258 NOLOOSE(
[0eacfd4]259 verify( linkToInsert.prev == 0p );
[65b0402]260 verify( linkToInsert.next == 0p );
[7806f91]261 )
[0eacfd4]262
[65b0402]263 tE & list_pos_elem = *(tE *)ORIGIN_TAG_CLEAR( (size_t)&after );
264 dlink( tE ) & list_pos_links = list_pos_elem`inner;
[9dd1dd6]265 asm( "" : : : "memory" );
[65b0402]266 tE & after_raw = *list_pos_links.next;
267 tE & after_elem = *(tE *)ORIGIN_TAG_CLEAR( (size_t)&after_raw );
268 linkToInsert.prev = &after;
269 linkToInsert.next = &after_raw;
270 dlink( tE ) & afterLinks = after_elem`inner;
271 afterLinks.prev = &node;
272 list_pos_links.next = &node;
[9dd1dd6]273 asm( "" : : : "memory" );
[65b0402]274 return node;
[6091b88a]275 }
[6cbc5a62]276 // FIXME: Change from pointer to reference for node, when tuple type can handle references.
277 forall( List ... | { void insert_after( tE & after, List ); } )
278 void insert_after( tE & after, tE * node, List args ) {
279 insert_after( after, *node );
280 insert_after( after, args );
281 }
282 void insert_after( tE & after, tE * node ) {
283 insert_after( after, *node );
284 }
[6091b88a]285
[65b0402]286 tE & remove( tE & node ) {
287 verify( &node != 0p );
288 verify( ! ORIGIN_TAG_QUERY( (size_t)&node) );
289
290 dlink( tE ) & list_pos_links = node`inner;
291 tE & before_raw = *list_pos_links.prev;
292 tE & before_elem = *(tE *)ORIGIN_TAG_CLEAR( (size_t)&before_raw );
293 dlink( tE ) & before_links = before_elem`inner;
294 tE & after_raw = *list_pos_links.next;
295 tE & after_elem = *(tE *)ORIGIN_TAG_CLEAR( (size_t)&after_raw );
296 dlink( tE ) & after_links = after_elem`inner;
[9dd1dd6]297 before_links.next = &after_raw;
298 after_links.prev = &before_raw;
[7806f91]299
[6cbc5a62]300 NOLOOSE(
[9dd1dd6]301 asm( "" : : : "memory" );
[69914cbc]302 list_pos_links.prev = 0p;
303 list_pos_links.next = 0p;
[9dd1dd6]304 asm( "" : : : "memory" );
[7806f91]305 )
[65b0402]306 return node;
[9dd1dd6]307 }
[6cbc5a62]308 // FIXME: Change from pointer to reference for node, when tuple type can handle references.
309 forall( List ... | { void remove( List ); } )
310 void remove( tE * node, List args ) {
311 remove( *node );
312 remove( args );
313 }
314 void remove( tE * node ) {
315 remove( *node );
316 }
[65b0402]317
[6b33e89]318 tE & iter( dlist( tE, tLinks ) & list ) {
[65b0402]319 tE * origin = $get_list_origin_addr( list );
[9dd1dd6]320 return *origin;
321 }
322
[6b33e89]323 bool recede( tE && refx ) {
[9dd1dd6]324 tE && ref_inner = refx;
[65b0402]325 tE & oldReferent = *(tE*)ORIGIN_TAG_CLEAR( (size_t)&ref_inner );
[6b33e89]326 &ref_inner = oldReferent`inner.prev;
[65b0402]327 return &ref_inner != 0p && ! ORIGIN_TAG_QUERY( (size_t)&ref_inner );
[0eacfd4]328 }
[9dd1dd6]329
[6b33e89]330 bool advance( tE && refx ) {
[9dd1dd6]331 tE && ref_inner = refx;
[65b0402]332 tE & oldReferent = *(tE*)ORIGIN_TAG_CLEAR( (size_t)&ref_inner );
[6b33e89]333 &ref_inner = oldReferent`inner.next;
[65b0402]334 return &ref_inner != 0p && ! ORIGIN_TAG_QUERY( (size_t)&ref_inner );
[0eacfd4]335 }
[9dd1dd6]336
[e6e250d]337 bool first( tE & node ) {
[6cbc5a62]338 return recede( node );
339 }
[9dd1dd6]340
[e6e250d]341 bool last( tE & node ) {
[6cbc5a62]342 return advance( node );
[6b33e89]343 }
[9dd1dd6]344
[6b33e89]345 tE & prev( tE & node ) {
346 if ( recede( node ) ) return node;
[0eacfd4]347 return *0p;
[9dd1dd6]348 }
349
[6b33e89]350 tE & next( tE & node ) {
351 if ( advance( node ) ) return node;
[0eacfd4]352 return *0p;
[9dd1dd6]353 }
354
[65b0402]355 tE & insert_first( dlist( tE, tLinks ) & list, tE & node ) {
[6b33e89]356 insert_after( iter( list ), node );
[65b0402]357 return node;
[9dd1dd6]358 }
[6cbc5a62]359 // FIXME: Change from pointer to reference for node, when tuple type can handle references.
360 forall( List ... | { void insert_first( dlist( tE, tLinks ) & list, List ); } )
361 void insert_first( dlist( tE, tLinks ) & list, tE * node, List args ) {
362 insert_first( list, *node );
363 insert_first( list, args );
364 }
365 void insert_first( dlist( tE, tLinks ) & list, tE * node ) {
366 insert_first( list, *node );
367 }
[9dd1dd6]368
[65b0402]369 tE & insert_last( dlist( tE, tLinks ) & list, tE & node ) {
[6b33e89]370 insert_before( iter( list ), node );
[65b0402]371 return node;
372 }
[6cbc5a62]373 // FIXME: Change from pointer to reference for node, when tuple type can handle references.
374 forall( List ... | { void insert_last( dlist( tE, tLinks ) & list, List ); } )
375 void insert_last( dlist( tE, tLinks ) & list, tE * node, List args ) {
376 insert_last( list, *node );
377 insert_last( list, args );
378 }
379 void insert_last( dlist( tE, tLinks ) & list, tE * node ) {
380 insert_last( list, *node );
381 }
382
[6b33e89]383 tE & insert( dlist( tE, tLinks ) & list, tE & node ) { // synonym for insert_last
[65b0402]384 insert_last( list, node );
385 return node;
[0eacfd4]386 }
[6cbc5a62]387 // FIXME: Change from pointer to reference for node, when tuple type can handle references.
388 forall( List ... | { void insert( dlist( tE, tLinks ) & list, List ); } )
389 void insert( dlist( tE, tLinks ) & list, tE * node, List args ) {
390 insert( list, *node );
391 insert( list, args );
392 }
393 void insert( dlist( tE, tLinks ) & list, tE * node ) {
394 insert( list, *node );
395 }
[65b0402]396
397 tE & remove_first( dlist( tE, tLinks ) & list ) {
[6b33e89]398 tE & first_node = first( list );
399 if ( &first_node ) return remove( first_node );
400 return first_node;
[65b0402]401 }
402
403 tE & remove_last( dlist( tE, tLinks ) & list ) {
[6b33e89]404 tE & last_node = last( list );
405 if ( &last_node ) return remove( last_node );
406 return last_node;
[9dd1dd6]407 }
408
[65b0402]409 // Transfer the "from" list to the end of this sequence; the "from" list is empty after the transfer.
410// void transfer( dlist( tE, tLinks ) & to, dlist( tE, tLinks ) & from ) {
[81ab5eb]411// if ( empty( from ) ) return; // "from" list empty ?
412// if ( empty( to ) ) { // "to" list empty ?
[65b0402]413// root = from.root;
414// } else { // "to" list not empty
415// T * toEnd = (T *)uBack( root );
416// T * fromEnd = (T *)from.uBack( from.root );
417// uBack( root ) = fromEnd;
418// from.uNext( fromEnd ) = root;
419// from.uBack( from.root ) = toEnd;
420// uNext( toEnd ) = from.root;
421// } // if
422// from.root = nullptr; // mark "from" list empty
423// }
424
425 // Transfer the "from" list up to node "n" to the end of this list; the "from" list becomes the sequence after node "n".
426 // Node "n" must be in the "from" list.
427// void split( dlist( tE, tLinks ) & to, dlist( tE, tLinks ) & from, tE & node ) {
428// #ifdef __U_DEBUG__
429// if ( ! n->listed() ) abort( "(uSequence &)%p.split( %p ) : Node is not on a list.", this, n );
430// #endif // __U_DEBUG__
431// uSequence<T> to;
432// to.root = from.root; // start of "to" list
433// from.root = (T *)uNext( n ); // start of "from" list
434// if ( to.root == from.root ) { // last node in list ?
435// from.root = nullptr; // mark "from" list empty
436// } else {
437// uBack( from.root ) = (T *)uBack( to.root ); // fix "from" list
438// uNext( uBack( to.root ) ) = from.root;
439// uNext( n ) = to.root; // fix "to" list
440// uBack( to.root ) = n;
441// } // if
442// transfer( to );
443// }
444
[0eacfd4]445 #if ! defined(NDEBUG) && (defined(__CFA_DEBUG__) || defined(__CFA_VERIFY__))
[65b0402]446 bool $validate_fwd( dlist( tE, tLinks ) & this ) {
[6b33e89]447 if ( ! & first( this ) ) return &last( this ) == 0p;
[69914cbc]448
[9dd1dd6]449 tE & lagElem = *0p;
[6b33e89]450 while ( tE & it = iter( this ); advance( it ) ) {
451 if ( & lagElem == 0p && &it != & first( this ) ) return false;
[0eacfd4]452 &lagElem = &it;
[9dd1dd6]453 }
[69914cbc]454
[6b33e89]455 if ( &lagElem != &last( this ) ) return false;
[69914cbc]456
[6b33e89]457 // TODO: verify that it is back at iter( this );
[9dd1dd6]458 return true;
[6091b88a]459 }
460
[65b0402]461 bool $validate_rev( dlist( tE, tLinks ) & this ) {
[6b33e89]462 if ( ! & last( this ) ) return &first( this ) == 0p;
[f2d05e9]463
[0eacfd4]464 tE & lagElem = *0p;
[6b33e89]465 while ( tE & it = iter( this ); recede( it ) ) {
466 if ( &lagElem == 0p && &it != & last( this ) ) return false;
[0eacfd4]467 &lagElem = &it;
[9dd1dd6]468 }
[f2d05e9]469
[6b33e89]470 if ( &lagElem != &first( this ) ) return false;
[f2d05e9]471
[6b33e89]472 // TODO: verify that it is back at iter( this );
[9dd1dd6]473 return true;
[69914cbc]474 }
[0eacfd4]475
[65b0402]476 bool validate( dlist( tE, tLinks ) & this ) {
477 return $validate_fwd( this ) && $validate_rev( this );
[f2d05e9]478 }
[0eacfd4]479 #endif
[6091b88a]480}
481
[0eacfd4]482// TEMPORARY, until foreach statement created.
[6b33e89]483#define FOREACH( list, index ) for ( typeof(iter( list )) & (index) = iter( list ); advance( index ); )
484#define FOREACH_REV( list, index ) for ( typeof(iter( list )) & (index) = iter( list ); recede( index ); )
485#define FOREACH_COND( list, index, expr ) for ( typeof(iter( list )) & (index) = iter( list ); advance( index ) && !(expr); )
486#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.