Graph

A graph of nodes.

These nodes are comprised of two components. The first of which is their associated value of type T, then the second are their children nodes (if any). The latter are of type Graph!(T) and therefore when constructing one such node it can also be added as a child of another node, therefore allowing you to build your graph as you see fit.

Some notable functionality, other than the obvious, is the pluggable dfs method which let's you perform a recursive search on the graph, parameterized by two stratergies. The first is the so-called TouchStratergy which specifies the function to be called on the current node when dfs is called on it - this is the first thing that is done. The other parameter is the VisitationStratergy which is a predicate that will be called BEFORE entering the dfs (recursing) of a candidate child node. With this things like trees can be built or rather _derived_ from a graph. This is infact what the visitation tree type does.

Constructors

this
this(T value)

Constructs a new graph with the given value to set

this
this()

Creates a new graph without associating any value with itself

Members

Functions

appendNode
void appendNode(Graph!(T) node)

Appends another graph node to the array of children of this node's

dfs
T[] dfs(InclusionStratergy!(T) strat, TouchStratergy!(T) touch)

Performs a depth first search on the graph by firstly calling the TouchStratergy on the current node and then iterating over all of its children and only recursing on each of them if the InclusionStratergy allows it.

findByValue
Optional!(Graph!(T)) findByValue(T val)

This will attempt to find the graph node which contains the provided value.

getValue
T getValue()

Obtains the value associated with this graph node

opDollar
size_t opDollar()

Returns the number of children attached to this node

opIndex
E opIndex(size_t idx)

Returns the element of the child at the given index.

opIndex
T opIndex(size_t idx)

Returns the value of the child node at the provided index

opSlice
E[] opSlice()

Returns a slice of the requested type. This is either Graph!(T) or T itself, therefore returning an array of either

opSlice
T[] opSlice()

Returns an array of all the childrens' associated values

removeNode
bool removeNode(Graph!(T) node)

Removes a given graph node from th array of children of thie node's

setValue
void setValue(T value)

Sets the graph node's associated value

toString
string toString()

Returns a string representation of this node and its value

Properties

length
size_t length [@property getter]

Returns the number of children attached to this node

Examples

Test out usage of the Graph!(T)

Graph!(string) treeOfStrings = new Graph!(string)("Top");

Graph!(string) subtree_1 = new Graph!(string)("1");
Graph!(string) subtree_2 = new Graph!(string)("2");
Graph!(string) subtree_3 = new Graph!(string)("3");

treeOfStrings.appendNode(subtree_1);
treeOfStrings.appendNode(subtree_2);
treeOfStrings.appendNode(subtree_3);

assert(treeOfStrings.opIndex!(Graph!(string))(0) == subtree_1);
assert(treeOfStrings.opIndex!(Graph!(string))(1) == subtree_2);
assert(treeOfStrings.opIndex!(Graph!(string))(2) == subtree_3);

assert(treeOfStrings[0] == subtree_1.getValue());
assert(treeOfStrings[1] == subtree_2.getValue());
assert(treeOfStrings[2] == subtree_3.getValue());

assert(treeOfStrings.opDollar() == 3);

Optional!(Graph!(string)) match_opt = treeOfStrings.findByValue("2");
assert(match_opt.isPresent());
assert(match_opt.get() is subtree_2);

InclusionStratergy!(string) strat = toDelegate(&Always!(string));
TouchStratergy!(string) touch = toDelegate(&DebugTouch!(string));

string[] result = treeOfStrings.dfs(strat, touch);
writeln("dfs: ", result);

assert(result[0] == "1");
assert(result[1] == "2");
assert(result[2] == "3");
assert(result[3] == "Top");


auto i = treeOfStrings.opSlice!(Graph!(string))();
writeln("Siblings: ", i);
assert(i[0] == subtree_1);
assert(i[1] == subtree_2);
assert(i[2] == subtree_3);

auto p = treeOfStrings.opSlice!(string)();
writeln("Siblings (vals): ", p);
assert(p == treeOfStrings[]);


assert(treeOfStrings.removeNode(subtree_1));
assert(!treeOfStrings.removeNode(subtree_1));

See Also

VisitationTree

Meta