topsort

package module
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Apr 25, 2021 License: MIT Imports: 1 Imported by: 9

README

topsort

This package provides a handy interface to do a topological sort of some data in a pretty lightweight way.

Example

package main

import (
	"fmt"

	"pault.ag/go/topsort"
)

func main() {
	network := topsort.NewNetwork()

	network.AddNode("watch tv while eating", nil)
	network.AddNode("make dinner", nil)
	network.AddNode("clean my kitchen", nil)

	/* Right, so the order of operations is next */

	network.AddEdge("clean my kitchen", "make dinner")
	// I need to clean the kitchen before I make dinner.

	network.AddEdge("make dinner", "watch tv while eating")
	// Need to make dinner before I can eat it.

	nodes, err := network.Sort()
	if err != nil {
		panic(err)
	}

	for _, step := range nodes {
		fmt.Printf(" -> %s\n", step.Name)
	}
	/* Output is:
	 *
	 * -> clean my kitchen
	 * -> make dinner
	 * -> watch tv while eating
	 */
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Network

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

func NewNetwork

func NewNetwork() *Network

func (*Network) AddEdge

func (tn *Network) AddEdge(from string, to string) error

func (*Network) AddEdgeAndNodes added in v0.1.1

func (tn *Network) AddEdgeAndNodes(from string, to string) error

func (*Network) AddNode

func (tn *Network) AddNode(name string, value interface{}) *Node

func (*Network) Get

func (tn *Network) Get(name string) *Node

func (*Network) Sort

func (tn *Network) Sort() ([]*Node, error)

type Node

type Node struct {
	Name          string
	Value         interface{}
	OutboundEdges []*Node
	InboundEdges  []*Node
	Marked        bool
}

func (*Node) IsCanidate

func (node *Node) IsCanidate() bool

Jump to

Keyboard shortcuts

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