set

package
v0.2.3 Latest Latest
Warning

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

Go to latest
Published: Apr 1, 2025 License: MIT Imports: 4 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Set

type Set[T comparable] struct {
	// contains filtered or unexported fields
}

Set represents a thread-safe collection of unique elements. The zero value is not usable; use NewSet to create a new Set.

func NewSet

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

NewSet creates and initializes a new empty Set.

Example:

s := NewSet[string]()
s.Add("foo")

func (*Set[T]) Add

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

Add inserts an element into the Set. If the element already exists, the Set remains unchanged.

Example:

s := NewSet[int]()
s.Add(1) // Set now contains 1
s.Add(1) // Set still contains just 1

func (*Set[T]) Clear

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

Clear removes all elements from the Set. This operation is thread-safe.

Example:

s := NewSet[int]()
s.Add(1)
s.Add(2)
s.Clear() // Set is now empty
fmt.Println(s.Len()) // Output: 0

func (*Set[T]) Contains

func (s *Set[T]) Contains(member T) bool

Contains returns true if the element exists in the Set, false otherwise. This operation is thread-safe.

Example:

s := NewSet[string]()
s.Add("foo")
fmt.Println(s.Contains("foo")) // Output: true
fmt.Println(s.Contains("bar")) // Output: false

func (*Set[T]) Difference

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

Difference returns a new set containing elements that are present in the current set but not in the other set. This operation is thread-safe and does not modify the original sets.

Example:

s1 := NewSet[int]()
s1.Add(1)
s1.Add(2)
s1.Add(3)
s2 := NewSet[int]()
s2.Add(2)
s2.Add(3)
s2.Add(4)
result := s1.Difference(s2)
fmt.Println(result.Members()) // Output: [1]

func (*Set[T]) GobDecode added in v0.2.3

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

GobDecode implements the gob.GobDecoder interface. This allows the set to be decoded from binary data. This operation is thread-safe and will replace all existing elements.

Example:

s := NewSet[int]()
err := s.GobDecode(data)

func (*Set[T]) GobEncode added in v0.2.3

func (s *Set[T]) GobEncode() ([]byte, error)

GobEncode implements the gob.GobEncoder interface. This allows the set to be encoded for binary transmission. This operation is thread-safe.

Example:

s := NewSet[int]()
s.Add(1)
data, err := s.GobEncode()

func (*Set[T]) Intersect

func (s *Set[T]) Intersect(other *Set[T]) *Set[T]

Intersect returns a new set containing elements that are present in both sets. This operation is thread-safe and does not modify the original sets.

Example:

s1 := NewSet[int]()
s1.Add(1)
s1.Add(2)
s2 := NewSet[int]()
s2.Add(2)
s2.Add(3)
result := s1.Intersect(s2)
fmt.Println(result.Members()) // Output: [2]

func (*Set[T]) MarshalJSON added in v0.2.3

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

MarshalJSON implements the json.Marshaler interface. The set is marshaled as a JSON array of its elements. This operation is thread-safe.

Example:

s := NewSet[int]()
s.Add(1)
s.Add(2)
data, err := json.Marshal(s)
fmt.Println(string(data)) // Output: [1,2]

func (*Set[T]) Members

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

Members returns a slice containing all elements in the Set. The order of elements is not guaranteed to be stable between calls.

Example:

s := NewSet[int]()
s.Add(1)
s.Add(2)
fmt.Println(s.Members()) // Output: [1 2] (order not guaranteed)

func (*Set[T]) Remove

func (s *Set[T]) Remove(member T)

Remove deletes an element from the Set. If the element doesn't exist, the Set remains unchanged. This operation is thread-safe.

Example:

s := NewSet[int]()
s.Add(1)
s.Remove(1) // Set is now empty
s.Remove(1) // No effect - element wasn't present

func (*Set[T]) Size

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

Size returns the number of elements in the Set. This operation is thread-safe.

Example:

s := NewSet[int]()
s.Add(1)
s.Add(2)
fmt.Println(s.Size()) // Output: 2

func (*Set[T]) Union

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

Union returns a new set containing all elements from both sets. This operation is thread-safe and does not modify the original sets.

Example:

s1 := NewSet[int]()
s1.Add(1)
s1.Add(2)
s2 := NewSet[int]()
s2.Add(2)
s2.Add(3)
result := s1.Union(s2)
fmt.Println(result.Members()) // Output: [1 2 3]

func (*Set[T]) UnmarshalJSON added in v0.2.3

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

UnmarshalJSON implements the json.Unmarshaler interface. The set is unmarshaled from a JSON array of elements. This operation is thread-safe and will replace all existing elements.

Example:

s := NewSet[int]()
data := []byte("[1,2,3]")
err := json.Unmarshal(data, &s)
fmt.Println(s.Members()) // Output: [1 2 3]

Jump to

Keyboard shortcuts

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