sets

package
v0.1.3 Latest Latest
Warning

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

Go to latest
Published: Apr 4, 2025 License: MIT Imports: 7 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrFullSet occurs when a `Insert()` method is called on a set
	// that is full. Readers must return this error as is and not wrap
	// it as callers are expected to check for this error with the ==
	// operator.
	ErrFullSet error = errors.New("set is full")
)

Functions

func Equals

func Equals[E comparable](s1, s2 Set[E]) bool

Equals checks if the two sets are equal. Two sets are equal if the call of IsSubset(s1, s2) and IsSubset(s2, s1) return true.

Parameters:

  • s1: The first set.
  • s2: The second set.

Returns:

  • bool: True if the two sets are equal, false otherwise.

func Insert

func Insert[E comparable](s Set[E], elems ...E) (uint32, error)

Insert adds elements to the set.

Parameters:

  • s: The set to add elements to.
  • elems: The elements to add.

Returns:

  • uint32: The number of elements added.
  • error: An error if the elements could not be added.

Errors:

  • common.ErrBadParam: If the set is nil.
  • common.ErrMaxSizeExceeded: If the number of elements added would exceed the maximum size of uint32.
  • any other error: Depends on the implementation of the `Insert()` method.

func IsSubset

func IsSubset[E comparable](s1, s2 Set[E]) bool

IsSubset checks if the first set is a subset of the second set.

Parameters:

  • s1: The first set.
  • s2: The second set.

Returns:

  • bool: True if the first set is a subset of the second set, false otherwise.

Types

type OrderSet

type OrderSet[E comparable] struct {
	// contains filtered or unexported fields
}

OrderSet is a set that keeps the order of insertion.

An empty OrderSet can be created with either the `var s OrderSet[E]` syntax or the `s := new(OrderSet[E])` constructor.

func (OrderSet[E]) All

func (s OrderSet[E]) All() iter.Seq[E]

All implements Set.

func (OrderSet[E]) Contains

func (s OrderSet[E]) Contains(elem E) bool

Contains implements Set.

func (*OrderSet[E]) Insert

func (s *OrderSet[E]) Insert(elem E) (bool, error)

Insert implements Set.

func (OrderSet[E]) IsEmpty

func (s OrderSet[E]) IsEmpty() bool

IsEmpty implements Set.

func (OrderSet[E]) IsFull

func (s OrderSet[E]) IsFull() bool

IsFull implements Set.

func (*OrderSet[E]) Reset

func (s *OrderSet[E]) Reset() error

Reset implements Set.

type SeenSet

type SeenSet[E comparable] struct {
	// contains filtered or unexported fields
}

SeenSet is a set can can see elements.

An empty SeenSet can be created by using either the `var s SeenSet[E]` syntax or the `s := new(SeenSet[E])` constructor.

func (SeenSet[E]) All

func (s SeenSet[E]) All() iter.Seq[E]

All implements Set.

func (SeenSet[E]) Contains

func (s SeenSet[E]) Contains(elem E) bool

Contains implements Set.

func (*SeenSet[E]) Insert

func (s *SeenSet[E]) Insert(elem E) (bool, error)

Insert implements Set.

func (SeenSet[E]) IsEmpty

func (s SeenSet[E]) IsEmpty() bool

IsEmpty implements Set.

func (SeenSet[E]) IsFull

func (s SeenSet[E]) IsFull() bool

IsFull implements Set.

Always returns false.

func (*SeenSet[E]) Reset

func (s *SeenSet[E]) Reset() error

Reset implements Set.

type Set

type Set[E comparable] interface {
	// Insert adds an element to the set. Returns false and nil if the
	// element is already in the set.
	//
	// Parameters:
	//   - elem: The element to add.
	//
	// Returns:
	//   - bool: True if the element was added, false if the element
	//   - error: An error if the element could not be added.
	//
	// Errors:
	//   - ErrFullSet: If the set is full.
	//   - common.ErrNilReceiver: If the receiver is nil.
	//   - any other error: Implementation-dependent.
	Insert(elem E) (bool, error)

	// Contains checks if the set contains the element.
	//
	// Parameters:
	//   - elem: The element to check.
	//
	// Returns:
	//   - bool: True if the set contains the element, false otherwise.
	Contains(elem E) bool

	// IsEmpty checks if the set is empty.
	//
	// Returns:
	//   - bool: True if the set is empty, false otherwise.
	IsEmpty() bool

	// IsFull checks if the set is full.
	//
	// Returns:
	//   - bool: True if the set is full, false otherwise.
	IsFull() bool

	// All returns a sequence of all elements in the set. The order of
	// the elements is undefined.
	//
	// Returns:
	//   - iter.Seq[E]: A sequence of all elements in the set. Never
	//     returns nil.
	All() iter.Seq[E]

	// Reset clears the set for reuse.
	//
	// Returns:
	//   - error: An error if the set could not be reset.
	//
	// Errors:
	//   - common.ErrNilReceiver: If the receiver is nil.
	//   - any other error: Implementation-dependent.
	Reset() error
}

Set is the interface for set-like data structures.

type SortSet

type SortSet[E cmp.Ordered] struct {
	// contains filtered or unexported fields
}

SortSet is a set that uses the < operator to compare elements.

An empty SortSet can be created with either the `var s SortSet[E]` syntax or the `s := new(SortSet[E])` constructor.

func (SortSet[E]) All

func (s SortSet[E]) All() iter.Seq[E]

All implements Set.

func (SortSet[E]) Contains

func (s SortSet[E]) Contains(elem E) bool

Contains implements Set.

func (*SortSet[E]) Insert

func (s *SortSet[E]) Insert(elem E) (bool, error)

Insert implements Set.

func (SortSet[E]) IsEmpty

func (s SortSet[E]) IsEmpty() bool

IsEmpty implements Set.

func (SortSet[E]) IsFull

func (s SortSet[E]) IsFull() bool

IsFull implements Set.

func (*SortSet[E]) Reset

func (s *SortSet[E]) Reset() error

Reset implements Set.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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