set

package module
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Aug 26, 2025 License: MIT Imports: 3 Imported by: 1

README

set

Go Reference Go Report Card codecov

A simple and efficient Go package providing a generic set data structure. Supports fast membership checks, addition, removal, and common set operations for any comparable type. Ideal for use cases requiring unique collections and mathematical set logic.

Install

Run go get github.com/jokruger/set

License

This project is licensed under the MIT License. See the LICENSE file for details.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Option

type Option func(*Options)

func WithCapacity

func WithCapacity(capacity int) Option

type Options

type Options struct {
	// contains filtered or unexported fields
}

type Set

type Set[T comparable] map[T]struct{}

Set is a set of elements.

func New

func New[T comparable](options ...Option) Set[T]

New creates a new set with the given options.

func NewFromElements

func NewFromElements[T comparable](elements ...T) Set[T]

NewFromElements creates a new set with the given elements.

func NewFromMapKeys added in v1.0.2

func NewFromMapKeys[T comparable, V any](m map[T]V) Set[T]

NewFromMapKeys creates a new set with the keys of the map.

func NewFromMapValues added in v1.0.2

func NewFromMapValues[T comparable, V comparable](m map[T]V) Set[V]

NewFromMapValues creates a new set with the values of the map.

func NewFromSeq

func NewFromSeq[T comparable](source iter.Seq[T]) Set[T]

NewFromSeq creates a new set with the elements of the sequence.

func NewFromSet

func NewFromSet[T comparable](source Set[T]) Set[T]

NewFromSet creates a new set with the elements of the other set.

func NewFromSlice

func NewFromSlice[T comparable](source []T) Set[T]

NewFromSlice creates a new set with the elements of the slice.

func (Set[T]) Add

func (s Set[T]) Add(element T) bool

Add adds the element to the set. Returns true if the element was added. Returns false if the element was already in the set.

func (Set[T]) AddMany

func (s Set[T]) AddMany(elements ...T) Set[T]

AddMany adds the elements to the set. Modifies the set in place. Returns the set itself.

func (Set[T]) AddSeq

func (s Set[T]) AddSeq(source iter.Seq[T]) Set[T]

AddSeq adds the elements of the sequence to the set. Modifies the set in place. Returns the set itself.

func (Set[T]) AddSet

func (s Set[T]) AddSet(source Set[T]) Set[T]

AddSet adds the elements of the other set to the set. Modifies the set in place. Returns the set itself.

func (Set[T]) AddSlice

func (s Set[T]) AddSlice(source []T) Set[T]

AddSlice adds the elements of the slice to the set. Modifies the set in place. Returns the set itself.

func (Set[T]) All

func (s Set[T]) All(f func(T) bool) bool

All returns true if all elements in set satisfy the predicate f. If set is empty, All returns true.

func (Set[T]) Any

func (s Set[T]) Any(f func(T) bool) bool

Any returns true if any element in set satisfies the predicate, false otherwise. If set is empty, Any returns false.

func (Set[T]) Clear

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

Clear removes all elements from the set. Modifies the set in place. Returns the set itself.

func (Set[T]) Clone

func (s Set[T]) Clone() Set[T]

Clone returns a copy of the set.

func (Set[T]) Contains

func (s Set[T]) Contains(elements ...T) bool

Contains returns true if the set contains all elements.

func (Set[T]) ContainsAny

func (s Set[T]) ContainsAny(elements ...T) bool

ContainsAny returns true if the set contains any of the elements.

func (Set[T]) ContainsAnyFromSeq

func (s Set[T]) ContainsAnyFromSeq(source iter.Seq[T]) bool

ContainsAnyFromSeq returns true if the set contains any of the elements of the sequence.

func (Set[T]) ContainsAnyFromSet

func (s Set[T]) ContainsAnyFromSet(other Set[T]) bool

ContainsAnyFromSet returns true if the set contains any of the elements of the other set.

