set

package module
v2.0.0 Latest Latest
Warning

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

Go to latest
Published: Aug 23, 2023 License: MIT Imports: 1 Imported by: 0

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 map functions call

    // or

    // second (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

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

Contains returns true if `a` has all elements from `b`.

func ContainsAny

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

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

func Equal

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

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

Types

type Set

type Set[T comparable] interface {
	// Add adds item to the set.
	Add(T)
	// Del removes item, no-op if not present.
	Del(T)
	// Has checks if item is already present.
	Has(T) bool
	// TryAdd takes attempt to add item, returns false if it already there.
	TryAdd(T) bool
	// 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.
	Iter(func(T) bool)
	// ToSlice returns set as slice of items.
	ToSlice() []T
	// Clear removes all items.
	Clear()
}

Set is the primary interface provided by the set package.

func Diff

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

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

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

Load populates given set with values.

func NewOrdered

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

NewOrdered create empty ordered set for given type.

func NewUnordered

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

NewUnordered create empty unordered Set for given type.

func Union

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

Union returns new set with elements from both sets.

type Unordered

type Unordered[T comparable] map[T]stub

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

func (Unordered[T]) Add

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

Add implements Set interface.

func (Unordered[T]) Clear

func (u Unordered[T]) Clear()

Clear implements Set interface.

func (Unordered[T]) Clone

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

Clone implements Set interface.

func (Unordered[T]) Del

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

Del implements Set interface.

func (Unordered[T]) Has

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

Has implements Set interface.

func (Unordered[T]) Iter

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

Iter implements Set interface.

func (Unordered[T]) Len

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

Len implements Set interface.

func (Unordered[T]) Pop

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

Pop implements Set interface.

func (Unordered[T]) ToSlice

func (u Unordered[T]) ToSlice() (rv []T)

ToSlice implements Set interface.

func (Unordered[T]) TryAdd

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

TryAdd implements Set interface.

Jump to

Keyboard shortcuts

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