Documentation
¶
Overview ¶
Package set provides a generic set data interface and implementations of this interface.
The interface is based on the mathematical definition of a set and provides operations like union, intersection, difference, and subset relationships. The package also provides a map-based in-memory implementation of the Set interface called hashSet.
The CartesianProduct and PowerSet functions are also provided separately to avoid type dependency cycles while still maintaining the complete set of operations from set theory.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Pair ¶ added in v0.2.0
type Pair[T comparable] struct { First T Second T }
Pair represents an ordered pair of elements for use in Cartesian products.
type Set ¶
type Set[T comparable] interface { // Insert adds the element to the set. // If the element already exists, the set remains unchanged. Insert(elem T) // Remove deletes the element from the set. // If the element does not exist, the set remains unchanged. Remove(elem T) // Contains reports whether the element exists in the set. Contains(elem T) bool // Cardinality returns the number of elements (a natural, counting number) in this finite set. Cardinality() int // IsEmpty reports whether the set has no elements. IsEmpty() bool // Equals reports whether this set contains exactly the same elements as the other set. Equals(other Set[T]) bool // IsSubsetOf reports whether this set is a subset of the other set. // A set **X** is a subset of set **Y** if every element of **X** is also an element of **Y**. IsSubsetOf(other Set[T]) bool // IsSupersetOf reports whether this set is a superset of the other set. // A set **X** is a superset of set **Y** if every element of **Y** is also an element of **X**. IsSupersetOf(other Set[T]) bool // IsProperSubsetOf reports whether this set is a proper subset of the other set. // A set **X** is a proper subset of set **Y** if **X** is subset of **Y** and **X** ≠ **Y**. IsProperSubsetOf(other Set[T]) bool // IsProperSupersetOf reports whether this set is a proper superset of the other set. // A set **X** is a proper superset of set **Y** if **X** is superset of **Y** and **X** ≠ **Y**. IsProperSupersetOf(other Set[T]) bool // Union returns a new set containing all elements that are in either this set // or the other set (**X** ∪ **Y**). Union(other Set[T]) Set[T] // Intersection returns a new set containing all elements that are in both this set // and the other set (**X** ∩ **Y**). Intersection(other Set[T]) Set[T] // Difference returns a new set containing all elements that are in this set // but not in the other set (**X** \ **Y**). Difference(other Set[T]) Set[T] // SymmetricDifference returns a new set containing all elements that are in either this set // or the other set, but not in both (**X** Δ **Y**). SymmetricDifference(other Set[T]) Set[T] // ToSlice returns a slice containing all elements in the set. // **Note**: The order of elements is not guaranteed to be stable between calls. ToSlice() []T // String returns a string representation of the set. // For sets of strings, the elements are sorted lexicographically. String() string }
Set is a generic interface that defines the operations that can be performed on a set. A set is defined as an unordered collection of unique, arbitrary elements. The zero value of a set is an empty set.
func CartesianProduct ¶ added in v0.2.0
func CartesianProduct[T comparable](s1, s2 Set[T]) Set[Pair[T]]
CartesianProduct returns a new set containing all possible ordered pairs (x, y) where x is from the first set and y is from the second set. For sets A and B, it returns A × B = {(x, y) | x ∈ A, y ∈ B}.
For example, if A = {1, 2} and B = {3, 4}, then CartesianProduct(A, B) = {(1, 3), (1, 4), (2, 3), (2, 4)}.
func NewHashSet ¶
func NewHashSet[T comparable]() Set[T]
NewHashSet creates and returns a new empty set.
func PowerSet ¶ added in v0.2.0
func PowerSet[T comparable](s Set[T]) Set[Set[T]]
PowerSet returns a slice containing all possible subsets of the input set. For a set S, it returns P(S) = {T | T ⊆ S}.
For a set with n elements, the power set contains 2^n elements. For example, if S = {1, 2}, then PowerSet(S) = {{}, {1}, {2}, {1, 2}}.