set

package module
v0.1.14 Latest Latest
Warning

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

Go to latest
Published: Aug 17, 2023 License: MPL-2.0 Imports: 3 Imported by: 45

README

go-set

Go Reference Run CI Tests GitHub

The go-set repository provides a set package containing a few generic Set implementations for Go.

Each implementation is optimal for a particular use case.

Set is ideal for comparable types.

  • backed by map builtin
  • commonly used with string, int, simple struct types, etc.

HashSet is useful for types that implement a Hash() function.

  • backed by map builtin
  • commonly used with complex structs

TreeSet is useful for comparable data (via Compare[T])

  • backed by Red-Black Binary Search Tree
  • commonly used with complex structs with extrinsic order
  • efficient iteration in sort order
  • additional methods Min / Max / TopK / BottomK

This package is not thread-safe.

Documentation

The full documentation is available on pkg.go.dev.

Motivation

Package set helps reduce the boiler plate of using a map[<type>]struct{} as a set.

Say we want to de-duplicate a slice of strings

items := []string{"mitchell", "armon", "jack", "dave", "armon", "dave"}

A typical example of the classic way using map built-in:

m := make(map[string]struct{})
for _, item := range items {
  m[item] = struct{}{}
}
list := make([]string, 0, len(items))
for k := range m {
  list = append(list, k)
}

The same result, but in one line using package go-set.

list := set.From[string](items).Slice()

Set

The go-set package includes Set for types that satisfy the comparable constraint. Note that complex structs (i.e. is a pointer or contains pointer fields) will not be "comparable" in the sense of deep equality, but rather in the sense of pointer addresses. The Set type should be used with builtin types like string, int, or simple struct types with no pointers.

HashSet

The go-set package includes HashSet for types that implement a Hash() function. The custom type must satisfy HashFunc[H Hash] - essentially any Hash() function that returns a string or integer. This enables types to use string-y hash functions like md5, sha1, or even GoString(), but also enables types to implement an efficient hash function using a hash code based on prime multiples.

TreeSet

The go-set package includes TreeSet for creating sorted sets. A TreeSet may be used with any type T as the comparison between elements is provided by implementing Compare[T]. The Cmp[builtin] helper provides a convenient implementation of Compare for builtin types like string or int. A TreeSet is backed by an underlying balanced binary search tree, making operations like in-order traversal efficient, in addition to enabling functions like Min(), Max(), TopK(), and BottomK().

Methods

Implements the following set operations

  • Insert
  • InsertSlice
  • InsertSet
  • Remove
  • RemoveSlice
  • RemoveSet
  • RemoveFunc
  • Contains
  • ContainsAll
  • ContainsSlice
  • Subset
  • Size
  • Empty
  • Union
  • Difference
  • Intersect

Provides helper methods

  • Equal
  • Copy
  • Slice
  • String

TreeSet helper methods

  • Min
  • Max
  • TopK
  • BottomK
  • FirstAbove
  • FirstAboveEqual
  • Above
  • AboveEqual
  • FirstBelow
  • FirstBelowEqual
  • Below
  • BelowEqual

Install

go get github.com/hashicorp/go-set@latest
import "github.com/hashicorp/go-set"

Set Examples

Below are simple example usages of Set

s := set.New[int](10)
s.Insert(1)
s.InsertSlice([]int{2, 3, 4})
s.Size()
s := set.From[string]([]string{"one", "two", "three"})
s.Contains("three")
s.Remove("one")
a := set.From[int]([]int{2, 4, 6, 8})
b := set.From[int]([]int{4, 5, 6})
a.Intersect(b)

HashSet Examples

Below are simple example usages of HashSet

(using a hash code)

type inventory struct {
    item   int
    serial int
}

func (i *inventory) Hash() int {
    code := 3 * item * 5 * serial
    return code
}

i1 := &inventory{item: 42, serial: 101}

s := set.NewHashSet[*inventory, int](10)
s.Insert(i1)

(using a string hash)

type employee struct {
    name string
    id   int
}

func (e *employee) Hash() string {
    return fmt.Sprintf("%s:%d", e.name, e.id)
}

e1 := &employee{name: "armon", id: 2}

s := set.NewHashSet[*employee, string](10)
s.Insert(e1)

TreeSet Examples

Below are simple example usages of TreeSet

(using Cmp as Compare)

ts := NewTreeSet[int, Compare[int]](Cmp[int])
ts.Insert(5)

(using custom Compare)

type waypoint struct {
    distance int
    name     string
}

cmp := func(w1, w2 *waypoint) int {
    return w1.distance - w2.distance
}

ts := NewTreeSet[*waypoint, Compare[*waypoint]](cmp)
ts.Insert(&waypoint{distance: 42, name: "tango"})
ts.Insert(&waypoint{distance: 13, name: "alpha"})
ts.Insert(&waypoint{distance: 71, name: "xray"})

Documentation

Overview

Package set provides a basic generic set implementation.

https://en.wikipedia.org/wiki/Set_(mathematics)

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Cmp added in v0.1.10

func Cmp[B BuiltIn](x, y B) int

Cmp is a Compare function for the specified builtin type B.

Common to use with string, int, etc.

Example (Ints)
s := NewTreeSet[int, Compare[int]](Cmp[int])
s.Insert(50)
s.Insert(42)
s.Insert(100)

fmt.Println(s)
fmt.Println("min:", s.Min())
fmt.Println("max:", s.Max())
Output:

[42 50 100]
min: 42
max: 100
Example (Strings)
s := NewTreeSet[string, Compare[string]](Cmp[string])
s.Insert("red")
s.Insert("green")
s.Insert("blue")

fmt.Println(s)
fmt.Println("min:", s.Min())
fmt.Println("max:", s.Max())
Output:

[blue green red]
min: blue
max: red

func InsertSetFunc added in v0.1.13

func InsertSetFunc[T, E any](a Common[T], b Common[E], transform func(T) E) bool

InsertSetFunc inserts the elements of a into b, applying the transform function to each element before insertion.

Returns true if b was modified as a result.

func InsertSliceFunc added in v0.1.13

func InsertSliceFunc[T, E any](s Common[T], items []E, f func(element E) T)

InsertSliceFunc inserts all elements from the slice into the set

func SliceFunc added in v0.1.13

func SliceFunc[T, E any](s Common[T], transform func(T) E) []E

SliceFunc produces a slice of the elements in s, applying the transform function to each element first.

Types

type BuiltIn added in v0.1.10

type BuiltIn interface {
	~string | ~int | ~uint | ~int64 | ~uint64 | ~int32 | ~uint32 | ~int16 | ~uint16 | ~int8 | ~uint8
}

