pathtree
package pathtree
import "go.abhg.dev/doc2go/internal/pathtree"
Package pathtree provides a data structure that stores values organized under a tree-like hierarchy where values from higher levels cascade down to lower levels unless the lower levels define their own values.
For example, if 'foo/bar' defines a value X, foo/bar, foo/bar/baz, and foo/bar/qux and all their descendants inherit this value.
t.Set("foo/bar", X) t.Get("foo/bar") // == X t.Get("foo/bar/baz") // == X t.Get("foo/bar/qux") // == X
However, if 'foo/bar/baz' defines a different value Y, it and its descendants use that value.
t.Set("foo/bar", X) t.Set("foo/bar/baz", Y) t.Get("foo/bar") // == X t.Get("foo/bar/qux") // == X t.Get("foo/bar/baz") // == Y t.Get("foo/bar/baz/qux") // == Y
Index
Types
type Root
type Root[T any] struct { // contains filtered or unexported fields }
Root is the starting point of the path tree. The zero-value of Root is an empty tree.
func (*Root[T]) Lookup
func (r *Root[T]) Lookup(p string) (v T, ok bool)
Lookup retrieves the value for the given path, inheriting values specified for parents of this path if it didn't get its own value.
Lookup reports true if a value was found--even if it was inherited.
func (*Root[T]) Set
func (r *Root[T]) Set(p string, v T)
Set adds a value to the tree under the given path. All descendants of this path that do not have an explicit value will inherit this value. If this path already had a value specified, it will be overwritten.
func (*Root[T]) Snapshot
func (r *Root[T]) Snapshot() []Snapshot[T]
Snapshot builds and returns a snapshot of all values in this path tree.
The returned slice holds nodes closest to root.
type Snapshot
type Snapshot[T any] struct { // Value in the tree, // or nil if this node doesn't have an explicit value. Value *T // Path to this node. Path string // Children of this node. Children []Snapshot[T] }
Snapshot is a snapshot of values added to the tree presented in a hierarchical manner.