set

package
v0.0.0-...-ed41e97 Latest Latest
Warning

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

Go to latest
Published: Jul 20, 2023 License: MIT Imports: 2 Imported by: 0

Documentation

Overview

Package set implements a set data structure and common set theory operations.

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 {
	base.Collection[T]
}

Set is an implementation of a set data structure. For usage examples see the tests.

func NewSet

func NewSet[T comparable]() *Set[T]

NewSet returns a new empty set.

Example
fmt.Println(NewSet[int]())
Output:
[]

func NewSetFromSlice

func NewSetFromSlice[T comparable](slice []T) *Set[T]

NewSetFromSlice returns a new set with the elements of the slice. The slice is not modified and the duplicates are removed.

Example
fmt.Println(NewSetFromSlice[int]([]int{2, 2}))
Output:
[2]

func (*Set[T]) Add

func (set *Set[T]) Add(element T)

Add adds the element to the set.

Example
s := NewSet[int]()
s.Add(1)
fmt.Println(s)
Output:
[1]

func (*Set[T]) Difference

func (set *Set[T]) Difference(other *Set[T]) *Set[T]

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

Example
s := NewSetFromSlice[int]([]int{1, 2, 3})
o := NewSetFromSlice[int]([]int{2, 3, 40})
fmt.Println(s.Difference(o))
Output:
[1]

func (*Set[T]) Intersection

func (set *Set[T]) Intersection(other *Set[T]) *Set[T]

Intersection returns a new set with elements in the set and the others. The sets are not modified.

Example
s := NewSetFromSlice[int]([]int{1, 2, 3})
o := NewSetFromSlice[int]([]int{2, 30, 40})
fmt.Println(s.Intersection(o))
Output:
[2]

func (*Set[T]) Remove

func (set *Set[T]) Remove(element T)

Remove removes the element from the set.

Example
s := NewSetFromSlice[int]([]int{1, 2})
s.Remove(1)
fmt.Println(s)
Output:
[2]

func (*Set[T]) String

func (set *Set[T]) String() string

String returns a string representation of the set.

Example
s := NewSetFromSlice[int]([]int{1})
fmt.Println(s.String())
Output:
[1]

func (*Set[T]) SymmetricDifference

func (set *Set[T]) SymmetricDifference(other *Set[T]) *Set[T]

SymmetricDifference returns a new set with elements in the set or the others but not in both. The sets are not modified.

Example
s := NewSetFromSlice[int]([]int{2, 30})
o := NewSetFromSlice[int]([]int{2, 30, 1})
fmt.Println(s.SymmetricDifference(o))
Output:
[1]

func (*Set[T]) Union

func (set *Set[T]) Union(other *Set[T]) *Set[T]

Union returns a new set with elements in the set or the others. The sets are not modified.

Example
s := NewSetFromSlice[int]([]int{1})
o := NewSetFromSlice[int]([]int{2})
fmt.Println(s.Union(o))
Output:
[1 2]

Jump to

Keyboard shortcuts

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