set

package module
v0.0.0-...-fa89c1c Latest Latest
Warning

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

Go to latest
Published: Feb 20, 2025 License: MIT Imports: 4 Imported by: 0

README

go-set

A set type to use in my Go projects. The set is not threadsafe so synchronization is requred if reading/writing to the set from multiple goroutines.

Usage:

// Create a new empty set of type `string`
s1 := set.New[string]()

// Insert a single item to the set:
s1.Insert("hello")

// Insert multiple items to a set:
s1.Insert("this", "is", "a", "set")

// or from a slice:
s1.Insert([]string{"this", "is", "a", "set"}...)


//A set can also be created from multiple items:
s2 := set.From("hello", "world")

// Remove an item from the set - returns true if the item existed:
removed := s2.Remove("world")

// Create a union of two sets as a new set:
union := s1.Union(s2)

// Get the difference of two sets as a new set:
diff := s1.Difference(s2)

// Get the intersection of the two sets as a new set:
common := s1.Intersect(s2)

// Check if the set contains an item:
contains := s1.Contains("hello")

// Get a slice of all of the items of the set
items := s1.ToSlice()

// Range over all items in the set:
s1.Range(func (s string) bool {
    if s == "hello" {
        return false
    }
    fmt.Println(s)
    return true
})

// Get an iter.Seq[T] of the items in the set:
iter := s.Iter()

// Get the current size (cardinality) of the set:
s1.Size()

// Clear all items in the set:
s1.Clear()


// If type T of Set[T] is orderable, you can get a slice of sorted items from a set:
intSet := set.From(5, 4, 3, 2, 1)
sorted := set.SortedItems(intSet)
// sorted: [1, 2, 3, 4, 5]

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func SortedItems

func SortedItems[T cmp.Ordered](s Set[T]) []T

Types

type Set

type Set[T comparable] map[T]empty

func From

func From[T comparable](items ...T) Set[T]

From creates a new set of one or more items of type T

func New

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

New[T] creates a new empty set of type T

func (Set[T]) Clear

func (s Set[T]) Clear()

func (Set[T]) Contains

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

Contains returns true if item of type T is present in the set.

func (Set[T]) Difference

func (s Set[T]) Difference(s2 Set[T]) Set[T]

Difference returns a new set of type T containing only the items uncommon bewteen the two sets.

func (Set[T]) Insert

func (s Set[T]) Insert(items ...T)

Insert adds one or more items of type T to the set.

func (Set[T]) Intersect

func (s Set[T]) Intersect(s2 Set[T]) Set[T]

Intersect returns a new set of type T containing only the items common between the two sets.

func (Set[T]) Iter

func (s Set[T]) Iter() iter.Seq[T]

Iter is an iterator over the set of items of type T

func (Set[T]) Range

func (s Set[T]) Range(cb func(T) bool)

Range loops over the set and calls the provided callback. If the callback returns true, iteration continues. If the callback returns false, iteration stops.

func (Set[T]) Remove

func (s Set[T]) Remove(item T) bool

Remove removes an item of type T from the set and returns true if the item existed. Remove returns false if the item did not exist in the set.

func (Set[T]) Size

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

func (Set[T]) ToSlice

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

ToSlice returns an unordered slice of the sets items of type T

func (Set[T]) Union

func (s Set[T]) Union(s2 Set[T]) Set[T]

Union combines the items of two different sets of type T into a single new set

Jump to

Keyboard shortcuts

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