BuiltIn types compatible with Cmp

type Common added in v0.1.13

type Common[T any] interface {

	// Slice returns a slice of all elements in the set.
	//
	// Note: order of elements depends on the underlying implementation.
	Slice() []T

	// Insert an element into the set.
	//
	// Returns true if the set is modified as a result.
	Insert(T) bool

	// InsertSlice inserts all elements from the slice into the set.
	//
	// Returns true if the set was modified as a result.
	InsertSlice([]T) bool

	// Size returns the number of elements in the set.
	Size() int

	// ForEach will call the callback function for each element in the set.
	// If the callback returns false, the iteration will stop.
	//
	// Note: iteration order depends on the underlying implementation.
	ForEach(func(T) bool)
}

Common is a minimal interface that all sets implement.

type Compare added in v0.1.10

type Compare[T any] func(T, T) int

Compare represents a function that compares two elements.

Must return < 0 if the first parameter is less than the second parameter 0 if the two parameters are equal > 0 if the first parameters is greater than the second parameter

Example (Contestant)
type contestant struct {
	name  string
	score int
}

compare := func(a, b contestant) int {
	return a.score - b.score
}

s := NewTreeSet[contestant, Compare[contestant]](compare)
s.Insert(contestant{name: "alice", score: 80})
s.Insert(contestant{name: "dave", score: 90})
s.Insert(contestant{name: "bob", score: 70})

fmt.Println(s)
Output:

[{bob 70} {alice 80} {dave 90}]

type Hash added in v0.1.5

type Hash interface {
	~string | ~int | ~uint | ~int64 | ~uint64 | ~int32 | ~uint32 | ~int16 | ~uint16 | ~int8 | ~uint8
}

Hash represents the output type of a Hash() function defined on a type.

A Hash could be string-like or int-like. A string hash could be something like and md5, sha1, or GoString() representation of a type. An int hash could be something like the prime multiple hash code of a type.

type HashFunc added in v0.1.5

type HashFunc[H Hash] interface {
	Hash() H
}

HashFunc is a generic type constraint for any type that implements a Hash() method with a Hash return type.

type HashSet added in v0.1.5

type HashSet[T HashFunc[H], H Hash] struct {
	// contains filtered or unexported fields
}

HashSet is a generic implementation of the mathematical data structure, oriented around the use of a HashFunc to make hash values from other types.

func HashSetFrom added in v0.1.5

func HashSetFrom[T HashFunc[H], H Hash](items []T) *HashSet[T, H]

HashSetFrom creates a new HashSet containing each item in items.

T must implement HashFunc[H], where H is of type Hash. This allows custom types that include non-comparable fields to provide their own hash algorithm.

func NewHashSet added in v0.1.5

func NewHashSet[T HashFunc[H], H Hash](size int) *HashSet[T, H]

NewHashSet creates a HashSet with underlying capacity of size.

A HashSet will automatically grow or shrink its capacity as items are added or removed.

T must implement HashFunc[H], where H is of Hash type. This allows custom types that include non-comparable fields to provide their own hash algorithm.

func (*HashSet[T, H]) Contains added in v0.1.5

func (s *HashSet[T, H]) Contains(item T) bool

Contains returns whether item is present in s.

Example
anna := &person{name: "anna", id: 94}
bill := &person{name: "bill", id: 50}
carl := &person{name: "carl", id: 10}
dave := &person{name: "dave", id: 32}
s := HashSetFrom[*person, string]([]*person{anna, bill, carl})

fmt.Println(s.Contains(anna))
fmt.Println(s.Contains(dave))
Output:

true
false

func (*HashSet[T, H]) ContainsAll added in v0.1.5

func (s *HashSet[T, H]) ContainsAll(items []T) bool

ContainsAll returns whether s contains at least every item in items.

Example
anna := &person{name: "anna", id: 94}
bill := &person{name: "bill", id: 50}
carl := &person{name: "carl", id: 10}
dave := &person{name: "dave", id: 32}
s := HashSetFrom[*person, string]([]*person{anna, bill, carl})

fmt.Println(s.ContainsAll([]*person{anna, bill}))
fmt.Println(s.ContainsAll([]*person{anna, dave}))
Output:

true
false

func (*HashSet[T, H]) ContainsSlice added in v0.1.5

func (s *HashSet[T, H]) ContainsSlice(items []T) bool

ContainsSlice returns whether s contains the same set of of elements that are in items. The elements of items may contain duplicates.

If the slice is known to be set-like (no duplicates), EqualSlice provides a more efficient implementation.

Example
anna := &person{name: "anna", id: 94}
bill := &person{name: "bill", id: 50}
carl := &person{name: "carl", id: 10}
dave := &person{name: "dave", id: 32}
s := HashSetFrom[*person, string]([]*person{anna, bill, carl})

fmt.Println(s.ContainsSlice([]*person{anna, bill}))
fmt.Println(s.ContainsSlice([]*person{anna, bill, carl}))
fmt.Println(s.ContainsSlice([]*person{carl, dave}))
Output:

false
true
false

func (*HashSet[T, H]) Copy added in v0.1.5

func (s *HashSet[T, H]) Copy() *HashSet[T, H]

Copy creates a shallow copy of s.

Example
anna := &person{name: "anna", id: 94}
bill := &person{name: "bill", id: 50}
carl := &person{name: "carl", id: 10}
s := HashSetFrom[*person, string]([]*person{anna, bill, carl})
c := s.Copy()

fmt.Println(c)
Output:

[anna bill carl]

func (*HashSet[T, H]) Difference added in v0.1.5

func (s *HashSet[T, H]) Difference(o *HashSet[T, H]) *HashSet[T, H]

Difference returns a set that contains elements of s that are not in o.

Example
anna := &person{name: "anna", id: 94}
bill := &person{name: "bill", id: 50}
carl := &person{name: "carl", id: 10}
dave := &person{name: "dave", id: 32}
s1 := HashSetFrom[*person, string]([]*person{anna, bill, carl})
s2 := HashSetFrom[*person, string]([]*person{anna, bill, dave})
difference := s1.Difference(s2)

fmt.Println(s1)
fmt.Println(s2)
fmt.Println(difference)
Output:

[anna bill carl]
[anna bill dave]
[carl]

func (*HashSet[T, H]) Empty added in v0.1.7

func (s *HashSet[T, H]) Empty() bool

Empty returns true if s contains no elements, false otherwise.

Example
s := NewHashSet[*person, string](0)

fmt.Println(s.Empty())
Output:

true

func (*HashSet[T, H]) Equal added in v0.1.5

func (s *HashSet[T, H]) Equal(o *HashSet[T, H]) bool

Equal returns whether s and o contain the same elements.

