set

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Mar 26, 2025 License: MIT Imports: 2 Imported by: 1

README

English | 中文

Set Collection

Set is a generic implementation of the set data structure that supports any comparable type.

Key Features

  • Generic support for any comparable type
  • Capacity limit support
  • Rich set operations
  • Iterator support
  • Not thread-safe

Basic Operations

// Create a set
s := set.CreateSet[int]()

// Add element
s.Add(1)

// Check if element exists
exists := s.IsElementOf(1)

// Get set size
size := s.Size()

// Remove element
s.Remove(1)

// Clear set
s.Clear()

Advanced Operations

// Create a set with capacity
s := set.CreateSetWithCapacity[int](10)

// Create set from multiple elements
s := set.BuildSet(1, 2, 3)

// Create set from iterator
s := set.Collect(someIterator)

// Filter set
filtered := s.Filter(func(x int) bool { return x > 0 })

// Map set
mapped := s.Map(func(x int) int { return x * 2 })

// Check if set is empty
isEmpty := set.IsEmptySet(s)

// Check if two sets are equal
equal := s1.Equal(s2)

Iteration

// Iterate using iterator
for x := range s.Iter() {
    fmt.Println(x)
}

// Get all elements as list
elements := s.Enumerate()

Notes

  • Sets are not thread-safe, locking is required in concurrent environments
  • When capacity limit is set, exceeding it will return an error
  • Element order in sets is not guaranteed

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func IsEmptySet

func IsEmptySet[T comparable](s *Set[T]) bool

IsEmptySet checks whether the set S is empty.

Types

type Set

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

https://en.wikipedia.org/wiki/Set_(abstract_data_type)#Operations

func BuildSet

func BuildSet[T comparable](xn ...T) *Set[T]

BuildSet creates a set structure with values x1,x2,...,xn.

func Collect

func Collect[T comparable](collection iter.Seq[T]) *Set[T]

Collect creates a new set structure containing all the elements of the given collection or all the elements returned by the given iterator.

func CreateSet

func CreateSet[T comparable]() *Set[T]

CreateSet creates a new, initially empty set structure.

func CreateSetWithCapacity

func CreateSetWithCapacity[T comparable](n int) *Set[T]

CreateSetWithCapacity creates a new set structure, initially empty but capable of holding up to n elements.

func (*Set[T]) Add

func (s *Set[T]) Add(x T) error

Add adds the element x to S, if it is not present already.

func (*Set[T]) Capacity

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

Capacity returns the maximum number of values that S can hold.

func (*Set[T]) Clear

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

Clear deletes all elements of S.

func (*Set[T]) Enumerate

func (s *Set[T]) Enumerate() []T

Enumerate returns a list containing the elements of S in some arbitrary order.

func (*Set[T]) Equal

func (s *Set[T]) Equal(s2 *Set[T]) bool

Equal checks whether the two given sets are equal (i.e. contain all and only the same elements).

func (*Set[T]) Filter

func (s *Set[T]) Filter(p func(T) bool) *Set[T]

Filter returns the subset containing all elements of S that satisfy a given predicate P.

func (*Set[T]) Hash

func (s *Set[T]) Hash() string

Hash returns a hash value for the static set S such that if equal(S1, S2) then hash(S1) = hash(S2)

func (*Set[T]) IsElementOf

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

IsElementOf checks whether the value x is in the set S.

func (*Set[T]) Iter

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

Iter returns an iterator over the elements of the set.

func (*Set[T]) Map

func (s *Set[T]) Map(f func(T) T) *Set[T]

Map returns the set of distinct values resulting from applying function F to each element of S.

func (*Set[T]) Pick

func (s *Set[T]) Pick()

Pick returns an arbitrary element of S. Functionally, the mutator pop can be interpreted as the pair of selectors (pick, rest), where rest returns the set consisting of all elements except for the arbitrary element. Can be interpreted in terms of iterate.

func (*Set[T]) Pop

func (s *Set[T]) Pop() (v T, ok bool)

Pop returns an arbitrary element of S, deleting it from S

func (*Set[T]) Remove

func (s *Set[T]) Remove(x T)

Remove removes the element x from S, if it is present.

func (*Set[T]) Size

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

Size returns the number of elements in S.

Jump to

Keyboard shortcuts

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