go_set

package module
v1.2.6 Latest Latest
Warning

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

Go to latest
Published: Mar 26, 2026 License: MIT Imports: 6 Imported by: 0

README

go-set

Go Reference Go Version License

A powerful, generic, and high-performance Set implementation for Go (1.23+), designed for both simplicity and advanced functional use cases.

Overview

go-set provides a flexible and efficient way to work with sets in Go using generics. It supports both standard set operations and functional programming patterns, along with a thread-safe variant for concurrent environments.

The library is built on top of native Go maps, ensuring excellent performance with minimal overhead.

No external dependencies required.


Features

Core
  • Generic type support: Set[T comparable]

  • Constant-time operations (average):

    • Add
    • Remove
    • Contains
  • Zero external dependencies

  • Optimized memory allocation

Functional Programming
  • Map
  • Filter
  • Reduce
  • FlatMap
  • Any, All, Count
  • Partition
Iterators (Go 1.23+)
  • Native iteration via iter.Seq

  • Lazy evaluation support:

    • Iter
    • FilterIter
    • MapIter
    • FlatMapIter
Set Algebra
  • Union
  • Intersection
  • Difference
  • SymmetricDifference
  • IsSubset
  • IsSuperset
  • IsDisjoint
  • Equals
Utilities
  • Min, Max, Sum
  • Sort
  • ToSlice, Copy, Clear
Concurrency
  • SyncSet (thread-safe wrapper)
  • Built with sync.RWMutex
  • Safe for concurrent reads and writes

Installation

go get github.com/encomers/go-set

Requirements

  • Go 1.23+

Basic Usage

package main

import (
    "fmt"
    set "github.com/encomers/go-set"
)

func main() {
    s := set.NewSet(1, 2, 3)

    s.Add(4, 5)
    s.Remove(2)

    fmt.Println(s.Contains(3)) // true
    fmt.Println(s.Size())      // 4
    fmt.Println(s.IsEmpty())   // false
}

Functional Style

s := set.NewSet(1, 2, 3, 4, 5)

// Filter
evens := s.Filter(func(v int) bool {
    return v%2 == 0
})

// Map
doubled := s.Map(func(v int) int {
    return v * 2
})

// Reduce
sum := s.Reduce(0, func(acc, v int) int {
    return acc + v
})

Iteration (Go 1.23+)

for v := range s.Iter() {
    fmt.Println(v)
}
Lazy Iterators
for v := range s.FilterIter(func(v int) bool {
    return v > 2
}) {
    fmt.Println(v)
}

Set Operations

a := set.NewSet(1, 2, 3)
b := set.NewSet(3, 4, 5)

union := a.Union(b)
intersection := a.Intersection(b)
difference := a.Difference(b)
symDiff := a.SymmetricDifference(b)

Utilities

min := set.Min(s)
max := set.Max(s)
sum := set.Sum(s)

sorted := set.Sort(s, func(a, b int) bool {
    return a < b
})

Thread-Safe Usage

s := set.NewSyncSet[int]()

s.Add(1, 2, 3)

if s.Contains(2) {
    fmt.Println("exists")
}
Notes on SyncSet
  • Safe for concurrent access
  • Uses read-write locking
  • Some methods return non-sync Set for performance

Design Notes

  • Backed by map[T]struct{}
  • Iteration order is non-deterministic
  • Iterators do not create snapshots (for performance)

Best Practices

  • Use Set for single-threaded scenarios
  • Use SyncSet when working with goroutines
  • Avoid modifying a set during iteration
  • Prefer iterators for large datasets (lazy evaluation)

License

MIT

Documentation

Overview

Package go_set provides a generic set implementation based on maps. It supports standard set operations like Add, Remove, Contains, and functional-style operations like Map, Filter, and FlatMap.

This package utilizes Go 1.23+ features (iter.Seq) for iteration.

Index

Constants

This section is empty.

Variables

View Source
var ErrNilSet = errors.New("go_set: cannot marshal/unmarshal nil set")

Functions

func Max

func Max[T Ordered](s ISet[T]) T

Max returns the maximum element in the set according to the natural ordering of the elements.

func Min

func Min[T Ordered](s ISet[T]) T

Min returns the minimum element in the set according to the natural ordering of the elements.