Example
anna := &person{name: "anna", id: 94}
bill := &person{name: "bill", id: 50}
carl := &person{name: "carl", id: 10}
dave := &person{name: "dave", id: 32}

s1 := HashSetFrom[*person, string]([]*person{anna, bill, carl})
s2 := HashSetFrom[*person, string]([]*person{anna, bill, carl})
s3 := HashSetFrom[*person, string]([]*person{anna, bill, dave})

fmt.Println(s1.Equal(s2))
fmt.Println(s1.Equal(s3))
Output:

true
false

func (*HashSet[T, H]) EqualSlice added in v0.1.5

func (s *HashSet[T, H]) EqualSlice(items []T) bool

EqualSlice returns whether s and items contain the same elements.

If items contains duplicates EqualSlice will return false; it is assumed that items is itself set-like. For comparing equality with a slice that may contain duplicates, use ContainsSlice.

func (*HashSet[T, H]) ForEach added in v0.1.13

func (s *HashSet[T, H]) ForEach(visit func(T) bool)

func (*HashSet[T, H]) Insert added in v0.1.5

func (s *HashSet[T, H]) Insert(item T) bool

Insert item into s.

Return true if s was modified (item was not already in s), false otherwise.

Example
s := NewHashSet[*person, string](10)
s.Insert(&person{name: "dave", id: 108})
s.Insert(&person{name: "armon", id: 101})
s.Insert(&person{name: "mitchell", id: 100})
s.Insert(&person{name: "armon", id: 101})

fmt.Println(s)
Output:

[armon dave mitchell]

func (*HashSet[T, H]) InsertAll deprecated added in v0.1.5

func (s *HashSet[T, H]) InsertAll(items []T) bool

InsertAll will insert each item in items into s.

Return true if s was modified (at least one item was not already in s), false otherwise.

Deprecated: use InsertSlice instead.

func (*HashSet[T, H]) InsertSet added in v0.1.5

func (s *HashSet[T, H]) InsertSet(o *HashSet[T, H]) bool

InsertSet will insert each element of o into s.

Return true if s was modified (at least one item of o was not already in s), false otherwise.

Example
anna := &person{name: "anna", id: 94}
bill := &person{name: "bill", id: 50}
carl := &person{name: "carl", id: 10}
dave := &person{name: "dave", id: 32}
s1 := HashSetFrom[*person, string]([]*person{anna, carl})
s2 := HashSetFrom[*person, string]([]*person{carl, dave, bill})
s2.InsertSet(s1)

fmt.Println(s1)
fmt.Println(s2)
Output:

[anna carl]
[anna bill carl dave]

func (*HashSet[T, H]) InsertSlice added in v0.1.10

func (s *HashSet[T, H]) InsertSlice(items []T) bool

InsertSlice will insert each item in items into s.

Return true if s was modified (at least one item was not already in s), false otherwise.

Example
s := NewHashSet[*person, string](10)
s.InsertSlice([]*person{
	{name: "dave", id: 108},
	{name: "mitchell", id: 100},
	{name: "dave", id: 108},
	{name: "armon", id: 101},
})

fmt.Println(s)
Output:

[armon dave mitchell]

func (*HashSet[T, H]) Intersect added in v0.1.5

func (s *HashSet[T, H]) Intersect(o *HashSet[T, H]) *HashSet[T, H]

Intersect returns a set that contains elements that are present in both s and o.

Example
anna := &person{name: "anna", id: 94}
bill := &person{name: "bill", id: 50}
carl := &person{name: "carl", id: 10}
dave := &person{name: "dave", id: 32}
s1 := HashSetFrom[*person, string]([]*person{anna, bill, carl})
s2 := HashSetFrom[*person, string]([]*person{anna, bill, dave})
intersect := s1.Intersect(s2)

fmt.Println(s1)
fmt.Println(s2)
fmt.Println(intersect)
Output:

[anna bill carl]
[anna bill dave]
[anna bill]

func (*HashSet[T, H]) List deprecated added in v0.1.5

func (s *HashSet[T, H]) List() []T

List creates a copy of s as a slice.

Deprecated: use Slice() instead.

func (*HashSet[T, H]) MarshalJSON added in v0.1.11

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

MarshalJSON implements the json.Marshaler interface.

func (*HashSet[T, H]) Remove added in v0.1.5

func (s *HashSet[T, H]) Remove(item T) bool

Remove will remove item from s.

Return true if s was modified (item was present), false otherwise.

Example
anna := &person{name: "anna", id: 94}
bill := &person{name: "bill", id: 50}
carl := &person{name: "carl", id: 10}
dave := &person{name: "dave", id: 32}
s := HashSetFrom[*person, string]([]*person{anna, carl, dave, bill})

fmt.Println(s)

s.Remove(carl)

fmt.Println(s)
Output:

[anna bill carl dave]
[anna bill dave]

func (*HashSet[T, H]) RemoveAll deprecated added in v0.1.5

func (s *HashSet[T, H]) RemoveAll(items []T) bool

RemoveAll will remove each item in items from s.

Return true if s was modified (any item was present), false otherwise.

Deprecated: use RemoveSlice instead.

func (*HashSet[T, H]) RemoveFunc added in v0.1.9

func (s *HashSet[T, H]) RemoveFunc(f func(item T) bool) bool

RemoveFunc will remove each element from s that satisfies condition f.

Return true if s was modified, false otherwise.

Example
anna := &person{name: "anna", id: 94}
bill := &person{name: "bill", id: 50}
carl := &person{name: "carl", id: 10}
dave := &person{name: "dave", id: 32}
s := HashSetFrom[*person, string]([]*person{anna, bill, carl, dave})

idAbove50 := func(p *person) bool {
	return p.id >= 50
}

fmt.Println(s)

s.RemoveFunc(idAbove50)

fmt.Println(s)
Output:

[anna bill carl dave]
[carl dave]

func (*HashSet[T, H]) RemoveSet added in v0.1.5

func (s *HashSet[T, H]) RemoveSet(o *HashSet[T, H]) bool

RemoveSet will remove each element of o from s.

Return true if s was modified (any item of o was present in s), false otherwise.

Example
anna := &person{name: "anna", id: 94}
bill := &person{name: "bill", id: 50}
carl := &person{name: "carl", id: 10}
dave := &person{name: "dave", id: 32}
s := HashSetFrom[*person, string]([]*person{carl, dave, bill})
r := HashSetFrom[*person, string]([]*person{anna, carl})

fmt.Println(s)

s.RemoveSet(r)

fmt.Println(s)
Output:

[bill carl dave]
[bill dave]

func (*HashSet[T, H]) RemoveSlice added in v0.1.10

