set

package
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Jul 4, 2025 License: Apache-2.0 Imports: 7 Imported by: 2

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

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:

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

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 New

func New[T comparable](members ...T) *Set[T]

New creates a new set with initial members.

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]) Add

func (s *Set[T]) Add(v T) bool

Add adds element v to set. If element is already member of set, return false.

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

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

Clone returns a copy of the set.

💡 NOTE: Members are copied using assignment (=).

func (*Set[T]) Contains

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

Contains returns true if element v is member of set.

func (*Set[T]) ContainsAll

func (s *Set[T]) ContainsAll(vs ...T) bool

ContainsAll returns true if all elements are member of set.

💡 NOTE: If no element given, ContainsAll always return true.

func (*Set[T]) ContainsAny

func (s *Set[T]) ContainsAny(vs ...T) bool

ContainsAny returns true if one of elements is member of set.

💡 NOTE: If no element given, ContainsAny always return false.

func (*Set[T]) Diff

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

Diff returns the difference of sets as a new set.

func (*Set[T]) DiffInplace

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

DiffInplace removes all elements of set other from set s.

func (*Set[T]) Equal

func (s *Set[T]) Equal(other *Set[T]) bool

Equal returns whether set s and other are equal.

func (*Set[T]) Intersect

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

Intersect returns the intersection of sets as a new set.

func (*Set[T]) IntersectInplace

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

IntersectInplace updates set s with the intersection of itself and set other.

func (*Set[T]) IsSubset

func (s *Set[T]) IsSubset(other *Set[T]) bool

IsSubset returns whether another set contains this set.

func (*Set[T]) IsSuperset

func (s *Set[T]) IsSuperset(other *Set[T]) bool

IsSuperset returns whether this set contains another set.

func (*Set[T]) Len

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

Len returns the number of elements of set s. The complexity is O(1).

func (*Set[T]) MarshalJSON

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

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

func (s *Set[T]) Range(f func(T) bool)

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

func (s *Set[T]) Remove(v T) bool

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

func (s *Set[T]) String() 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]) Union

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

Union returns the unions of sets as a new set.

func (*Set[T]) UnionInplace

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

UnionInplace updates set s with union itself and set other.

func (*Set[T]) UnmarshalJSON

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

UnmarshalJSON implements encoding/json.Unmarshaler.

Experimental: This API is experimental and may change in the future.

func (*Set[T]) Update

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

Update is alias of Set.UnionInplace.

Jump to

Keyboard shortcuts

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