set

package module
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Aug 24, 2023 License: MIT Imports: 1 Imported by: 2

README

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())
}

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(T) bool
	// Has checks if item is already present.
	Has(T) bool
	// Del removes item, no-op if not present.
	Del(T)
	// Pop removes and returns an arbitrary item.
	Pop() (v 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(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