func (s *HashSet[T, H]) RemoveSlice(items []T) bool

RemoveSlice will remove each item in items from s.

Return true if s was modified (any item was present), false otherwise.

Example
anna := &person{name: "anna", id: 94}
bill := &person{name: "bill", id: 50}
carl := &person{name: "carl", id: 10}
dave := &person{name: "dave", id: 32}
s := HashSetFrom[*person, string]([]*person{anna, carl, dave, bill})

fmt.Println(s)

s.RemoveSlice([]*person{anna, carl})

fmt.Println(s)
Output:

[anna bill carl dave]
[bill dave]

func (*HashSet[T, H]) Size added in v0.1.5

func (s *HashSet[T, H]) Size() int

Size returns the cardinality of s.

Example
anna := &person{name: "anna", id: 94}
bill := &person{name: "bill", id: 50}
carl := &person{name: "carl", id: 10}
s := HashSetFrom[*person, string]([]*person{anna, bill, carl})

fmt.Println(s.Size())
Output:

3

func (*HashSet[T, H]) Slice added in v0.1.8

func (s *HashSet[T, H]) Slice() []T

Slice creates a copy of s as a slice.

The result is not ordered.

Example
anna := &person{name: "anna", id: 94}
bill := &person{name: "bill", id: 50}
carl := &person{name: "carl", id: 10}
s := HashSetFrom[*person, string]([]*person{anna, bill, carl})

slice := s.Slice()
sort.Slice(slice, func(a, b int) bool {
	return slice[a].id < slice[b].id
})

fmt.Println(slice)
Output:

[carl bill anna]

func (*HashSet[T, H]) String added in v0.1.5

func (s *HashSet[T, H]) String() string

String creates a string representation of s, using "%v" printf formatting to transform each element into a string. The result contains elements sorted by their lexical string order.

Example
anna := &person{name: "anna", id: 94}
bill := &person{name: "bill", id: 50}
carl := &person{name: "carl", id: 10}
s := HashSetFrom[*person, string]([]*person{anna, bill, carl})

fmt.Println(s.String())
Output:

[anna bill carl]

func (*HashSet[T, H]) StringFunc added in v0.1.8

func (s *HashSet[T, H]) StringFunc(f func(element T) string) string

StringFunc creates a string representation of s, using f to transform each element into a string. The result contains elements sorted by their string order.

func (*HashSet[T, H]) Subset added in v0.1.5

func (s *HashSet[T, H]) Subset(o *HashSet[T, H]) bool

Subset returns whether o is a subset of s.

Example
anna := &person{name: "anna", id: 94}
bill := &person{name: "bill", id: 50}
carl := &person{name: "carl", id: 10}
dave := &person{name: "dave", id: 32}
s1 := HashSetFrom[*person, string]([]*person{anna, bill, carl})
s2 := HashSetFrom[*person, string]([]*person{anna, bill})
s3 := HashSetFrom[*person, string]([]*person{bill, carl, dave})

fmt.Println(s1.Subset(s2))
fmt.Println(s1.Subset(s3))
Output:

true
false

func (*HashSet[T, H]) Union added in v0.1.5

func (s *HashSet[T, H]) Union(o *HashSet[T, H]) *HashSet[T, H]

Union returns a set that contains all elements of s and o combined.

Example
anna := &person{name: "anna", id: 94}
bill := &person{name: "bill", id: 50}
carl := &person{name: "carl", id: 10}
dave := &person{name: "dave", id: 32}
s1 := HashSetFrom[*person, string]([]*person{anna, bill, carl})
s2 := HashSetFrom[*person, string]([]*person{anna, bill, dave})
union := s1.Union(s2)

fmt.Println(s1)
fmt.Println(s2)
fmt.Println(union)
Output:

[anna bill carl]
[anna bill dave]
[anna bill carl dave]

func (*HashSet[T, H]) UnmarshalJSON added in v0.1.11

func (s *HashSet[T, H]) UnmarshalJSON(data []byte) error

UnmarshalJSON implements the json.Unmarshaler interface.

type Set

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

Set is a simple, generic implementation of the set mathematical data structure. It is optimized for correctness and convenience, as a replacement for the use of map[interface{}]struct{}.

func From

func From[T comparable](items []T) *Set[T]

From creates a new Set containing each item in items.

T must *not* be of pointer type, nor contain pointer fields, which are comparable but not in the way you expect. For these types, use HashSet instead.

func FromFunc added in v0.1.3

func FromFunc[A any, T comparable](items []A, conversion func(A) T) *Set[T]

FromFunc creates a new Set containing a conversion of each item in items.

T must *not* be of pointer type, nor contain pointer fields, which are comparable but not in the way you expect. For these types, use HashSet instead.

func New

func New[T comparable](size int) *Set[T]

New creates a new Set with initial underlying capacity of size.

A Set will automatically grow or shrink its capacity as items are added or removed.

T must *not* be of pointer type, nor contain pointer fields, which are comparable but not in the way you expect. For these types, use HashSet instead.

func (*Set[T]) Contains

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

Contains returns whether item is present in s.

Example
s := From([]string{"red", "green", "blue"})

fmt.Println(s.Contains("red"))
fmt.Println(s.Contains("orange"))
Output:

true
false

func (*Set[T]) ContainsAll

func (s *Set[T]) ContainsAll(items []T) bool

ContainsAll returns whether s contains at least every item in items.

Example
s := From([]string{"red", "green", "blue"})

fmt.Println(s.ContainsAll([]string{"red", "blue"}))
fmt.Println(s.ContainsAll([]string{"red", "orange"}))
Output:

true
false

func (*Set[T]) ContainsSlice added in v0.1.4

func (s *Set[T]) ContainsSlice(items []T) bool

ContainsSlice returns whether s contains the same set of of elements that are in items. The elements of items may contain duplicates.

If the slice is known to be set-like (no duplicates), EqualSlice provides a more efficient implementation.

Example
s := From([]string{"red", "green", "blue"})

fmt.Println(s.ContainsSlice([]string{"red", "blue"}))
fmt.Println(s.ContainsSlice([]string{"red", "blue", "orange"}))
fmt.Println(s.ContainsSlice([]string{"red", "blue", "green"}))
Output:

false
false
true

func (*Set[T]) Copy

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

Copy creates a copy of s.

Example
s := From([]string{"red", "green", "blue"})
t := s.Copy()

fmt.Println(t)
Output:

[blue green red]

func (*Set[T]) Difference

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

Difference returns a set that contains elements of s that are not in o.

Example
t1 := From([]string{"red", "green", "blue"})
t2 := From([]string{"red", "blue"})
t3 := From([]string{"red", "orange"})

