gset

package
v0.0.0-...-d5aba35 Latest Latest
Warning

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

Go to latest
Published: Oct 22, 2024 License: MIT Imports: 4 Imported by: 0

README

Style

There are two style of set operations: in-place and chain.

In-place operation:

A in-place operation is applied to the first argument.

	A := make(map[int]struct{})
	B := map[int]struct{}{1: {}, 2: {}}
	gset.Add(A, B)
	fmt.Println(A) // Output: map[1:{} 2:{}]

Chain operation:

A chain operation returns a new set.

	// Initialize HashSets
	set1 := gset.HashSet[string]{"apple": {}, "banana": {}}
	set2 := gset.HashSet[string]{"pear": {}, "orange": {}}
	set3 := gset.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 gset.Sorted(result) {
		fmt.Println(k)
	}

	// Output:
	// apple
	// banana
	// durian
	// orange
	// pear
Documents and examples: https://pkg.go.dev/github.com/szmcdull/glinq/gset

Documentation

Index

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 Copy

func Copy[M ~map[T]struct{}, T comparable](other M) M

Shallow copy

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

func (s HashSet[T]) Add(other map[T]struct{}) HashSet[T]

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 (s HashSet[T]) AddItemChecked(v T) bool

func (HashSet[T]) And

func (s HashSet[T]) And(other map[T]struct{}) HashSet[T]

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]) Contains

func (s HashSet[T]) Contains(s2 HashSet[T]) bool

func (HashSet[T]) ContainsItem

func (s HashSet[T]) ContainsItem(v T) bool

func (HashSet[T]) RemoveItem

func (s HashSet[T]) RemoveItem(v T)

func (HashSet[T]) RemoveItemChecked

func (s HashSet[T]) RemoveItemChecked(v T) bool

func (HashSet[T]) String

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

func (HashSet[T]) Sub

func (s HashSet[T]) Sub(other map[T]struct{}) HashSet[T]

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

func (HashSet[T]) ToSlice

func (s HashSet[T]) ToSlice() []T
Example
set := HashSet[int]{}
AddItems(set, 3, 2, 1, 1, 2, 3)
fmt.Println(Sorted(set))
Output:

[1 2 3]

Jump to

Keyboard shortcuts

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