sets

package
v0.0.0-...-84d6781 Latest Latest
Warning

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

Go to latest
Published: Nov 6, 2024 License: MIT Imports: 4 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var SD setsT

SD is the namespace for SD-like functions.

Functions

func Merge

func Merge[T any](from, other Set[T]) error

Merge merges the elements of another set into the specified set. The elements will be added to the set in the order they are returned by the other set's Elem method.

Parameters:

  • from: The set that the elements will be added to.
  • other: The set whose elements will be merged into from.

Returns:

  • error: Returns an error if the sets cannot be merged.

Errors:

  • common.ErrBadParam: If other has at least one element and from is nil.

Types

type OrderedSet

type OrderedSet[T cmp.Ordered] struct {
	// contains filtered or unexported fields
}

OrderedSet is an ordered set in ascending order. An empty ordered set can either be created with the `var set OrderedSet[T]` syntax or with the `new(OrderedSet[T])` constructor.

func NewOrderedSet

func NewOrderedSet[T cmp.Ordered](elems []T) *OrderedSet[T]

NewOrderedSet creates a new ordered set from the provided elements. The set will contain unique elements in ascending order.

Parameters:

  • elems: A slice of elements to initialize the set. Elements must be of an ordered type.

Returns:

  • *OrderedSet[T]: A pointer to the newly created ordered set containing unique elements.

func (*OrderedSet[T]) Add

func (s *OrderedSet[T]) Add(elem T) error

Add implements the Set interface.

func (*OrderedSet[T]) AddMany

func (s *OrderedSet[T]) AddMany(elems []T) error

AddMany implements the Set interface.

func (OrderedSet[T]) Contains

func (s OrderedSet[T]) Contains(elem T) bool

Contains implements the Set interface.

func (OrderedSet[T]) Elem

func (s OrderedSet[T]) Elem() iter.Seq[T]

Elem implements the Set interface.

func (OrderedSet[T]) IsEmpty

func (s OrderedSet[T]) IsEmpty() bool

IsEmpty implements the Set interface.

func (*OrderedSet[T]) Reset

func (s *OrderedSet[T]) Reset()

Reset implements the Set interface.

func (OrderedSet[T]) Size

func (s OrderedSet[T]) Size() int

Size implements the Set interface.

type Set

type Set[T any] interface {
	// Size returns the number of elements in the set.
	//
	// Returns:
	//   - int: The number of elements in the set. Never negative.
	Size() int

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

	// Reset resets the set for reuse.
	Reset()

	// Add inserts an element into the set if it is not already present.
	//
	// Parameters:
	//   - elem: The element to add to the set.
	//
	// Returns:
	//   - error: Returns an error if the receiver is nil.
	Add(elem T) error

	// AddMany adds multiple elements to the set. It must be equal to calling Add
	// multiple times but can be more efficient than adding each element individually.
	//
	// Parameters:
	//   - elems: The elements to add to the set.
	//
	// Returns:
	//   - error: Returns an error if the receiver is nil.
	AddMany(elems []T) error

	// Contains checks whether the specified element is present in the set.
	//
	// Parameters:
	//   - elem: The element to check for presence in the set.
	//
	// Returns:
	//   - bool: True if the element is present in the set, false otherwise.
	Contains(elem T) bool

	// Elem iterates through the elements in the set. The order of elements
	// is not guaranteed to be the same as the order in which they were added.
	//
	// Returns:
	//   - iter.Seq[T]: The elements in the set. Never returns nil.
	Elem() iter.Seq[T]
}

Set is an interface used by any set-like data structure.

func New

func New[T comparable](elems ...T) Set[T]

New creates a new set of comparable elements from the provided elements.

Parameters:

  • elems: The elements to add to the set.

Returns:

  • Set[T]: The new set. Never returns nil.

Jump to

Keyboard shortcuts

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