set

package
v0.7.1 Latest Latest
Warning

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

Go to latest
Published: Dec 26, 2024 License: MIT Imports: 1 Imported by: 0

Documentation

Overview

Package set defines a Set type that holds a set of elements.

Package set defines a Set type that holds a set of elements.

Example
// Create a new set.
s := NewSet[int]()
s.Add(1)
s.AddSet(OfSet(2, 3))

// Iterate through list and print its contents.
for i := 1; i < 5; i++ {
	fmt.Println(s.Contains(i))
}
Output:

true
true
true
false

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Set

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

Set is a set of elements of some comparable type. Sets are implemented using maps, and have similar performance characteristics. Like maps, Sets are reference types. That is, for Sets s1 = s2 will leave s1 and s2 pointing to the same set of elements: changes to s1 will be reflected in s2 and vice-versa.

func Difference

func Difference[E comparable](s1, s2 Set[E]) Set[E]

Difference constructs a new set containing the elements of s1 that are not present in s2.

func Intersection

func Intersection[E comparable](s1, s2 Set[E]) Set[E]

Intersection constructs a new set containing the intersection of s1 and s2.

func NewSet

func NewSet[E comparable]() Set[E]

NewSet returns a new set.

func OfSet

func OfSet[E comparable](v ...E) Set[E]

OfSet returns a new set containing the listed elements.

func Union

func Union[E comparable](s1, s2 Set[E]) Set[E]

Union constructs a new set containing the union of s1 and s2.

func (Set[E]) Add

func (s Set[E]) Add(v ...E)

Add adds elements to a set.

func (Set[E]) AddSet

func (s Set[E]) AddSet(s2 Set[E])

AddSet adds the elements of set s2 to s.

func (*Set[E]) Clear

func (s *Set[E]) Clear()

Clear removes all elements from s, leaving it empty.

func (Set[E]) Clone

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

Clone returns a copy of s. The elements are copied using assignment, so this is a shallow clone.

func (Set[E]) Contains

func (s Set[E]) Contains(v E) bool

Contains reports whether v is in the set.

func (Set[E]) ContainsAll

func (s Set[E]) ContainsAll(s2 Set[E]) bool

ContainsAll reports whether all of the elements in s2 are in s.

func (Set[E]) ContainsAny

func (s Set[E]) ContainsAny(s2 Set[E]) bool

ContainsAny reports whether any of the elements in s2 are in s.

func (Set[E]) Do

func (s Set[E]) Do(f func(E) bool)

Do calls f on every Eent in the set s, stopping if f returns false. f should not change s. f will be called on values in an indeterminate order.

func (Set[E]) Equal

func (s Set[E]) Equal(s2 Set[E]) bool

Equal reports whether s and s2 contain the same elements.

func (Set[E]) Filter

func (s Set[E]) Filter(keep func(E) bool)

Filter deletes any elements from s for which keep returns false.

func (Set[E]) Len

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

Len returns the number of elements in s.

func (Set[E]) Remove

func (s Set[E]) Remove(v ...E)

Remove removes elements from a set. elements that are not present are ignored.

func (Set[E]) RemoveSet

func (s Set[E]) RemoveSet(s2 Set[E])

RemoveSet removes the elements of set s2 from s. elements present in s2 but not s are ignored.

func (Set[E]) Values

func (s Set[E]) Values() []E

Values returns the elements in the set s as a slice. The values will be in an indeterminate order.

type SortedSet

type SortedSet[E comparable] maps.OrderedMap[E, struct{}]

Set is a set of elements of some comparable type in insert order.

func DifferenceSortedSet

func DifferenceSortedSet[E comparable](s1, s2 *SortedSet[E]) *SortedSet[E]

DifferenceSortedSet constructs a new set containing the elements of s1 that are not present in s2.

func IntersectionSortedSet

func IntersectionSortedSet[E comparable](s1, s2 *SortedSet[E]) *SortedSet[E]

IntersectionSortedSet constructs a new set containing the intersection of s1 and s2.

func NewSortedSet

func NewSortedSet[E comparable](capability int) *SortedSet[E]

NewSortedSet returns a new SortedSet.

func OfSortedSet

func OfSortedSet[E comparable](v ...E) *SortedSet[E]

OfSortedSet returns a new SortedSet containing the listed elements.

func UnionSortedSet

func UnionSortedSet[E comparable](s1, s2 *SortedSet[E]) *SortedSet[E]

UnionSortedSet constructs a new set containing the union of s1 and s2.

func (*SortedSet[E]) Add

func (s *SortedSet[E]) Add(v ...E)

Add adds elements to a set.

func (*SortedSet[E]) AddSet

func (s *SortedSet[E]) AddSet(s2 *SortedSet[E])

AddSet adds the elements of set s2 to s.

func (*SortedSet[E]) Clear

func (s *SortedSet[E]) Clear()

Clear removes all elements from s, leaving it empty.

func (*SortedSet[E]) Clone

func (s *SortedSet[E]) Clone() *SortedSet[E]

Clone returns a copy of s. The elements are copied using assignment, so this is a shallow clone.

func (*SortedSet[E]) Contains

func (s *SortedSet[E]) Contains(v E) bool

Contains reports whether v is in the set.

func (*SortedSet[E]) ContainsAll

func (s *SortedSet[E]) ContainsAll(s2 *SortedSet[E]) bool

ContainsAll reports whether all of the elements in s2 are in s.

func (*SortedSet[E]) ContainsAny

func (s *SortedSet[E]) ContainsAny(s2 *SortedSet[E]) bool

ContainsAny reports whether any of the elements in s2 are in s.

func (*SortedSet[E]) Do

func (s *SortedSet[E]) Do(f func(E) bool)

Do calls f on every Eent in the set s, stopping if f returns false. f should not change s. f will be called on values in an indeterminate order.

func (*SortedSet[E]) Equal

func (s *SortedSet[E]) Equal(s2 *SortedSet[E]) bool

Equal reports whether s and s2 contain the same elements.

func (*SortedSet[E]) Filter

func (s *SortedSet[E]) Filter(keep func(E) bool)

Filter deletes any elements from s for which keep returns false.

func (*SortedSet[E]) Len

func (s *SortedSet[E]) Len() int

Len returns the number of elements in s.

func (*SortedSet[E]) Newest

func (s *SortedSet[E]) Newest() (E, bool)

Newest returns the newest element in the set.

func (*SortedSet[E]) Oldest

func (s *SortedSet[E]) Oldest() (E, bool)

Oldest returns the oldest element in the set.

func (*SortedSet[E]) Remove

func (s *SortedSet[E]) Remove(v ...E)

Remove removes elements from a set. elements that are not present are ignored.

func (*SortedSet[E]) RemoveSet

func (s *SortedSet[E]) RemoveSet(s2 *SortedSet[E])

RemoveSet removes the elements of set s2 from s. elements present in s2 but not s are ignored.

func (*SortedSet[E]) Values

func (s *SortedSet[E]) Values() []E

Values returns the elements in the set s as a slice. The values will be in an indeterminate order.

Jump to

Keyboard shortcuts

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