1 | #include <fstream.hfa> |
---|
2 | #include <stdlib.hfa> |
---|
3 | #include <bits/stack.hfa> |
---|
4 | #include <bits/queue.hfa> |
---|
5 | #include <bits/sequence.hfa> |
---|
6 | |
---|
7 | struct Task; // node type |
---|
8 | |
---|
9 | struct TaskDL { |
---|
10 | inline Seqable; |
---|
11 | Task & node; |
---|
12 | }; |
---|
13 | void ?{}( TaskDL & this, Task & task ) with( this ) { |
---|
14 | ((Seqable &)this){}; |
---|
15 | &node = &task; // pointer to containing node |
---|
16 | } |
---|
17 | Task & task( TaskDL & this ) with( this ) { // getter routine for containing node |
---|
18 | return node; |
---|
19 | } |
---|
20 | |
---|
21 | TaskDL *& Back( TaskDL * n ) { |
---|
22 | return (TaskDL *)Back( (Seqable *)n ); |
---|
23 | } |
---|
24 | |
---|
25 | TaskDL *& Next( TaskDL * n ) { |
---|
26 | return (TaskDL *)Next( (Colable *)n ); |
---|
27 | } |
---|
28 | |
---|
29 | struct TaskSL { |
---|
30 | inline Colable; |
---|
31 | Task & node; |
---|
32 | }; |
---|
33 | void ?{}( TaskSL & this, Task & task ) with( this ) { |
---|
34 | ((Colable &)this){}; |
---|
35 | &node = &task; // pointer to containing node |
---|
36 | } |
---|
37 | Task & task( TaskSL & this ) with( this ) { // getter routine for containing node |
---|
38 | return node; |
---|
39 | } |
---|
40 | |
---|
41 | TaskSL *& Next( TaskSL * n ) { |
---|
42 | return (TaskSL *)Next( (Colable *)n ); |
---|
43 | } |
---|
44 | |
---|
45 | struct 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 | }; |
---|
51 | void ?{}( Task & this, int id ) with( this ) { |
---|
52 | ((TaskDL &)clusterRef){ this }; |
---|
53 | ((TaskDL &)readyRef){ this }; |
---|
54 | ((TaskSL &)mutexRef){ this }; |
---|
55 | this.id = id; |
---|
56 | } |
---|
57 | |
---|
58 | int 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 | } |
---|