Documentation
¶
Overview ¶
Package dag implements a generic directed acyclic graph with cycle detection, topological sorting, and ancestor chain computation.
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ErrDag = errors.New("dag error")
ErrDag is returned when a DAG operation fails.
Functions ¶
func Condense ¶
func Condense[K comparable](chain []K) []K
Condense returns a copy of `chain` with only *adjacent* duplicates removed.
["base","base","custom"] → ["base","custom"] ["base","custom","base"] → (unchanged) ["a","a","b","b","b","c","c"] → ["a","b","c"]
The input slice is never modified.
Types ¶
type CycleError ¶
type CycleError[K comparable] struct { // The node where the cycle was detected Node K // The path that forms the cycle Path []K }
CycleError represents a cycle in the graph with the path that forms the cycle.
func (*CycleError[K]) Error ¶
func (e *CycleError[K]) Error() string
Error implements the error interface for CycleError.
type DAG ¶
type DAG[K comparable] struct { // contains filtered or unexported fields }
DAG is an immutable, validated representation of a dependency graph. The type parameter K must be comparable so it can act as a map key.
func Build ¶
func Build[K comparable]( nodes []K, parentsFn func(K) []K, ) (*DAG[K], error)
Build constructs the graph and performs **two** validations:
- Every parent returned by parentsFn(node) must itself be present in the `nodes` slice.
- The graph must be acyclic.
On success a *DAG is returned; otherwise an error explains the problem.