sorted

package
v0.0.0-...-2be5c02 Latest Latest
Warning

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

Go to latest
Published: Nov 5, 2025 License: Apache-2.0 Imports: 2 Imported by: 0

Documentation

Overview

Package sorted provides data structures that maintain the elements in a sorted order.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Set

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

Set is a sorted (ordered) set of T.

func NewSet

func NewSet[T cmp.Ordered]() *Set[T]

NewSet creates a new sorted set of T, using < for comparisons.

func NewSetFunc

func NewSetFunc[T any](cmp func(T, T) int) *Set[T]

NewSetFunc creates a new set of T which is ordered according to cmp.

If T is or contains a pointer, the values referenced by it must not be changed in a way that affects the order for as long as it is in the Set.

func (*Set[T]) All

func (s *Set[T]) All() iter.Seq[T]

All returns an iterator over all elements in the set in sorted order.

func (*Set[T]) Backward

func (s *Set[T]) Backward() iter.Seq[T]

Backward returns an iterator over all elements in the set in reverse order.

func (*Set[T]) Delete

func (s *Set[T]) Delete(x T) (deleted bool)

Delete removes x from the set if it exists. The return value indicates whether the removal happened.

func (*Set[T]) FindGreaterThanOrEqual

func (s *Set[T]) FindGreaterThanOrEqual(x T) (*SetItem[T], bool)

FindGreaterThanOrEqual returns the first SetItem that is greater than or equal to x, and true. If there is no such element, returns false.

func (*Set[T]) First

func (s *Set[T]) First() (*SetItem[T], bool)

First returns the smallest SetItem and true, or nil and false if the set is empty.

func (*Set[T]) Has

func (s *Set[T]) Has(x T) bool

Has reports whether x is in the set.

func (*Set[T]) Insert

func (s *Set[T]) Insert(x T) (added bool)

Insert adds x to the set. Returns whether the insertion happened, i.e. returns false if an element that compares as equal to x was already in the set, otherwise returns true.

func (*Set[T]) Last

func (s *Set[T]) Last() (*SetItem[T], bool)

Last returns the largest SetItem and true, or nil and false if the set is empty.

func (*Set[T]) Len

func (s *Set[T]) Len() int

Len returns the number of elements in the set.

func (*Set[T]) Max

func (s *Set[T]) Max() (T, bool)

Max returns the largest value in the set and true. If the set is empty returns false.

func (*Set[T]) Min

func (s *Set[T]) Min() (T, bool)

Min returns the smallest value in the set and true. If the set is empty returns false.

type SetItem

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

SetItem refers to an element in the Set.

func (*SetItem[T]) InsertNearby

func (si *SetItem[T]) InsertNearby(x T) (*SetItem[T], bool)

InsertNearby inserts x to the same set as si. It is more efficient than calling Set.Insert if x would end up right before or after si.

Returns the SetItem whose value is x, and a bool indicating whether this is a newly added item.

func (*SetItem[T]) Next

func (si *SetItem[T]) Next() (*SetItem[T], bool)

Next returns the next larger item in the set and true, or nil and false if si is already the largest element.

func (*SetItem[T]) Prev

func (si *SetItem[T]) Prev() (*SetItem[T], bool)

Prev returns the next smaller item in the set and true, or nil and false if si is already the smallest element.

func (*SetItem[T]) Value

func (si *SetItem[T]) Value() T

Value returns the item's value.

Jump to

Keyboard shortcuts

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