func Sort

func Sort[T Ordered](s ISet[T], sortFunc func(a, b T) bool) []T

Sort returns a slice of the set's elements sorted according to the provided sort function. The sort function takes two elements of type T and returns true if the first element should be sorted before the second. Note: The order of elements in the set is non-deterministic, so the resulting slice will be sorted based on the provided sort function.

func Sum

func Sum[T Ordered](s ISet[T]) T

Sum calculates the sum of all elements in the set.

Types

type Hashable added in v1.2.6

type Hashable interface {
	~int | ~int8 | ~int16 | ~int32 | ~int64 |
		~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 |
		~string
}

type IOrderedSet

type IOrderedSet[T Ordered] interface {
	ISet[T]
	Min() T
	Max() T
	Sum() T
	Sort(sortFunc func(a, b T) bool) []T
	Sorted() []T
}

type ISet

type ISet[T comparable] interface {
	// Add inserts the provided elements into the set.
	// Duplicate elements are ignored.
	Add(elements ...T)

	// Remove deletes the provided elements from the set.
	// If an element does not exist, it is ignored.
	Remove(elements ...T)

	// Contains checks if the specified element exists in the set.
	// Returns true if the element is present, false otherwise.
	Contains(element T) bool

	// Size returns the number of elements currently in the set.
	Size() int

	// Clear removes all elements from the set, making it empty.
	Clear()

	// IsEmpty checks if the set contains no elements.
	// Returns true if the size is 0, false otherwise.
	IsEmpty() bool

	// ToSlice converts the set elements into a slice.
	// The order of elements in the slice is non-deterministic.
	ToSlice() []T

	// Iter returns an iterator (iter.Seq) over the elements of the set.
	// This allows using the set in a range loop (Go 1.23+).
	//
	// Note: Modifications to the set during iteration may cause undefined behavior.
	Iter() iter.Seq[T]

	// FilterIter returns an iterator that yields only elements satisfying the predicate.
	//
	// Note: Modifications to the set during iteration may cause undefined behavior.
	FilterIter(predicate func(T) bool) iter.Seq[T]

	// MapIter returns an iterator that yields transformed elements using the mapper function.
	//
	// Note: Modifications to the set during iteration may cause undefined behavior.
	MapIter(mapper func(T) T) iter.Seq[T]

	// FlatMapIter returns an iterator that flattens sequences produced by the mapper function.
	//
	// Note: Modifications to the set during iteration may cause undefined behavior.
	FlatMapIter(mapper func(T) iter.Seq[T]) iter.Seq[T]

	// ForEach executes the given action function for each element in the set.
	//
	// Note: Modifying the set (especially adding elements) during execution
	// may cause undefined behavior. Use RemoveIf for safe removal.
	ForEach(action func(T))

	// Map creates a new set by applying the mapper function to each element.
	// Duplicates produced by the mapper will be collapsed in the resulting set.
	// The original set is not modified.
	Map(mapper func(T) T) ISet[T]

	// Any returns true if at least one element in the set satisfies the predicate.
	// Returns false if the set is empty or no elements match.
	Any(predicate func(T) bool) bool

	// All returns true if all elements in the set satisfy the predicate.
	// Returns true if the set is empty.
	All(predicate func(T) bool) bool

	// Filter creates a new set containing only the elements that satisfy the predicate.
	// The original set is not modified.
	Filter(predicate func(T) bool) ISet[T]

	// Reduce applies the reducer function to each element in the set, starting with the initial value.
	// The reducer function takes the accumulated value and the current element, and returns a new accumulated value.
	// The final accumulated value is returned after processing all elements in the set.
	// The original set is not modified.
	Reduce(initial T, reducer func(T, T) T) T

	// FlatMap creates a new set by applying the mapper function to each element
	// and merging the resulting sets.
	// The original set is not modified.
	FlatMap(mapper func(T) ISet[T]) ISet[T]

	// Copy creates a shallow copy of the set.
	// The new set contains the same elements as the original.
	Copy() ISet[T]

	// Union returns a new set containing all elements from both sets.
	// The original sets are not modified.
	Union(other ISet[T]) ISet[T]

	// Intersection returns a new set containing only elements that exist in both sets.
	// The original sets are not modified.
	Intersection(other ISet[T]) ISet[T]

	// Difference returns a new set containing elements that exist in this set
	// but not in the other set.
	// The original sets are not modified.
	Difference(other ISet[T]) ISet[T]

	// SymmetricDifference returns a new set containing elements that exist in either set,
	// but not in both (XOR operation).
	// The original sets are not modified.
	SymmetricDifference(other ISet[T]) ISet[T]

	// IsSubset returns true if all elements of this set are contained in the other set.
	IsSubset(other ISet[T]) bool

	// IsSuperset returns true if this set contains all elements of the other set.
	IsSuperset(other ISet[T]) bool

	// IsDisjoint returns true if this set and the other set have no elements in common.
	IsDisjoint(other ISet[T]) bool

	// Equals returns true if both sets contain the same elements.
	Equals(other ISet[T]) bool

	// String implements the fmt.Stringer interface.
	// Returns a string representation of the set.
	String() string

	// Count returns the number of elements in the set that satisfy the given predicate.
	Count(predicate func(T) bool) int

	// Partition splits the set into two sets based on the given predicate.
	// The first set contains elements that satisfy the predicate, while the second set contains the rest.
	// The original set is not modified.
	Partition(predicate func(T) bool) (ISet[T], ISet[T])

	// Retain removes all elements from the set that do NOT satisfy the predicate.
	// Change the set to contain only elements that satisfy the predicate.
	Retain(predicate func(T) bool)

	// EqualsWith returns true if both sets contain the same elements according to the provided equality function.
	// The equality function takes two elements of type T and returns true if they are considered equal.
	// This allows for custom equality logic (e.g., comparing struct fields instead of the whole struct).
	EqualsWith(other ISet[T], eqFunc func(T, T) bool) bool
}

