Skip to content

drkennetz/set

Repository files navigation

set

GitHub go.mod Go version GitHub Workflow Status Issues

Provides a set data structure for Go.

A set can be instantiated from any comparable type.

Usage

The current implementation of set is not concurrency safe, although this addition is welcome / will be added as I continue to develop.

Set Basics

mySet := set.NewSet[int]()
// alternatively, from a slice
// mySet := NewSetFromSlice[int]([]int{1, 2}) 
mySet.Add(1)
mySet.Add(2)
// Remove 2 from set
mySet.Remove(2)
// Check if set contains 1
mySet.Contains(1) // true
mySet.Contains(2) // false
// Length of set
mySet.Len() // 1
// Clear the set
mySet.Clear()
mySet.Len() // 0
// Pop an arbitrary element from the set
mySet.Pop() // 0
mySet.Add(1)
a := mySet.Pop() // 1, 1 is removed from set

Common Set Operations

setA := set.NewSet[int]()
setA.Add(1)
setA.Add(2)
setA.Add(3)

setB := set.NewSet[int]()
setB.Add(3)
setB.Add(4)
setB.Add(5)

setC := setA.Union(setB) // {1, 2, 3, 4, 5}
setD := setA.Intersect(setB) // {3}
setE := setA.Difference(setB) // {1, 2}
setF := setA.SymmetricDifference(setB) // {1, 2, 4, 5}

Common Set Helpers

setA := set.NewSet[int]()
setA.Add(3)

setB := set.NewSet[int]()
setB.Add(3)
setB.Add(4)
setB.Add(5)

// Checks if setA is a subset of setB (setA ⊆ setB)
setA.IsSubset(setB) // true
// Checks if setA is a superset of setB (setA ⊇ setB)
setA.IsSuperset(setB) // false
// Checks if setA is disjoint from setB (setA ∩ setB = ∅)
setA.IsDisjoint(setB) // false
// Checks if setA is equal to setB (setA == setB)
setA.IsEqual(setB) // false
// Checks if setA is empty (|setA| = 0)
setA.IsEmpty() // false
// Converts setA to a slice, arbitrary order
setA.ToSlice() // []int{3}
// Copies setA to a new set
setC := setA.Copy() // {3}
// String representation of setA
setA.String() // "{3}"

Slightly More Advanced Set Methods

setA := set.NewSet[string]()
setA.Add("cat")
setA.Add("dog")
setA.Add("fish")
lenThree := func(s string) bool {
	return len(s) == 3
}
// Filter setA by lenThree
setB := setA.Filter(lenThree) // {"cat", "dog"}
// Map setA to a new set
setC := setA.Map(func(s string) string {
	return s + "s"
}) // {"cats", "dogs", "fishs"}
// Reduce setA to a single value
setD := setA.Reduce(func(s1, s2 string) string {
	return s1 + s2
}) // "catdogfish"
// Check if all elements in setA satisfy a predicate
setA.All(func(s string) bool {
	return len(s) > 1
}) // true
// Check if any elements in setA satisfy a predicate
setA.Any(func(s string) bool {
	return len(s) > 3
}) // true

Contributing

Please follow the Contributing Guidelines when contributing to this project.

Issues

To report a bug, request a new feature, improve documentation, optimize code, improve tests, or suggest design improvements, please open the relevant issue type. If none of the issue types seem to match your needs, please open them as a feature request.

License

This project is licensed under the Apache License 2.0.

Code of Conduct

Please note that github.com/drkennetz/set has a Code of Conduct, and that all interactions with this project are governed by it. By participating in this project you agree to abide by its terms. Failure to do so may result in disciplinary action up to and including being blocked from contributing to the project.

About

defines the set data structure in go

Resources

License

Code of conduct

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages