treeset

package
v1.0.26 Latest Latest
Warning

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

Go to latest
Published: Sep 20, 2024 License: MIT Imports: 8 Imported by: 1

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type TreeSet

type TreeSet[T any] struct {
	// contains filtered or unexported fields
}

TreeSet implements an tree set that keeps the compare order of keys. The zero value for TreeSet is an empty set ready to use.

https://en.wikipedia.org/wiki/Red%E2%80%93black_tree

To iterate over a tree set (where ts is a *TreeSet):

for it := ts.Iterator(); it.Next(); {
	// do something with it.Value()
}

func NewTreeSet

func NewTreeSet[T any](compare cog.Compare[T], vs ...T) *TreeSet[T]

NewTreeSet creates a new TreeSet. Example: cog.NewTreeSet(cog.CompareString, "v1", "v2")

Example
set := NewTreeSet(cmp.CompareInt)
set.Add(1)              // 1
set.Adds(2, 2, 3, 4, 5) // 1, 2, 3, 4, 5 (in order, duplicates ignored)
set.Remove(4)           // 1, 2, 3, 5 (in order)
set.Removes(2, 3)       // 1, 5 (in order)
set.Contain(1)          // true
set.Contains(1, 5)      // true
set.Contains(1, 6)      // false
_ = set.Values()        // []int{1,5} (in order)
set.Clear()             // empty
set.IsEmpty()           // true
set.Len()               // 0

func (*TreeSet[T]) Add

func (ts *TreeSet[T]) Add(v T)

Add add item v.

func (*TreeSet[T]) AddCol

func (ts *TreeSet[T]) AddCol(ac cog.Collection[T])

AddCol adds all items of another collection

func (*TreeSet[T]) Adds

func (ts *TreeSet[T]) Adds(vs ...T)

Adds adds all items of vs.

func (*TreeSet[T]) Ceiling

func (ts *TreeSet[T]) Ceiling(v T) *T

Ceiling finds ceiling node of the input key, return the ceiling node's value or nil if no ceiling is found.

Ceiling node is defined as the smallest node that is larger than or equal to the given node. A ceiling node may not be found, either because the tree is empty, or because all nodes in the tree are smaller than the given node.

key should adhere to the comparator's type assertion, otherwise method panics.

func (*TreeSet[T]) Clear

func (ts *TreeSet[T]) Clear()

Clear clears the set

func (*TreeSet[T]) Contain

func (ts *TreeSet[T]) Contain(v T) bool

Contain Test to see if the list contains the value v

func (*TreeSet[T]) ContainCol

func (ts *TreeSet[T]) ContainCol(ac cog.Collection[T]) bool

ContainCol Test to see if the collection contains all items of another collection

func (*TreeSet[T]) ContainIter

func (ts *TreeSet[T]) ContainIter(it cog.Iterator[T]) bool

ContainIter Test to see if the collection contains all items of iterator 'it'

func (*TreeSet[T]) Contains

func (ts *TreeSet[T]) Contains(vs ...T) bool

Contains Test to see if the collection contains all items of vs

func (*TreeSet[T]) Each

func (ts *TreeSet[T]) Each(f func(int, T) bool)

Each call f for each item in the set

func (*TreeSet[T]) Floor

func (ts *TreeSet[T]) Floor(v T) *T

Floor Finds floor node of the input key, return the floor node's value or nil if no floor is found.

Floor node is defined as the largest node that is smaller than or equal to the given node. A floor node may not be found, either because the tree is empty, or because all nodes in the tree are larger than the given node.

key should adhere to the comparator's type assertion, otherwise method panics.

func (*TreeSet[T]) Graph

func (ts *TreeSet[T]) Graph() string

Graph return the set's graph

func (*TreeSet[T]) Head

func (ts *TreeSet[T]) Head() (v T)

Head returns the first item of set ts or nil if the set is empty.

func (*TreeSet[T]) IsEmpty

func (ts *TreeSet[T]) IsEmpty() bool

IsEmpty returns true if the set has no items

func (*TreeSet[T]) Iterator

func (ts *TreeSet[T]) Iterator() cog.Iterator[T]

Iterator returns a iterator for the set

func (*TreeSet[T]) Len

func (ts *TreeSet[T]) Len() int

Len returns the length of the tree set.

func (*TreeSet[T]) MarshalJSON

func (ts *TreeSet[T]) MarshalJSON() ([]byte, error)

MarshalJSON implements type json.Marshaler interface, so can be called in json.Marshal(ts)

func (*TreeSet[T]) PeekHead

func (ts *TreeSet[T]) PeekHead() (v T, ok bool)

PeekHead get the first item of set.

func (*TreeSet[T]) PeekTail

func (ts *TreeSet[T]) PeekTail() (v T, ok bool)

PeekTail get the last item of set.

func (*TreeSet[T]) PollHead

func (ts *TreeSet[T]) PollHead() (v T, ok bool)

PollHead remove the first item of set.

func (*TreeSet[T]) PollTail

func (ts *TreeSet[T]) PollTail() (v T, ok bool)

PollTail remove the last item of set.

func (*TreeSet[T]) Remove

func (ts *TreeSet[T]) Remove(v T)

Remove remove all items with associated value v of vs

func (*TreeSet[T]) RemoveCol

func (ts *TreeSet[T]) RemoveCol(ac cog.Collection[T])

RemoveCol remove all of this collection's elements that are also contained in the specified collection

func (*TreeSet[T]) RemoveFunc

func (ts *TreeSet[T]) RemoveFunc(f func(T) bool)

RemoveFunc remove all items that function f returns true

func (*TreeSet[T]) RemoveIter

func (ts *TreeSet[T]) RemoveIter(it cog.Iterator[T])

RemoveIter remove all items in the iterator it

func (*TreeSet[T]) Removes

func (ts *TreeSet[T]) Removes(vs ...T)

Removes remove all items in the array vs

func (*TreeSet[T]) RetainCol

func (ts *TreeSet[T]) RetainCol(ac cog.Collection[T])

RetainCol Retains only the elements in this collection that are contained in the specified collection.

func (*TreeSet[T]) RetainFunc

func (ts *TreeSet[T]) RetainFunc(f func(T) bool)

RetainFunc Retains all items that function f returns true

func (*TreeSet[T]) Retains

func (ts *TreeSet[T]) Retains(vs ...T)

Retains Retains only the elements in this collection that are contained in the argument array vs.

func (*TreeSet[T]) ReverseEach

func (ts *TreeSet[T]) ReverseEach(f func(int, T) bool)

ReverseEach call f for each item in the set with reverse order

func (*TreeSet[T]) String

func (ts *TreeSet[T]) String() string

String print set to string

func (*TreeSet[T]) Tail

func (ts *TreeSet[T]) Tail() (v T)

Tail returns the last item of set ts or nil if the set is empty.

func (*TreeSet[T]) UnmarshalJSON

func (ts *TreeSet[T]) UnmarshalJSON(data []byte) error

UnmarshalJSON implements type json.Unmarshaler interface, so can be called in json.Unmarshal(data, ts)

func (*TreeSet[T]) Values

func (ts *TreeSet[T]) Values() []T

Values returns the value slice

Jump to

Keyboard shortcuts

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