ISet is the interface that defines the contract for a set data structure. It provides methods for basic set operations, functional-style transformations, and set theory operations.

func MapTo

func MapTo[T comparable, U comparable](s ISet[T], mapper func(T) U) ISet[U]

MapTo creates a new set of type U by applying the mapper function to each element of the input set. This allows transforming the set into a set of a different type (e.g., Set[int] to Set[string]). Duplicates produced by the mapper will be collapsed in the resulting set.

Note: This is a standalone function because Go methods cannot have additional type parameters.

type Ordered

type Ordered interface {
	Hashable | ~uintptr |
		~float32 | ~float64
}

type OrderedSet

type OrderedSet[T Ordered] struct {
	*Set[T]
}

OrderedSet is a set that provides additional methods for ordered types (int, float, string, etc.).

func NewOrderedSet

func NewOrderedSet[T Ordered](elements ...T) *OrderedSet[T]

Create a new OrderedSet with the provided optional elements. In based on Set by default, but can be based on SyncSet if needed (see NewOrderedSyncSet).

func NewOrderedSetWithCapacity

func NewOrderedSetWithCapacity[T Ordered](capacity int, elements ...T) *OrderedSet[T]

Create a new OrderedSet with the provided optional elements and initial capacity.

func (*OrderedSet[T]) MarshalJSON

func (s *OrderedSet[T]) MarshalJSON() ([]byte, error)

MarshalJSON marshals OrderedSet as a JSON array in sorted order.

func (*OrderedSet[T]) Max

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

Max returns the maximum element in the set.

func (*OrderedSet[T]) Min

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

Min returns the minimum element in the set.

func (*OrderedSet[T]) Sort

func (s *OrderedSet[T]) Sort(sortFunc func(a, b T) bool) []T

Sort returns a slice of the set's elements sorted according to the provided sort function.

func (*OrderedSet[T]) Sorted

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

Sorted returns a sorted slice of the elements in the set. (A convenient wrapper that is not available in the base functions).

func (*OrderedSet[T]) Sum

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

Sum returns the sum of all elements in the set.

func (*OrderedSet[T]) UnmarshalJSON

func (s *OrderedSet[T]) UnmarshalJSON(data []byte) error

UnmarshalJSON unmarshals from JSON array.

type Set

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

Set is a generic collection of unique elements. It is backed by a map[T]struct{} for O(1) average time complexity for lookups and insertions.

Note: This structure is NOT thread-safe.

func NewSet

