topo

package
v12.6.0 Latest Latest
Warning

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

Go to latest
Published: Jun 7, 2026 License: GPL-3.0 Imports: 5 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrSelfReferential is returned when attempting to create an edge where a node depends on itself.
	ErrSelfReferential = errors.New(" self-referential dependencies not allowed")

	// ErrConflictingAlias is reserved for when a "provides" alias is defined more than once.
	// Note: current Graph APIs overwrite provides entries; this error is not currently returned.
	ErrConflictingAlias = errors.New(" alias already defined")

	// ErrCircular is returned when attempting to create an edge that would introduce a cycle.
	ErrCircular = errors.New(" circular dependencies not allowed")
)

Functions

This section is empty.

Types

type CheckFn

type CheckFn[T comparable, V any] func(T, V) error

CheckFn is a callback used by traversal helpers. It receives the node id plus the node's value.

type DepMap

type DepMap[T comparable] map[T]NodeSet[T]

DepMap maps a node to a set of nodes. In Graph this is used for adjacency: - dependencies: node -> its direct dependencies - dependents: node -> its direct dependents

type DependencyInfo

type DependencyInfo[T comparable] struct {
	Provider T
	alpm.Depend
}

DependencyInfo describes which node provides a given dependency/satisfier, along with the original dependency metadata (alpm.Depend).

type Graph

type Graph[T comparable, V any] struct {
	// contains filtered or unexported fields
}

Graph is a directed dependency graph.

Edge direction: - An edge is added with DependOn(child, parent), meaning "child depends on parent". - Internally, dependencies maps child -> parents (direct dependencies). - Internally, dependents maps parent -> children (direct dependents).

func New

func New[T comparable, V any]() *Graph[T, V]

New returns an empty Graph.

func (*Graph[T, V]) AddNode

func (g *Graph[T, V]) AddNode(node T)

AddNode adds node to the graph. It is safe to call multiple times.

func (*Graph[T, V]) AddProvides added in v12.6.0

func (g *Graph[T, V]) AddProvides(provides T, depInfo *alpm.Depend, node T)

AddProvides registers that node provides the given provides key.

Note: despite the "Add" name, this is a single mapping; calling it again with the same provides key overwrites the previous entry.

func (*Graph[T, V]) DependOn

func (g *Graph[T, V]) DependOn(child, parent T) error

DependOn adds an edge meaning "child depends on parent".

This ensures both nodes exist in the graph and rejects: - self edges (ErrSelfReferential) - edges that would introduce a cycle (ErrCircular)

func (*Graph[T, V]) Dependencies

func (g *Graph[T, V]) Dependencies(child T) NodeSet[T]

Dependencies returns all transitive dependencies of child (excluding child itself). The returned set is nil if child is not present in the graph.

func (*Graph[T, V]) Dependents

func (g *Graph[T, V]) Dependents(parent T) NodeSet[T]

Dependents returns all transitive dependents of parent (excluding parent itself). The returned set is nil if parent is not present in the graph.

func (*Graph[T, V]) DependsOn

func (g *Graph[T, V]) DependsOn(child, parent T) bool

DependsOn reports whether child depends (transitively) on parent.

func (*Graph[T, V]) Exists

func (g *Graph[T, V]) Exists(node T) bool

Exists reports whether node exists in the graph's node set.

func (*Graph[T, V]) ForEach

func (g *Graph[T, V]) ForEach(f CheckFn[T, V]) error

ForEach calls f for every node in the graph.

The value passed to f is the node's NodeInfo.Value if set via SetNodeInfo; otherwise it is the zero value of V.

func (*Graph[T, V]) GetNodeInfo

func (g *Graph[T, V]) GetNodeInfo(node T) *NodeInfo[V]

GetNodeInfo returns metadata/value for node, or nil if none was set.

func (*Graph[T, V]) GetProviderInfo added in v12.6.0

func (g *Graph[T, V]) GetProviderInfo(provides T) *DependencyInfo[T]

GetProviderInfo returns the dependency info for a provider.

func (*Graph[T, V]) HasDependent

func (g *Graph[T, V]) HasDependent(parent, dependent T) bool

HasDependent reports whether parent has dependent as a (transitive) dependent.

func (*Graph[T, V]) HasProvides added in v12.6.0

func (g *Graph[T, V]) HasProvides(provides T) bool

HasProvides reports whether the given provides key is registered.

func (*Graph[T, V]) ImmediateDependencies

func (g *Graph[T, V]) ImmediateDependencies(node T) NodeSet[T]

ImmediateDependencies returns the direct dependencies of node. The returned set is nil if node has no direct dependencies (or is not present).

func (*Graph[T, V]) ImmediateDependents added in v12.6.0

func (g *Graph[T, V]) ImmediateDependents(node T) NodeSet[T]

ImmediateDependents returns the direct dependents of node. The returned set is nil if node has no direct dependents (or is not present).

func (*Graph[T, V]) Len

func (g *Graph[T, V]) Len() int

Len returns the number of nodes currently present in the graph.

func (*Graph[T, V]) Prune

func (g *Graph[T, V]) Prune(node T) []T

Prune removes the node, its dependencies if there are no other dependents and its dependents

It returns the list of nodes that were removed (including node). The returned order is based on recursive traversal and is not guaranteed to be stable.

func (*Graph[T, V]) SetNodeInfo

func (g *Graph[T, V]) SetNodeInfo(node T, nodeInfo *NodeInfo[V])

SetNodeInfo sets metadata and value for node. Node does not need to already exist in the graph.

func (*Graph[T, V]) String

func (g *Graph[T, V]) String() string

String renders the graph in GraphViz DOT format.

Nodes are emitted as `"node"` entries with optional color metadata from NodeInfo. Edges are emitted in the direction: dependent -> dependency (child -> parent).

func (*Graph[T, V]) TopoSortedLayers added in v12.6.0

func (g *Graph[T, V]) TopoSortedLayers(checkFn CheckFn[T, V]) []map[T]V

TopoSortedLayers returns a slice of all of the graph nodes in topological sort order with their node info.

The returned slice is layered: each element is a "layer" of nodes that have no remaining dependencies at that stage of the process.

Practical meaning with this graph's edge direction (DependOn(child, parent)): - Earlier layers contain nodes with fewer/zero dependencies (i.e. dependencies-first order). - A node appears only after all of its dependencies have appeared in earlier layers.

If checkFn is non-nil, it is called once per node when it is emitted in a layer. Returning an error causes TopoSortedLayers to return nil.

type NodeInfo

type NodeInfo[V any] struct {
	Color      string
	Background string
	Value      V
}

NodeInfo carries optional rendering metadata (Color/Background) plus the node's value.

type NodeSet

type NodeSet[T comparable] map[T]bool

NodeSet is a set of nodes represented as a map for O(1) membership checks. The boolean value is not meaningful; presence of the key indicates membership.

func (NodeSet[T]) Slice

func (n NodeSet[T]) Slice() []T

Slice returns the set contents as a slice in unspecified order.

type ProvidesMap

type ProvidesMap[T comparable] map[T]*DependencyInfo[T]

ProvidesMap maps a "provides" key (an alias/satisfier name) to information about the node that provides it.

Jump to

Keyboard shortcuts

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