source: tests/collections/multi_list.cfa@ d829c6d

Last change on this file since d829c6d was 657c36f, checked in by Peter A. Buhr <pabuhr@…>, 5 years ago

formatting, update iterator operator, add some additional tests

  • Property mode set to 100644
File size: 3.0 KB
Line 
1#include <fstream.hfa>
2#include <stdlib.hfa>
3#include <bits/stack.hfa>
4#include <bits/queue.hfa>
5#include <bits/sequence.hfa>
6
7struct Task; // node type
8
9struct TaskDL {
10 inline Seqable;
11 Task & node;
12};
13void ?{}( TaskDL & this, Task & task ) with( this ) {
14 ((Seqable &)this){};
15 &node = &task; // pointer to containing node
16}
17Task & task( TaskDL & this ) with( this ) { // getter routine for containing node
18 return node;
19}
20
21TaskDL *& Back( TaskDL * n ) {
22 return (TaskDL *)Back( (Seqable *)n );
23}
24
25TaskDL *& Next( TaskDL * n ) {
26 return (TaskDL *)Next( (Colable *)n );
27}
28
29struct TaskSL {
30 inline Colable;
31 Task & node;
32};
33void ?{}( TaskSL & this, Task & task ) with( this ) {
34 ((Colable &)this){};
35 &node = &task; // pointer to containing node
36}
37Task & task( TaskSL & this ) with( this ) { // getter routine for containing node
38 return node;
39}
40
41TaskSL *& Next( TaskSL * n ) {
42 return (TaskSL *)Next( (Colable *)n );
43}
44
45struct Task {
46 TaskDL clusterRef; // list of tasks on cluster
47 TaskDL readyRef; // list of tasks on ready queue
48 TaskSL mutexRef; // list of tasks on mutex queue
49 int id;
50};
51void ?{}( Task & this, int id ) with( this ) {
52 ((TaskDL &)clusterRef){ this };
53 ((TaskDL &)readyRef){ this };
54 ((TaskSL &)mutexRef){ this };
55 this.id = id;
56}
57
58int main() {
59 Sequence(TaskDL) clustList, readyList; // task lists
60 Queue(TaskSL) mutexList;
61 enum { Lnth = 10 };
62
63 for ( id; Lnth ) {
64 Task & task = *new( id ); // create task node
65 addHead( clustList, task.clusterRef ); // insert on lists in opposite directions
66 addTail( readyList, task.readyRef );
67 addHead( mutexList, task.mutexRef );
68 }
69
70 SeqIter(TaskDL) sqiter;
71 TaskDL & dl; // iterator index
72 TaskSL & sl;
73
74 sout | nlOff;
75 for ( over( sqiter, clustList ); sqiter | dl; ) { // print lists
76 Task & tmp = task( dl ); sout | tmp.id;
77 // sout | task( dl ).id;
78 }
79 sout | nl;
80 for ( over( sqiter, readyList ); sqiter | dl; ) {
81 Task & tmp = task( dl ); sout | tmp.id;
82 // sout | task( dl ).id;
83 }
84 sout | nl;
85 for ( QueueIter(TaskSL) qiter = { mutexList }; qiter | sl; ) { // print lists
86 Task & tmp = task( sl ); sout | tmp.id;
87 // sout | task( sl ).id;
88 }
89 sout | nl;
90 for ( Lnth ) { // remove nodes from clustList. mutexList
91 dropHead( clustList );
92 drop( mutexList );
93 }
94 // Simultaneous deletion only safe if all nodes are traversed in same direction.
95 for ( Lnth ) { // remove nodes from readyList and safe to delete nodes
96 delete( &task( dropHead( readyList ) ) );
97 }
98
99 // Re-purpose Seqable as Colable
100 Stack(TaskDL) mutexStack;
101 for ( id; Lnth ) {
102 Task & task = *new( id ); // create task node
103 push( mutexStack, task.clusterRef ); // insert on lists in opposite directions
104 }
105 for ( StackIter(TaskDL) stiter = { mutexStack }; stiter | dl; ) {
106 Task & tmp = task( dl ); sout | tmp.id;
107 // sout | task( dl ).id;
108 }
109 sout | nl;
110 for ( Lnth ) { // remove nodes from readyList and safe to delete nodes
111 delete( &task( pop( mutexStack ) ) );
112 }
113}
Note: See TracBrowser for help on using the repository browser.