octree

package
v0.1.494 Latest Latest
Warning

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

Go to latest
Published: Feb 2, 2022 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Overview

Package ctree implements a Tree container whose methods are all thread-safe allowing concurrent access for multiple goroutines. This container was designed to support concurrent reads using sync.RWLock and are optimized for situations where reads and updates to previously Added values are dominant. The acquisition of write locks is avoided wherever possible.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Leaf

type Leaf Tree

Leaf is a Tree node that represents a leaf.

Leaf is safe for use from multiple goroutines and will return the latest value. This means that if value in this leaf was updated after Leaf was retrieved, Value will return the updated content, not the original one. This also means that multiple calls to Value may return different results.

func DetachedLeaf

func DetachedLeaf(val interface{}) *Leaf

DetachedLeaf returns a Leaf that's not attached to any tree.

func (*Leaf) Update

func (l *Leaf) Update(val interface{})

Update sets the value of this Leaf to val.

func (*Leaf) Value

func (l *Leaf) Value() interface{}

Value returns the latest value stored in this leaf. Value is safe to call on nil Leaf.

type Tree

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

Tree is a thread-safe container.

func (*Tree) Add

func (t *Tree) Add(path []string, value interface{}) error

Add adds value to the Tree at the specified path and returns true on success.

func (*Tree) Children

func (t *Tree) Children() map[string]*Tree

Children returns a mapping of child nodes if current node represents a branch, nil otherwise.

func (*Tree) Delete

func (t *Tree) Delete(subpath []string) [][]string

Delete removes all leaves at or below subpath as well as any ancestors with no children, returning the list of all leaves removed. Deletes prevent all other concurrent access.

func (*Tree) DeleteConditional

func (t *Tree) DeleteConditional(subpath []string, condition func(interface{}) bool) [][]string

DeleteConditional removes all leaves at or below subpath as well as any ancestors with no children for those leaves which the given conditional function returns true, returning the list of all leaves removed. DeleteConditional prevents all other concurrent access.

func (*Tree) Get

func (t *Tree) Get(path []string) *Tree

Get returns the Tree node if path points to it, nil otherwise. All nodes in path must be fully specified with no globbing (*).

func (*Tree) GetLeaf

func (t *Tree) GetLeaf(path []string) *Leaf

GetLeaf returns the leaf node if path points to a leaf in t, nil otherwise. All nodes in path must be fully specified with no globbing (*).

func (*Tree) GetLeafValue

func (t *Tree) GetLeafValue(path []string) interface{}

GetLeafValue returns the leaf value if path points to a leaf in t, nil otherwise. All nodes in path must be fully specified with no globbing (*).

func (*Tree) IsBranch

func (t *Tree) IsBranch() bool

IsBranch returns whether the Tree node represents a branch. Returns false if called on a nil node.

func (*Tree) Query

func (t *Tree) Query(path []string, f VisitFunc) error

Query calls f for all leaves that match a given query where zero or more nodes in path may be specified by globs (*). Results and their full paths are passed to f as they are found in the Tree. No ordering of paths is guaranteed.

func (*Tree) String

func (t *Tree) String() string

String implements the string interface for Tree returning a stable output sorting keys at each level.

func (*Tree) Value

func (t *Tree) Value() interface{}

Value returns the latest value stored in node t if it represents a leaf, nil otherwise. Value is safe to call on nil Tree.

func (*Tree) Walk

func (t *Tree) Walk(f VisitFunc) error

Walk calls f for all leaves.

func (*Tree) WalkSorted

func (t *Tree) WalkSorted(f VisitFunc) error

WalkSorted calls f for all leaves in string sorted order.

type VisitFunc

type VisitFunc func(path []string, l *Leaf, val interface{}) error

VisitFunc is a callback func triggered on leaf values by Query and Walk.

The provided Leaf is the leaf node of the tree, val is the value stored inside of it. l can be retained after VisitFunc returns and l.Value() can be called to get the latest value for that leaf.

Note that l.Value can *not* be called inside VisitFunc, because the node is already locked by Query/Walk.

If the function returns an error, the Walk or Query will terminate early and return the supplied error.

Jump to

Keyboard shortcuts

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