sets

package
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Jun 22, 2026 License: MPL-2.0 Imports: 2 Imported by: 0

Documentation

Overview

Package sets provides utility functions for common operations on sets and slices. Sets are represented as map[string]struct{} for efficient lookups.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Equal

func Equal(a, b Set) bool

Equal checks if two sets contain the exact same elements.

func GetSplitIndex

func GetSplitIndex(length int) int

GetSplitIndex calculates the index at which to split a slice of a given length into two halves. The first half will be larger if the length is odd.

func Split

func Split(slice OrderedSet) (OrderedSet, OrderedSet)

Split divides a slice into two approximately equal halves.

func SubtractSlices

func SubtractSlices(a []string, b []string) []string

SubtractSlices returns a new slice containing elements from the 'a' slice that are not present in the 'b' slice. The order of elements in 'a' is preserved.

Types

type OrderedSet

type OrderedSet []string

OrderedSet represents a slice of strings. Note: This is a type alias and does not enforce uniqueness or order at compile time. Functions that return an OrderedSet, like MakeSlice, guarantee the slice is sorted and unique.

func MakeSlice

func MakeSlice(set Set) OrderedSet

MakeSlice converts a Set into a new, sorted slice of strings (an OrderedSet).

type Set

type Set map[string]struct{}

Set represents a collection of unique string values.

func AddInPlace

func AddInPlace(a, b Set) Set

AddInPlace adds all elements from b to a and returns the result.

func Copy

func Copy(original Set) Set

Copy returns a new set containing all elements from the original set.

func Intersection

func Intersection(a, b Set) Set

Intersection returns a new set containing only the elements present in both set a and set b.

func MakeSet

func MakeSet(slice []string) Set

MakeSet converts a slice of strings into a Set for efficient lookups. Duplicates in the slice are removed.

func Subtract

func Subtract(a, b Set) Set

Subtract returns a new set containing elements from set a that are not present in set b.

func SubtractInPlace

func SubtractInPlace(a, b Set) Set

Subtract removes all elements in b from a and returns the result.

func Union

func Union(a, b Set) Set

Union returns a new set containing all elements present in either set a or set b.

type SetFormatter

type SetFormatter struct {
	// contains filtered or unexported fields
}

SetFormatter provides a lazy, fmt.Stringer-compliant way to format a Set for logging. The conversion to a sorted string only happens when the String() method is called by the logging framework.

func FormatSet

func FormatSet(set Set) SetFormatter

FormatSet returns a SetFormatter that can be used in logging statements. It delays the expensive work of sorting and joining until it's actually needed.

func (SetFormatter) String

func (sf SetFormatter) String() string

String implements the fmt.Stringer interface.

Jump to

Keyboard shortcuts

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