func NewSet[T comparable](elements ...T) *Set[T]

NewSet creates and initializes a new Set with the provided optional elements. If no elements are provided, an empty set is returned.

Example:

s := NewSet(1, 2, 3)
s := NewSet[int]() // empty set

func NewSetWithCapacity

func NewSetWithCapacity[T comparable](capacity int, elements ...T) *Set[T]

NewSetWithCapacity creates and initializes a new Set with the provided optional elements and initial capacity. If the number of provided elements exceeds the specified capacity, the capacity will be automatically increased to accommodate all elements.

Example:

s := NewSetWithCapacity(100, 1, 2, 3) // initial capacity 100, but only 3 elements
s := NewSetWithCapacity[int](50) // empty set with initial capacity 50

s := NewSetWithCapacity(2, 1, 2, 3) // initial capacity 2, but will be increased to accommodate 3 elements

func (*Set[T]) Add

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

Add inserts the provided elements into the set. Duplicate elements are ignored.

func (*Set[T]) All

func (s *Set[T]) All(predicate func(T) bool) bool

All returns true if all elements in the set satisfy the predicate. Returns false if the set is empty.

func (*Set[T]) Any

func (s *Set[T]) Any(predicate func(T) bool) bool

Any returns true if at least one element in the set satisfies the predicate. Returns false if the set is empty or no elements match.

func (*Set[T]) Clear

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

Clear removes all elements from the set, making it empty.

func (*Set[T]) Contains

func (s *Set[T]) Contains(element T) bool

Contains checks if the specified element exists in the set. Returns true if the element is present, false otherwise.

func (*Set[T]) Copy

func (s *Set[T]) Copy() ISet[T]

Copy creates a shallow copy of the set. The new set contains the same elements as the original.

func (*Set[T]) Count

func (s *Set[T]) Count(predicate func(T) bool) int

Count returns the number of elements in the set that satisfy the given predicate.

func (*Set[T]) Difference

func (s *Set[T]) Difference(other ISet[T]) ISet[T]

Difference returns a new set containing elements that exist in this set but not in the other set. The original sets are not modified.

func (*Set[T]) Equals

func (s *Set[T]) Equals(other ISet[T]) bool

Equals returns true if both sets contain the same elements.

func (*Set[T]) EqualsWith

func (s *Set[T]) EqualsWith(other ISet[T], eqFunc func(T, T) bool) bool

EqualsWith returns true if both sets contain the same elements according to the provided equality function. The equality function takes two elements of type T and returns true if they are considered equal. This allows for custom equality logic (e.g., comparing struct fields instead of the whole struct).

func (*Set[T]) Filter

func (s *Set[T]) Filter(predicate func(T) bool) ISet[T]

Filter creates a new set containing only the elements that satisfy the predicate. The original set is not modified.

func (*Set[T]) FilterIter

func (s *Set[T]) FilterIter(predicate func(T) bool) iter.Seq[T]

FilterIter returns an iterator that yields only elements satisfying the predicate.

Performance Note: This method iterates directly over the underlying map without creating a snapshot. Modifying the set during iteration is not recommended.

func (*Set[T]) FlatMap

func (s *Set[T]) FlatMap(mapper func(T) ISet[T]) ISet[T]

FlatMap creates a new set by applying the mapper function to each element and merging the resulting sets. The mapper function should return a *Set[T] for each element. The original set is not modified.

func (*Set[T]) FlatMapIter

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

FlatMapIter returns an iterator that flattens sequences produced by the mapper function.

Performance Note: This method iterates directly over the underlying map without creating a snapshot. Modifying the set during iteration is not recommended.

func (*Set[T]) ForEach

func (s *Set[T]) ForEach(action func(T))

ForEach executes the given action function for each element in the set.

Performance Note: This method iterates directly over the underlying map without creating a snapshot. Modifying the set (especially adding elements) during execution may cause undefined behavior or panic. Use RemoveIf for safe removal.

func (*Set[T]) Intersection

func (s *Set[T]) Intersection(other ISet[T]) ISet[T]

Intersection returns a new set containing only elements that exist in both sets. The original sets are not modified.

func (*Set[T]) IsDisjoint

func (s *Set[T]) IsDisjoint(other ISet[T]) bool

