tree

package
v2.0.390 Latest Latest
Warning

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

Go to latest
Published: May 12, 2024 License: GPL-3.0 Imports: 14 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var TreeEnd = time.Date(9999, 12, 31, 23, 59, 59, 999999999, time.UTC)

TreeEnd is the maximum time value that can be represented by a Tree node.

View Source
var TreeEndStr = TreeEnd.Format(time.RFC3339)

TreeEndStr is the string representation of TreeEnd.

View Source
var TreeStart = time.Date(0, 1, 1, 0, 0, 0, 0, time.UTC)

TreeStart is the minimum time value that can be represented by a Tree node.

View Source
var TreeStartStr = TreeStart.Format(time.RFC3339)

TreeStartStr is the string representation of TreeStart.

Functions

func Dump

func Dump(tree *Node, path string)

Dump is a helper function that prints the tree structure to stdout.

func Expect

func Expect(tree *Node, pathStr, startStr, endStr string, priority float64) error

Expect is a test helper function that checks if the given tree node's interval has the expected start and end times and priority. pathStr is the path to the node in the tree, where 'l' means to go left and 'r' means to go right. An empty pathStr means to check the root node.

func Insert

func Insert(tree *Node, startStr, endStr string, priority float64) interval.Interval

Insert is a test helper function that inserts an interval into the tree and returns the interval that was inserted.

func InsertExpect

func InsertExpect(tree *Node, pathStr, startStr, endStr string, priority float64) error

InsertExpect is a test helper function that inserts an interval into the tree and checks if the tree has the expected structure.

func Match

func Match(iv interval.Interval, startStr, endStr string, priority float64) error

Match is a test helper function that checks if the given interval has the expected start and end times and priority.

func NewInterval

func NewInterval(startStr, endStr string, priority float64) interval.Interval

NewInterval is a test helper function that creates a new interval with the given start and end times and priority.

func SaveDot

func SaveDot(tree *Node)

SaveDot is a test helper function that saves the tree as a dot file

func ShowDot

func ShowDot(tree *Node, bg bool)

ShowDot displays the tree in xdot. If bg is true, then the xdot window is displayed from a background process.

func Verify

func Verify(t *testing.T, tree *Node, ckBalance bool, show bool)

Verify is a test helper function that verifies the tree. If there is an error, it shows the tree as a dot file.

Types

type Iterator

type Iterator struct {
	Tree *Node

	Fwd bool
	// contains filtered or unexported fields
}

Iterator allows iterating over the nodes in the tree in either forward or reverse order. If fwd is true, then the iterator will iterate in forward order, otherwise it will iterate in reverse order.

func NewIterator

func NewIterator(t *Node, fwd bool) *Iterator

NewIterator returns a new iterator for the given tree and direction.

func (*Iterator) Next

func (it *Iterator) Next() (path Path)

Next returns the path to the next node in the tree. If the iterator is in forward mode, then the nodes are returned in order of start time. If the iterator is in reverse mode, then the nodes are returned in reverse order of start time.

type Node

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

Node represents a node in an interval tree.

func Get

func Get(tree *Node, pathStr string) *Node

Get is a test helper function that returns the tree node at the given path in the tree. pathStr is the path to the node in the tree, where 'l' means to go left and 'r' means to go right. An empty pathStr means to return the root node.

func NewTree

func NewTree() *Node

NewTree creates and returns a new Tree node containing a free interval spanning all time.

func (*Node) AllIntervals

func (t *Node) AllIntervals() []interval.Interval

AllIntervals returns a slice of all intervals in all leaf nodes of the tree.

func (*Node) AsDot

func (t *Node) AsDot(path Path) string

AsDot returns a string representation of the tree in Graphviz DOT format

func (*Node) Busy

func (t *Node) Busy() bool

Busy returns true if the interval is busy.

func (*Node) BusyIntervals

func (t *Node) BusyIntervals() (intervals []interval.Interval)

BusyIntervals returns a slice of all busy intervals in all leaf nodes of the tree.

func (*Node) CalcHeight

func (t *Node) CalcHeight() int

CalcHeight returns the height of the tree.

func (*Node) Conflicts

func (t *Node) Conflicts(iv interval.Interval, includeFree bool) []interval.Interval

Conflicts returns a slice of intervals in leaf nodes that overlap with the given interval. If includeFree is true, then this function returns all intervals that conflict with the given interval, otherwise it returns only busy intervals.

func (*Node) Delete

func (t *Node) Delete(interval interval.Interval) (out *Node, err error)

Delete removes an interval from the tree

func (*Node) End

func (t *Node) End() time.Time

End returns the end time of the interval in the node.

func (*Node) FindExact

func (t *Node) FindExact(interval interval.Interval) (path Path)

FindExact returns the path to the node containing the exact interval that matches the given interval. If the exact interval is not found, then the path is nil.

func (*Node) FindFree

func (t *Node) FindFree(first bool, minStart, maxEnd time.Time, duration time.Duration) (free interval.Interval)

FindFree returns a free interval that has the given duration. The interval starts as early as possible if first is true, and as late as possible if first is false. The minStart and maxEnd times are inclusive. The duration is exclusive.

This function works by walking the tree in a depth-first manner, following the left child first if first is set, otherwise following the right child first.

func (*Node) FindLowerPriority

func (t *Node) FindLowerPriority(first bool, searchStart, searchEnd time.Time, duration time.Duration, priority float64) (window []*Node, out Path)

