sets

package module
v1.0.4 Latest Latest
Warning

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

Go to latest
Published: Mar 22, 2024 License: Apache-2.0 Imports: 6 Imported by: 6

README

Go Reference Go Report Card GitHub Release Coverage

sets

Sets and Set operations in golang

Internally as simple as a map[T]struct{} but with additional helper functions, operations like Union, Intersect, XOR, to/from Slices conversion, to/from JSON, etc... and hiding the struct{}{} etc...

See https://pkg.go.dev/fortio.org/sets

Documentation

Overview

Sets and Set type and operations of any comparable type (go 1.18+ generics) Intersection, Union, Set.Subset, difference aka Set.Minus, XOR, JSON serialization and deserialization and more.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func RemoveCommon

func RemoveCommon[T comparable](a, b Set[T])

RemoveCommon removes elements from both sets that are in both, leaving only the delta. Useful when a is an old value and b is new and you want to apply some operation on all removed and added elements.

func Sort added in v0.3.0

func Sort[Q constraints.Ordered](s Set[Q]) []Q

Sort returns a sorted slice of the elements in the set. Only applicable for when the type is sortable.

func XOR added in v0.4.0

func XOR[T comparable](a, b Set[T])

XOR is an alias for RemoveCommon, efficiently removes from each set the common elements.

Types

type Set

type Set[T comparable] map[T]struct{}

Set defines a low memory footprint set of any comparable type. Based on `map[T]struct{}`.

func FromSlice

func FromSlice[T comparable](items []T) Set[T]

FromSlice constructs a Set from a slice. [Elements] is the inverse function, getting back a slice from the Set. This is a short cut/alias for New[T](items...).

func Intersection added in v0.4.0

func Intersection[T comparable](sets ...Set[T]) Set[T]

Intersection returns a new set that has the elements common to all the input sets.

func New

func New[T comparable](item ...T) Set[T]

New returns a new set containing the given elements.

func Union added in v0.4.0

func Union[T comparable](sets ...Set[T]) Set[T]

Union returns a new set that has all the elements of all the sets. Note that Union(s1) == s1.Clone() and Union[T]() == New[T]().

func (Set[T]) Add

func (s Set[T]) Add(item ...T)

Add items to the set.

func (Set[T]) Clear added in v1.0.0

func (s Set[T]) Clear()

Clear removes all elements from the set.

func (Set[T]) Clone

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

Clone returns a copy of the set.

func (Set[T]) Elements added in v0.4.0

func (s Set[T]) Elements() []T

Elements returns a slice of the elements in the set.

func (Set[T]) Equals added in v1.0.0

func (s Set[T]) Equals(other Set[T]) bool

Equals returns true if the two sets have the same elements.

func (Set[T]) Has

func (s Set[T]) Has(item T) bool

Has returns true if the item is present in the set.

func (Set[T]) Len added in v1.0.0

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

Len returns the number of elements in the set (same as len(s) but as a method).

func (Set[T]) MarshalJSON added in v1.0.0

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

MarshalJSON implements the json.Marshaler interface and only gets the elements as an array.

func (Set[T]) Minus added in v1.0.0

func (s Set[T]) Minus(other Set[T]) Set[T]

Minus mutates the receiver to remove all the elements of the passed in set. If you want a copy use s.Clone().Minus(other). Returns the receiver for chaining.

func (Set[T]) Plus added in v1.0.0

func (s Set[T]) Plus(others ...Set[T]) Set[T]

Plus is similar to Union but mutates the receiver. Added for symmetry with Minus. Returns the receiver for chaining.

func (Set[T]) Remove

func (s Set[T]) Remove(item ...T)

Remove items from the set.

func (Set[T]) String

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

String() returns a coma separated list of the elements in the set. This is mostly for troubleshooting/debug output unless the [T] serializes to a string that doesn't contain commas.

func (Set[T]) Subset added in v1.0.0

func (s Set[T]) Subset(bigger Set[T]) bool

Subset returns true if all elements of s are in the passed in set.

func (*Set[T]) UnmarshalJSON added in v1.0.0

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

UnmarshalJSON implements the json.Unmarshaler interface turns the slice back to a Set.

Jump to

Keyboard shortcuts

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