fmt.Println(t1.Difference(t2))
fmt.Println(t1.Difference(t3))
Output:

[green]
[blue green]

func (*Set[T]) Empty added in v0.1.7

func (s *Set[T]) Empty() bool

Empty returns true if s contains no elements, false otherwise.

Example
s := New[string](10)

fmt.Println(s.Empty())
Output:

true

func (*Set[T]) Equal added in v0.1.2

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

Equal returns whether s and o contain the same elements.

Example
t1 := From([]string{"red", "green", "blue"})
t2 := From([]string{"red", "blue"})
t3 := From([]string{"red", "green", "yellow"})
t4 := From([]string{"red", "green", "blue"})

fmt.Println(t1.Equal(t2))
fmt.Println(t1.Equal(t3))
fmt.Println(t1.Equal(t4))
Output:

false
false
true

func (*Set[T]) EqualSlice added in v0.1.4

func (s *Set[T]) EqualSlice(items []T) bool

EqualSlice returns whether s and items contain the same elements.

If items contains duplicates EqualSlice will return false; it is assumed that items is itself set-like. For comparing equality with a slice that may contain duplicates, use ContainsSlice.

func (*Set[T]) ForEach added in v0.1.13

func (s *Set[T]) ForEach(visit func(T) bool)

func (*Set[T]) Insert

func (s *Set[T]) Insert(item T) bool

Insert item into s.

Return true if s was modified (item was not already in s), false otherwise.

Example
s := New[int](10)
s.Insert(1)
s.Insert(1)
s.Insert(2)
s.Insert(3)
s.Insert(2)

fmt.Println(s)
Output:

[1 2 3]

func (*Set[T]) InsertAll deprecated

func (s *Set[T]) InsertAll(items []T) bool

InsertAll will insert each item in items into s.

Return true if s was modified (at least one item was not already in s), false otherwise.

Deprecated: use InsertSlice instead.

func (*Set[T]) InsertSet

func (s *Set[T]) InsertSet(o *Set[T]) bool

InsertSet will insert each element of o into s.

Return true if s was modified (at least one item of o was not already in s), false otherwise.

Example
s := New[int](10)
s.InsertSet(From([]int{1, 1, 2, 3, 2}))

fmt.Println(s)
Output:

[1 2 3]

func (*Set[T]) InsertSlice added in v0.1.10

func (s *Set[T]) InsertSlice(items []T) bool

InsertSlice will insert each item in items into s.

Return true if s was modified (at least one item was not already in s), false otherwise.

Example
s := New[int](10)
s.InsertSlice([]int{1, 1, 2, 3, 2})

fmt.Println(s)
Output:

[1 2 3]

func (*Set[T]) Intersect

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

Intersect returns a set that contains elements that are present in both s and o.

Example
t1 := From([]string{"red", "green", "blue"})
t2 := From([]string{"red", "blue"})
t3 := From([]string{"red", "orange"})
t4 := From([]string{"yellow"})

fmt.Println(t1.Intersect(t2))
fmt.Println(t1.Intersect(t3))
fmt.Println(t1.Intersect(t4))
Output:

[blue red]
[red]
[]

func (*Set[T]) List deprecated added in v0.1.1

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

List creates a copy of s as a slice.

Deprecated: use Slice() instead.

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

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

MarshalJSON implements the json.Marshaler interface.

func (*Set[T]) Remove

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

Remove will remove item from s.

Return true if s was modified (item was present), false otherwise.

Example
s := New[int](10)
s.InsertSlice([]int{1, 1, 2, 3, 2})
s.Remove(2)

fmt.Println(s)
Output:

[1 3]

func (*Set[T]) RemoveAll deprecated

func (s *Set[T]) RemoveAll(items []T) bool

RemoveAll will remove each item in items from s.

Return true if s was modified (any item was present), false otherwise.

Deprecated: use RemoveSlice instead.

func (*Set[T]) RemoveFunc added in v0.1.9

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

RemoveFunc will remove each element from s that satisfies condition f.

Return true if s was modified, false otherwise.

Example
s := From([]int{1, 2, 3, 4, 5, 6, 7, 8, 9, 0})
even := func(i int) bool {
	return i%2 == 0
}
s.RemoveFunc(even)

fmt.Println(s)
Output:

[1 3 5 7 9]

func (*Set[T]) RemoveSet

func (s *Set[T]) RemoveSet(o *Set[T]) bool

RemoveSet will remove each element of o from s.

Return true if s was modified (any item of o was present in s), false otherwise.

Example
s := New[int](10)
s.InsertSlice([]int{1, 1, 2, 3, 2})
s.RemoveSet(From([]int{2, 3}))

fmt.Println(s)
Output:

[1]

func (*Set[T]) RemoveSlice added in v0.1.10

func (s *Set[T]) RemoveSlice(items []T) bool

RemoveSlice will remove each item in items from s.

Return true if s was modified (any item was present), false otherwise.

Example
s := New[int](10)
s.InsertSlice([]int{1, 1, 2, 3, 2})
s.RemoveSlice([]int{2, 3})

fmt.Println(s)
Output:

[1]

func (*Set[T]) Size

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

Size returns the cardinality of s.

Example
s := From([]string{"red", "green", "blue"})

fmt.Println(s.Size())
Output:

3

func (*Set[T]) Slice added in v0.1.8

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

Slice creates a copy of s as a slice. Elements are in no particular order.

Example
s := From([]string{"red", "green", "blue"})
t := s.Slice()

sort.Strings(t)
fmt.Println(t)
Output:

[blue green red]

func (*Set[T]) String added in v0.1.1

func (s *Set[T]) String() string

String creates a string representation of s, using "%v" printf formating to transform each element into a string. The result contains elements sorted by their lexical string order.

Example
s := From([]string{"red", "green", "blue"})

fmt.Println(s.String())
Output:

[blue green red]

func (*Set[T]) StringFunc added in v0.1.8

func (s *Set[T]) StringFunc(f func(element T) string) string

StringFunc creates a string representation of s, using f to transform each element into a string. The result contains elements sorted by their lexical string order.

func (*Set[T]) Subset added in v0.1.2

func (s *Set[T]) Subset(o *Set[T]) bool

Subset returns whether o is a subset of s.

Example
t1 := From([]string{"red", "green", "blue"})
t2 := From([]string{"red", "blue"})
t3 := From([]string{"red", "orange"})

fmt.Println(t1.Subset(t2))
fmt.Println(t1.Subset(t3))
Output:

true
false

func (*Set[T]) Union

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

Union returns a set that contains all elements of s and o combined.

Example
t1 := From([]string{"red", "green", "blue"})
t2 := From([]string{"red", "blue"})
t3 := From([]string{"red", "orange"})

