source: tests/multi_list.cfa@ 39d22ef

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 39d22ef 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
RevLine 
[9082e0f1]1#include <fstream.hfa>
2#include <stdlib.hfa>
[edf3ff1]3#include <bits/stack.hfa>
4#include <bits/queue.hfa>
5#include <bits/sequence.hfa>
[9082e0f1]6
7struct Task; // node type
8
9struct TaskDL {
10 inline Seqable;
[f7386f7]11 Task & node;
[9082e0f1]12};
13void ?{}( TaskDL & this, Task & task ) with( this ) {
14 ((Seqable &)this){};
[f7386f7]15 &node = &task; // pointer to containing node
[9082e0f1]16}
17Task & task( TaskDL & this ) with( this ) { // getter routine for containing node
[f7386f7]18 return node;
19}
20
[c8025a21]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
[f7386f7]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;
[9082e0f1]43}
44
[c8025a21]45TaskSL *& Next( TaskSL * n ) {
46 return (TaskSL *)Next( (Colable *)n );
47}
48
49bool listed( TaskSL * n ) {
50 return Next( (Colable *)n ) != 0p;
51}
52
[9082e0f1]53struct Task {
54 TaskDL clusterRef; // list of tasks on cluster
55 TaskDL readyRef; // list of tasks on ready queue
[f7386f7]56 TaskSL mutexRef; // list of tasks on mutex queue
57 int id;
[9082e0f1]58};
[f7386f7]59void ?{}( Task & this, int id ) with( this ) {
[9082e0f1]60 ((TaskDL &)clusterRef){ this };
61 ((TaskDL &)readyRef){ this };
[f7386f7]62 ((TaskSL &)mutexRef){ this };
63 this.id = id;
[9082e0f1]64}
65
66int main() {
67 Sequence(TaskDL) clustList, readyList; // task lists
[f7386f7]68 Queue(TaskSL) mutexList;
[9082e0f1]69 enum { Lnth = 10 };
70
[f7386f7]71 for ( id; Lnth ) {
72 Task & task = *new( id ); // create task node
[9082e0f1]73 addHead( clustList, task.clusterRef ); // insert on lists in opposite directions
74 addTail( readyList, task.readyRef );
[f7386f7]75 addHead( mutexList, task.mutexRef );
[9082e0f1]76 }
77
[1ac1f0b]78 SeqIter(TaskDL) sqiter;
[7c1144b]79 TaskDL & dl; // iterator index
[f7386f7]80 TaskSL & sl;
[9082e0f1]81
82 sout | nlOff;
[f7386f7]83 for ( over( sqiter, clustList ); sqiter >> dl; ) { // print lists
[4f649cb]84 Task & tmp = task( dl ); sout | tmp.id;
85 // sout | task( dl ).id;
[9082e0f1]86 }
87 sout | nl;
[f7386f7]88 for ( over( sqiter, readyList ); sqiter >> dl; ) {
[4f649cb]89 Task & tmp = task( dl ); sout | tmp.id;
90 // sout | task( dl ).id;
[9082e0f1]91 }
92 sout | nl;
[f7386f7]93 for ( QueueIter(TaskSL) qiter = { mutexList }; qiter >> sl; ) { // print lists
[4f649cb]94 Task & tmp = task( sl ); sout | tmp.id;
95 // sout | task( sl ).id;
[f7386f7]96 }
97 sout | nl;
[4f649cb]98 for ( Lnth ) { // remove nodes from clustList. mutexList
[9082e0f1]99 dropHead( clustList );
[f7386f7]100 drop( mutexList );
[9082e0f1]101 }
[4f649cb]102 // Simultaneous deletion only safe if all nodes are traversed in same direction.
[9082e0f1]103 for ( Lnth ) { // remove nodes from readyList and safe to delete nodes
104 delete( &task( dropHead( readyList ) ) );
105 }
106
[34dcc78c]107 // Re-purpose Seqable as Colable
108 Stack(TaskDL) mutexStack;
[f7386f7]109 for ( id; Lnth ) {
110 Task & task = *new( id ); // create task node
[34dcc78c]111 push( mutexStack, task.clusterRef ); // insert on lists in opposite directions
112 }
[1ac1f0b]113 for ( StackIter(TaskDL) stiter = { mutexStack }; stiter >> dl; ) {
[4f649cb]114 Task & tmp = task( dl ); sout | tmp.id;
115 // sout | task( dl ).id;
[34dcc78c]116 }
117 sout | nl;
118 for ( Lnth ) { // remove nodes from readyList and safe to delete nodes
119 delete( &task( pop( mutexStack ) ) );
120 }
[9082e0f1]121}
Note: See TracBrowser for help on using the repository browser.