Documentation
¶
Overview ¶
Package set provides a collection that contains no duplicate elements for comparable type.
💡 NOTE: Set is not concurrent-safe. If you need a high-performance, scalable, concurrent-safe set, use github.com/bytedance/gg/collection/skipset.
Structures ¶
Operations ¶
- Constructor: New, …
- CRUD operations: set.Set.Add, set.Set.Remove, set.Set.Contains, set.Set.ContainsAll, set.Set.ContainsAny, …
- Set operations: set.Set.Union, set.Set.Intersect, set.Set.Diff, … and its variants set.Set.UnionInplace, set.Set.IntersectInplace, …
- Predicates: set.Set.Equal, set.Set.IsSubset, set.Set.IsSuperset, …
- Conversion: set.Set.String, set.Set.ToSlice, …
- Range operations: set.Set.Range, set.Set.Iter, …
JSON ¶
set.Set implements encoding/json.Marshaler and encoding/json.Unmarshaler, so you can use it in JSON marshaling/unmarshaling. See set.Set.MarshalJSON and set.Set.UnmarshalJSON.
Unspecified iteration order ¶
As set.Set.Range said, the iteration order over sets is not specified.
If you need fixed order iteration, you can:
- Use set.Set.ToSlice and use github.com/bytedance/gg/gslice.Sort before iteration
Example ¶
s := New(10, 10, 12, 15) fmt.Println(s.Len()) // 3 fmt.Println(s.Add(10)) // false fmt.Println(s.Add(11)) // true fmt.Println(s.Remove(11) && s.Remove(12)) // true fmt.Println(s) // set[10 15] fmt.Println(s.ContainsAny(10, 15)) // true fmt.Println(s.ContainsAny(11, 12)) // false fmt.Println(s.ContainsAny()) // false fmt.Println(s.ContainsAll(10, 15)) // true fmt.Println(s.ContainsAll(10, 11)) // false fmt.Println(s.ContainsAll()) // true fmt.Println(len(s.ToSlice())) // 2
Output: 3 false true true set[10 15] true false false true false true 2
Index ¶
- type Set
- func (s *Set[T]) Add(v T) bool
- func (s *Set[T]) AddN(vs ...T)
- func (s *Set[T]) Clone() *Set[T]
- func (s *Set[T]) Contains(v T) bool
- func (s *Set[T]) ContainsAll(vs ...T) bool
- func (s *Set[T]) ContainsAny(vs ...T) bool
- func (s *Set[T]) Diff(other *Set[T]) *Set[T]
- func (s *Set[T]) DiffInplace(other *Set[T])
- func (s *Set[T]) Equal(other *Set[T]) bool
- func (s *Set[T]) Intersect(other *Set[T]) *Set[T]
- func (s *Set[T]) IntersectInplace(other *Set[T])
- func (s *Set[T]) IsSubset(other *Set[T]) bool
- func (s *Set[T]) IsSuperset(other *Set[T]) bool
- func (s *Set[T]) Len() int
- func (s *Set[T]) MarshalJSON() ([]byte, error)
- func (s *Set[T]) Range(f func(T) bool)
- func (s *Set[T]) Remove(v T) bool
- func (s *Set[T]) RemoveN(vs ...T)
- func (s *Set[T]) String() string
- func (s *Set[T]) ToSlice() []T
- func (s *Set[T]) Union(other *Set[T]) *Set[T]
- func (s *Set[T]) UnionInplace(other *Set[T])
- func (s *Set[T]) UnmarshalJSON(data []byte) error
- func (s *Set[T]) Update(other *Set[T])
Examples ¶
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 is a set for comparable type.
💡 NOTE: Set is not concurrent-safe.
If you need a high-performance, scalable, concurrent-safe set, use [github.com/bytedance/gg/collection/skipset].
func NewWithCap ¶ added in v1.1.0
func NewWithCap[T comparable](capacity int) *Set[T]
NewWithCap creates a new set with capacity.
func (*Set[T]) AddN ¶
func (s *Set[T]) AddN(vs ...T)
AddN is a variant of set.Set.Add, adds multiple elements to set. It will not tell you which elements have been successfully added.
func (*Set[T]) Clone ¶
Clone returns a copy of the set.
💡 NOTE: Members are copied using assignment (=).
func (*Set[T]) ContainsAll ¶
ContainsAll returns true if all elements are member of set.
💡 NOTE: If no element given, ContainsAll always return true.
func (*Set[T]) ContainsAny ¶
ContainsAny returns true if one of elements is member of set.
💡 NOTE: If no element given, ContainsAny always return false.
func (*Set[T]) DiffInplace ¶
DiffInplace removes all elements of set other from set s.
func (*Set[T]) IntersectInplace ¶
IntersectInplace updates set s with the intersection of itself and set other.
func (*Set[T]) IsSuperset ¶
IsSuperset returns whether this set contains another set.
func (*Set[T]) MarshalJSON ¶
MarshalJSON implements encoding/json.Marshaler.
NOTE: The returned bytes is null or JSON array. Elements of array are sorted lexicographically.
Experimental: This API is experimental and may change in the future.
func (*Set[T]) Range ¶
Range calls f sequentially for each member in the set. If f returns false, range stops the iteration.
💡 NOTE: The iteration order over sets is not specified and is not guaranteed to be the same from one iteration to the next.
func (*Set[T]) Remove ¶
Remove removes element v from set. If element is not member of set, return false.
func (*Set[T]) RemoveN ¶
func (s *Set[T]) RemoveN(vs ...T)
RemoveN is a variant of set.Set.Remove, removes multiple elements from set. It will not tell you which elements have been successfully removed.
func (*Set[T]) String ¶
String implements fmt.Stringer.
Experimental: This API is experimental and may change in the future.
func (*Set[T]) ToSlice ¶
func (s *Set[T]) ToSlice() []T
ToSlice collects all members to slice.
💡 NOTE: The order of returned slice is not specified and is not guaranteed to be the same from another ToSlice call.
func (*Set[T]) UnionInplace ¶
UnionInplace updates set s with union itself and set other.
func (*Set[T]) UnmarshalJSON ¶
UnmarshalJSON implements encoding/json.Unmarshaler.
Experimental: This API is experimental and may change in the future.