IsDisjoint returns true if this set and the other set have no elements in common.

func (*Set[T]) IsEmpty

func (s *Set[T]) IsEmpty() bool

IsEmpty checks if the set contains no elements. Returns true if the size is 0, false otherwise.

func (*Set[T]) IsSubset

func (s *Set[T]) IsSubset(other ISet[T]) bool

IsSubset returns true if all elements of this set are contained in the other set.

func (*Set[T]) IsSuperset

func (s *Set[T]) IsSuperset(other ISet[T]) bool

IsSuperset returns true if this set contains all elements of the other set.

func (*Set[T]) Iter

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

Iter returns an iterator (iter.Seq) over the elements of the set. This allows using the set in a range loop (Go 1.23+).

Performance Note: This method iterates directly over the underlying map without creating a snapshot. Modifying the set (adding/removing elements) during iteration may cause undefined behavior or panic.

func (*Set[T]) Map

func (s *Set[T]) Map(mapper func(T) T) ISet[T]

Map creates a new set by applying the mapper function to each element of the current set. Duplicates produced by the mapper will be collapsed in the resulting set. The original set is not modified.

func (*Set[T]) MapIter

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

MapIter returns an iterator that yields transformed elements using the mapper function.

Performance Note: This method iterates directly over the underlying map without creating a snapshot. Modifying the set during iteration is not recommended.

func (*Set[T]) MarshalJSON

func (s *Set[T]) MarshalJSON() ([]byte, error)

MarshalJSON implements the json.Marshaler interface. Set is marshaled as a JSON array. Order is non-deterministic.

func (*Set[T]) Partition

func (s *Set[T]) Partition(predicate func(T) bool) (ISet[T], ISet[T])

Partition splits the set into two sets based on the given predicate. The first set contains elements that satisfy the predicate, while the second set contains the rest. The original set is not modified.

func (*Set[T]) Reduce

func (s *Set[T]) Reduce(initial T, reducer func(T, T) T) T

Reduce applies the reducer function to each element in the set, starting with the initial value. The reducer function takes the accumulated value and the current element, and returns a new accumulated value. The final accumulated value is returned after processing all elements in the set.

The original set is not modified.

func (*Set[T]) Remove

func (s *Set[T]) Remove(elements ...T)

Remove deletes the provided elements from the set. If an element does not exist, it is ignored.

func (*Set[T]) Retain

func (s *Set[T]) Retain(predicate func(T) bool)

Retain removes all elements from the set that do NOT satisfy the predicate. Change the set to contain only elements that satisfy the predicate.

func (*Set[T]) Size

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

Size returns the number of elements currently in the set.

func (*Set[T]) String

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

String implements the fmt.Stringer interface. Returns a string representation of the set.

func (*Set[T]) SymmetricDifference

func (s *Set[T]) SymmetricDifference(other ISet[T]) ISet[T]

SymmetricDifference returns a new set containing elements that exist in either set, but not in both (XOR operation). The original sets are not modified.

func (*Set[T]) ToSlice

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

ToSlice converts the set elements into a slice. The order of elements in the slice is non-deterministic.

func (*Set[T]) Union

func (s *Set[T]) Union(other ISet[T]) ISet[T]

Union returns a new set containing all elements from both sets. The original sets are not modified.

func (*Set[T]) UnmarshalJSON

func (s *Set[T]) UnmarshalJSON(data []byte) error

UnmarshalJSON implements the json.Unmarshaler interface. Expects a JSON array. Duplicates are automatically ignored.

type SyncOrderedSet

type SyncOrderedSet[T Ordered] struct {
	*SyncSet[T]
}

OrderedSet is a set that provides additional methods for ordered types (int, float, string, etc.).

func NewSyncOrderedSet

func NewSyncOrderedSet[T Ordered](elements ...T) *SyncOrderedSet[T]

func NewSyncOrderedSetWithCapacity

func NewSyncOrderedSetWithCapacity[T Ordered](capacity int, elements ...T) *SyncOrderedSet[T]

Create a new OrderedSet with the provided optional elements and initial capacity.

func (*SyncOrderedSet[T]) MarshalJSON

func (s *SyncOrderedSet[T]) MarshalJSON() ([]byte, error)

MarshalJSON marshals as JSON array in sorted order (thread-safe).

