set

package module
v0.8.0 Latest Latest
Warning

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

Go to latest
Published: May 2, 2025 License: MIT Imports: 2 Imported by: 0

README

set

Go Reference

This module provides a Go implementation of a generic set data type along with a small library of common operations.

Installation:

go get -u github.com/jaz303/set

Usage Examples

Create a new empty set
// Standard make() function works fine
s := make(set.Set[int])

// Capacity hint can be specified
s := make(set.Set[int], 3000)

// The Make helper function saves a couple of keystrokes...
// (capacity is optional parameter)
s := set.Make[int]()

// ...and can also be used as a function parameter if necessary:
config := Config{
    SetFactory: set.Make[int],
}
Create a set from known values
s1 := set.Of("foo", "bar", "baz")
s2 := set.OfSlice([]string{"foo", "bar", "baz"})
Query the set
s.Empty() // returns true if s is empty, false otherwise
s.Size() // return number of items in s
s.Contains(1) // return true if s contains item, false otherwise
s.ContainsSlice([]int{1,2,3}) // returns true if s contains all items in slice, false otherwise
s.Items() // returns a slice of all items in s

A set is implemented as a map[T]struct{} so the standard length/iteration operations are of course available:

len(s) // return number of items in s

// iterate over s's contents
for v := range s {

}
Add/remove items
s.Add(1) // add a single item
s.AddSlice([]int{1,2,3}) // add all items from slice
s.AddSet(set.Of(4,5,6)) // add all items from other set

s.Remove(1) // remove a single item
s.RemoveSlice([]int{1,2,3}) // remove all items in slice
s.RemoveSet(set.Of(4,5,6)) // remove all items in other set

s.Clear() // remove all items from set
Set operations
set.Union(a, b) // returns a new set containing the union of sets a and b
set.Intersection(a, b) // returns a new set containing the intersection of sets a and b
set.Difference(a, b) // returns a new set containing the difference between sets a and b (i.e. a - b)

JSON Support

The Set type implements the json.Marshaler and json.Unmarshaler interfaces; sets are encoded as JSON arrays.

Unmarshalling into a non-empty set produces the union of the existing and unmarshalled data.

Documentation

Overview

Package set provides a generic set implementation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AddMapKeys added in v0.6.0

func AddMapKeys[K comparable, V any](dst Set[K], src map[K]V)

func AddMapValues added in v0.6.0

func AddMapValues[K comparable, V comparable](dst Set[V], src map[K]V)

Types

type Set

type Set[T comparable] map[T]struct{}

Set implements a set - an unordered collection of items wherein each item is unique.

The underlying storage mechanism is a map of set items to struct{}. To create a set, use make():

set := make(Set[int])

func Difference

func Difference[T comparable](a, b Set[T]) Set[T]

Difference returns a new set representing the difference of sets a and b; that is, those items which are members of set a but not of set b.

func Intersection

func Intersection[T comparable](a, b Set[T]) Set[T]

Intersection returns a new set representing the intersection of sets a and b; that is, those items which are members of both set a and set b.

func Make added in v0.4.0

func Make[T comparable](capacity ...int) Set[T]

Make returns an empty set of T

func Of

func Of[T comparable](vs ...T) Set[T]

Of returns a set comprising the specified items.

func OfSeq added in v0.8.0

func OfSeq[T comparable](s iter.Seq[T]) Set[T]

OfSeq returns a set comprising all items in the specified sequence.

func OfSlice added in v0.5.0

func OfSlice[T comparable](vs []T) Set[T]

OfSlice returns a set comprising all items in the specified slice.

func Union

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

Union returns a new set representing the union of sets a and b; that is, those items which are members of either set a or set b.

func (Set[T]) Add

func (s Set[T]) Add(v T)

Add adds v to the set.

func (Set[T]) AddSet

func (s Set[T]) AddSet(vs Set[T])

AddSet adds all items in set vs to the set.

func (Set[T]) AddSlice

func (s Set[T]) AddSlice(vs []T)

AddSlice adds all elements of vs to the set.

func (Set[T]) Clear

func (s Set[T]) Clear()

Clear removes all items from the set.

func (Set[T]) Contains

func (s Set[T]) Contains(v T) bool

Contains returns true if the set contains v, false otherwise.

func (Set[T]) ContainsSlice

func (s Set[T]) ContainsSlice(vs []T) bool

ContainsSlice returns true is the set contains all elements of vs, false otherwise.

func (Set[T]) Empty

func (s Set[T]) Empty() bool

Empty returns true if the set is empty, false otherwise.

func (Set[T]) Items

func (s Set[T]) Items() []T

Items returns a slice of all items in the set.

func (Set[T]) MarshalJSON added in v0.3.0

func (s Set[T]) MarshalJSON() ([]byte, error)

func (Set[T]) Remove

func (s Set[T]) Remove(v T)

Remove removes v from the set.

func (Set[T]) RemoveSet

func (s Set[T]) RemoveSet(vs Set[T])

RemoveSet removes all items in vs from the set.

func (Set[T]) RemoveSlice

func (s Set[T]) RemoveSlice(vs []T)

RemoveSlice removes all elements of vs from the set.

func (Set[T]) Size

func (s Set[T]) Size() int

Size returns the number of items in the set.

func (*Set[T]) UnmarshalJSON added in v0.3.0

func (s *Set[T]) UnmarshalJSON(b []byte) error

Jump to

Keyboard shortcuts

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