trees

package
v0.0.0-...-83792b2 Latest Latest
Warning

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

Go to latest
Published: Nov 1, 2024 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DefaultTreePath = []string{"#root"}

DefaultTreePath is used whenever an empty treePath is given.

Functions

func ValuesAsList

func ValuesAsList[T any](tree *Tree[T]) []T

ValuesAsList extracts the leaf values of Tree into a list.

It's generated in alphabetical order -- see OrderedLeaves to see or generate the order.

Types

type Path

type Path []string

Path is usually used as the path from the root node.

type Tree

type Tree[T any] struct {
	// Value is set for leaf nodes only.
	Value T

	// Map is set for non-leaf nodes (and nil in leaf nodes).
	Map map[string]*Tree[T]
}

Tree represent both a root of a tree, and a tree node.

It can either be a Value or a Map of its children -- but not both.

func FromValuesAndTree

func FromValuesAndTree[T1, T2 any](values []T1, tree *Tree[T2]) *Tree[T1]

FromValuesAndTree creates a Tree[T1] with the given values, but borrowing the structure from the given tree (but ignoring the tree's values).

func Map

func Map[T1, T2 any](tree1 *Tree[T1], mapFn func(Path, T1) T2) *Tree[T2]

Map converts a Tree[T1] to a Tree[T2] by calling mapFn at every element.

func New

func New[T any]() *Tree[T]

New creates a new empty tree.

func NewLeaf

func NewLeaf[T any](value T) *Tree[T]

NewLeaf creates a new leaf node with the given value.

func (*Tree[T]) Get

func (tree *Tree[T]) Get(treePath ...string) (value T, err error)

Get value in treePath. It returns an error if such a leaf node doesn't exist.

Empty values in treePath are not used.

func (*Tree[T]) IsLeaf

func (n *Tree[T]) IsLeaf() bool

func (*Tree[T]) Leaves

func (tree *Tree[T]) Leaves() iter.Seq2[Path, T]

Leaves returns an iterator that goes over all the leaf nodes of the Tree. The key is a Path, and value is T.

func (*Tree[T]) NumLeaves

func (tree *Tree[T]) NumLeaves() int

NumLeaves traverses the trees and returns the number of leaf nodes.

func (*Tree[T]) OrderedLeaves

func (tree *Tree[T]) OrderedLeaves() iter.Seq2[Path, T]

OrderedLeaves returns an iterator that goes over all the leaf nodes of the Tree in alphabetical order of the tree nodes (depth-first).

The key is a Path, and value is T.

func (*Tree[T]) Set

func (tree *Tree[T]) Set(treePath Path, value T) error

Set value in treePath, populating intermediary nodes where needed.

Empty values in treePath are not used.

It returns an error if one is trying to set the value to an existing non-leaf node: nodes can either be a leaf or a Map (non-leaf), but not both.

func (*Tree[T]) String

func (tree *Tree[T]) String() string

String implements fmt.String

Jump to

Keyboard shortcuts

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