Documentation ¶
Overview ¶
Package sets provides several implementation of a Set and related helpers.
Index ¶
- Variables
- type Set
- func (set Set[C]) Add(elements ...C)
- func (set Set[C]) Contains(elements ...C) bool
- func (set Set[C]) Difference(other Set[C]) Set[C]
- func (set Set[C]) DifferenceWith(elements ...C) Set[C]
- func (set Set[C]) Elements() []C
- func (set Set[C]) Intersection(other Set[C]) Set[C]
- func (set Set[C]) IntersectionWith(elements ...C) Set[C]
- func (set Set[C]) Len() int
- func (set Set[T]) MarshalJSON() ([]byte, error)
- func (set Set[T]) MarshalYAML() (interface{}, error)
- func (set Set[C]) Remove(elements ...C)
- func (set Set[C]) String() string
- func (set Set[C]) ThreeWay(other Set[C]) [3]Set[C]
- func (set Set[C]) Union(other Set[C]) Set[C]
- func (set Set[C]) UnionWith(elements ...C) Set[C]
- func (set *Set[T]) UnmarshalJSON(data []byte) error
- func (set *Set[T]) UnmarshalYAML(value *yaml.Node) error
- type TinySet
- func (tinySet *TinySet[C]) Add(elements ...C)
- func (tinySet TinySet[C]) Contains(elements ...C) bool
- func (tinySet TinySet[C]) Difference(other TinySet[C]) TinySet[C]
- func (tinySet TinySet[C]) DifferenceWith(elements ...C) TinySet[C]
- func (tinySet TinySet[C]) Elements() []C
- func (tinySet TinySet[C]) Intersection(other TinySet[C]) TinySet[C]
- func (tinySet TinySet[C]) IntersectionWith(elements ...C) TinySet[C]
- func (tinySet TinySet[C]) Len() int
- func (tinySet TinySet[T]) MarshalJSON() ([]byte, error)
- func (tinySet TinySet[T]) MarshalYAML() (interface{}, error)
- func (tinySet *TinySet[C]) Remove(elements ...C)
- func (tinySet TinySet[C]) String() string
- func (tinySet TinySet[C]) ThreeWay(other TinySet[C]) [3]TinySet[C]
- func (tinySet TinySet[C]) Union(other TinySet[C]) TinySet[C]
- func (tinySet TinySet[C]) UnionWith(elements ...C) TinySet[C]
- func (tinySet *TinySet[T]) UnmarshalJSON(data []byte) error
- func (tinySet *TinySet[T]) UnmarshalYAML(value *yaml.Node) error
- type Unit
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var U = api.U
U is the unit value used to populate the set.
Functions ¶
This section is empty.
Types ¶
type Set ¶
type Set[C comparable] map[C]Unit
Set uses a go map to represent a set of elements of type C as keys with no value.
Example ¶
package main import ( "fmt" "sort" "github.com/toolvox/utilgo/pkg/sets" ) func main() { // Initialize a new set of integers s := sets.NewSet(1, 2, 3) // Add an element to the set s.Add(4) var resultValues []int = []int{1, 2, 3, 4} // Display the set fmt.Println("Set:", resultValues) // Create another set other := sets.NewSet(3, 4, 5) // UnionWith of s and other UnionWith := s.Union(other) resultValues = UnionWith.Elements() sort.Ints(resultValues) fmt.Println("UnionWith:", resultValues) // IntersectionWith of s and other IntersectionWith := s.Intersection(other) resultValues = IntersectionWith.Elements() sort.Ints(resultValues) fmt.Println("IntersectionWith:", resultValues) // DifferenceWith of s and other DifferenceWith := s.Difference(other) resultValues = DifferenceWith.Elements() sort.Ints(resultValues) fmt.Println("DifferenceWith:", resultValues) // Check if an element is in the set fmt.Println("Contains 3:", s.Contains(3)) fmt.Println("Contains 6:", s.Contains(6)) }
Output: Set: [1 2 3 4] UnionWith: [1 2 3 4 5] IntersectionWith: [3 4] DifferenceWith: [1 2] Contains 3: true Contains 6: false
func NewSet ¶
func NewSet[C comparable](elements ...C) Set[C]
NewSet initializes a new Set with the given elements, ensuring uniqueness.
func (Set[C]) Add ¶
func (set Set[C]) Add(elements ...C)
Add unique elements to the Set. Repeated elements will be discarded.
func (Set[C]) Difference ¶
Difference creates a set of elements in the first set but not in the second.
func (Set[C]) DifferenceWith ¶
DifferenceWith removes specified elements from the set.
func (Set[C]) Elements ¶
func (set Set[C]) Elements() []C
Elements returns the unique elements of the Set.
func (Set[C]) Intersection ¶
Intersection creates a set of elements common to both sets.
func (Set[C]) IntersectionWith ¶
IntersectionWith forms a set from common elements of the set and the provided elements.
func (Set[T]) MarshalJSON ¶
MarshalJSON converts the Set to a JSON array.
func (Set[T]) MarshalYAML ¶
MarshalYAML converts the Set to a YAML array.
func (Set[C]) Remove ¶
func (set Set[C]) Remove(elements ...C)
Remove any existing elements from the Set.
func (Set[C]) ThreeWay ¶
ThreeWay splits elements into three sets: common, only in the first set, and only in the second set.
func (Set[C]) UnionWith ¶
UnionWith adds multiple elements to the set and returns the resulting set.
func (*Set[T]) UnmarshalJSON ¶
UnmarshalJSON converts the JSON []byte to a Set.
type TinySet ¶
type TinySet[C comparable] []C
TinySet represents a set of elements of type C using a slice for storage. It is optimized for small sets where the overhead of a map is not justified.
func NewTinySet ¶
func NewTinySet[C comparable](elements ...C) *TinySet[C]
NewTinySet initializes a new TinySet with the given elements, ensuring uniqueness.
func (*TinySet[C]) Add ¶
func (tinySet *TinySet[C]) Add(elements ...C)
Add unique elements to the TinySet. Repeated elements will be discarded.
func (TinySet[C]) Difference ¶
Difference returns a new TinySet containing elements present in this set but not in the other set.
func (TinySet[C]) DifferenceWith ¶
DifferenceWith returns a new TinySet containing elements present in this set but not among the provided elements.
func (TinySet[C]) Elements ¶
func (tinySet TinySet[C]) Elements() []C
Elements returns the unique elements of the TinySet.
func (TinySet[C]) Intersection ¶
Intersection returns a new TinySet containing elements that exist in both sets.
func (TinySet[C]) IntersectionWith ¶
IntersectionWith returns a new TinySet containing elements that exist in both this set and the provided elements.
func (TinySet[T]) MarshalJSON ¶
MarshalJSON converts the TinySet to a JSON array.
func (TinySet[T]) MarshalYAML ¶
MarshalYAML converts the TinySet to a YAML array.
func (*TinySet[C]) Remove ¶
func (tinySet *TinySet[C]) Remove(elements ...C)
Remove any existing elements from the TinySet.
func (TinySet[C]) ThreeWay ¶
ThreeWay splits elements into three [TinySets]: common to both, only in the first, and only in the second.
func (TinySet[C]) Union ¶
Union merges the current set with another set and returns the result as a new TinySet.
func (TinySet[C]) UnionWith ¶
UnionWith adds multiple elements to the set and returns the resulting set. This operation does not modify the original set but returns a new one.
func (*TinySet[T]) UnmarshalJSON ¶
UnmarshalJSON converts the JSON []byte to a TinySet.