set

package
v0.0.6 Latest Latest
Warning

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

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

Documentation

Overview

Package set provides an implementation of a set, meaning a collection of unique, unordered values.

It implements basic collection options as add, remove, contains, len and clear as well as common set operations like union, intersection and difference.

The implementation is based on normal go map, thus is not thread-safe.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Set

type Set[T comparable] struct {
	// contains filtered or unexported fields
}

Set implements a collection of unique, unordered values.

It is not thread-safe.

func DifferenceOf

func DifferenceOf[T comparable](sets ...Set[T]) Set[T]

DifferenceOf creates a new set that contains all values from the first set which are not contained in any of the other sets.

Example
package main

import (
	"fmt"

	"github.com/KrischanCS/go-toolbox/set"
)

func main() {
	setA := set.Of(1, 2, 3, 4)
	setB := set.Of(3, 6)
	setC := set.Of(4, 7)

	diff := set.DifferenceOf(setA, setB)
	fmt.Println("A - B:", diff)
	fmt.Println("Original is not modified:", setA)

	fmt.Println()

	diff = set.DifferenceOf(setA, setB, setC)
	fmt.Println("A - B - C:", diff)

}
Output:

A - B: (Set[int]: [1 2 4])
Original is not modified: (Set[int]: [1 2 3 4])

A - B - C: (Set[int]: [1 2])

func IntersectionOf

func IntersectionOf[T comparable](sets ...Set[T]) Set[T]

IntersectionOf creates a new set that contains the values which are present in all given sets.

Example
package main

import (
	"fmt"

	"github.com/KrischanCS/go-toolbox/set"
)

func main() {
	setA := set.Of(1, 2, 3, 4)
	setB := set.Of(3, 2, 5)
	setC := set.Of(3, 1, 5)

	fmt.Println("A ∩ B:", set.IntersectionOf(setA, setB))
	fmt.Println("B ∩ C:", set.IntersectionOf(setB, setC))
	fmt.Println("C ∩ A:", set.IntersectionOf(setC, setA))

	fmt.Println("A ∩ B ∩ C:", set.IntersectionOf(setA, setB, setC))

	fmt.Println()

	fmt.Println("Original sets are not modified:")
	fmt.Println("A:", setA)
	fmt.Println("B:", setB)
	fmt.Println("C:", setC)

}
Output:

A ∩ B: (Set[int]: [2 3])
B ∩ C: (Set[int]: [3 5])
C ∩ A: (Set[int]: [1 3])
A ∩ B ∩ C: (Set[int]: [3])

Original sets are not modified:
A: (Set[int]: [1 2 3 4])
B: (Set[int]: [2 3 5])
C: (Set[int]: [1 3 5])

func Of

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

Of creates a new set with the given values.

func UnionOf

func UnionOf[T comparable](sets ...Set[T]) Set[T]

UnionOf creates a new set that contains all values from the given sets.

Example
package main

import (
	"fmt"

	"github.com/KrischanCS/go-toolbox/set"
)

func main() {
	setA := set.Of(1, 2, 3, 4)
	setB := set.Of(3, 6)
	setC := set.Of(4, 7)

	fmt.Println("A ∪ B:", set.UnionOf(setA, setB))
	fmt.Println("B ∪ C:", set.UniqueOf(setB, setC))
	fmt.Println("C ∪ A:", set.UnionOf(setC, setA))
	fmt.Println("A ∪ B ∪ C:", set.UnionOf(setA, setB, setC))

	fmt.Println()

	fmt.Println("Originals are not modified:")
	fmt.Println("A:", setA)
	fmt.Println("B:", setB)
	fmt.Println("C:", setC)

}
Output:

A ∪ B: (Set[int]: [1 2 3 4 6])
B ∪ C: (Set[int]: [3 4 6 7])
C ∪ A: (Set[int]: [1 2 3 4 7])
A ∪ B ∪ C: (Set[int]: [1 2 3 4 6 7])

Originals are not modified:
A: (Set[int]: [1 2 3 4])
B: (Set[int]: [3 6])
C: (Set[int]: [4 7])

func UniqueOf

func UniqueOf[T comparable](sets ...Set[T]) Set[T]

UniqueOf creates a new set that contains all values which appear only in one of the given sets.

Example
package main

import (
	"fmt"

	"github.com/KrischanCS/go-toolbox/set"
)

func main() {
	setA := set.Of(1, 2, 3, 4)
	setB := set.Of(3, 6)
	setC := set.Of(4, 7)

	fmt.Println("A ∆ B:", set.UniqueOf(setA, setB))
	fmt.Println("B ∆ C:", set.UniqueOf(setB, setC))
	fmt.Println("C ∆ A:", set.UniqueOf(setC, setA))
	fmt.Println("A ∆ B ∆ C:", set.UniqueOf(setA, setB, setC))

	fmt.Println()

	fmt.Println("Originals are not modified:")

	fmt.Println("A:", setA)
	fmt.Println("B:", setB)
	fmt.Println("C:", setC)

}
Output:

A ∆ B: (Set[int]: [1 2 4 6])
B ∆ C: (Set[int]: [3 4 6 7])
C ∆ A: (Set[int]: [1 2 3 7])
A ∆ B ∆ C: (Set[int]: [1 2 6 7])

Originals are not modified:
A: (Set[int]: [1 2 3 4])
B: (Set[int]: [3 6])
C: (Set[int]: [4 7])

func WithCapacity

func WithCapacity[T comparable](capacity int) Set[T]

WithCapacity creates a Set with the given capacity.

