source: libcfa/src/collections/list.hfa@ 00675ed4

Last change on this file since 00675ed4 was 00675ed4, checked in by Peter A. Buhr <pabuhr@…>, 7 days ago

1st attempt at harmonizing isOp functions, e.g., isEmpty, to C/C++ form empty

  • Property mode set to 100644
File size: 15.9 KB
Line 
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
11// Last Modified By : Peter A. Buhr
12// Last Modified On : Fri Mar 27 08:02:56 2026
13// Update Count : 99
14//
15
16#pragma once
17
18#include <assert.h>
19
20forall( Decorator &, T & )
21struct tytagref {
22 inline T &;
23};
24
25forall( tOuter &, tMid &, tInner & )
26trait embedded {
27 tytagref( tMid, tInner ) ?`inner( tOuter & );
28};
29
30// embedded is reflexive, with no info (void) as the type tag
31forall( T & )
32static inline tytagref(void, T) ?`inner ( T & this ) { tytagref( void, T ) ret = {this}; return ret; }
33
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 {
39// int a, b, c;
40// inline (bar);
41// };
42// P9_EMBEDDED( foo, bar )
43//
44
45// usual version, for structs that are top-level declarations
46#define P9_EMBEDDED( derived, immedBase ) P9_EMBEDDED_DECL_( derived, immedBase, static ) P9_EMBEDDED_BDY_( immedBase )
47
48// special version, for structs that are declared in functions
49#define P9_EMBEDDED_INFUNC( derived, immedBase ) P9_EMBEDDED_DECL_( derived, immedBase, ) P9_EMBEDDED_BDY_( immedBase )
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
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, ) ;
55
56// private helpers
57#define P9_EMBEDDED_DECL_( derived, immedBase, STORAGE ) \
58 forall( Tbase &, TdiscardPath & | { tytagref( TdiscardPath, Tbase ) ?`inner( immedBase & ); } ) \
59 STORAGE inline tytagref( immedBase, Tbase ) ?`inner( derived & this )
60
61#define P9_EMBEDDED_BDY_( immedBase ) { \
62 immedBase & ib = this; \
63 Tbase & b = ib`inner; \
64 tytagref( immedBase, Tbase ) result = { b }; \
65 return result; \
66 }
67
68#define EMBEDDED_VIA( OUTER, MID, INNER ) (struct { tytagref( MID, INNER ) ( * ?`inner ) ( OUTER & ); }){ ?`inner }
69
70#define DLINK_VIA( TE, TLINK ) EMBEDDED_VIA( TE, TLINK, dlink( TE ) )
71
72
73// The origin is the position encountered at the start of iteration, signifying, "need to advance to the first element,"
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.
81
82#ifdef __EXPERIMENTAL_DISABLE_OTAG__ // Perf experimention alt mode
83
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.
87
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
94
95#else // Normal
96
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 )
132
133#endif
134
135
136#ifdef __EXPERIMENTAL_LOOSE_SINGLES__ // Perf experimention alt mode
137
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.
143
144 #define NOLOOSE(...)
145 #define LOOSEONLY(...) __VA_ARGS__
146
147#else // Normal
148
149 #define NOLOOSE(...) __VA_ARGS__
150 #define LOOSEONLY(...)
151
152#endif
153
154
155forall( tE & ) {
156 struct dlink{
157 tE * next, * prev;
158 };
159
160 static inline void ?{}( dlink( tE ) & this ) {
161 NOLOOSE(
162 this.next = this.prev = 0p;
163 )
164 }
165
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 }
184 }
185 }
186}
187
188
189static inline forall( tE &, tLinks & | embedded( tE, tLinks, dlink( tE ) ) ) {
190 bool isListed( tE & node ) {
191 NOLOOSE(
192 verify( &node != 0p );
193 dlink( tE ) & node_links = node`inner;
194 return (node_links.prev != 0p) || (node_links.next != 0p);
195 )
196 LOOSEONLY(
197 verify(false && "isListed is undefined");
198 return true;
199 )
200 }
201
202 bool empty( dlist( tE, tLinks ) & list ) {
203 tE * firstPtr = list.next;
204 if ( ORIGIN_TAG_QUERY(( size_t)firstPtr) ) firstPtr = 0p;
205 return firstPtr == 0p;
206 }
207
208 bool isEmpty( dlist( tE, tLinks ) & list ) {
209 return empty( list );
210 }
211
212 tE & first( dlist( tE, tLinks ) & list ) {
213 tE * firstPtr = list.next;
214 if ( ORIGIN_TAG_QUERY( (size_t)firstPtr ) ) firstPtr = 0p;
215 return *firstPtr;
216 }
217
218 tE & last( dlist( tE, tLinks ) & list ) {
219 tE * lastPtr = list.prev;
220 if ( ORIGIN_TAG_QUERY( (size_t)lastPtr) ) lastPtr = 0p;
221 return *lastPtr;
222 }
223
224 tE & insert_before( tE & before, tE & node ) {
225 verify( &before != 0p );
226 verify( &node != 0p );
227
228 dlink( tE ) & linkToInsert = node`inner;
229 NOLOOSE(
230 verify( linkToInsert.next == 0p );
231 verify( linkToInsert.prev == 0p );
232 )
233
234 tE & list_pos_elem = *(tE *)ORIGIN_TAG_CLEAR( (size_t)&before );
235 dlink( tE ) & list_pos_links = list_pos_elem`inner;
236 asm( "" : : : "memory" );
237 tE & before_raw = *list_pos_links.prev;
238 tE & before_elem = *(tE *) ORIGIN_TAG_CLEAR( (size_t)&before_raw );
239 linkToInsert.next = &before;
240 linkToInsert.prev = &before_raw;
241 dlink( tE ) & beforeLinks = before_elem`inner;
242 beforeLinks.next = &node;
243 list_pos_links.prev = &node;
244 asm( "" : : : "memory" );
245 return node;
246 }
247 // FIXME: Change from pointer to reference for node, when tuple type can handle references.
248 forall( List ... | { void insert_before( tE & before, List ); } )
249 void insert_before( tE & before, tE * node, List args ) {
250 insert_before( before, *node );
251 insert_before( before, args );
252 }
253 void insert_before( tE & before, tE * node ) {
254 insert_before( before, *node );
255 }
256
257 tE & insert_after( tE & after, tE & node ) {
258 verify( &after != 0p );
259 verify( &node != 0p );
260
261 dlink( tE ) & linkToInsert = node`inner;
262 NOLOOSE(
263 verify( linkToInsert.prev == 0p );
264 verify( linkToInsert.next == 0p );
265 )
266
267 tE & list_pos_elem = *(tE *)ORIGIN_TAG_CLEAR( (size_t)&after );
268 dlink( tE ) & list_pos_links = list_pos_elem`inner;
269 asm( "" : : : "memory" );
270 tE & after_raw = *list_pos_links.next;
271 tE & after_elem = *(tE *)ORIGIN_TAG_CLEAR( (size_t)&after_raw );
272 linkToInsert.prev = &after;
273 linkToInsert.next = &after_raw;
274 dlink( tE ) & afterLinks = after_elem`inner;
275 afterLinks.prev = &node;
276 list_pos_links.next = &node;
277 asm( "" : : : "memory" );
278 return node;
279 }
280 // FIXME: Change from pointer to reference for node, when tuple type can handle references.
281 forall( List ... | { void insert_after( tE & after, List ); } )
282 void insert_after( tE & after, tE * node, List args ) {
283 insert_after( after, *node );
284 insert_after( after, args );
285 }
286 void insert_after( tE & after, tE * node ) {
287 insert_after( after, *node );
288 }
289
290 tE & remove( tE & node ) {
291 verify( &node != 0p );
292 verify( ! ORIGIN_TAG_QUERY( (size_t)&node) );
293
294 dlink( tE ) & list_pos_links = node`inner;
295 tE & before_raw = *list_pos_links.prev;
296 tE & before_elem = *(tE *)ORIGIN_TAG_CLEAR( (size_t)&before_raw );
297 dlink( tE ) & before_links = before_elem`inner;
298 tE & after_raw = *list_pos_links.next;
299 tE & after_elem = *(tE *)ORIGIN_TAG_CLEAR( (size_t)&after_raw );
300 dlink( tE ) & after_links = after_elem`inner;
301 before_links.next = &after_raw;
302 after_links.prev = &before_raw;
303
304 NOLOOSE(
305 asm( "" : : : "memory" );
306 list_pos_links.prev = 0p;
307 list_pos_links.next = 0p;
308 asm( "" : : : "memory" );
309 )
310 return node;
311 }
312 // FIXME: Change from pointer to reference for node, when tuple type can handle references.
313 forall( List ... | { void remove( List ); } )
314 void remove( tE * node, List args ) {
315 remove( *node );
316 remove( args );
317 }
318 void remove( tE * node ) {
319 remove( *node );
320 }
321
322 tE & iter( dlist( tE, tLinks ) & list ) {
323 tE * origin = $get_list_origin_addr( list );
324 return *origin;
325 }
326
327 bool recede( tE && refx ) {
328 tE && ref_inner = refx;
329 tE & oldReferent = *(tE*)ORIGIN_TAG_CLEAR( (size_t)&ref_inner );
330 &ref_inner = oldReferent`inner.prev;
331 return &ref_inner != 0p && ! ORIGIN_TAG_QUERY( (size_t)&ref_inner );
332 }
333
334 bool advance( tE && refx ) {
335 tE && ref_inner = refx;
336 tE & oldReferent = *(tE*)ORIGIN_TAG_CLEAR( (size_t)&ref_inner );
337 &ref_inner = oldReferent`inner.next;
338 return &ref_inner != 0p && ! ORIGIN_TAG_QUERY( (size_t)&ref_inner );
339 }
340
341 bool isFirst( tE & node ) {
342 return recede( node );
343 }
344
345 bool isLast( tE & node ) {
346 return advance( node );
347 }
348
349 tE & prev( tE & node ) {
350 if ( recede( node ) ) return node;
351 return *0p;
352 }
353
354 tE & next( tE & node ) {
355 if ( advance( node ) ) return node;
356 return *0p;
357 }
358
359 tE & insert_first( dlist( tE, tLinks ) & list, tE & node ) {
360 insert_after( iter( list ), node );
361 return node;
362 }
363 // FIXME: Change from pointer to reference for node, when tuple type can handle references.
364 forall( List ... | { void insert_first( dlist( tE, tLinks ) & list, List ); } )
365 void insert_first( dlist( tE, tLinks ) & list, tE * node, List args ) {
366 insert_first( list, *node );
367 insert_first( list, args );
368 }
369 void insert_first( dlist( tE, tLinks ) & list, tE * node ) {
370 insert_first( list, *node );
371 }
372
373 tE & insert_last( dlist( tE, tLinks ) & list, tE & node ) {
374 insert_before( iter( list ), node );
375 return node;
376 }
377 // FIXME: Change from pointer to reference for node, when tuple type can handle references.
378 forall( List ... | { void insert_last( dlist( tE, tLinks ) & list, List ); } )
379 void insert_last( dlist( tE, tLinks ) & list, tE * node, List args ) {
380 insert_last( list, *node );
381 insert_last( list, args );
382 }
383 void insert_last( dlist( tE, tLinks ) & list, tE * node ) {
384 insert_last( list, *node );
385 }
386
387 tE & insert( dlist( tE, tLinks ) & list, tE & node ) { // synonym for insert_last
388 insert_last( list, node );
389 return node;
390 }
391 // FIXME: Change from pointer to reference for node, when tuple type can handle references.
392 forall( List ... | { void insert( dlist( tE, tLinks ) & list, List ); } )
393 void insert( dlist( tE, tLinks ) & list, tE * node, List args ) {
394 insert( list, *node );
395 insert( list, args );
396 }
397 void insert( dlist( tE, tLinks ) & list, tE * node ) {
398 insert( list, *node );
399 }
400
401 tE & remove_first( dlist( tE, tLinks ) & list ) {
402 tE & first_node = first( list );
403 if ( &first_node ) return remove( first_node );
404 return first_node;
405 }
406
407 tE & remove_last( dlist( tE, tLinks ) & list ) {
408 tE & last_node = last( list );
409 if ( &last_node ) return remove( last_node );
410 return last_node;
411 }
412
413 // Transfer the "from" list to the end of this sequence; the "from" list is empty after the transfer.
414// void transfer( dlist( tE, tLinks ) & to, dlist( tE, tLinks ) & from ) {
415// if ( isEmpty( from ) ) return; // "from" list empty ?
416// if ( isEmpty( to ) ) { // "to" list empty ?
417// root = from.root;
418// } else { // "to" list not empty
419// T * toEnd = (T *)uBack( root );
420// T * fromEnd = (T *)from.uBack( from.root );
421// uBack( root ) = fromEnd;
422// from.uNext( fromEnd ) = root;
423// from.uBack( from.root ) = toEnd;
424// uNext( toEnd ) = from.root;
425// } // if
426// from.root = nullptr; // mark "from" list empty
427// }
428
429 // Transfer the "from" list up to node "n" to the end of this list; the "from" list becomes the sequence after node "n".
430 // Node "n" must be in the "from" list.
431// void split( dlist( tE, tLinks ) & to, dlist( tE, tLinks ) & from, tE & node ) {
432// #ifdef __U_DEBUG__
433// if ( ! n->listed() ) abort( "(uSequence &)%p.split( %p ) : Node is not on a list.", this, n );
434// #endif // __U_DEBUG__
435// uSequence<T> to;
436// to.root = from.root; // start of "to" list
437// from.root = (T *)uNext( n ); // start of "from" list
438// if ( to.root == from.root ) { // last node in list ?
439// from.root = nullptr; // mark "from" list empty
440// } else {
441// uBack( from.root ) = (T *)uBack( to.root ); // fix "from" list
442// uNext( uBack( to.root ) ) = from.root;
443// uNext( n ) = to.root; // fix "to" list
444// uBack( to.root ) = n;
445// } // if
446// transfer( to );
447// }
448
449 #if ! defined(NDEBUG) && (defined(__CFA_DEBUG__) || defined(__CFA_VERIFY__))
450 bool $validate_fwd( dlist( tE, tLinks ) & this ) {
451 if ( ! & first( this ) ) return &last( this ) == 0p;
452
453 tE & lagElem = *0p;
454 while ( tE & it = iter( this ); advance( it ) ) {
455 if ( & lagElem == 0p && &it != & first( this ) ) return false;
456 &lagElem = &it;
457 }
458
459 if ( &lagElem != &last( this ) ) return false;
460
461 // TODO: verify that it is back at iter( this );
462 return true;
463 }
464
465 bool $validate_rev( dlist( tE, tLinks ) & this ) {
466 if ( ! & last( this ) ) return &first( this ) == 0p;
467
468 tE & lagElem = *0p;
469 while ( tE & it = iter( this ); recede( it ) ) {
470 if ( &lagElem == 0p && &it != & last( this ) ) return false;
471 &lagElem = &it;
472 }
473
474 if ( &lagElem != &first( this ) ) return false;
475
476 // TODO: verify that it is back at iter( this );
477 return true;
478 }
479
480 bool validate( dlist( tE, tLinks ) & this ) {
481 return $validate_fwd( this ) && $validate_rev( this );
482 }
483 #endif
484}
485
486// TEMPORARY, until foreach statement created.
487#define FOREACH( list, index ) for ( typeof(iter( list )) & (index) = iter( list ); advance( index ); )
488#define FOREACH_REV( list, index ) for ( typeof(iter( list )) & (index) = iter( list ); recede( index ); )
489#define FOREACH_COND( list, index, expr ) for ( typeof(iter( list )) & (index) = iter( list ); advance( index ) && !(expr); )
490#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.