Skip to content

Commit

Permalink
Fix #34
Browse files Browse the repository at this point in the history
  • Loading branch information
polygonal committed May 25, 2016
1 parent 3d59b16 commit 6f7b100
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 20 deletions.
23 changes: 12 additions & 11 deletions src/de/polygonal/ds/Graph.hx
Expand Up @@ -186,11 +186,12 @@ class Graph<T> implements Collection<T>
}

/**
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<T>, target:GraphNode<T>, cost:Float = 1.)
public function addSingleArc(source:GraphNode<T>, target:GraphNode<T>)
{
assert(source != null, "source is null");
assert(target != null, "target is null");
Expand All @@ -207,7 +208,7 @@ class Graph<T> implements Collection<T>
{
if (walker == target)
{
sourceNode.addArc(walker, cost);
sourceNode.addArc(walker);
break;
}
walker = walker.next;
Expand All @@ -219,11 +220,12 @@ class Graph<T> implements Collection<T>
}

/**
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<T>, target:GraphNode<T>, cost:Float = 1.)
public function addMutualArc(source:GraphNode<T>, target:GraphNode<T>)
{
assert(source != null, "source is null");
assert(target != null, "target is null");
Expand All @@ -242,11 +244,10 @@ class Graph<T> implements Collection<T>
{
if (walker == target)
{
sourceNode.addArc(walker, cost);
walker.addArc(sourceNode, cost);
sourceNode.addArc(walker);
walker.addArc(sourceNode);
break;
}

walker = walker.next;
}
break;
Expand Down Expand Up @@ -1307,7 +1308,7 @@ class Graph<T> implements Collection<T>
a = n.arcList;
while (a != null)
{
m.addArc(a.node, a.cost);
m.addArc(a.node, a.userData);
a = a.next;
}
n = n.next;
Expand Down
10 changes: 5 additions & 5 deletions src/de/polygonal/ds/GraphArc.hx
Expand Up @@ -43,9 +43,9 @@ class GraphArc<T> implements Hashable
public var node:GraphNode<T>;

/**
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.
Expand All @@ -68,12 +68,12 @@ class GraphArc<T> 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<T>, cost:Float)
public function new(node:GraphNode<T>, ?userData:Dynamic)
{
this.node = node;
this.cost = cost;
this.userData = userData;
next = null;
prev = null;
}
Expand Down
8 changes: 4 additions & 4 deletions src/de/polygonal/ds/GraphNode.hx
Expand Up @@ -168,18 +168,18 @@ class GraphNode<T> 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<T>, cost:Float = 1)
public function addArc(target:GraphNode<T>, 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<T>(target, cost);
new GraphArc<T>(target, userData);
arc.next = arcList;
if (arcList != null) arcList.prev = arc;
arcList = arc;
Expand Down

0 comments on commit 6f7b100

Please sign in to comment.