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 map[any]bool

Set is a set data structure.

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

5

func Of

func Of(elements ...any) Set

Of creates a new set with the provided elements and returns it.

func (*Set) Add

func (s *Set) Add(element any) *Set

Add adds the specified element to the set.

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

false
true

func (*Set) Clear

func (s *Set) Clear() *Set

Clear removes all elements from the set.

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

false
true
{}

func (Set) Copy

func (s Set) Copy() Set

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

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

true

func (Set) Difference

func (s Set) Difference(another Set) Set

Difference returns a new set containing elements that are present in the receiver set but not in the another 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) Discard

func (s *Set) Discard(element any) bool

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

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

true
false

func (Set) Empty

func (s Set) Empty() bool

Empty checks if the set is empty.

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

false
true

func (Set) Equal

func (s Set) Equal(another Set) bool

Equal checks if the set is equal to another set.

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

true

func (Set) Has

func (s Set) Has(element any) bool

Has checks if the set contains the given element.

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

true
false

func (Set) Intersection

func (s Set) Intersection(another Set) Set

Intersection returns a new set containing elements that are present both in the receiver set and another set.

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

{c}

func (Set) IsDisjoint

func (s Set) IsDisjoint(another Set) bool

IsDisjoint checks if the set is disjoint from another set.

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) IsSubset

func (s Set) IsSubset(another Set) bool

IsSubset checks if the set is a subset of another set.

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) IsSuperset

func (s Set) IsSuperset(another Set) bool

IsSuperset checks if the set is a superset of another set.

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) MarshalJSON

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

MarshalJSON customizes the JSON encoding for the set.

func (*Set) Pop

func (s *Set) Pop() (element any, err error)

Pop removes and returns an arbitrary element from the set.

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

1

func (Set) Size

func (s Set) 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) String

func (s Set) String() string

String returns the string representation of the set.

func (Set) SymmetricDifference

func (s Set) SymmetricDifference(another Set) Set

SymmetricDifference returns a new set containing elements that are present in either the receiver set or another set, 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) Union

func (s Set) Union(another Set) Set

Union returns a new set containing all elements from both the receiver set and another 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) UnmarshalJSON

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

UnmarshalJSON customizes the JSON decoding for the set.

func (*Set) Update

func (s *Set) Update(another Set) *Set

Update adds the elements from another set to the current 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