fmt.Println(t1.Union(t2))
fmt.Println(t1.Union(t3))
Output:

[blue green red]
[blue green orange red]

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

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

UnmarshalJSON implements the json.Unmarshaler interface.

type TreeNodeVisit added in v0.1.13

type TreeNodeVisit[T any] func(*node[T]) (next bool)

TreeNodeVisit is a function that is called for each node in the tree.

type TreeSet added in v0.1.10

type TreeSet[T any, C Compare[T]] struct {
	// contains filtered or unexported fields
}

TreeSet provides a generic sortable set implementation for Go. Enables fast storage and retrieval of ordered information. Most effective in cases where data is regularly being added and/or removed and fast lookup properties must be maintained.

The underlying data structure is a Red-Black Binary Search Tree. https://en.wikipedia.org/wiki/Red–black_tree

Not thread safe, and not safe for concurrent modification.

func NewTreeSet added in v0.1.10

func NewTreeSet[T any, C Compare[T]](compare C) *TreeSet[T, C]

NewTreeSet creates a TreeSet of type T, comparing elements via C.

T may be any type.

C is an implementation of Compare[T]. For builtin types, Cmp provides a convenient Compare implementation.

func TreeSetFrom added in v0.1.10

func TreeSetFrom[T any, C Compare[T]](items []T, compare C) *TreeSet[T, C]

TreeSetFrom creates a new TreeSet containing each item in items.

T may be any type.

C is an implementation of Compare[T]. For builtin types, Cmp provides a convenient Compare implementation.

func (*TreeSet[T, C]) Above added in v0.1.12

func (s *TreeSet[T, C]) Above(item T) *TreeSet[T, C]

After returns a TreeSet containing the elements of s that are > item.

Example
s := TreeSetFrom[int, Compare[int]]([]int{1, 2, 3, 4, 5}, Cmp[int])

fmt.Println(s.Above(3))
fmt.Println(s.Above(5))
fmt.Println(s.Above(10))
Output:

[4 5]
[]
[]

func (*TreeSet[T, C]) AboveEqual added in v0.1.12

func (s *TreeSet[T, C]) AboveEqual(item T) *TreeSet[T, C]

AfterEqual returns a TreeSet containing the elements of s that are ≥ item.

Example
s := TreeSetFrom[int, Compare[int]]([]int{1, 2, 3, 4, 5}, Cmp[int])

fmt.Println(s.AboveEqual(3))
fmt.Println(s.AboveEqual(5))
fmt.Println(s.AboveEqual(10))
Output:

[3 4 5]
[5]
[]

func (*TreeSet[T, C]) Below added in v0.1.12

func (s *TreeSet[T, C]) Below(item T) *TreeSet[T, C]

Below returns a TreeSet containing the elements of s that are < item.

Example
s := TreeSetFrom[int, Compare[int]]([]int{1, 2, 3, 4, 5}, Cmp[int])

fmt.Println(s.Below(1))
fmt.Println(s.Below(3))
fmt.Println(s.Below(10))
Output:

[]
[1 2]
[1 2 3 4 5]

func (*TreeSet[T, C]) BelowEqual added in v0.1.12

func (s *TreeSet[T, C]) BelowEqual(item T) *TreeSet[T, C]

BelowEqual returns a TreeSet containing the elements of s that are ≤ item.

Example
s := TreeSetFrom[int, Compare[int]]([]int{1, 2, 3, 4, 5}, Cmp[int])

fmt.Println(s.BelowEqual(1))
fmt.Println(s.BelowEqual(3))
fmt.Println(s.BelowEqual(10))
Output:

[1]
[1 2 3]
[1 2 3 4 5]

func (*TreeSet[T, C]) BottomK added in v0.1.10

func (s *TreeSet[T, C]) BottomK(n int) []T

BottomK returns the bottom n (largest) elements in s, in descending order.

Example
s := TreeSetFrom[int, Compare[int]]([]int{1, 2, 3, 4, 5}, Cmp[int])

fmt.Println(s.BottomK(0))
fmt.Println(s.BottomK(1))
fmt.Println(s.BottomK(3))
fmt.Println(s.BottomK(5))
Output:

[]
[5]
[5 4 3]
[5 4 3 2 1]

func (*TreeSet[T, C]) Contains added in v0.1.10

func (s *TreeSet[T, C]) Contains(item T) bool

Contains returns whether item is present in s.

Example
s := TreeSetFrom[string, Compare[string]]([]string{"red", "green", "blue"}, Cmp[string])

fmt.Println(s.Contains("green"))
fmt.Println(s.Contains("orange"))
Output:

true
false

func (*TreeSet[T, C]) ContainsSlice added in v0.1.10

func (s *TreeSet[T, C]) ContainsSlice(items []T) bool

ContainsSlice returns whether s contains the same set of elements that are in items. The items slice may contain duplicate elements.

If the items slice is known to be set-like (no duplicates), EqualSlice provides a more efficient implementation.

Example
s := TreeSetFrom[string, Compare[string]]([]string{"red", "green", "blue"}, Cmp[string])

fmt.Println(s.ContainsSlice([]string{"red", "green"}))
fmt.Println(s.ContainsSlice([]string{"red", "orange"}))
Output:

true
false

func (*TreeSet[T, C]) Copy added in v0.1.10

func (s *TreeSet[T, C]) Copy() *TreeSet[T, C]

Copy creates a copy of s.

Individual elements are reference copies.

Example
s := TreeSetFrom[int, Compare[int]]([]int{1, 2, 3, 4, 5}, Cmp[int])
c := s.Copy()
s.Remove(2)
s.Remove(4)

fmt.Println(s)
fmt.Println(c)
Output:

[1 3 5]
[1 2 3 4 5]

func (*TreeSet[T, C]) Difference added in v0.1.10

func (s *TreeSet[T, C]) Difference(o *TreeSet[T, C]) *TreeSet[T, C]

Difference returns a set that contains elements of s that are not in o.

Example
s := TreeSetFrom[int, Compare[int]]([]int{1, 2, 3, 4, 5}, Cmp[int])
t := TreeSetFrom[int, Compare[int]]([]int{5, 4, 3, 2, 1}, Cmp[int])
f := TreeSetFrom[int, Compare[int]]([]int{1, 3, 5, 7, 9}, Cmp[int])

fmt.Println(s.Difference(t))
fmt.Println(s.Difference(f))
Output:

[]
[2 4]

func (*TreeSet[T, C]) Empty added in v0.1.10

func (s *TreeSet[T, C]) Empty() bool

Empty returns true if there are no elements in s.

