sets

package
v1.12.0-alpha Latest Latest
Warning

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

Go to latest
Published: Mar 2, 2026 License: MIT Imports: 5 Imported by: 0

README

sets
----
set datastructure for go with generics and iterators. the
api is supposed to mimic rust's std::collections::HashSet api.

    s1 := sets.Collect(slices.Values([]int{1, 2, 3, 4}))
    s2 := sets.Collect(slices.Values([]int{1, 2, 3, 4, 5, 6}))

    union     := sets.Collect(s1.Union(s2))
    intersect := sets.Collect(s1.Intersection(s2))
    diff      := sets.Collect(s1.Difference(s2))
    symdiff   := sets.Collect(s1.SymmetricDifference(s2))

    s1.Len()          // 4
    s1.Contains(1)    // true
    s1.IsEmpty()      // false
    s1.IsSubset(s2)   // true
    s1.IsSuperset(s2) // false
    s1.IsDisjoint(s2) // false

    if exists := s1.Insert(1); exists {
        // already existed in set
    }

    if existed := s1.Remove(1); existed {
        // existed in set, now removed
    }


testing
-------
includes property-based tests using the wonderful
testing/quick module!

    go test -v

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Set

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

func Collect

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

func New

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

func Singleton

func Singleton[T comparable](item T) Set[T]

func (Set[T]) All

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

func (*Set[T]) Clear

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

func (Set[T]) Clone

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

func (Set[T]) Contains

func (s Set[T]) Contains(item T) bool

func (Set[T]) Difference

func (s Set[T]) Difference(other Set[T]) iter.Seq[T]

func (Set[T]) Equal

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

func (Set[T]) Generate

func (_ Set[T]) Generate(rand *rand.Rand, size int) reflect.Value

func (*Set[T]) Insert

func (s *Set[T]) Insert(item T) bool

func (Set[T]) Intersection

func (s Set[T]) Intersection(other Set[T]) iter.Seq[T]

func (Set[T]) IsDisjoint

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

func (Set[T]) IsEmpty

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

func (Set[T]) IsSubset

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

func (Set[T]) IsSuperset

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

func (Set[T]) Len

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

func (*Set[T]) Remove

func (s *Set[T]) Remove(item T) bool

func (Set[T]) SymmetricDifference

func (s Set[T]) SymmetricDifference(other Set[T]) iter.Seq[T]

func (Set[T]) Union

func (s Set[T]) Union(other Set[T]) iter.Seq[T]

Source Files

  • gen.go
  • set.go

Jump to

Keyboard shortcuts

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