treeset

package
v0.0.0-...-8cfa9df Latest Latest
Warning

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

Go to latest
Published: May 30, 2024 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package treeset implements a tree backed by a red-black tree.

Structure is not thread safe.

Reference: http://en.wikipedia.org/wiki/Set_%28abstract_data_type%29

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Set

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

Set holds elements in a red-black tree

func New

func New[T cmp.Ordered](values ...T) *Set[T]

func NewWith

func NewWith[T comparable](comparator dsgo.Comparator[T], values ...T) *Set[T]

NewWith instantiates a new empty set with the custom comparator.

func (*Set[T]) Add

func (s *Set[T]) Add(items ...T)

Add adds the items (one or more) to the set.

func (*Set[T]) Ceiling

func (m *Set[T]) Ceiling(element T) (foundElement T, ok bool)

Ceiling finds the ceiling element for the input element. In case that no ceiling is found, then 0-value, false will be returned.

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

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

func (*Set[T]) Clear

func (set *Set[T]) Clear()

Clear clears all values in the set.

func (*Set[T]) Contains

func (set *Set[T]) Contains(items ...T) bool

Contains checks weather items (one or more) are present in the set. All items have to be present in the set for the method to return true. Returns true if no arguments are passed at all, i.e. set is always superset of empty set.

func (*Set[T]) Difference

func (s *Set[T]) Difference(another *Set[T]) *Set[T]

Difference returns the difference between two sets. The two sets should have the same comparators, otherwise the result is empty set. The new set consists of all elements that are in "set" but not in "another". Ref: https://proofwiki.org/wiki/Definition:Set_Difference

func (*Set[T]) Empty

func (set *Set[T]) Empty() bool

Empty returns true if set does not contain any elements.

func (*Set[T]) Floor

func (m *Set[T]) Floor(element T) (foundElement T, ok bool)

Floor finds the floor element for the input element. In case that no floor is found, then o-value, false will be returned.

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

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

func (*Set[T]) Inorder

func (s *Set[T]) Inorder(handler func(key T))

Inorer travels the tree in-order with a handler.

func (*Set[T]) Intersection

func (s *Set[T]) Intersection(another *Set[T]) *Set[T]

Intersection returns the intersection between two sets. The new set consists of all elements that are both in "set" and "another". The two sets should have the same comparators, otherwise the result is empty set. Ref: https://en.wikipedia.org/wiki/Intersection_(set_theory)

func (*Set[T]) Len

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

Len returns number of elements within the set.

func (*Set[T]) Max

func (m *Set[T]) Max() (element T, ok bool)

Max returns the maximum element from the tree set. Returns 0-value, false if set is empty.

func (*Set[T]) Min

func (m *Set[T]) Min() (element T, ok bool)

Min returns the minimum element from the tree set. Returns 0-value, false if set is empty.

func (*Set[T]) Remove

func (set *Set[T]) Remove(items ...T)

Remove removes the items (one or more) from the set.

func (*Set[T]) String

func (set *Set[T]) String() string

String returns a string representation of container

func (*Set[T]) Union

func (s *Set[T]) Union(another *Set[T]) *Set[T]

Union returns the union of two sets. The new set consists of all elements that are in "set" or "another" (possibly both). The two sets should have the same comparators, otherwise the result is empty set. Ref: https://en.wikipedia.org/wiki/Union_(set_theory)

func (*Set[T]) Values

func (set *Set[T]) Values() []T

Values returns all items in the set.

Jump to

Keyboard shortcuts

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