graph

package
v0.0.0-...-6023aa6 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 19, 2016 License: MPL-2.0 Imports: 12 Imported by: 3

Documentation

Overview

Package graph contains the graph data structure to represent dependency graphs. Nodes are fmt.Stringers, and are uniquely identified by their String() method. Edges don't have a public interface, but the various graph.*Edge methods work with them.

Contains utility functions for printing graphs that work with every implementation of graph.Interface.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func PrintDepTree

func PrintDepTree(graph Interface, start Node)

PrintDepTree pretty prints the dependency tree of the specified startNode to stdout.

func PrintFullDepTree

func PrintFullDepTree(graph Interface)

PrintFullDepTree prints the dependency tree of the whole graph to stdout.

func WriteDot

func WriteDot(graph Interface, writer io.Writer)

WriteDot writes the given graph to the given io.Writer in dot language syntax.

func WriteGraph

func WriteGraph(graph Interface, writer io.Writer, syntax *syntax.Syntax)

WriteGraph writes a machine-readable version of the graph to writer, matching the given syntax.

Types

type Graph

type Graph struct {
	// contains filtered or unexported fields
}

Graph represents the dependency graph in memory. Reades and writes at the same time are not concurrency-safe and therefore need to be synchronized. Checkout graph.Synced for a thread-safe version of Graph. It satisfies the graph.Interface and fmt.Stringer interfaces. The zero value is an uninitialized graph, use graph.New() to get an initialized Graph.

This implementation prefers fast node lookups and edge removals over dependency lookups.

func New

func New(capacities ...uint) *Graph

New returns a freshly initialized, ready-to-use Graph. It takes an optional first parameter specifying the capacity of nodes that the Graph will contain, and an optional second uint specifying the number of edges it will hold.

func (*Graph) AddEdge

func (g *Graph) AddEdge(source, target string)

AddEdge adds an edge from the source Node to the target Node to the graph.

This operation takes constant time, O(1).

func (*Graph) AddEdgeAndNodes

func (g *Graph) AddEdgeAndNodes(source, target Node)

AddEdgeAndNodes adds a new edge from source to target to the Graph. The nodes are added to the Graph if they were not present.

This operation takes constant time, O(1).

func (*Graph) AddNode

func (g *Graph) AddNode(node Node, targetNames ...string)

AddNode adds the node with edges to the nodes with the given target names to this graph.

This operation takes constant time, O(1) (but proportional to the number of targetsNames).

func (*Graph) AddNodes

func (g *Graph) AddNodes(nodes ...Node)

AddNodes adds the given nodes to this graph.

This operation takes constant time, O(1) (but proportional to the number of new nodes).

func (*Graph) Copy

func (g *Graph) Copy() Interface

Copy returns a deep copy of the Graph.

This operation takes time proportional to the sum of the number of nodes and the number of edges in g, O(n+e).

func (*Graph) FromScanner

func (g *Graph) FromScanner(scanner *bufio.Scanner, syntaxes ...*syntax.Syntax) (*Graph, error)

FromScanner reads data from the given scanner, building up the dependency tree.

func (*Graph) GetDependants

func (g *Graph) GetDependants(node string) []Node

GetDependants returns a slice containing all dependants of the Node with the given string. The Graph contains an edge from each item in dependants to the Node.

This operation takes time proportional to the number of nodes in g, O(n).

func (*Graph) GetDependencies

func (g *Graph) GetDependencies(node string) []Node

GetDependencies returns a slice containing all dependencies of the Node with the given string. The Graph contains an edge from the Node to each item in the returned dependencies.

This operation takes time proportional to the number of nodes in g, O(n).

func (*Graph) GetDependencyGraph

func (g *Graph) GetDependencyGraph(nodename string) *Graph

GetDependencyGraph builds the dependency graph for the node. Returns nil if no node with the given name was found in g.

This operation takes time proportional to product of the number of nodes and the number of edges in g, O(n*e).

func (*Graph) GetNode

func (g *Graph) GetNode(name string) Node

GetNode returns the Node with the specified name, or nil if the name couldn't be found in g.

This operation takes constant time, O(1).

func (*Graph) GetNodes

func (g *Graph) GetNodes() []Node

GetNodes returns all Node objects saved in the Graph as a slice. If it is empty, an empty slice is returned.

This operation takes time proportional to the number of nodes in g, O(n).

func (*Graph) HasEdge

func (g *Graph) HasEdge(source, target string) bool

HasEdge returns true if g has an edge from the source Node to the target Node, false otherwise.

This operation takes constant time, O(1).

func (*Graph) RemoveEdge

func (g *Graph) RemoveEdge(source, target string) bool

RemoveEdge removes the edge from the source Node to the target Node. Returns false if the graph didn't have an edge from source to target, true otherwise.

This operation takes constant time, O(1).

func (*Graph) RemoveNode

func (g *Graph) RemoveNode(name string) bool

RemoveNode removes the Node with the given name including its edges from the graph. Returns false if the graph didn't have a matching Node, true otherwise.

This operation takes time proportional to the number of nodes in g, O(n).

func (*Graph) String

func (g *Graph) String() string

String returns a simple string representation consisting of all edges.

This operation takes time proportional to the number of edges in g, O(e).

type Interface

