1 | #include <fstream.hfa> |
---|
2 | #include <stdlib.hfa> |
---|
3 | #include "sequence.hfa" |
---|
4 | #include "stack.hfa" |
---|
5 | |
---|
6 | struct Task; // node type |
---|
7 | |
---|
8 | struct TaskDL { |
---|
9 | inline Seqable; |
---|
10 | Task & task_; |
---|
11 | }; |
---|
12 | void ?{}( TaskDL & this, Task & task ) with( this ) { |
---|
13 | ((Seqable &)this){}; |
---|
14 | &task_ = &task; // pointer to containing node |
---|
15 | } |
---|
16 | Task & task( TaskDL & this ) with( this ) { // getter routine for containing node |
---|
17 | return task_; |
---|
18 | } |
---|
19 | |
---|
20 | struct Task { |
---|
21 | TaskDL clusterRef; // list of tasks on cluster |
---|
22 | TaskDL readyRef; // list of tasks on ready queue |
---|
23 | // other stuff |
---|
24 | }; |
---|
25 | void ?{}( Task & this ) with( this ) { |
---|
26 | ((TaskDL &)clusterRef){ this }; |
---|
27 | ((TaskDL &)readyRef){ this }; |
---|
28 | } |
---|
29 | |
---|
30 | int main() { |
---|
31 | Sequence(TaskDL) clustList, readyList; // task lists |
---|
32 | enum { Lnth = 10 }; |
---|
33 | |
---|
34 | for ( Lnth ) { |
---|
35 | Task & task = *new(); // create task node |
---|
36 | addHead( clustList, task.clusterRef ); // insert on lists in opposite directions |
---|
37 | addTail( readyList, task.readyRef ); |
---|
38 | } |
---|
39 | |
---|
40 | SeqIter(TaskDL) iter; |
---|
41 | TaskDL & dl; |
---|
42 | |
---|
43 | sout | nlOff; |
---|
44 | for ( over( iter, clustList ); iter >> dl; ) { // print lists |
---|
45 | sout | &task( dl ); |
---|
46 | } |
---|
47 | sout | nl; |
---|
48 | for ( SeqIter(TaskDL) iter = { readyList }; iter >> dl; ) { |
---|
49 | sout | &task( dl ); |
---|
50 | } |
---|
51 | sout | nl; |
---|
52 | for ( Lnth ) { // remove nodes from clustList |
---|
53 | dropHead( clustList ); |
---|
54 | } |
---|
55 | for ( Lnth ) { // remove nodes from readyList and safe to delete nodes |
---|
56 | delete( &task( dropHead( readyList ) ) ); |
---|
57 | } |
---|
58 | |
---|
59 | // Re-purpose Seqable as Colable |
---|
60 | Stack(TaskDL) mutexStack; |
---|
61 | for ( Lnth ) { |
---|
62 | Task & task = *new(); // create task node |
---|
63 | push( mutexStack, task.clusterRef ); // insert on lists in opposite directions |
---|
64 | } |
---|
65 | for ( StackIter(TaskDL) iter = { mutexStack }; (StackIter(TaskDL) &)iter >> dl; ) { // cast required |
---|
66 | sout | &task( dl ); |
---|
67 | } |
---|
68 | sout | nl; |
---|
69 | for ( Lnth ) { // remove nodes from readyList and safe to delete nodes |
---|
70 | delete( &task( pop( mutexStack ) ) ); |
---|
71 | } |
---|
72 | } |
---|