sets

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Nov 20, 2023 License: MIT Imports: 1 Imported by: 1

README

Go Sets!

A minimal set library for golang. This tinylib eschews any dependencies and implements common, optimized set operations on type Set[E comparable] map[E]struct{}

Go Reference Go Report Card CI

Supported set operations:

  • Union: A⋃B
  • Intersection: A⋂B
  • Difference: A-B

Supported checks:

  • Subset: A⊆B
  • Superset: A⊇B
  • Equality: A=B

Usage

package main

import (
  "fmt"

  "github.com/R167/go-sets"
)

func main() {
  a := sets.New(1, 2, 3)
  b := sets.New(3, 4, 5)

  fmt.Println(a.Union(b)) // {1, 2, 3, 4, 5}
  fmt.Println(a.Intersect(b)) // {3}
  fmt.Println(a.Difference(b)) // {1, 2}

  a2 := sets.New(1, 2)
  fmt.Println(a2.Subset(a)) // true
  fmt.Println(a.Subset(a2)) // false
  fmt.Println(a.Equal(b)) // false
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Set

type Set[E comparable] map[E]empty

Set is a set of comparable elements of type E. Note since Set is backed by a map, the zero value of Set is not an empty set. Use New[E]() to create an empty set.

Set is not thread-safe.

func FromMap

func FromMap[E comparable, T any, M ~map[E]T](m M) Set[E]

FromMap returns a new Unordered set containing the keys of the given map.

func New

func New[E comparable](elements ...E) Set[E]

New returns a new set containing the given elements.

func (Set[E]) Add added in v0.2.0

func (s Set[E]) Add(e ...E) Set[E]

Add the given element to s and returns itself.

func (Set[E]) Clone

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

Clone returns a shallow copy of s.

func (Set[E]) Delete

func (s Set[E]) Delete(e E) bool

Delete the given element from s. Returns true if the element was in s. If you need to remove multiple elements at once, use Subtract.

func (Set[E]) Difference

func (s Set[E]) Difference(other Set[E]) Set[E]

Difference returns a new set containing the elements in s that are not in other.

s := New[int](1, 2, 3)
other := New[int](3, 4, 5)
sMinusOther := s.Difference(other)
// contains 1, 2
otherMinusS := other.Difference(s)
// contains 4, 5

func (Set[E]) Equal

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

Equal returns true if s and other contain all the same elements.

func (Set[E]) Has

func (s Set[E]) Has(e E) bool

Has returns true if e is in s.

func (Set[E]) Intersection

func (s Set[E]) Intersection(other Set[E]) Set[E]

Intersection returns a new set containing the elements in both s and other.

s := New[int](1, 2, 3)
other := New[int](3, 4, 5)
intersection := s.Intersection(other)
// intersection contains 3

func (Set[E]) Slice

func (s Set[E]) Slice() []E

Slice returns a slice of all the elements in s.

Note: The order of the output elements is undefined.

func (Set[E]) String

func (s Set[E]) String() string

String returns a string representation of s.

func (Set[E]) Subset

func (s Set[E]) Subset(other Set[E]) bool

Subset returns true if s is a subset of other.

func (Set[E]) Subtract added in v0.2.0

func (s Set[E]) Subtract(e ...E) Set[E]

Subtract removes multipe elements from s and returns itself

func (Set[E]) Superset

func (s Set[E]) Superset(other Set[E]) bool

Superset returns true if s is a superset of other.

func (Set[E]) Union

func (s Set[E]) Union(other Set[E]) Set[E]

Union returns a new set containing all the elements in s and other.

s := New[int](1, 2, 3)
other := New[int](3, 4, 5)
union := s.Union(other)
// union contains 1, 2, 3, 4, 5

Jump to

Keyboard shortcuts

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