func (*SyncOrderedSet[T]) Max

func (s *SyncOrderedSet[T]) Max() T

Max returns the maximum element in the set.

func (*SyncOrderedSet[T]) Min

func (s *SyncOrderedSet[T]) Min() T

Min returns the minimum element in the set.

func (*SyncOrderedSet[T]) Sort

func (s *SyncOrderedSet[T]) Sort(sortFunc func(a, b T) bool) []T

Sort returns a slice of the set's elements sorted according to the provided sort function.

func (*SyncOrderedSet[T]) Sorted

func (s *SyncOrderedSet[T]) Sorted() []T

Sorted returns a sorted slice of the elements in the set. (A convenient wrapper that is not available in the base functions).

func (*SyncOrderedSet[T]) Sum

func (s *SyncOrderedSet[T]) Sum() T

Sum returns the sum of all elements in the set.

func (*SyncOrderedSet[T]) UnmarshalJSON

func (s *SyncOrderedSet[T]) UnmarshalJSON(data []byte) error

UnmarshalJSON unmarshals from JSON array (thread-safe).

type SyncSet

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

SyncSet is a thread-safe wrapper around Set. It uses sync.RWMutex to protect concurrent access.

func NewSyncSet

func NewSyncSet[T comparable](elements ...T) *SyncSet[T]

NewSyncSet creates and initializes a new SyncSet with the provided optional elements.

func NewSyncSetWithCapacity

func NewSyncSetWithCapacity[T comparable](capacity int, elements ...T) *SyncSet[T]

NewSyncSetWithCapacity creates and initializes a new SyncSet with the provided optional elements.

func (*SyncSet[T]) Add

func (s *SyncSet[T]) Add(elements ...T)

Add inserts the provided elements into the set. Thread-safe.

func (*SyncSet[T]) All

func (s *SyncSet[T]) All(predicate func(T) bool) bool

All returns true if all elements in the set satisfy the predicate. Thread-safe.

func (*SyncSet[T]) Any

func (s *SyncSet[T]) Any(predicate func(T) bool) bool

Any returns true if at least one element in the set satisfies the predicate. Thread-safe.

func (*SyncSet[T]) Clear

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

Clear removes all elements from the set, making it empty. Thread-safe.

func (*SyncSet[T]) Contains

func (s *SyncSet[T]) Contains(element T) bool

Contains checks if the specified element exists in the set. Thread-safe.

func (*SyncSet[T]) Copy

func (s *SyncSet[T]) Copy() ISet[T]

Copy creates a shallow copy of the set. Thread-safe. Returns a new SyncSet.

func (*SyncSet[T]) Count

func (s *SyncSet[T]) Count(predicate func(T) bool) int

Count returns the number of elements in the set that satisfy the given predicate. Thread-safe.

func (*SyncSet[T]) Difference

func (s *SyncSet[T]) Difference(other ISet[T]) ISet[T]

Difference returns a new set containing elements that exist in this set but not in the other set. Thread-safe. Returns a non-sync Set.

func (*SyncSet[T]) Equals

func (s *SyncSet[T]) Equals(other ISet[T]) bool

Equals returns true if both sets contain the same elements. Thread-safe.

func (*SyncSet[T]) EqualsWith

func (s *SyncSet[T]) EqualsWith(other ISet[T], eqFunc func(T, T) bool) bool

EqualsWith checks if this set is equal to another set using a custom equality function. Thread-safe.

func (*SyncSet[T]) Filter

func (s *SyncSet[T]) Filter(predicate func(T) bool) ISet[T]

Filter creates a new set containing only the elements that satisfy the predicate. Thread-safe. Returns a non-sync Set.

func (*SyncSet[T]) FilterIter

func (s *SyncSet[T]) FilterIter(predicate func(T) bool) iter.Seq[T]

FilterIter returns an iterator that yields only elements satisfying the predicate. Thread-safe. Holds a read lock during iteration.

func (*SyncSet[T]) FlatMap

func (s *SyncSet[T]) FlatMap(mapper func(T) ISet[T]) ISet[T]

FlatMap creates a new set by applying the mapper function to each element and merging results. Thread-safe. Returns a non-sync Set.

func (*SyncSet[T]) FlatMapIter

