Documentation ¶
Index ¶
- func Add[M ~map[T]struct{}, T comparable](A, B M)
- func AddItems[M ~map[T]struct{}, T comparable](s M, v ...T)
- func And[M ~map[T]struct{}, T comparable](A, B M)
- func Copy[M ~map[T]struct{}, T comparable](other M) M
- func FromSlice[S ~[]T, T comparable](l S) map[T]struct{}
- func Sorted[T constraints.Ordered](s HashSet[T]) []T
- func Sub[M ~map[T]struct{}, T comparable](A, B M)
- func ToSlice[M ~map[T]struct{}, T comparable](m M) []T
- type HashSet
- func (s HashSet[T]) Add(other map[T]struct{}) HashSet[T]
- func (s HashSet[T]) AddItemChecked(v T) bool
- func (s HashSet[T]) And(other map[T]struct{}) HashSet[T]
- func (s HashSet[T]) Contains(s2 HashSet[T]) bool
- func (s HashSet[T]) ContainsItem(v T) bool
- func (s HashSet[T]) RemoveItem(v T)
- func (s HashSet[T]) RemoveItemChecked(v T) bool
- func (s HashSet[T]) String() string
- func (s HashSet[T]) Sub(other map[T]struct{}) HashSet[T]
- func (s HashSet[T]) ToSlice() []T
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Add ¶
func Add[M ~map[T]struct{}, T comparable](A, B M)
Add all in B to A
Example ¶
A := make(map[int]struct{}) B := map[int]struct{}{1: {}, 2: {}} Add(A, B) fmt.Println(A)
Output: map[1:{} 2:{}]
func AddItems ¶
func AddItems[M ~map[T]struct{}, T comparable](s M, v ...T)
Example ¶
s := make(map[int]struct{}) AddItems(s, 1, 2, 3) fmt.Println(s)
Output: map[1:{} 2:{} 3:{}]
func And ¶
func And[M ~map[T]struct{}, T comparable](A, B M)
Remove anything not in B from A
Example ¶
A := map[int]struct{}{1: {}, 2: {}, 3: {}} B := map[int]struct{}{2: {}, 3: {}, 4: {}} And(A, B) fmt.Println(A)
Output: map[2:{} 3:{}]
func FromSlice ¶
func FromSlice[S ~[]T, T comparable](l S) map[T]struct{}
func Sorted ¶
func Sorted[T constraints.Ordered](s HashSet[T]) []T
Sorted returns a sorted set as []T
Example ¶
// create a new HashSet and add some elements set := HashSet[int]{} AddItems(set, 5, 1, 9) // get the sorted list of elements sortedList := Sorted(set) // print the sorted list fmt.Println(sortedList)
Output: [1 5 9]
func Sub ¶
func Sub[M ~map[T]struct{}, T comparable](A, B M)
Remove all in B from A
Example ¶
A := map[int]struct{}{1: {}, 2: {}, 3: {}} B := map[int]struct{}{2: {}} Sub(A, B) fmt.Println(A)
Output: map[1:{} 3:{}]
func ToSlice ¶
func ToSlice[M ~map[T]struct{}, T comparable](m M) []T
Types ¶
type HashSet ¶
type HashSet[T comparable] map[T]struct{}
func NewFromSlice ¶
func NewFromSlice[S ~[]T, T comparable](source S) HashSet[T]
func (HashSet[T]) Add ¶
Calculates the union of s and other, resulting a new HashSet
Example ¶
// Initialize HashSets set1 := HashSet[string]{"apple": {}, "banana": {}} set2 := HashSet[string]{"pear": {}, "orange": {}} set3 := HashSet[string]{"banana": {}, "durian": {}} // calculate the union of them result := set1. Add(set2). Add(set3) // Print the contents of the updated set for _, k := range Sorted(result) { fmt.Println(k) }
Output: apple banana durian orange pear
func (HashSet[T]) AddItemChecked ¶
func (HashSet[T]) And ¶
Calculates the complement of s and other, resulting a new HashSet
Example ¶
// Initialize HashSet set := HashSet[string]{"apple": {}, "banana": {}, "pear": {}, "orange": {}} // Keep only elements also in other other := HashSet[string]{"apple": {}, "pear": {}, "grape": {}} result := set.And(other) // Print the contents of the updated set for _, k := range Sorted(result) { fmt.Println(k) }
Output: apple pear
func (HashSet[T]) ContainsItem ¶
func (HashSet[T]) RemoveItem ¶
func (s HashSet[T]) RemoveItem(v T)
func (HashSet[T]) RemoveItemChecked ¶
func (HashSet[T]) Sub ¶
Calculates (s - other), resulting a new HashSet
Example ¶
// Initialize HashSet set := HashSet[string]{"apple": {}, "banana": {}, "pear": {}, "orange": {}} // Remove elements in other from set other := map[string]struct{}{"pear": {}, "orange": {}} result := set.Sub(other) // Print the contents of the updated set for _, k := range Sorted(result) { fmt.Println(k) }
Output: apple banana
Click to show internal directories.
Click to hide internal directories.