Documentation
¶
Overview ¶
A simple library for managing slices and maps as sets.
All OrderedX functions (eg: OrderedUnion) work with slices or any type based on a slice as long as the slice elements are comparable. These slices are treated as ordered sets.
For slices, all operations here automatically remove duplicates and preserve the order of the elements in the original slice. Whenever possible, they also seek to avoid allocations. So they preserve nilness, they don't make a new set out of a slice which already adheres to the set property, etc. All operations are variadic and applied left-to-right (index 0 and up).
It's "naive" in that it has no tricks to speed up performance. It will deliberately sacrifice cycles to favor avoiding an allocation, for example.
All UnorderedX functions (eg: UnorderedUnion) work with maps and any type based on a map. Since all map keys are already comparable, there is no restriction to their use. These maps are treated as unordered sets.
For maps, no order is preserved and the operations are faster. Allocations are still avoided. Map keys are treated as the set elements, not their values. As such, values found in duplicate keys will be clobbered. This mimics the conventional use of maps as ad-hoc sets, so they should work in the same way. Whenever possible, the values for the first given set will be in the output.
As "nil" is common nomenclature for the empty set ({}, or 0), nil is accepted as input to mean an empty slice or empty map, and empty set results such as the intersection of disjoint sets are similarly returned as nil.
All sets are treated as if immutable - no modifications will occur on any of these operations, and instead copies will be returned.
Index ¶
- func Append[S ~[]E, E comparable](s S, e ...E) S
- func CloneOrderedSet[S ~[]E, E comparable](s S) S
- func CloneUnorderedSet[S ~map[K]V, K comparable, V any](s S) S
- func Deduplicate[S ~[]E, E comparable](s S) S
- func Insert[S ~map[K]V, K comparable, V any](s S, e S) S
- func IsOrderedSubsetOf[S ~[]E, E comparable](super, sub S) bool
- func IsUnorderedSubsetOf[S ~map[K]V, K comparable, V any](super, sub S) bool
- func OrderedComplement[S ~[]E, E comparable](sets ...S) S
- func OrderedIntersect[S ~[]E, E comparable](sets ...S) S
- func OrderedRemove[S ~[]E, E comparable](s S, e ...E) S
- func OrderedUnion[S ~[]E, E comparable](sets ...S) S
- func Prepend[S ~[]E, E comparable](s S, e ...E) S
- func RemoveFirstN[S ~[]E, E comparable](s S, n int) S
- func RemoveLastN[S ~[]E, E comparable](s S, n int) S
- func Splice[S ~[]E, E comparable](s S, i int, e ...E) S
- func UnorderedComplement[S ~map[K]V, K comparable, V any](sets ...S) S
- func UnorderedIntersect[S ~map[K]V, K comparable, V any](sets ...S) S
- func UnorderedRemove[S ~map[K]V, K comparable, V any](s S, k K) S
- func UnorderedUnion[S ~map[K]V, K comparable, V any](sets ...S) S
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Append ¶ added in v1.1.0
func Append[S ~[]E, E comparable](s S, e ...E) S
Helper function. Equivalent to OrderedUnion(s, e)
func CloneOrderedSet ¶ added in v1.1.0
func CloneOrderedSet[S ~[]E, E comparable](s S) S
Helper function. Equivalent to OrderedUnion(nil, s)
func CloneUnorderedSet ¶ added in v1.1.0
func CloneUnorderedSet[S ~map[K]V, K comparable, V any](s S) S
Helper function. Equivalent to UnorderedUnion(nil, s)
func Deduplicate ¶ added in v1.1.0
func Deduplicate[S ~[]E, E comparable](s S) S
Helper function. Equivalent to OrderedUnion(s)
func Insert ¶ added in v1.1.0
func Insert[S ~map[K]V, K comparable, V any](s S, e S) S
Helper function. Equivalent to UnorderedUnion(e, s)
func IsOrderedSubsetOf ¶
func IsOrderedSubsetOf[S ~[]E, E comparable](super, sub S) bool
Returns whether sub is a subset of super. It is a subset if and only if every element in sub is also in super.
This version is meant to be for slices.
func IsUnorderedSubsetOf ¶
func IsUnorderedSubsetOf[S ~map[K]V, K comparable, V any](super, sub S) bool
Returns whether sub is a subset of super. It is a subset if and only if every key in sub is also in super.
This version is meant to be for maps.
func OrderedComplement ¶
func OrderedComplement[S ~[]E, E comparable](sets ...S) S
Produces the relative complements of slices as sets. The output will be all elements of the first argument which do not appear in any the the rest of the input slices, without suplicates. Preserves order.
Each argument is getting complemented to the relative complement of the previous two arguments. Applies in left-to-right order, so OrderedComplement(a, b, c) is the set of all elements in a not in b, and also not in c.
func OrderedIntersect ¶
func OrderedIntersect[S ~[]E, E comparable](sets ...S) S
Produces the intersection of slices as sets. The output will be a new slice containing the elements in all input slices, without duplicates. Preserves order.
Each argument is getting intersected with the intersection of the previous two arguments.
func OrderedRemove ¶ added in v1.1.0
func OrderedRemove[S ~[]E, E comparable](s S, e ...E) S
Helper function. Equivalent to OrderedComplement(s, e)
func OrderedUnion ¶
func OrderedUnion[S ~[]E, E comparable](sets ...S) S
Produces the union of slices as sets. The output will be a new slice containing the elements of all input slices, without duplicates. Preserves order.
func Prepend ¶ added in v1.1.0
func Prepend[S ~[]E, E comparable](s S, e ...E) S
Helper function. Equivalent to OrderedUnion(S{e}, s)
func RemoveFirstN ¶ added in v1.1.0
func RemoveFirstN[S ~[]E, E comparable](s S, n int) S
Helper function. Equivalent to OrderedComplement(s, S{s[n-1]})
func RemoveLastN ¶ added in v1.1.0
func RemoveLastN[S ~[]E, E comparable](s S, n int) S
Helper function. Equivalent to OrderedComplement(s, S{s[len(s)-n]})
func Splice ¶ added in v1.1.0
func Splice[S ~[]E, E comparable](s S, i int, e ...E) S
Helper function. Equivalent to OrderedUnion(s[:i], e, s[i:])
func UnorderedComplement ¶
func UnorderedComplement[S ~map[K]V, K comparable, V any](sets ...S) S
Produces the relative complement of maps as sets, where the maps' keys are the set's members. The output will be a new map with the keys of the first argument which do not appear in the rest of the input maps.
func UnorderedIntersect ¶
func UnorderedIntersect[S ~map[K]V, K comparable, V any](sets ...S) S
Produces the intersection of maps as sets, where the maps' keys are the set's members. The output will be a new map where the keys are the ones of the first given set which also appear in every other given set.
The values are those of the first set for they keys still remaining after intersection.
func UnorderedRemove ¶ added in v1.1.0
func UnorderedRemove[S ~map[K]V, K comparable, V any](s S, k K) S
Helper function. Equivalent to UnorderedComplement(s, S{k: v})
func UnorderedUnion ¶
func UnorderedUnion[S ~map[K]V, K comparable, V any](sets ...S) S
Produces the union of maps as sets, where the maps' keys are the set's members. The output will be a new map containing the keys of all the sets. Union is applied in argument order, with the values of repeated keys clobbering each other, so the last value applied for a key us what's in the set.
Types ¶
This section is empty.