func (Set[T]) ContainsAnyFromSlice

func (s Set[T]) ContainsAnyFromSlice(source []T) bool

ContainsAnyFromSlice returns true if the set contains any of the elements of the slice.

func (Set[T]) ContainsSeq

func (s Set[T]) ContainsSeq(source iter.Seq[T]) bool

ContainsSeq returns true if the set contains all elements of the sequence.

func (Set[T]) ContainsSet

func (s Set[T]) ContainsSet(other Set[T]) bool

ContainsSet returns true if the set contains all elements of the other set.

func (Set[T]) ContainsSlice

func (s Set[T]) ContainsSlice(source []T) bool

ContainsSlice returns true if the set contains all elements of the slice.

func (Set[T]) Count

func (s Set[T]) Count(f func(T) bool) int

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

func (Set[T]) Diff

func (s Set[T]) Diff(other Set[T]) Set[T]

Diff returns the difference between the set and another set.

func (Set[T]) Equal

func (s Set[T]) Equal(other Set[T]) bool

Equal returns true if the set is equal to another set.

func (Set[T]) Filter

func (s Set[T]) Filter(f func(T) bool) Set[T]

Filter returns a new set containing only the elements that satisfy the predicate f.

func (Set[T]) ForEach added in v1.0.2

func (s Set[T]) ForEach(f func(T))

ForEach executes the function f for each element in the set.

func (Set[T]) IsEmpty

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

IsEmpty returns true if the set is empty.

func (Set[T]) IsProperSubsetOf

func (s Set[T]) IsProperSubsetOf(other Set[T]) bool

IsProperSubsetOf returns true if the set is a proper subset of the other set.

func (Set[T]) IsProperSupersetOf

func (s Set[T]) IsProperSupersetOf(other Set[T]) bool

IsProperSupersetOf returns true if the set is a proper superset of the other set.

func (Set[T]) IsSubsetOf

func (s Set[T]) IsSubsetOf(other Set[T]) bool

IsSubsetOf returns true if the set is a subset of the other set.

func (Set[T]) IsSupersetOf

func (s Set[T]) IsSupersetOf(other Set[T]) bool

IsSupersetOf returns true if the set is a superset of the other set.

func (Set[T]) Len

func (s Set[T]) Len() int

Len returns the number of elements in the set.

func (Set[T]) None added in v1.0.2

func (s Set[T]) None(f func(T) bool) bool

None returns true if no elements in set satisfy the predicate f.

func (Set[T]) Remove

func (s Set[T]) Remove(element T) bool

Remove removes the element from the set. Returns true if the element was removed. Returns false if the element was not in the set.

func (Set[T]) RemoveMany

func (s Set[T]) RemoveMany(elements ...T) Set[T]

RemoveMany removes the elements from the set. Modifies the set in place. Returns the set itself.

func (Set[T]) RemoveSeq

func (s Set[T]) RemoveSeq(source iter.Seq[T]) Set[T]

RemoveSeq removes the elements of the sequence from the set. Modifies the set in place. Returns the set itself.

func (Set[T]) RemoveSet

func (s Set[T]) RemoveSet(source Set[T]) Set[T]

RemoveSet removes the elements of the other set from the set. Modifies the set in place. Returns the set itself.

func (Set[T]) RemoveSlice

func (s Set[T]) RemoveSlice(source []T) Set[T]

RemoveSlice removes the elements of the slice from the set. Modifies the set in place. Returns the set itself.

func (Set[T]) SymmetricDiff

func (s Set[T]) SymmetricDiff(other Set[T]) Set[T]

SymmetricDiff returns the symmetric difference between the set and another set.

func (Set[T]) ToSeq

func (s Set[T]) ToSeq() iter.Seq[T]

func (Set[T]) ToSlice

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

func (Set[T]) Union

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

Union returns the union of the set and another set.

Jump to

Keyboard shortcuts

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