func (Set[T]) Add

func (s Set[T]) Add(values ...T)

Add adds the given values to the set if it is not already present.

func (Set[T]) All

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

All creates an iterator over all values in the set without any particular order.

func (Set[T]) Clear

func (s Set[T]) Clear()

Clear removes all values from the set.

func (Set[T]) Clone

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

Clone creates a shallow copy of the set.

func (Set[T]) Contains

func (s Set[T]) Contains(values ...T) bool

Contains checks if the set contains all the given values.

Example
package main

import (
	"fmt"

	"github.com/KrischanCS/go-toolbox/set"
)

func main() {
	intSet := set.Of(1, 2, 3, 4)

	fmt.Println(intSet.Contains(2))
	fmt.Println(intSet.Contains(5))
	fmt.Println(intSet.Contains(1, 2, 3))
	fmt.Println(intSet.Contains(1, 2, 5))
	fmt.Println(intSet.Contains(1, 2, 3, 4))
	fmt.Println(intSet.Contains(1, 2, 3, 4, 5))

}
Output:

true
false
true
false
true
false

func (Set[T]) ContainsExactly

func (s Set[T]) ContainsExactly(values ...T) bool

ContainsExactly checks if the set contains all the given values and no more.

Example
package main

import (
	"fmt"

	"github.com/KrischanCS/go-toolbox/set"
)

func main() {
	intSet := set.Of(1, 2, 3, 4)

	fmt.Println(intSet.ContainsExactly(2))
	fmt.Println(intSet.ContainsExactly(5))
	fmt.Println(intSet.ContainsExactly(1, 2, 3))
	fmt.Println(intSet.ContainsExactly(1, 2, 3, 4))
	fmt.Println(intSet.ContainsExactly(1, 2, 3, 4, 5))

}
Output:

false
false
false
true
false

func (Set[T]) Difference

func (s Set[T]) Difference(others ...Set[T])

Difference removes all values from the set that are contained in the other sets.

Example
package main

import (
	"fmt"

	"github.com/KrischanCS/go-toolbox/set"
)

func main() {
	setA := set.Of(1, 2, 3, 4)
	setB := set.Of(3, 4, 5, 6)

	setA.Difference(setB)
	fmt.Println("A = A - B:", setA)

	setC := set.Of(3, 5)
	setD := set.Of(6, 7)

	setB.Difference(setC, setD)
	fmt.Println("B = B - C - D:", setB)

}
Output:

A = A - B: (Set[int]: [1 2])
B = B - C - D: (Set[int]: [4])

func (Set[T]) Intersection

func (s Set[T]) Intersection(others ...Set[T])

Intersection removes all values from the set that are not contained in all other given sets.

Example
package main

import (
	"fmt"

	"github.com/KrischanCS/go-toolbox/set"
)

func main() {
	setA := set.Of(1, 2, 3, 4)
	setB := set.Of(3, 2, 5)

	setA.Intersection(setB)
	fmt.Println("A = A ∩ B:", setA)

	setC := set.Of(3, 1, 5)
	setD := set.Of(3, 4)

	setB.Intersection(setC, setD)
	fmt.Println("B = B ∩ C ∩ D:", setB)

}
Output:

A = A ∩ B: (Set[int]: [2 3])
B = B ∩ C ∩ D: (Set[int]: [3])

func (Set[T]) IsEmpty

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

IsEmpty returns true if the set is empty.

func (Set[T]) Len

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

Len returns the number of values in the set.

func (Set[T]) Remove

func (s Set[T]) Remove(value ...T)

Remove removes the given values from the set.

func (Set[T]) String

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

String returns a string representation in the format:

  • If Present: "(Set[{{type}}]: [{{value 1}} {{value 2}} ...])"
  • If Empty: "(Set[{{type}}]: <empty>)"

The values are sorted by their string representation for easier overview, the actual set is not sorted.

func (Set[T]) Union

func (s Set[T]) Union(sets ...Set[T])

Union adds all values from the given sets to the current set.

Example
package main

import (
	"fmt"

	"github.com/KrischanCS/go-toolbox/set"
)

func main() {
	setA := set.Of(1, 2, 3, 4)
	setB := set.Of(3, 6)

	setA.Union(setB)
	fmt.Println("A = A ∪ B:", setA)

	setC := set.Of(3, 1, 5)
	setD := set.Of(3, 4)
	setB.Union(setC, setD)

	fmt.Println("B = B ∪ C ∪ D:", setB)

}
Output:

A = A ∪ B: (Set[int]: [1 2 3 4 6])
B = B ∪ C ∪ D: (Set[int]: [1 3 4 5 6])

func (Set[T]) Unique

func (s Set[T]) Unique(others ...Set[T])

Unique modifies the set, to only contain values which appear only in one set, including the set itself and all given other sets.

Example
package main

import (
	"fmt"

	"github.com/KrischanCS/go-toolbox/set"
)

func main() {
	setA := set.Of(1, 2, 3, 4)
	setB := set.Of(3, 6)

	setA.Unique(setB)
	fmt.Println("A = A ∆ B:", setA)

	setC := set.Of(3, 1, 5)
	setD := set.Of(3, 4)
	setC.Unique(setC, setD)
	fmt.Println("B = B ∆ C ∆ D:", setC)

}
Output:

A = A ∆ B: (Set[int]: [1 2 4 6])
B = B ∆ C ∆ D: (Set[int]: [4])

func (Set[T]) Values

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

Values returns a slice of all values in the set without any particular order.

Jump to

Keyboard shortcuts

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