From 6f7b10086686a59fbe137dbff644fe3280d70bd2 Mon Sep 17 00:00:00 2001 From: Michael Baczynski Date: Wed, 25 May 2016 10:10:47 +0200 Subject: [PATCH] Fix #34 --- src/de/polygonal/ds/Graph.hx | 23 ++++++++++++----------- src/de/polygonal/ds/GraphArc.hx | 10 +++++----- src/de/polygonal/ds/GraphNode.hx | 8 ++++---- 3 files changed, 21 insertions(+), 20 deletions(-) diff --git a/src/de/polygonal/ds/Graph.hx b/src/de/polygonal/ds/Graph.hx index afea85f..5a96be6 100644 --- a/src/de/polygonal/ds/Graph.hx +++ b/src/de/polygonal/ds/Graph.hx @@ -186,11 +186,12 @@ class Graph implements Collection } /** - Creates an uni-directional link between two nodes with a weight of `cost` (default is 1.0). + Creates an uni-directional link between two nodes. + This creates an arc pointing from `source` to `target`. - This creates an arc pointing from the `source` node to the `target` node. + The newly created arc can be accessed via `source.arcList`. **/ - public function addSingleArc(source:GraphNode, target:GraphNode, cost:Float = 1.) + public function addSingleArc(source:GraphNode, target:GraphNode) { assert(source != null, "source is null"); assert(target != null, "target is null"); @@ -207,7 +208,7 @@ class Graph implements Collection { if (walker == target) { - sourceNode.addArc(walker, cost); + sourceNode.addArc(walker); break; } walker = walker.next; @@ -219,11 +220,12 @@ class Graph implements Collection } /** - Creates a bi-directional link between two nodes with a weight of `cost` (default is 1.0). + Creates an uni-directional link between two nodes. + This creates two arcs; an arc pointing from `source` to `target` and vv. - This creates two arcs - an arc that points from the `source` node to the `target` node and vice versa. + The newly created arcs can be accessed via `source.arcList` (pointing to `target`) and `target.arcList` (pointing to `source`). **/ - public function addMutualArc(source:GraphNode, target:GraphNode, cost:Float = 1.) + public function addMutualArc(source:GraphNode, target:GraphNode) { assert(source != null, "source is null"); assert(target != null, "target is null"); @@ -242,11 +244,10 @@ class Graph implements Collection { if (walker == target) { - sourceNode.addArc(walker, cost); - walker.addArc(sourceNode, cost); + sourceNode.addArc(walker); + walker.addArc(sourceNode); break; } - walker = walker.next; } break; @@ -1307,7 +1308,7 @@ class Graph implements Collection a = n.arcList; while (a != null) { - m.addArc(a.node, a.cost); + m.addArc(a.node, a.userData); a = a.next; } n = n.next; diff --git a/src/de/polygonal/ds/GraphArc.hx b/src/de/polygonal/ds/GraphArc.hx index e7b3008..3441905 100644 --- a/src/de/polygonal/ds/GraphArc.hx +++ b/src/de/polygonal/ds/GraphArc.hx @@ -43,9 +43,9 @@ class GraphArc implements Hashable public var node:GraphNode; /** - The weight (or cost) of this arc. + Custom data associated with this arc. **/ - public var cost:Float; + public var userData:Float; /** A reference to the next graph arc in the list. @@ -68,12 +68,12 @@ class GraphArc implements Hashable inline function get_val():T return node.val; /** - Creates a graph arc pointing to `node` with a weight of `cost` . + Creates a graph arc pointing to `node`. **/ - public function new(node:GraphNode, cost:Float) + public function new(node:GraphNode, ?userData:Dynamic) { this.node = node; - this.cost = cost; + this.userData = userData; next = null; prev = null; } diff --git a/src/de/polygonal/ds/GraphNode.hx b/src/de/polygonal/ds/GraphNode.hx index 5368599..8a91885 100644 --- a/src/de/polygonal/ds/GraphNode.hx +++ b/src/de/polygonal/ds/GraphNode.hx @@ -168,18 +168,18 @@ class GraphNode implements Hashable /** Adds an arc pointing from this node to the specified `target` node. - @param cost defines how "hard" it is to get from one node to the other. Default is 1.0. + @param userData custom data stored in the arc (optional). For example `userData` could store a number defining how "hard" it is to get from one node to another. **/ - public function addArc(target:GraphNode, cost:Float = 1) + public function addArc(target:GraphNode, userData:Dynamic = 1) { assert(target != this, "target is null"); assert(getArc(target) == null, "arc to target already exists"); var arc = if (mGraph.borrowArc != null) - mGraph.borrowArc(target, cost); + mGraph.borrowArc(target, userData); else - new GraphArc(target, cost); + new GraphArc(target, userData); arc.next = arcList; if (arcList != null) arcList.prev = arc; arcList = arc;