type Interface interface {
	// AddNode adds the node with edges to the nodes with the given target names to this graph.
	AddNode(node Node, targetNames ...string)
	// GetNode returns the Node with the specified name, or nil if the name couldn't be found in the graph.
	GetNode(name string) Node
	// GetNodes returns all Node objects saved in the graph as a slice. If the graph is empty, an empty slice is returned.
	GetNodes() []Node
	// RemoveNode removes the Node with the given name including its edges from the graph.
	// Returns false if the graph didn't have a matching Node, true otherwise.
	RemoveNode(name string) bool

	// AddEdge adds an edge from the source Node to the target Node to the graph.
	AddEdge(source, target string)
	// HasEdge returns true if the graph has an edge from the source Node to the target Node, false otherwise.
	HasEdge(source, target string) bool
	// RemoveEdge removes the edge from the source Node to the target Node.
	// Returns false if the graph didn't have an edge from source to target, true otherwise.
	RemoveEdge(source, target string) bool
	// GetDependencies returns a slice containing all dependencies of the Node with the given string.
	GetDependencies(node string) []Node
	// GetDependants returns a slice containing all dependants of the Node with the given string.
	GetDependants(node string) []Node

	// Copy returns a deep copy of the graph.
	Copy() Interface
}

Interface defines a basic graph-like data structure. The stored data is accessed by the String() method, so every Node needs to implement fmt.Stringer.

type Node

type Node fmt.Stringer

Node represents a single data point stored in a Graph. Its String() method should return a unique string identifier. The String() method call should be fast (ideally inline), as it will be called often!

type Synced

type Synced struct {
	Graph
	sync.RWMutex
}

Synced embeds a graph.Graph and synchronizes read and write access with an embedded sync.RWMutex, making it concurrency-safe. It satisfies the graph.Interface and fmt.Stringer interfaces. The zero value is an uninitialized graph, use graph.NewSynced() to get an initialized graph.Synced.

This implementation prefers fast node lookups and edge removals over dependency lookups.

func NewSynced

func NewSynced(capacities ...uint) *Synced

NewSynced returns a freshly initialized, ready-to-use graph.Synced It takes an optional first parameter specifying the capacity of nodes that the Graph will contain, and an optional second uint specifying the number of edges it will hold.

func (*Synced) AddEdge

func (g *Synced) AddEdge(source, target string)

AddEdge adds an edge from the source Node to the target Node to the graph.

This operation takes constant time, O(1).

func (*Synced) AddEdgeAndNodes

func (g *Synced) AddEdgeAndNodes(source, target Node)

AddEdgeAndNodes adds a new edge from source to target to the Graph. The nodes are added to the Graph if they were not present.

This operation takes constant time, O(1).

func (*Synced) AddNode

func (g *Synced) AddNode(node Node, targetNames ...string)

AddNode adds the node with edges to the nodes with the given target names to this graph.

This operation takes constant time, O(1) (but proportional to the number of targetsNames).

func (*Synced) AddNodes

func (g *Synced) AddNodes(nodes ...Node)

AddNodes adds the given nodes to this graph.

This operation takes constant time, O(1) (but proportional to the number of new nodes).

func (*Synced) Copy

func (g *Synced) Copy() Interface

Copy returns a deep copy of the graph.Synced.

This operation takes time proportional to the sum of the number of nodes and the number of edges in g, O(n+e).

func (*Synced) FromScanner

func (g *Synced) FromScanner(scanner *bufio.Scanner, syntaxes ...*syntax.Syntax) (*Synced, error)

FromScanner reads data from the given scanner, building up the dependency tree. This uses multiple workers to concurrently write the read edges.

func (*Synced) GetDependants

func (g *Synced) GetDependants(node string) []Node

GetDependants returns a slice containing all dependants of the Node with the given string. The graph.Synced contains an edge from each item in dependants to the Node.

This operation takes time proportional to the number of nodes in g, O(n).

func (*Synced) GetDependencies

func (g *Synced) GetDependencies(node string) []Node

GetDependencies returns a slice containing all dependencies of the Node with the given string. The graph.Synced contains an edge from the Node to each item in the returned dependencies.

This operation takes time proportional to the number of nodes in g, O(n).

func (*Synced) GetDependencyGraph

func (g *Synced) GetDependencyGraph(nodename string) *Graph

GetDependencyGraph builds the dependency graph for the node. Returns nil if no node with the given name was found in g. If read/write access to the dependency graph shall be thread-safe as well you need to embed it in a Synced!

This operation takes time proportional to the product of the number of nodes and the number of edges in g, O(n*e).

func (*Synced) GetNode

func (g *Synced) GetNode(name string) Node

GetNode returns the Node with the specified name, or nil if the name couldn't be found in g.

This operation takes constant time, O(1).

func (*Synced) GetNodes

func (g *Synced) GetNodes() []Node

GetNodes returns all Node objects saved in the Graph as a slice. If it is empty, an empty slice is returned.

This operation takes time proportional to the number of nodes in g, O(n).

func (*Synced) HasEdge

func (g *Synced) HasEdge(source, target string) bool

HasEdge returns true if g has an edge from the source Node to the target Node, false otherwise.

This operation takes constant time, O(1).

func (*Synced) RemoveEdge

func (g *Synced) RemoveEdge(source, target string) bool

RemoveEdge removes the edge from the source Node to the target Node. Returns false if the graph didn't have an edge from source to target, true otherwise.

This operation takes constant time, O(1).

func (*Synced) RemoveNode

func (g *Synced) RemoveNode(name string) bool

RemoveNode removes the Node with the given name including its edges from the graph. Returns false if the graph didn't have a matching Node, true otherwise.

This operation takes time proportional to the number of nodes in g, O(n).

func (*Synced) String

func (g *Synced) String() string

String returns a simple string representation consisting of all edges.

This operation takes time proportional to the number of edges in g, O(e).

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL