Documentation
¶
Overview ¶
Package multiset provides a generic sorted multiset backed by a treap (randomized binary search tree), giving O(log n) expected time for all structural operations — equivalent to C++'s std::multiset.
Priority is determined by a user-supplied Less function:
less(a, b) returns true if a should be ordered before b
Ascending order (default for numbers):
ms := multiset.New(func(a, b int) bool { return a < b })
Descending order:
ms := multiset.New(func(a, b int) bool { return a > b })
Custom struct ordered by a field:
type Task struct { Name string; Priority int }
ms := multiset.New(func(a, b Task) bool { return a.Priority < b.Priority })
Index ¶
- type Multiset
- func (ms *Multiset[T]) Ceiling(v T) (T, bool)
- func (ms *Multiset[T]) Contains(v T) bool
- func (ms *Multiset[T]) Count(v T) int
- func (ms *Multiset[T]) Floor(v T) (T, bool)
- func (ms *Multiset[T]) Insert(v T)
- func (ms *Multiset[T]) IsEmpty() bool
- func (ms *Multiset[T]) Kth(k int) (T, bool)
- func (ms *Multiset[T]) Max() (T, bool)
- func (ms *Multiset[T]) Min() (T, bool)
- func (ms *Multiset[T]) Pop() (T, bool)
- func (ms *Multiset[T]) Rank(v T) int
- func (ms *Multiset[T]) Remove(v T) bool
- func (ms *Multiset[T]) RemoveAll(v T) bool
- func (ms *Multiset[T]) Size() int
- func (ms *Multiset[T]) ToSlice() []T
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Multiset ¶
type Multiset[T any] struct { // contains filtered or unexported fields }
Multiset is a generic sorted multiset. Duplicate elements are allowed. The zero value is not usable; construct with New.
func New ¶
New returns an empty Multiset ordered by less. less(a, b) must return true when a is ordered strictly before b.
func (*Multiset[T]) Ceiling ¶
Ceiling returns the smallest element that is >= v. Returns the zero value and false if no such element exists. O(log n) expected.
func (*Multiset[T]) Contains ¶
Contains reports whether v is present at least once. O(log n) expected.
func (*Multiset[T]) Floor ¶
Floor returns the largest element that is <= v. Returns the zero value and false if no such element exists. O(log n) expected.
func (*Multiset[T]) Insert ¶
func (ms *Multiset[T]) Insert(v T)
Insert adds one occurrence of v. O(log n) expected.
func (*Multiset[T]) Kth ¶
Kth returns the k-th smallest element (0-indexed, duplicates counted separately). Returns the zero value and false if k is out of range. O(log n) expected.
func (*Multiset[T]) Max ¶
Max returns the largest element. Returns the zero value and false if the multiset is empty. O(log n).
func (*Multiset[T]) Min ¶
Min returns the smallest element. Returns the zero value and false if the multiset is empty. O(log n).
func (*Multiset[T]) Pop ¶
Pop removes and returns the smallest (first-ordered) element. Returns the zero value and false if the multiset is empty. O(log n) expected.
func (*Multiset[T]) Rank ¶
Rank returns the number of elements strictly less than v. O(log n) expected.
func (*Multiset[T]) Remove ¶
Remove removes one occurrence of v. Returns false if v is not present. O(log n) expected.
func (*Multiset[T]) RemoveAll ¶
RemoveAll removes every occurrence of v. Returns false if v is not present. O(log n) expected.