set

package module
v1.2.4 Latest Latest
Warning

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

Go to latest
Published: Apr 25, 2025 License: MIT Imports: 1 Imported by: 5

README

PkgGoDev License Go Version Tag

CI Go Report Card Maintainability Code Coverage Issues

set

generic set types for golang

features

  • both un-ordered and ordered types
  • union, diff, intersect, comparision for any two sets
  • simple API
  • zero-alloc
  • zero-dependency
  • 100% test coverage

example

import (
    "fmt"

    "github.com/s0rg/set"
)

func main() {
    // create new, empty set of int's
    s := make(set.Unordered[int]) // fastest variant as it direct functions call

    // or

    // second (a bit slower) form for unordered constructor, it uses indirect calls via interface
    // s := set.NewUnordered[int]()

    // ordered constructor, only this form
    // s := set.NewOrdered[int]()

    // add some values
    s.Add(1)
    s.Add(2)

    // and some more...
    set.Load(s, 2, 3)

    // check set for value
    if !s.Has(2) {
        panic("2 not found")
    }

    // check and add
    if s.TryAdd(4) {
        fmt.Println("value 4 wasnt in set, it there now")
    }

    // delete item
    s.Del(1)

    fmt.Println("Set length:", s.Len())
    fmt.Println("Set contents:", s.ToSlice())

    // iter over items
    for i := range s.Iter {
        fmt.Printf("iter: %d\n", i)
    }

    s.Clear()

    fmt.Println("Set length:", s.Len())
    fmt.Println("Set contents:", s.ToSlice())
}

benchmarks

cpu: AMD Ryzen 5 5500U with Radeon Graphics
BenchmarkSetUnorderedDirect/Add-12      7092192        174.1 ns/op      49 B/op          0 allocs/op
BenchmarkSetUnorderedDirect/Has-12      17680040       97.33 ns/op       0 B/op          0 allocs/op
BenchmarkSetUnorderedDirect/Len-12      1000000000    0.2496 ns/op       0 B/op          0 allocs/op
BenchmarkSetUnorderedDirect/Pop-12      6251703         5282 ns/op       0 B/op          0 allocs/op
BenchmarkSetUnorderedDirect/Del-12      561568860      2.145 ns/op       0 B/op          0 allocs/op
BenchmarkSetUnorderedIndirect/Add-12    7479926        193.6 ns/op      46 B/op          0 allocs/op
BenchmarkSetUnorderedIndirect/Has-12    17168162       80.38 ns/op       0 B/op          0 allocs/op
BenchmarkSetUnorderedIndirect/Len-12    802026860      1.496 ns/op       0 B/op          0 allocs/op
BenchmarkSetUnorderedIndirect/Pop-12    6782689         6487 ns/op       0 B/op          0 allocs/op
BenchmarkSetUnorderedIndirect/Del-12    393659181      3.072 ns/op       0 B/op          0 allocs/op
BenchmarkSetOrdered/Add-12              6576442        178.7 ns/op      67 B/op          0 allocs/op
BenchmarkSetOrdered/Has-12              17831434       84.19 ns/op       0 B/op          0 allocs/op
BenchmarkSetOrdered/Len-12              473897580      2.494 ns/op       0 B/op          0 allocs/op
BenchmarkSetOrdered/Pop-12              297457706      4.054 ns/op       0 B/op          0 allocs/op
BenchmarkSetOrdered/Del-12              144014604      8.326 ns/op       0 B/op          0 allocs/op

Documentation

Overview

Package set provides ordered and un-ordered generic sets, and handy operations between them.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Contains added in v1.1.0

func Contains[T comparable](a, b Set[T]) (yes bool)

Contains returns true if smallest of two sets (by length) fully contains inside bigger one, if sets equals in length the result is same as comparison.

func ContainsAny added in v1.1.0

func ContainsAny[T comparable](a, b Set[T]) (yes bool)

ContainsAny returns true if `a` has at least one element from `b`.

func Equal added in v1.1.0

func Equal[T comparable](a, b Set[T]) (yes bool)

Equal returns true if `a` and `b` has same length and elements.

func ToSlice added in v1.2.0

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

ToSlice returns set as slice of items.

Types

type Set

type Set[T comparable] interface {
	// Add adds item to the set.
	Add(val T) bool
	// Has checks if item is already present.
	Has(val T) bool
	// Del removes item, no-op if not present.
	Del(val T)
	// Pop removes and returns an arbitrary item.
	Pop() (val T, ok bool)
	// Len returns current items count.
	Len() int
	// Clone returns shallow copy.
	Clone() Set[T]
	// Iter iterates items until callback returns false.
	Iter(yield func(T) bool)
	// Clear removes all items.
	Clear()
}

Set is the primary interface provided by the set package.

func Diff added in v1.1.0

func Diff[T comparable](a, b Set[T]) (rv Set[T])

Diff returns new set with elements from `a` that arent in `b`.

func Intersect added in v1.1.0

func Intersect[T comparable](a, b Set[T]) (rv Set[T])

Intersect returns new set with keys from `a` that present in `b`.

func Load added in v1.1.0

func Load[T comparable](s Set[T], v ...T) Set[T]

Load populates given set with values.

func NewOrdered added in v1.1.0

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

NewOrdered create empty ordered set for given type.

func NewUnordered added in v1.1.0

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

NewUnordered create empty unordered Set for given type.

func Union added in v1.1.0

func Union[T comparable](a, b Set[T]) (rv Set[T])

Union returns new set with elements from both sets.

type Unordered added in v1.1.0

type Unordered[T comparable] map[T]stub

Unordered is a simple and effective un-ordered generic set.

func (Unordered[T]) Add added in v1.1.0

func (u Unordered[T]) Add(v T) bool

Add implements Set interface.

func (Unordered[T]) Clear added in v1.1.0

func (u Unordered[T]) Clear()

Clear implements Set interface.

func (Unordered[T]) Clone added in v1.1.0

func (u Unordered[T]) Clone() (rv Set[T])

Clone implements Set interface.

func (Unordered[T]) Del added in v1.1.0

func (u Unordered[T]) Del(v T)

Del implements Set interface.

func (Unordered[T]) Has added in v1.1.0

func (u Unordered[T]) Has(v T) (ok bool)

Has implements Set interface.

func (Unordered[T]) Iter added in v1.1.0

func (u Unordered[T]) Iter(it func(T) bool)

Iter implements Set interface.

func (Unordered[T]) Len added in v1.1.0

func (u Unordered[T]) Len() int

Len implements Set interface.

func (Unordered[T]) Pop added in v1.1.0

func (u Unordered[T]) Pop() (v T, ok bool)

Pop implements Set interface.

Jump to

Keyboard shortcuts

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