Example
s := TreeSetFrom[string, Compare[string]]([]string{}, Cmp[string])

fmt.Println(s.Empty())

s.InsertSlice([]string{"red", "green", "blue"})

fmt.Println(s.Empty())
Output:

true
false

func (*TreeSet[T, C]) Equal added in v0.1.10

func (s *TreeSet[T, C]) Equal(o *TreeSet[T, C]) bool

Equal return whether s and o contain the same elements.

Example
s := TreeSetFrom[int, Compare[int]]([]int{1, 2, 3, 4, 5}, Cmp[int])
t := TreeSetFrom[int, Compare[int]]([]int{5, 4, 3, 2, 1}, Cmp[int])
f := TreeSetFrom[int, Compare[int]]([]int{1, 3, 5, 7, 9}, Cmp[int])

fmt.Println(s.Equal(t))
fmt.Println(s.Equal(f))
Output:

true
false

func (*TreeSet[T, C]) EqualSlice added in v0.1.10

func (s *TreeSet[T, C]) EqualSlice(items []T) bool

EqualSlice returns whether s and items contain the same elements.

func (*TreeSet[T, C]) FilterSlice added in v0.1.13

func (s *TreeSet[T, C]) FilterSlice(filter func(T) bool) []T

FilterSlice returns the elements of s that satisfy the predicate f.

func (*TreeSet[T, C]) FirstAbove added in v0.1.12

func (s *TreeSet[T, C]) FirstAbove(item T) (T, bool)

FirstAbove returns the first element strictly above item.

A zero value and false are returned if no such element exists.

Example
s := TreeSetFrom[int, Compare[int]]([]int{1, 2, 3, 4, 5}, Cmp[int])

fmt.Println(s.FirstAbove(3))
fmt.Println(s.FirstAbove(5))
fmt.Println(s.FirstAbove(10))
Output:

4 true
0 false
0 false

func (*TreeSet[T, C]) FirstAboveEqual added in v0.1.12

func (s *TreeSet[T, C]) FirstAboveEqual(item T) (T, bool)

FirstAboveEqual returns the first element above item (or item itself if present).

A zero value and false are returned if no such element exists.

Example
s := TreeSetFrom[int, Compare[int]]([]int{1, 2, 3, 4, 5}, Cmp[int])

fmt.Println(s.FirstAboveEqual(3))
fmt.Println(s.FirstAboveEqual(5))
fmt.Println(s.FirstAboveEqual(10))
Output:

3 true
5 true
0 false

func (*TreeSet[T, C]) FirstBelow added in v0.1.12

func (s *TreeSet[T, C]) FirstBelow(item T) (T, bool)

FirstBelow returns the first element strictly below item.

A zero value and false are returned if no such element exists.

Example
s := TreeSetFrom[int, Compare[int]]([]int{1, 2, 3, 4, 5}, Cmp[int])

fmt.Println(s.FirstBelow(1))
fmt.Println(s.FirstBelow(3))
fmt.Println(s.FirstBelow(10))
Output:

0 false
2 true
5 true

func (*TreeSet[T, C]) FirstBelowEqual added in v0.1.12

func (s *TreeSet[T, C]) FirstBelowEqual(item T) (T, bool)

FirstBelowEqual returns the first element below item (or item itself if present).

A zero value and false are returned if no such element exists.

Example
s := TreeSetFrom[int, Compare[int]]([]int{1, 2, 3, 4, 5}, Cmp[int])

fmt.Println(s.FirstBelowEqual(1))
fmt.Println(s.FirstBelowEqual(3))
fmt.Println(s.FirstBelowEqual(10))
Output:

1 true
3 true
5 true

func (*TreeSet[T, C]) ForEach added in v0.1.13

func (s *TreeSet[T, C]) ForEach(visit func(T) bool)

func (*TreeSet[T, C]) Insert added in v0.1.10

func (s *TreeSet[T, C]) Insert(item T) bool

Insert item into s.

Returns true if s was modified (item was not already in s), false otherwise.

Example
s := TreeSetFrom[string, Compare[string]]([]string{}, Cmp[string])

fmt.Println(s)

s.Insert("red")
s.Insert("green")
s.Insert("blue")

fmt.Println(s)

// []
// [blue green red]
Output:

func (*TreeSet[T, C]) InsertSet added in v0.1.13

func (s *TreeSet[T, C]) InsertSet(o *TreeSet[T, C]) bool

InsertSet will insert each element of o into s.

Return true if s was modified (at least one item of o was not already in s), false otherwise.

Example
s1 := TreeSetFrom[string, Compare[string]]([]string{"red", "green"}, Cmp[string])
s2 := TreeSetFrom[string, Compare[string]]([]string{"green", "blue"}, Cmp[string])

fmt.Println(s1)
fmt.Println(s2)

s1.InsertSet(s2)

fmt.Println(s1)
Output:

[green red]
[blue green]
[blue green red]

func (*TreeSet[T, C]) InsertSlice added in v0.1.10

func (s *TreeSet[T, C]) InsertSlice(items []T) bool

InsertSlice will insert each item in items into s.

Return true if s was modified (at least one item was not already in s), false otherwise.

Example
s := TreeSetFrom[string, Compare[string]]([]string{}, Cmp[string])

fmt.Println(s)

s.InsertSlice([]string{"red", "green", "blue"})

fmt.Println(s)

// []
// [blue green red]
Output:

func (*TreeSet[T, C]) Intersect added in v0.1.10

func (s *TreeSet[T, C]) Intersect(o *TreeSet[T, C]) *TreeSet[T, C]

Intersect returns a set that contains elements that are present in both s and o.

Example
s := TreeSetFrom[int, Compare[int]]([]int{1, 2, 3, 4, 5}, Cmp[int])
t := TreeSetFrom[int, Compare[int]]([]int{5, 4, 3, 2, 1}, Cmp[int])
f := TreeSetFrom[int, Compare[int]]([]int{1, 3, 5, 7, 9}, Cmp[int])

fmt.Println(s.Intersect(t))
fmt.Println(s.Intersect(f))
Output:

[1 2 3 4 5]
[1 3 5]

func (*TreeSet[T, C]) MarshalJSON added in v0.1.11

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

MarshalJSON implements the json.Marshaler interface.

func (*TreeSet[T, C]) Max added in v0.1.10

func (s *TreeSet[T, C]) Max() T

Max returns the largest item in s.

Must not be called on an empty set.

Example
s := TreeSetFrom[int, Compare[int]]([]int{1, 2, 3, 4, 5}, Cmp[int])
r := TreeSetFrom[int, Compare[int]]([]int{1, 2, 3, 4, 5}, func(a int, b int) int {
	return b - a
})

