Skip to content

s0rg/set

Repository files navigation

PkgGoDev License Go Version Tag

CI Go Report Card Maintainability Test 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
  • 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())

    s.Clear()

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