Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import std.stdio;
- import std.conv;
- import std.parallelism;
- class Node {
- string name;
- Node[] clients;
- Node[] masters;
- bool dirty = true;
- this(string inname){
- this.name = inname;
- }
- void doProcess(){
- writeln("Cleaning ", this.name);
- this.dirty = false;
- }
- override string toString() const {
- return name;
- }
- void addMasters(Node[] inmasters ...){
- this.masters ~= inmasters;
- foreach (master; inmasters){
- master.clients ~= this;
- }
- }
- bool canProcess() const {
- foreach (node; this.masters){
- if (node.dirty){
- return false;
- }
- }
- return true;
- }
- }
- Node[] mkLinearDag(const int depth){
- // makes a simple linked list of named nodes
- Node[] nodes;
- auto root = new Node("RootNode");
- nodes ~= root;
- foreach (i; 0 .. depth){
- nodes ~= new Node("test" ~ to!string(i));
- nodes[$-1].addMasters(nodes[$-2]);
- }
- return nodes;
- }
- void cleanNodeSimple(Node node, TaskPool pool){
- node.doProcess();
- foreach (cli; node.clients){
- if (cli.canProcess()){
- pool.put( task!cleanNodeSimple(cli, pool) );
- }
- }
- }
- void main(){
- auto dag = mkLinearDag(5);
- auto pool = taskPool();
- pool.put( task!cleanNodeSimple(dag[0], pool));
- pool.finish(true);
- writeln("\n\nOutput:");
- foreach (d;dag){
- writeln(d);
- writeln(d.dirty ? "dirty" : "clean","\n");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement