treeset

package
v1.2.2 Latest Latest
Warning

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

Go to latest
Published: Aug 20, 2025 License: MIT Imports: 9 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.Compare[int])
set.Add(1)                // 1
set.AddAll(2, 2, 3, 4, 5) // 1, 2, 3, 4, 5 (in order, duplicates ignored)
set.Remove(4)             // 1, 2, 3, 5 (in order)
set.RemoveAll(2, 3)       // 1, 5 (in order)
set.Contains(1)           // true
set.ContainsAll(1, 5)     // true
set.ContainsAll(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]) AddAll added in v1.0.27

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

AddAll adds all items of vs.

func (*TreeSet[T]) AddCol

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

AddCol adds all items of another collection

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]) Contains

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

Contains Test to see if the list contains the value v

func (*TreeSet[T]) ContainsAll added in v1.0.27

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

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

func (*TreeSet[T]) ContainsCol added in v1.0.27

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

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

func (*TreeSet[T]) ContainsIter added in v1.0.27

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

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

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

func (*TreeSet[T]) RemoveAll added in v1.0.27

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

RemoveAll remove all items in the array 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]) RetainAll added in v1.0.27

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

RetainAll Retains only the elements in this collection that are contained in the argument 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]) 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]) Seq added in v1.0.27

func (ts *TreeSet[T]) Seq() iter.Seq[T]

Seq returns a iter.Seq[T] for range

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