Documentation
¶
Overview ¶
Package container provides common container types.
TODO(a.garipov): Consider adding a set interface.
TODO(a.garipov): Consider adding a intersect and union operations on sets.
Index ¶
- func MapSetToString[T constraints.Ordered](set *MapSet[T]) (s string)
- func MapSetToStringFunc[T comparable](set *MapSet[T], compare func(a, b T) (res int)) (s string)
- type MapSet
- func (set *MapSet[T]) Add(v T)
- func (set *MapSet[T]) Clear()
- func (set *MapSet[T]) Clone() (clone *MapSet[T])
- func (set *MapSet[T]) Delete(v T)
- func (set *MapSet[T]) Equal(other *MapSet[T]) (ok bool)
- func (set *MapSet[T]) Has(v T) (ok bool)
- func (set *MapSet[T]) Len() (n int)
- func (set *MapSet[T]) Range(f func(v T) (cont bool))
- func (set *MapSet[T]) Values() (values []T)
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func MapSetToString ¶
func MapSetToString[T constraints.Ordered](set *MapSet[T]) (s string)
MapSetToString converts a *MapSet of values of an ordered type into a reproducible string.
func MapSetToStringFunc ¶
func MapSetToStringFunc[T comparable](set *MapSet[T], compare func(a, b T) (res int)) (s string)
MapSetToStringFunc is like MapSetToString but uses an explicit comparison function.
Types ¶
type MapSet ¶
type MapSet[T comparable] struct { // contains filtered or unexported fields }
MapSet is a set that uses a map as its storage.
TODO(a.garipov): Figure out a way to add a reproducible String method.
Example ¶
package main
import (
"fmt"
"slices"
"github.com/AdguardTeam/golibs/container"
)
func main() {
const x = 1
set := container.NewMapSet[int]()
ok := set.Has(x)
fmt.Printf("%s contains %v is %t\n", container.MapSetToString(set), x, ok)
set.Add(x)
ok = set.Has(x)
fmt.Printf("%s contains %v is %t\n", container.MapSetToString(set), x, ok)
other := container.NewMapSet(x)
ok = set.Equal(other)
fmt.Printf(
"%s is equal to %s is %t\n",
container.MapSetToString(set),
container.MapSetToString(other),
ok,
)
set.Add(2)
values := set.Values()
slices.Sort(values)
fmt.Printf("values of %s are %v\n", container.MapSetToString(set), values)
set.Delete(x)
ok = set.Has(x)
fmt.Printf("%s contains %v is %t\n", container.MapSetToString(set), x, ok)
set.Range(func(n int) (cont bool) {
fmt.Printf("got value %d\n", n)
return false
})
set = container.NewMapSet(x)
fmt.Printf("%s has length %d\n", container.MapSetToString(set), set.Len())
set.Clear()
fmt.Printf("%s has length %d\n", container.MapSetToString(set), set.Len())
}
Output: [] contains 1 is false [1] contains 1 is true [1] is equal to [1] is true values of [1 2] are [1 2] [2] contains 1 is false got value 2 [1] has length 1 [] has length 0
Example (Nil) ¶
package main
import (
"fmt"
"github.com/AdguardTeam/golibs/container"
)
func main() {
const x = 1
var set *container.MapSet[int]
panicked := false
setPanicked := func() {
if v := recover(); v != nil {
panicked = true
}
}
func() {
defer setPanicked()
set.Clear()
}()
fmt.Printf("panic after clear: %t\n", panicked)
func() {
defer setPanicked()
set.Delete(x)
}()
fmt.Printf("panic after delete: %t\n", panicked)
func() {
defer setPanicked()
set.Has(x)
}()
fmt.Printf("panic after has: %t\n", panicked)
func() {
defer setPanicked()
set.Len()
}()
fmt.Printf("panic after len: %t\n", panicked)
func() {
defer setPanicked()
set.Range(func(n int) (cont bool) {
fmt.Printf("got value %d\n", n)
return true
})
}()
fmt.Printf("panic after range: %t\n", panicked)
func() {
defer setPanicked()
set.Values()
}()
fmt.Printf("panic after values: %t\n", panicked)
func() {
defer setPanicked()
set.Add(x)
}()
fmt.Printf("panic after add: %t\n", panicked)
}
Output: panic after clear: false panic after delete: false panic after has: false panic after len: false panic after range: false panic after values: false panic after add: true
func NewMapSet ¶
func NewMapSet[T comparable](values ...T) (set *MapSet[T])
NewMapSet returns a new map set containing values.
func (*MapSet[T]) Add ¶
func (set *MapSet[T]) Add(v T)
Add adds v to set. Add panics if set is a nil set, just like a nil map does.
func (*MapSet[T]) Clear ¶
func (set *MapSet[T]) Clear()
Clear clears set in a way that retains the internal storage for later reuse to reduce allocations. Calling Clear on a nil set has no effect, just like a clear on a nil map doesn't.
func (*MapSet[T]) Clone ¶
Clone returns a deep clone of set. If set is nil, clone is nil.
Example ¶
package main
import (
"fmt"
"github.com/AdguardTeam/golibs/container"
)
func main() {
var set *container.MapSet[int]
fmt.Printf("nil: %#v\n", set.Clone())
const x, y = 1, 2
set = container.NewMapSet(x)
clone := set.Clone()
clone.Add(y)
fmt.Printf("orig: %t %t\n", set.Has(x), set.Has(y))
fmt.Printf("clone: %t %t\n", clone.Has(x), clone.Has(y))
}
Output: nil: (*container.MapSet[int])(nil) orig: true false clone: true true
func (*MapSet[T]) Delete ¶
func (set *MapSet[T]) Delete(v T)
Delete deletes v from set. Calling Delete on a nil set has no effect, just like delete on a nil map doesn't.
func (*MapSet[T]) Equal ¶
Equal returns true if set is equal to other.
Example ¶
package main
import (
"fmt"
"github.com/AdguardTeam/golibs/container"
)
func main() {
const x, y = 1, 2
set := container.NewMapSet(x)
fmt.Printf("same: %t\n", set.Equal(container.NewMapSet(x)))
fmt.Printf("other elem: %t\n", set.Equal(container.NewMapSet(y)))
fmt.Printf("other len: %t\n", set.Equal(container.NewMapSet(x, y)))
fmt.Printf("nil: %t\n", set.Equal(nil))
fmt.Printf("nil eq nil: %t\n", (*container.MapSet[int])(nil).Equal(nil))
}
Output: same: true other elem: false other len: false nil: false nil eq nil: true
func (*MapSet[T]) Has ¶
Has returns true if v is in set. Calling Has on a nil set returns false, just like indexing on an empty map does.
func (*MapSet[T]) Len ¶
Len returns the length of set. A nil set has a length of zero, just like an empty map.