Documentation
¶
Index ¶
- type Set
- func (s *Set[T]) Add(member T)
- func (s *Set[T]) Clear()
- func (s *Set[T]) Contains(member T) bool
- func (s *Set[T]) Difference(other *Set[T]) *Set[T]
- func (s *Set[T]) GobDecode(data []byte) error
- func (s *Set[T]) GobEncode() ([]byte, error)
- func (s *Set[T]) Intersect(other *Set[T]) *Set[T]
- func (s *Set[T]) MarshalJSON() ([]byte, error)
- func (s *Set[T]) Members() []T
- func (s *Set[T]) Remove(member T)
- func (s *Set[T]) Size() int
- func (s *Set[T]) Union(other *Set[T]) *Set[T]
- func (s *Set[T]) UnmarshalJSON(data []byte) error
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 ¶
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 ¶
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
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
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 ¶
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
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 ¶
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 ¶
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
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]