source: tests/multi_list.cfa@ 18e4cd0

ADT arm-eh ast-experimental enum forall-pointer-decay jacob/cs343-translation new-ast-unique-expr pthread-emulation qualifiedEnum
Last change on this file since 18e4cd0 was c8025a21, checked in by Colby Alexander Parsons <caparsons@…>, 5 years ago

fixed multi list test to reflect changes to collections

  • Property mode set to 100644
File size: 3.1 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
29bool listed( TaskDL * n ) {
30 return Next( (Colable *)n ) != 0p;
31}
32
33struct TaskSL {
34 inline Colable;
35 Task & node;
36};
37void ?{}( TaskSL & this, Task & task ) with( this ) {
38 ((Colable &)this){};
39 &node = &task; // pointer to containing node
40}
41Task & task( TaskSL & this ) with( this ) { // getter routine for containing node
42 return node;
43}
44
45TaskSL *& Next( TaskSL * n ) {
46 return (TaskSL *)Next( (Colable *)n );
47}
48
49bool listed( TaskSL * n ) {
50 return Next( (Colable *)n ) != 0p;
51}
52
53struct Task {
54 TaskDL clusterRef; // list of tasks on cluster
55 TaskDL readyRef; // list of tasks on ready queue
56 TaskSL mutexRef; // list of tasks on mutex queue
57 int id;
58};
59void ?{}( Task & this, int id ) with( this ) {
60 ((TaskDL &)clusterRef){ this };
61 ((TaskDL &)readyRef){ this };
62 ((TaskSL &)mutexRef){ this };
63 this.id = id;
64}
65
66int main() {
67 Sequence(TaskDL) clustList, readyList; // task lists
68 Queue(TaskSL) mutexList;
69 enum { Lnth = 10 };
70
71 for ( id; Lnth ) {
72 Task & task = *new( id ); // create task node
73 addHead( clustList, task.clusterRef ); // insert on lists in opposite directions
74 addTail( readyList, task.readyRef );
75 addHead( mutexList, task.mutexRef );
76 }
77
78 SeqIter(TaskDL) sqiter;
79 TaskDL & dl; // iterator index
80 TaskSL & sl;
81
82 sout | nlOff;
83 for ( over( sqiter, clustList ); sqiter >> dl; ) { // print lists
84 Task & tmp = task( dl ); sout | tmp.id;
85 // sout | task( dl ).id;
86 }
87 sout | nl;
88 for ( over( sqiter, readyList ); sqiter >> dl; ) {
89 Task & tmp = task( dl ); sout | tmp.id;
90 // sout | task( dl ).id;
91 }
92 sout | nl;
93 for ( QueueIter(TaskSL) qiter = { mutexList }; qiter >> sl; ) { // print lists
94 Task & tmp = task( sl ); sout | tmp.id;
95 // sout | task( sl ).id;
96 }
97 sout | nl;
98 for ( Lnth ) { // remove nodes from clustList. mutexList
99 dropHead( clustList );
100 drop( mutexList );
101 }
102 // Simultaneous deletion only safe if all nodes are traversed in same direction.
103 for ( Lnth ) { // remove nodes from readyList and safe to delete nodes
104 delete( &task( dropHead( readyList ) ) );
105 }
106
107 // Re-purpose Seqable as Colable
108 Stack(TaskDL) mutexStack;
109 for ( id; Lnth ) {
110 Task & task = *new( id ); // create task node
111 push( mutexStack, task.clusterRef ); // insert on lists in opposite directions
112 }
113 for ( StackIter(TaskDL) stiter = { mutexStack }; stiter >> dl; ) {
114 Task & tmp = task( dl ); sout | tmp.id;
115 // sout | task( dl ).id;
116 }
117 sout | nl;
118 for ( Lnth ) { // remove nodes from readyList and safe to delete nodes
119 delete( &task( pop( mutexStack ) ) );
120 }
121}
Note: See TracBrowser for help on using the repository browser.