set

package
v0.1.3 Latest Latest
Warning

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

Go to latest
Published: Jul 3, 2024 License: Apache-2.0 Imports: 4 Imported by: 0

Documentation

Overview

Package set provides a set data structure.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Set

type Set[T comparable] map[T]bool

Set is a generic type representing a set data structure with elements of type T.

Example
s := Of(1, 2, 3, 4, 5, 5, 4, 3, 2, 1)
fmt.Println(s.Size())
Output:

5

func Of

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

Of creates and returns a new set containing the given elements.

func (*Set[T]) Add

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

Add inserts the specified element into the set and returns a pointer to the modified set.

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

false
true

func (*Set[T]) Clear

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

Clear removes all elements from the set and returns a pointer to the empty set.

Example
s := Of(1, 2)
fmt.Println(s.Empty())
s.Clear()
fmt.Println(s.Empty())
fmt.Println(s)
Output:

false
true
{}

func (Set[T]) Copy

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

Copy creates and returns a new set containing the same elements as the original set.

Example
s1 := Of("apple", "banana", "orange")
s2 := s1.Copy()
fmt.Println(s1.Equal(s2))
Output:

true

func (Set[T]) Difference

func (s Set[T]) Difference(another Set[T]) Set[T]

Difference returns a new set that contains elements from the receiver set that are not in the given set

Example
s1 := Of("a", "b", "c")
s2 := Of("b", "c", "d")
fmt.Println(s1.Difference(s2))
fmt.Println(s2.Difference(s1))
Output:

{a}
{d}

func (*Set[T]) Discard

func (s *Set[T]) Discard(element T) bool

Discard removes the specified element from the set and returns true if the element was present, false otherwise.

Example
s := Of(1, 2, 3)
fmt.Println(s.Has(2))
s.Discard(2)
fmt.Println(s.Has(2))
Output:

true
false

func (Set[T]) Empty

func (s Set[T]) Empty() bool

Empty returns true if the set is empty, otherwise false.

Example
s := Of(1, 2, 3)
s2 := Of[int]()
fmt.Println(s.Empty())
fmt.Println(s2.Empty())
Output:

false
true

func (Set[T]) Equal

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

Equal checks if the set is equal to another set by comparing the size and elements of the sets, and returns true if they are equal, otherwise false.

Example
set1 := Of("a", "b", "c", "a")
set2 := Of("c", "b", "a", "c")
fmt.Println(set1.Equal(set2))
Output:

true

func (Set[T]) Has

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

Has checks if the set contains the given element and returns true if found, otherwise false.

Example
s := Of(1, 2, 3)
fmt.Println(s.Has(1))
fmt.Println(s.Has(4))
Output:

true
false

func (Set[T]) Intersection

func (s Set[T]) Intersection(another Set[T]) Set[T]

Intersection returns a new set that contains elements that are common to both the receiver set and the given set

Example
s1 := Of("a", "b", "c")
s2 := Of("e", "c", "d")
fmt.Println(s1.Intersection(s2))
Output:

{c}

func (Set[T]) IsDisjoint

func (s Set[T]) IsDisjoint(another Set[T]) bool

IsDisjoint checks if the set is disjoint with another set and returns true if they have no common elements, otherwise false.

Example
set1 := Of("a", "b", "c")
set2 := Of("d", "e", "f")
set3 := Of("a", "e", "f")
fmt.Println(set1.IsDisjoint(set2))
fmt.Println(set1.IsDisjoint(set3))
Output:

true
false

func (Set[T]) IsSubset

func (s Set[T]) IsSubset(another Set[T]) bool

IsSubset checks if the set is a subset of another set and returns true if every element in the set is also in the other set, otherwise false.

Example
set1 := Of("a", "b")
set2 := Of("a", "b", "c")
set3 := Of("a", "c")
fmt.Println(set1.IsSubset(set2))
fmt.Println(set1.IsSubset(set3))
Output:

true
false

func (Set[T]) IsSuperset

func (s Set[T]) IsSuperset(another Set[T]) bool

IsSuperset checks if the set is a superset of another set and returns true if it contains every element of the other set, otherwise false.

Example
set1 := Of("a", "b", "c")
set2 := Of("a", "b")
set3 := Of("a", "e")
fmt.Println(set1.IsSuperset(set2))
fmt.Println(set1.IsSuperset(set3))
Output:

true
false

func (Set[T]) MarshalJSON

func (s Set[T]) MarshalJSON() ([]byte, error)

MarshalJSON converts the set into JSON format.

func (*Set[T]) Pop

func (s *Set[T]) Pop() (element T, err error)

Pop removes and returns an arbitrary element from the set, along with an error if the set is empty.

Example
s := Of(1)
element, _ := s.Pop()
fmt.Println(element)
Output:

1

func (Set[T]) Size

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

Size returns the number of elements in the set.

Example
s := Of(1, 2, 3, 4, 5)
fmt.Println(s.Size())
Output:

5

func (Set[T]) String

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

String returns a string representation of the set.

func (Set[T]) SymmetricDifference

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

SymmetricDifference returns a new set that contains elements that are in either of the sets, but not in both

Example
s1 := Of("a", "b", "c")
s2 := Of("b", "c", "d")
sd1 := s1.SymmetricDifference(s2)
sd2 := s2.SymmetricDifference(s1)
sd := Of("a", "d")
fmt.Println(sd1.Equal(sd2))
fmt.Println(sd1.Equal(sd))
Output:

true
true

func (Set[T]) Union

func (s Set[T]) Union(another Set[T]) Set[T]

Union returns a new set that contains all the unique elements from both the receiver set and the given set

Example
s1 := Of("a", "b", "c")
s2 := Of("b", "c", "d")
s3 := Of("a", "b", "c", "d")
union := s1.Union(s2)
fmt.Println(union.Equal(s3))
Output:

true

func (*Set[T]) UnmarshalJSON

func (s *Set[T]) UnmarshalJSON(data []byte) (err error)

UnmarshalJSON populates the set with data from JSON format.

func (*Set[T]) Update

func (s *Set[T]) Update(another Set[T]) *Set[T]

Update merges the elements from the given set 'another' into the current set and returns a pointer to the modified set.

Example
s1 := Of(1)
fmt.Println(s1)
s2 := Of(2)
fmt.Println(s2)
s1.Update(s2)
fmt.Println(s1.Has(2))
Output:

{1}
{2}
true

Jump to

Keyboard shortcuts

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