set

package
v0.0.0-...-0718798 Latest Latest
Warning

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

Go to latest
Published: Dec 31, 2025 License: MIT Imports: 8 Imported by: 1

Documentation

Overview

Package set provides an implementation of a set which accepts any values, even if not comparable.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Compare

func Compare[T any](s1, s2 *Set[T]) bool

Compare compares two sets for equality, returning true if they contain all the same elements.

Types

type Set

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

Set holds a collection of unique values.

It makes use of maphash.Hash to allow for adding values of any type, even ones that are uncomparable. The seed is set as a package global to ensure that all sets will produce hashes compatible for comparison.

All methods lazily initialize the underlying storage, so the zero value of Set can be used.

Example
package main

import (
	"fmt"

	"go.chrisrx.dev/x/set"
	"go.chrisrx.dev/x/slices"
)

func main() {
	var s set.Set[int]

	for range 10 {
		s.Add(1)
	}
	s.Add(2)

	fmt.Println(slices.Sort(s.List()))
}
Output:

[1 2]
Example (New)
package main

import (
	"fmt"

	"go.chrisrx.dev/x/set"
	"go.chrisrx.dev/x/slices"
)

func main() {
	s := set.New(1, 2, 2, 3, 3, 3, 4, 5, 5)

	fmt.Println(slices.Sort(s.List()))
}
Output:

[1 2 3 4 5]
Example (Uncomparable)
package main

import (
	"fmt"

	"go.chrisrx.dev/x/set"
	"go.chrisrx.dev/x/slices"
)

func main() {
	var s set.Set[[]byte]

	s.Add([]byte("1"))
	s.Add([]byte("1"))
	s.Add([]byte("2"))
	s.Add([]byte("2"))
	s.Add([]byte("3"))

	fmt.Println(slices.Sort(slices.Map(s.List(), func(elem []byte) string {
		return string(elem)
	})))
}
Output:

[1 2 3]

func New

func New[T any](elems ...T) *Set[T]

New constructs a new set. If any elements are provided the set is initialized with those values.

func Union

func Union[T any](a, b []T) *Set[T]

func (*Set[T]) Add

func (set *Set[T]) Add(elems ...T)

Add adds an element to the set.

func (*Set[T]) All

func (set *Set[T]) All(elems ...T) bool

All checks that the set contains all of the elements provided. If any are not part of the set, this returns false.

func (*Set[T]) Any

func (set *Set[T]) Any(elems ...T) bool

Any checks if the set contains at least one of the provided elements.

func (*Set[T]) Clear

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

Clear removes all values from the set.

func (*Set[T]) Contains

func (set *Set[T]) Contains(elem T) bool

Contains checks if the set contains the provided elements.

func (*Set[T]) Copy

func (set *Set[T]) Copy() *Set[T]

Copy constructs a new set, copying the internal state of this set into the new returned set.

func (*Set[T]) Difference

func (set *Set[T]) Difference(other *Set[T]) *Set[T]

Difference returns a new set containing all of the elements in this set that are not present in the other set.

func (*Set[T]) Each

func (set *Set[T]) Each(fn func(T))

Each applies the provided function to every element of the set.

func (*Set[T]) Equals

func (set *Set[T]) Equals(other *Set[T]) bool

Equals compares all the elements of two sets for equality.

func (*Set[T]) Intersection

func (set *Set[T]) Intersection(other *Set[T]) *Set[T]

Intersection returns a new set made up of all of the elements that are contained in both sets.

func (*Set[T]) IsEmpty

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

IsEmpty checks if the set is empty.

func (*Set[T]) Len

func (set *Set[T]) Len() int

Len returns how many elements are current in the set.

func (*Set[T]) List

func (set *Set[T]) List() []T

List returns all the values of the set as a slice.

func (*Set[T]) MarshalJSON

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

func (*Set[T]) Pop

func (set *Set[T]) Pop() T

Pop removes and returns one value from the set. The element returned is indeterministically selected.

func (*Set[T]) Remove

func (set *Set[T]) Remove(elems ...T)

Removes an element from the set, if present.

func (*Set[T]) Size

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

Size returns how many elements are current in the set. This is an alias to Set.Len

func (*Set[T]) Union

func (set *Set[T]) Union(other *Set[T]) *Set[T]

Union returns a new set made up of all elements from each set.

func (*Set[T]) UnmarshalJSON

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

func (*Set[T]) Values

func (set *Set[T]) Values() iter.Seq[T]

All returns all the values of the set as a sequence.

Jump to

Keyboard shortcuts

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