FindLowerPriority returns a contiguous set of nodes that have a lower priority than the given priority. The start time of the first node is on or before minStart, and the end time of the last node is on or after maxEnd. The nodes must total at least the given duration, and may be longer. If first is true, then the search starts at minStart and proceeds in order, otherwise the search starts at maxEnd and proceeds in reverse order. XXX this should be refactored to find and return a path to a subtree instead of a slice; the common parent of the set will always be a member of the set.

func (*Node) FirstNode

func (t *Node) FirstNode() *Node

FirstNode returns the first node in the tree.

func (*Node) FreeIntervals

func (t *Node) FreeIntervals() (intervals []interval.Interval)

FreeIntervals returns a slice of all free intervals in all leaf nodes of the tree.

func (*Node) Height

func (t *Node) Height() int

func (*Node) Insert

func (t *Node) Insert(newInterval interval.Interval) (out *Node, err error)

Insert clones the tree, adds the interval, and returns the new tree. Insertion fails if the new interval conflicts with any existing interval in the tree with a priority greater than 0. Insertion fails if the new interval is not busy. XXX return tree

func (*Node) Interval

func (t *Node) Interval() interval.Interval

Interval returns the node's interval.

func (*Node) LastNode

func (t *Node) LastNode() *Node

LastNode returns the last node in the tree.

func (*Node) Left

func (t *Node) Left() *Node

func (*Node) MaxEnd

func (t *Node) MaxEnd() time.Time

func (*Node) MaxPriority

func (t *Node) MaxPriority() float64

func (*Node) MinPriority

func (t *Node) MinPriority() float64

func (*Node) MinStart

func (t *Node) MinStart() time.Time

func (*Node) Parent

func (t *Node) Parent() *Node

func (*Node) RemoveRange

func (t *Node) RemoveRange(start, end time.Time) (out *Node, removed []interval.Interval)

RemoveRange removes all intervals that start or end within the given time range. It returns the removed intervals. It does not return intervals that are marked as free (priority 0) -- it instead adjusts free intervals to fill gaps in the tree.

func (*Node) Right

func (t *Node) Right() *Node

func (*Node) RotateLeft

func (t *Node) RotateLeft() (R *Node)

RotateLeft performs a left rotation on this node. XXX move to Tree

func (*Node) RotateRight

func (t *Node) RotateRight() (L *Node)

RotateRight performs a right rotation on this node. XXX move to Tree

func (*Node) SetInterval

func (t *Node) SetInterval(iv interval.Interval)

SetInterval sets the node's interval.

func (*Node) SetLeft

func (t *Node) SetLeft(left *Node)

SetLeft sets the left child of this node. It returns the old left child or nil if there was no old left child. If the given child node is already a child of another node, the right child of this node, or the parent of this node, then this function clears the old relationship before setting the new one. XXX move to Tree XXX return new Tree

func (*Node) SetParent

func (t *Node) SetParent(parent *Node) (out *Node)

func (*Node) SetRight

func (t *Node) SetRight(right *Node)

SetRight sets the right child of this node. It returns the old right child or nil if there was no old right child. If the given child node is already a child of another node, the left child of this node, or the parent of this node, then this function clears the old relationship before setting the new one. XXX move to Tree XXX return new Tree

func (*Node) Shuffle

func (t *Node) Shuffle(first bool, minStart, maxEnd time.Time, iv interval.Interval) (newIv interval.Interval, removed []interval.Interval, err error)

Shuffle inserts a new interval into the tree. It finds one or more lower-priority intervals using FindFreePriority, removes and returns them, adjusts the start and end times of the new interval to fit within the found free time, and inserts the new interval into the tree. Shuffle returns the newly created interval and the removed intervals if successful, otherwise it returns nil. The 'first' parameter determines whether to start searching for free time at the beginning or end of the given time range. Shuffle does not return intervals that are marked as free (priority 0) -- it instead adjusts free intervals to fill gaps in the tree.

func (*Node) Size

func (t *Node) Size() int

func (*Node) Start

func (t *Node) Start() time.Time

Start returns the start time of the interval in the node.

func (*Node) String

func (t *Node) String() string

String returns a string representation of the node.

func (*Node) Verify

func (t *Node) Verify(ckBalance bool) error

Verify checks the integrity of the tree structure. It makes sure that all nodes and intervals are correctly placed within the tree according to the interval tree properties.

func (*Node) XXXFindLowerPriority

func (t *Node) XXXFindLowerPriority(first bool, searchStart, searchEnd time.Time, duration time.Duration, priority float64) []*Node

type Path

type Path []*Node

Path is a slice of Nodes originating from the root of a Tree.

func (Path) Append

func (p Path) Append(nodes ...*Node) Path

Append returns a new Path with the given nodes appended to the end.

func (Path) Clone

func (p Path) Clone() Path

Clone returns a copy of the path.

func (Path) Equal

func (p Path) Equal(other Path) bool

Equal returns true if the paths are the same length and the nav strings are the same.

func (Path) First

func (p Path) First() *Node

First returns the first node in the path.

func (Path) Last

func (p Path) Last() *Node

Last returns the last node in the path.

func (Path) Nav

func (p Path) Nav() (nav []string)

Nav returns the navigation directions to get to a node from the root. If the path is empty, it returns an empty slice. If the tree has only a root node, the path is {T}. If the tree has a root node and a left child, the path is {T, L}, etc.

func (Path) Pop

func (p Path) Pop() Path

Pop returns a new Path with the last node removed.

func (Path) String

func (p Path) String() string

String returns a string representation of the path.

Jump to

Keyboard shortcuts

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