func (s *SyncSet[T]) FlatMapIter(mapper func(T) iter.Seq[T]) iter.Seq[T]

FlatMapIter returns an iterator that flattens sequences produced by the mapper function. Thread-safe. Holds a read lock during iteration.

func (*SyncSet[T]) ForEach

func (s *SyncSet[T]) ForEach(action func(T))

ForEach executes the given action function for each element in the set. Thread-safe. Holds a read lock during execution. Note: Do not modify the set within the action function to avoid deadlocks.

func (*SyncSet[T]) Intersection

func (s *SyncSet[T]) Intersection(other ISet[T]) ISet[T]

Intersection returns a new set containing only elements that exist in both sets. Thread-safe. Returns a non-sync Set.

func (*SyncSet[T]) IsDisjoint

func (s *SyncSet[T]) IsDisjoint(other ISet[T]) bool

IsDisjoint returns true if this set and the other set have no elements in common. Thread-safe.

func (*SyncSet[T]) IsEmpty

func (s *SyncSet[T]) IsEmpty() bool

IsEmpty checks if the set contains no elements. Thread-safe.

func (*SyncSet[T]) IsSubset

func (s *SyncSet[T]) IsSubset(other ISet[T]) bool

IsSubset returns true if all elements of this set are contained in the other set. Thread-safe.

func (*SyncSet[T]) IsSuperset

func (s *SyncSet[T]) IsSuperset(other ISet[T]) bool

IsSuperset returns true if this set contains all elements of the other set. Thread-safe.

func (*SyncSet[T]) Iter

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

Iter returns an iterator (iter.Seq) over the elements of the set. Thread-safe. Holds a read lock during iteration.

func (*SyncSet[T]) Map

func (s *SyncSet[T]) Map(mapper func(T) T) ISet[T]

Map creates a new set by applying the mapper function to each element. Thread-safe. Returns a non-sync Set.

func (*SyncSet[T]) MapIter

func (s *SyncSet[T]) MapIter(mapper func(T) T) iter.Seq[T]

MapIter returns an iterator that yields transformed elements using the mapper function. Thread-safe. Holds a read lock during iteration.

func (*SyncSet[T]) MarshalJSON

func (s *SyncSet[T]) MarshalJSON() ([]byte, error)

MarshalJSON implements the json.Marshaler interface (thread-safe).

func (*SyncSet[T]) Partition

func (s *SyncSet[T]) Partition(predicate func(T) bool) (ISet[T], ISet[T])

Partition splits the set into two sets based on the given predicate. Thread-safe. Returns non-sync Sets.

func (*SyncSet[T]) Reduce

func (s *SyncSet[T]) Reduce(initial T, reducer func(T, T) T) T

Reduce applies the reducer function to each element in the set. Thread-safe.

func (*SyncSet[T]) Remove

func (s *SyncSet[T]) Remove(elements ...T)

Remove deletes the provided elements from the set. Thread-safe.

func (*SyncSet[T]) Retain

func (s *SyncSet[T]) Retain(predicate func(T) bool)

Retain removes all elements from the set that do NOT satisfy the predicate. Thread-safe.

func (*SyncSet[T]) Size

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

Size returns the number of elements currently in the set. Thread-safe.

func (*SyncSet[T]) String

func (s *SyncSet[T]) String() string

String implements the fmt.Stringer interface. Thread-safe.

func (*SyncSet[T]) SymmetricDifference

func (s *SyncSet[T]) SymmetricDifference(other ISet[T]) ISet[T]

SymmetricDifference returns a new set containing elements that exist in either set, but not in both. Thread-safe. Returns a non-sync Set.

func (*SyncSet[T]) ToSlice

func (s *SyncSet[T]) ToSlice() []T

ToSlice converts the set elements into a slice. Thread-safe. Returns a snapshot of the current elements.

func (*SyncSet[T]) Union

func (s *SyncSet[T]) Union(other ISet[T]) ISet[T]

Union returns a new set containing all elements from both sets. Thread-safe. Returns a non-sync Set.

func (*SyncSet[T]) UnmarshalJSON

func (s *SyncSet[T]) UnmarshalJSON(data []byte) error

UnmarshalJSON implements the json.Unmarshaler interface (thread-safe).

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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