set

package
v9.0.0 Latest Latest
Warning

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

Go to latest
Published: Oct 31, 2022 License: Apache-2.0 Imports: 2 Imported by: 0

Documentation

Overview

Package set provides a common set based on generics.

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 is a set type.

Example
intset1 := NewSet(1, 2, 3)
intset2 := NewSet(7, 8, 9)
strset := NewSet("a", "b", "c")

intset1.Add(3, 4, 5)
strset.Add("c", "d", "e")
fmt.Println(intset1.Size(), strset.Size())
fmt.Println(intset1.Contains(4), intset1.Contains(5), intset1.Contains(6))
fmt.Println(strset.Contains("d"), strset.Contains("e"), strset.Contains("z"))
// 5 5
// true true false
// true true false

intset1.Remove(1, 2, 9)
strset.Remove("a", "b", "z")
fmt.Println(intset1.Size(), strset.Size())
fmt.Println(intset1.Contains(1), intset1.Contains(2))
fmt.Println(strset.Contains("a"), strset.Contains("b"))
// 3 3
// false false
// false false

list1 := intset1.Slice()
fmt.Println(list1[0])
fmt.Println(list1[1])
fmt.Println(list1[2])
// 3
// 4
// 5

list2 := strset.Slice()
fmt.Println(list2[0])
fmt.Println(list2[1])
fmt.Println(list2[2])
// c
// d
// e

union := intset1.Union(intset2)
diff := intset1.Difference(intset2)
inter := intset1.Intersection(intset2)
sdiff := intset1.SymmetricDifference(intset2)
fmt.Println(union.Size(), diff.Size(), inter.Size(), sdiff.Size())
// 6 3 0 6

fmt.Println(union.Difference(intset1).Equal(intset2))
fmt.Println(union.Equal(sdiff))
// true
// true

union.Range(func(element int) {
	fmt.Println(element)
})
for _, v := range union.Slice() {
	fmt.Println(v)
}
// 3
// 4
// 5
// 7
// 8
// 9
Output:

5 5
true true false
true true false
3 3
false false
false false
3
4
5
c
d
e
6 3 0 6
true
true
3
4
5
7
8
9
3
4
5
7
8
9

func NewSet

func NewSet[T comparable](elements ...T) Set[T]

NewSet returns a new Set.

If the element is string, it will ignore the empty string.

func NewSetFromSet

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

NewSetFromSet returns a new Set.

func (Set[T]) Add

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

Add adds some elements into the set.

func (Set[T]) Clear

func (s Set[T]) Clear()

Clear removes all the elements from the set.

func (Set[T]) Clone

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

Clone returns a copy of the current set.

func (Set[T]) Contains

func (s Set[T]) Contains(element T) bool

Contains returns true if the element is in the set. Or return false.

func (Set[T]) Difference

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

Difference returns a new set with elements in the set that are not in the others.

func (Set[T]) DifferenceUpdate

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

DifferenceUpdate updates the set, removing the elements found in others.

func (Set[T]) Equal

func (s Set[T]) Equal(other Set[T]) bool

Equal returns true if s == other.

func (Set[T]) Intersection

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

Intersection returns a new set with elements common to the set and all others.

func (Set[T]) IntersectionUpdate

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

IntersectionUpdate updates the set, keeping only elements found in it and all others.

func (Set[T]) Pop

func (s Set[T]) Pop() (element T, ok bool)

Pop removes and returns an arbitrary element from the set.

func (Set[T]) Range

func (s Set[T]) Range(f func(element T))

Range travels all the elements of the set.

func (Set[T]) Remove

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

Remove removes the elements from the set.

func (Set[T]) Size

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

Size returns the number of the elements in the set.

func (Set[T]) Slice

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

Slice converts the set to a slice.

func (Set[T]) String

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

func (Set[T]) SymmetricDifference

func (s Set[T]) SymmetricDifference(other Set[T]) Set[T]

SymmetricDifference returns a new set with elements in either the set or other but not both.

func (Set[T]) SymmetricDifferenceUpdate

func (s Set[T]) SymmetricDifferenceUpdate(other Set[T])

SymmetricDifferenceUpdate updates the set, keeping only elements found in either set, but not in both.

func (Set[T]) Union

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

Union returns a new set with elements from the set and all others.

func (Set[T]) UnionUpdate

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

UnionUpdate updates the set, adding the elements from all others.

Jump to

Keyboard shortcuts

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