fmt.Println("asc:", s.Max())
fmt.Println("desc:", r.Max())
Output:

asc: 5
desc: 1

func (*TreeSet[T, C]) Min added in v0.1.10

func (s *TreeSet[T, C]) Min() T

Min returns the smallest item in the set.

Must not be called on an empty set.

Example
s := TreeSetFrom[int, Compare[int]]([]int{1, 2, 3, 4, 5}, Cmp[int])
r := TreeSetFrom[int, Compare[int]]([]int{1, 2, 3, 4, 5}, func(a int, b int) int {
	return b - a
})

fmt.Println("asc:", s.Min())
fmt.Println("desc:", r.Min())
Output:

asc: 1
desc: 5

func (*TreeSet[T, C]) Remove added in v0.1.10

func (s *TreeSet[T, C]) Remove(item T) bool

Remove item from s.

Returns true if s was modified (item was in s), false otherwise.

Example
s := TreeSetFrom[string, Compare[string]]([]string{"red", "green", "blue"}, Cmp[string])

fmt.Println(s)

fmt.Println(s.Remove("green"))
fmt.Println(s.Remove("orange"))

fmt.Println(s)
Output:

[blue green red]
true
false
[blue red]

func (*TreeSet[T, C]) RemoveFunc added in v0.1.13

func (s *TreeSet[T, C]) RemoveFunc(f func(T) bool) bool

RemoveFunc will remove each element from s that satisifies condition f.

Return true if s was modified, false otherwise.

Example
s := TreeSetFrom[int, Compare[int]](ints(20), Cmp[int])

fmt.Println(s)

even := func(i int) bool {
	return i%3 != 0
}
s.RemoveFunc(even)

fmt.Println(s)
Output:

[1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20]
[3 6 9 12 15 18]

func (*TreeSet[T, C]) RemoveSet added in v0.1.13

func (s *TreeSet[T, C]) RemoveSet(o *TreeSet[T, C]) bool

RemoveSet will remove each element in o from s.

Returns true if s was modified (at least one item in o was in s), false otherwise.

Example
s1 := TreeSetFrom[string, Compare[string]]([]string{"a", "b", "c", "d", "e", "f"}, Cmp[string])
s2 := TreeSetFrom[string, Compare[string]]([]string{"e", "z", "a"}, Cmp[string])

fmt.Println(s1)
fmt.Println(s2)

s1.RemoveSet(s2)

fmt.Println(s1)
Output:

[a b c d e f]
[a e z]
[b c d f]

func (*TreeSet[T, C]) RemoveSlice added in v0.1.10

func (s *TreeSet[T, C]) RemoveSlice(items []T) bool

RemoveSlice will remove each item in items from s.

Return true if s was modified (any item was in s), false otherwise.

Example
s := TreeSetFrom[string, Compare[string]]([]string{"red", "green", "blue"}, Cmp[string])

fmt.Println(s)

fmt.Println(s.RemoveSlice([]string{"red", "blue"}))
fmt.Println(s.RemoveSlice([]string{"orange", "white"}))

fmt.Println(s)
Output:

[blue green red]
true
false
[green]

func (*TreeSet[T, C]) Size added in v0.1.10

func (s *TreeSet[T, C]) Size() int

Size returns the number of elements in s.

Example
s := TreeSetFrom[string, Compare[string]]([]string{"red", "green", "blue"}, Cmp[string])

fmt.Println(s.Size())
Output:

3

func (*TreeSet[T, C]) Slice added in v0.1.10

func (s *TreeSet[T, C]) Slice() []T

Slice returns the elements of s as a slice, in order.

Example
s := TreeSetFrom[int, Compare[int]]([]int{1, 2, 3, 4, 5}, Cmp[int])
slice := s.Slice()

fmt.Println(slice)
fmt.Println(len(slice))
Output:

[1 2 3 4 5]
5

func (*TreeSet[T, C]) String added in v0.1.10

func (s *TreeSet[T, C]) String() string

String creates a string representation of s, using "%v" printf formatting each element into a string. The result contains elements in order.

Example
s := TreeSetFrom[int, Compare[int]]([]int{1, 2, 3, 4, 5}, Cmp[int])

fmt.Println(s.String() == "[1 2 3 4 5]")
Output:

true

func (*TreeSet[T, C]) StringFunc added in v0.1.10

func (s *TreeSet[T, C]) StringFunc(f func(element T) string) string

StringFunc creates a string representation of s, using f to transform each element into a string. The result contains elements in order.

func (*TreeSet[T, C]) Subset added in v0.1.10

func (s *TreeSet[T, C]) Subset(o *TreeSet[T, C]) bool

Subset returns whether o is a subset of s.

Example
s1 := TreeSetFrom[string, Compare[string]]([]string{"a", "b", "c", "d", "e"}, Cmp[string])
s2 := TreeSetFrom[string, Compare[string]]([]string{"b", "d"}, Cmp[string])
s3 := TreeSetFrom[string, Compare[string]]([]string{"a", "z"}, Cmp[string])

fmt.Println(s1.Subset(s2))
fmt.Println(s1.Subset(s3))
Output:

true
false

func (*TreeSet[T, C]) TopK added in v0.1.10

func (s *TreeSet[T, C]) TopK(n int) []T

TopK returns the top n (smallest) elements in s, in ascending order.

Example
s := TreeSetFrom[int, Compare[int]]([]int{1, 2, 3, 4, 5}, Cmp[int])

fmt.Println(s.TopK(0))
fmt.Println(s.TopK(1))
fmt.Println(s.TopK(3))
fmt.Println(s.TopK(5))
Output:

[]
[1]
[1 2 3]
[1 2 3 4 5]

func (*TreeSet[T, C]) Union added in v0.1.10

func (s *TreeSet[T, C]) Union(o *TreeSet[T, C]) *TreeSet[T, C]

Union returns a set that contains all elements of s and o combined.

Example
s := TreeSetFrom[int, Compare[int]]([]int{1, 2, 3, 4, 5}, Cmp[int])
t := TreeSetFrom[int, Compare[int]]([]int{5, 4, 3, 2, 1}, Cmp[int])
f := TreeSetFrom[int, Compare[int]]([]int{1, 3, 5, 7, 9}, Cmp[int])

fmt.Println(s.Union(t))
fmt.Println(s.Union(f))
Output:

[1 2 3 4 5]
[1 2 3 4 5 7 9]

func (*TreeSet[T, C]) UnmarshalJSON added in v0.1.11

func (s *TreeSet[T, C]) UnmarshalJSON(data []byte) error

UnmarshalJSON implements the json.Unmarshaler interface.

Jump to

Keyboard shortcuts

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