set

package
v0.1.4 Latest Latest
Warning

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

Go to latest
Published: Nov 26, 2024 License: BSD-3-Clause Imports: 3 Imported by: 0

Documentation

Overview

Package set provides a set data structure that can be used to store unique elements. The set is implemented using a map[K]struct{} where K is a comparable type.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Set

type Set[K comparable] map[K]struct{}

Set is a collection of unique elements.

func New

func New[K comparable](items ...K) Set[K]

New creates a new set with the provided items.

Example
package main

import (
	"fmt"

	"github.com/elordeiro/goext/containers/set"
)

func main() {
	s := set.New(1, 2, 3)
	for v := range s.All() {
		fmt.Println(v)
	}
}
Output:

3
1
2

func (Set[K]) Add

func (s Set[K]) Add(item ...K)

Add adds the provided items to the set.

Example
package main

import (
	"fmt"

	"github.com/elordeiro/goext/containers/set"
)

func main() {
	s := set.New(1, 2, 3)
	s.Add(4)
	for v := range s.All() {
		fmt.Println(v)
	}
}
Output:

3
1
2
4

func (Set[K]) All

func (s Set[K]) All() iter.Seq[K]

All returns an iter.Seq[K] of all elements in the set.

Example
package main

import (
	"fmt"

	"github.com/elordeiro/goext/containers/set"
)

func main() {
	s := set.New(1, 2, 3)
	for v := range s.All() {
		fmt.Println(v)
	}
}
Output:

3
1
2

func (Set[K]) Contains

func (s Set[K]) Contains(item K) bool

Contains returns true if the provided item is in the set.

Example
package main

import (
	"fmt"

	"github.com/elordeiro/goext/containers/set"
)

func main() {
	s := set.New(1, 2, 3)
	fmt.Println(s.Contains(1))
}
Output:

true

func (Set[K]) Difference

func (s Set[K]) Difference(other Set[K]) Set[K]

Difference returns a new set with the difference of the provided set and the set.

Example
package main

import (
	"fmt"

	"github.com/elordeiro/goext/containers/set"
)

func main() {
	s1 := set.New(1, 2, 3)
	s2 := set.New(3, 4, 5)
	difference := s1.Difference(s2)
	for v := range difference.All() {
		fmt.Println(v)
	}
}
Output:

1
2

func (Set[K]) Equal

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

Equal returns true if the provided set is equal to the set. Two sets are equal if they have the same elements.

Example
package main

import (
	"fmt"

	"github.com/elordeiro/goext/containers/set"
)

func main() {
	s1 := set.New(1, 2, 3)
	s2 := set.New(1, 2, 3)
	fmt.Println(s1.Equal(s2))
}
Output:

true

func (Set[K]) Intersection

func (s Set[K]) Intersection(other Set[K]) Set[K]

Intersection returns a new set with the intersection of the provided set and the set.

Example
package main

import (
	"fmt"

	"github.com/elordeiro/goext/containers/set"
)

func main() {
	s1 := set.New(1, 2, 3)
	s2 := set.New(3, 4, 5)
	intersection := s1.Intersection(s2)
	for v := range intersection.All() {
		fmt.Println(v)
	}
}
Output:

3

func (Set[K]) IsEmpty

func (s Set[K]) IsEmpty() bool

IsEmpty returns true if the set is empty.

Example
package main

import (
	"fmt"

	"github.com/elordeiro/goext/containers/set"
)

func main() {
	s := set.New[int]()
	fmt.Println(s.IsEmpty())
	s.Add(1)
	fmt.Println(s.IsEmpty())
}
Output:

true
false

func (Set[K]) Len

func (s Set[K]) Len() int

Len returns the number of elements in the set.

Example
package main

import (
	"fmt"

	"github.com/elordeiro/goext/containers/set"
)

func main() {
	s := set.New(1, 2, 3)
	fmt.Println(s.Len())
}
Output:

3

func (Set[K]) Remove

func (s Set[K]) Remove(item K)

Remove removes the provided item from the set.

Example
package main

import (
	"fmt"

	"github.com/elordeiro/goext/containers/set"
)

func main() {
	s := set.New(1, 2, 3)
	s.Remove(2)
	for v := range s.All() {
		fmt.Println(v)
	}
}
Output:

3
1

func (Set[K]) String

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

String returns a string representation of the set.

Example
package main

import (
	"fmt"

	"github.com/elordeiro/goext/containers/set"
)

func main() {
	s := set.New(1)
	fmt.Println(s.String())
}
Output:

{1}

func (Set[K]) Union

func (s Set[K]) Union(other Set[K]) Set[K]

Union returns a new set with the union of the provided set and the set.

Example
package main

import (
	"fmt"

	"github.com/elordeiro/goext/containers/set"
)

func main() {
	s1 := set.New(1, 2, 3)
	s2 := set.New(3, 4, 5)
	union := s1.Union(s2)
	for v := range union.All() {
		fmt.Println(v)
	}
}
Output:

3
1
2
4
5

Jump to

Keyboard shortcuts

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