Documentation
¶
Index ¶
- type Interface
- type OrderedSet
- func (s OrderedSet[E]) Add(vals ...E)
- func (s OrderedSet[E]) AddImmutable(vals ...E) Interface[E]
- func (s OrderedSet[E]) Clear()
- func (s OrderedSet[E]) Contains(v E) bool
- func (s OrderedSet[E]) ContainsAll(vals ...E) bool
- func (s OrderedSet[E]) Difference(s2 Interface[E]) Interface[E]
- func (s OrderedSet[E]) Equal(s2 Interface[E]) bool
- func (s OrderedSet[E]) InsertionOrderMembers() []E
- func (s OrderedSet[E]) Intersection(s2 Interface[E]) Interface[E]
- func (s OrderedSet[E]) IsSubsetOf(s2 Interface[E]) bool
- func (s OrderedSet[E]) IsSupersetOf(s2 Interface[E]) bool
- func (s OrderedSet[E]) Len() int
- func (s OrderedSet[E]) MarshalJSON() (out []byte, err error)
- func (s OrderedSet[E]) Members() []E
- func (s OrderedSet[E]) Remove(vals ...E)
- func (s OrderedSet[E]) RemoveImmutable(vals ...E) Interface[E]
- func (s OrderedSet[E]) String() string
- func (s OrderedSet[E]) Union(s2 Interface[E]) Interface[E]
- func (s OrderedSet[E]) UnmarshalJSON(in []byte) (err error)
- type Set
- func (s Set[E]) Add(vals ...E)
- func (s Set[E]) AddImmutable(vals ...E) Interface[E]
- func (s Set[E]) Clear()
- func (s Set[E]) Contains(v E) bool
- func (s Set[E]) ContainsAll(vals ...E) bool
- func (s Set[E]) Difference(s2 Interface[E]) Interface[E]
- func (s Set[E]) Equal(s2 Interface[E]) bool
- func (s Set[E]) Intersection(s2 Interface[E]) Interface[E]
- func (s Set[E]) IsSubsetOf(s2 Interface[E]) bool
- func (s Set[E]) IsSupersetOf(s2 Interface[E]) bool
- func (s Set[E]) Len() int
- func (s Set[E]) MarshalJSON() (out []byte, err error)
- func (s Set[E]) Members() []E
- func (s Set[E]) Remove(vals ...E)
- func (s Set[E]) RemoveImmutable(vals ...E) Interface[E]
- func (s Set[E]) String() string
- func (s Set[E]) Union(s2 Interface[E]) Interface[E]
- func (s Set[E]) UnmarshalJSON(in []byte) (err error)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Interface ¶
type Interface[E comparable] interface { // Contains returns true if the set contains the given value. Contains(E) bool // ContainsAll returns true if the set contains all the given values. ContainsAll(...E) bool // Add adds the given values to the set. Add(...E) // AddImmutable adds the given values to the set and returns a new set. AddImmutable(...E) Interface[E] // Remove removes the given values from the set. Remove(...E) // RemoveImmutable removes the given values from the set and returns // a new set. RemoveImmutable(...E) Interface[E] // Clear removes all values from the set. Clear() // Members returns the members of the set as a slice. Members() []E // String returns a string representation of the set. String() string // Union returns the union of the set with another set. Union(Interface[E]) Interface[E] // Intersection returns the intersection of the set with another // set. Intersection(Interface[E]) Interface[E] // Difference returns the difference of the set with another set. Difference(Interface[E]) Interface[E] // IsSubsetOf returns true if the set is a subset of another set. IsSubsetOf(Interface[E]) bool // IsSupersetOf returns true if the set is a superset of another IsSupersetOf(Interface[E]) bool // Equal returns true if the set is equal to another set. Equal(Interface[E]) bool // Len returns the number of elements in the set. Len() int // MarshalJSON implements the json.Marshaler interface to convert the map into // a JSON object. MarshalJSON() ([]byte, error) // UnmarshalJSON implements the json.Unmarshaler interface to convert a json // object to a Set. UnmarshalJSON([]byte) error }
Interface is the interface used by all the Set types.
type OrderedSet ¶
OrderedSet is a set that maintains the order of its elements when returned. It leverages an underlying map for efficient membership checks, but imposes sorting when elements are retrieved.
Specifically the Members(), String(), and MarshalJSON() methods sort the elements. To get the unsorted elements, use InsertionOrderMembers().
func NewOrdered ¶
func NewOrdered[E cmp.Ordered](vals ...E) OrderedSet[E]
NewOrdered creates a new ordered set with the given values.
Example:
s := New(3, 1, 2)
func (OrderedSet[E]) Add ¶
func (s OrderedSet[E]) Add(vals ...E)
Add adds the given values to the set.
Example:
s := NewSet(1, 2, 3) s.Add(4, 5) fmt.Println(s.Members()) [1 2 3 4 5]
func (OrderedSet[E]) AddImmutable ¶
func (s OrderedSet[E]) AddImmutable(vals ...E) Interface[E]
AddImmutable adds the given values to the set and returns a new set.
Example:
s := NewSet(1, 2, 3) s2 := s.AddImmutable(4, 5) fmt.Println(s.Members()) [1 2 3] fmt.Println(s2.Members()) [1 2 3 4 5]
func (OrderedSet[E]) Clear ¶
func (s OrderedSet[E]) Clear()
Clear removes all values from the set.
Example:
s := NewSet(1, 2, 3) s.Clear() fmt.Println(s.Members()) []
func (OrderedSet[E]) Contains ¶
func (s OrderedSet[E]) Contains(v E) bool
Contains returns true if the set contains the given value.
Example:
s := NewSet(1, 2, 3) fmt.Println(s.Contains(2)) true
func (OrderedSet[E]) ContainsAll ¶
func (s OrderedSet[E]) ContainsAll(vals ...E) bool
ContainsAll returns true if the set contains all the given values.
Example:
s := NewSet(1, 2, 3) fmt.Println(s.ContainsAll(2, 3)) true
func (OrderedSet[E]) Difference ¶
func (s OrderedSet[E]) Difference(s2 Interface[E]) Interface[E]
Difference returns the difference of the set with another set.
Example:
s1 := NewSet(1, 2, 3) s2 := NewSet(2, 3, 4) fmt.Println(s1.Difference(s2)) [1]
func (OrderedSet[E]) Equal ¶
func (s OrderedSet[E]) Equal(s2 Interface[E]) bool
Equal returns true if the set is equal to another set.
Example:
s1 := NewSet(1, 2, 3) s2 := NewSet(1, 2, 3) fmt.Println(s1.Equal(s2)) true
func (OrderedSet[E]) InsertionOrderMembers ¶
func (s OrderedSet[E]) InsertionOrderMembers() []E
InsertionOrderMembers returns the members of the set as a slice in the order they were inserted.
Example:
s := NewSet(2, 3, 1) fmt.Println(s.Members()) [1 2 3] fmt.Println(s.InsertionOrderMembers()) [2 3 1]
func (OrderedSet[E]) Intersection ¶
func (s OrderedSet[E]) Intersection(s2 Interface[E]) Interface[E]
Intersection returns the intersection of the set with another set.
Example:
s1 := NewSet(1, 2, 3) s2 := NewSet(2, 3, 4) fmt.Println(s1.Intersection(s2)) [3]
func (OrderedSet[E]) IsSubsetOf ¶
func (s OrderedSet[E]) IsSubsetOf(s2 Interface[E]) bool
IsSubsetOf returns true if the set is a subset of another set.
Example:
s1 := NewSet(1, 2) s2 := NewSet(1, 2, 3) fmt.Println(s1.IsSubsetOf(s2)) true
func (OrderedSet[E]) IsSupersetOf ¶
func (s OrderedSet[E]) IsSupersetOf(s2 Interface[E]) bool
IsSupersetOf returns true if the set is a superset of another set.
Example:
s1 := NewSet(1, 2, 3) s2 := NewSet(1, 2) fmt.Println(s1.IsSupersetOf(s2)) true
func (OrderedSet[E]) Len ¶
func (s OrderedSet[E]) Len() int
Len returns the number of elements in the set.
Example:
s := NewSet(1, 2, 3) fmt.Println(s.Len()) 3
func (OrderedSet[E]) MarshalJSON ¶
func (s OrderedSet[E]) MarshalJSON() (out []byte, err error)
MarshalJSON implements the json.Marshaler interface to convert the map into a JSON object. The values are ordered (ascending).
func (OrderedSet[E]) Members ¶
func (s OrderedSet[E]) Members() []E
Members returns the members of the set as a slice. The result is sorted in ascending order.
Example:
s := NewSet(3, 1, 3) fmt.Println(s.Members()) [1 2 3]
func (OrderedSet[E]) Remove ¶
func (s OrderedSet[E]) Remove(vals ...E)
Remove removes the given values from the set.
Example:
s := NewSet(1, 2, 3) s.Remove(2, 3) fmt.Println(s.Members()) [1]
func (OrderedSet[E]) RemoveImmutable ¶
func (s OrderedSet[E]) RemoveImmutable(vals ...E) Interface[E]
RemoveImmutable removes the given values from the set and returns a new set.
Example:
s := NewSet(1, 2, 3) s2 := s.RemoveImmutable(2, 3) fmt.Println(s1.Members()) [1 2 3] fmt.Println(s2.Members()) [1]
func (OrderedSet[E]) String ¶
func (s OrderedSet[E]) String() string
String returns a string representation of the set. Order is guaranteed (ascending).
Example:
s := NewSet(1, 2, 3) fmt.Println(s) [1 2 3]
func (OrderedSet[E]) Union ¶
func (s OrderedSet[E]) Union(s2 Interface[E]) Interface[E]
Union returns the union of the set with another set.
Example:
s1 := NewSet(1, 2, 3) s2 := NewSet(2, 3, 4) fmt.Println(s1.Union(s2)) [1 2 3 4 5]
func (OrderedSet[E]) UnmarshalJSON ¶
func (s OrderedSet[E]) UnmarshalJSON(in []byte) (err error)
UnmarshalJSON implements the json.Unmarshaler interface to convert a json object to a Set.
The JSON must start with a list.
type Set ¶
type Set[E comparable] map[E]struct{}
Set is a set that does not maintain the order of its elements when returned. It leverages an underlying map for efficient membership checks.
func New ¶
func New[E comparable](vals ...E) Set[E]
New creates a new set with the given values.
Example:
s := New(1, 2, 3)
func (Set[E]) Add ¶
func (s Set[E]) Add(vals ...E)
Add adds the given values to the set.
Example:
s := NewSet(1, 2, 3) s.Add(4, 5) fmt.Println(s.Members()) [1 2 3 4 5]
func (Set[E]) AddImmutable ¶
AddImmutable adds the given values to the set and returns a new set.
Example:
s := NewSet(1, 2, 3) s2 := s.AddImmutable(4, 5) fmt.Println(s.Members()) [1 2 3] fmt.Println(s2.Members()) [1 2 3 4 5]
func (Set[E]) Clear ¶
func (s Set[E]) Clear()
Clear removes all values from the set.
Example:
s := NewSet(1, 2, 3) s.Clear() fmt.Println(s.Members()) []
func (Set[E]) Contains ¶
Contains returns true if the set contains the given value.
Example:
s := NewSet(1, 2, 3) fmt.Println(s.Contains(2)) true
func (Set[E]) ContainsAll ¶
ContainsAll returns true if the set contains all the given values.
Example:
s := NewSet(1, 2, 3) fmt.Println(s.ContainsAll(2, 3)) true
func (Set[E]) Difference ¶
Difference returns the difference of the set with another set.
Example:
s1 := NewSet(1, 2, 3) s2 := NewSet(2, 3, 4) fmt.Println(s1.Difference(s2)) [1]
func (Set[E]) Equal ¶
Equal returns true if the set is equal to another set.
Example:
s1 := NewSet(1, 2, 3) s2 := NewSet(1, 2, 3) fmt.Println(s1.Equal(s2)) true
func (Set[E]) Intersection ¶
Intersection returns the intersection of the set with another set.
Example:
s1 := NewSet(1, 2, 3) s2 := NewSet(2, 3, 4) fmt.Println(s1.Intersection(s2)) [3]
func (Set[E]) IsSubsetOf ¶
IsSubsetOf returns true if the set is a subset of another set.
Example:
s1 := NewSet(1, 2) s2 := NewSet(1, 2, 3) fmt.Println(s1.IsSubsetOf(s2)) true
func (Set[E]) IsSupersetOf ¶
IsSupersetOf returns true if the set is a superset of another set.
Example:
s1 := NewSet(1, 2, 3) s2 := NewSet(1, 2) fmt.Println(s1.IsSupersetOf(s2)) true
func (Set[E]) Len ¶
Len returns the number of elements in the set.
Example:
s := NewSet(1, 2, 3) fmt.Println(s.Len()) 3
func (Set[E]) MarshalJSON ¶
MarshalJSON implements the json.Marshaler interface to convert the map into a JSON object.
func (Set[E]) Members ¶
func (s Set[E]) Members() []E
Members returns the members of the set as a slice.
Example:
s := NewSet(1, 2, 3) fmt.Println(s.Members()) [1 2 3]
func (Set[E]) Remove ¶
func (s Set[E]) Remove(vals ...E)
Remove removes the given values from the set.
Example:
s := NewSet(1, 2, 3) s.Remove(2, 3) fmt.Println(s.Members()) [1]
func (Set[E]) RemoveImmutable ¶
RemoveImmutable removes the given values from the set and returns a new set.
Example:
s := NewSet(1, 2, 3) s2 := s.RemoveImmutable(2, 3) fmt.Println(s1.Members()) [1 2 3] fmt.Println(s2.Members()) [1]
func (Set[E]) String ¶
String returns a string representation of the set. Order is not guaranteed.
Example:
s := NewSet(1, 2, 3) fmt.Println(s) [1 2 3] *or* [3 2 1] *or* [2 1 3] *or* ...
func (Set[E]) Union ¶
Union returns the union of the set with another set.
Example:
s1 := NewSet(1, 2, 3) s2 := NewSet(2, 3, 4) fmt.Println(s1.Union(s2)) [1 2 3 4 5]
func (Set[E]) UnmarshalJSON ¶
UnmarshalJSON implements the json.Unmarshaler interface to convert a json object to a Set.
The JSON must start with a list.