Documentation
¶
Index ¶
- type Custom
- func CustomFrom[T any](cmp func(a, b T) int, items ...T) *Custom[T]
- func IntersectCustom[T any](compare func(a, b T) int, sets ...*Custom[T]) *Custom[T]
- func MergeCustom[T any](compare func(a, b T) int, sets ...*Custom[T]) *Custom[T]
- func NewCustom[T any](cmp func(a, b T) int, capacity int) *Custom[T]
- func (s *Custom[T]) Add(e T) bool
- func (s *Custom[T]) Ascend() iter.Seq2[int, T]
- func (s *Custom[T]) At(i int) T
- func (s *Custom[T]) BetweenAsc(min, max T) iter.Seq2[int, T]
- func (s *Custom[T]) BetweenDesc(max, min T) iter.Seq2[int, T]
- func (s *Custom[T]) Capacity() int
- func (s *Custom[T]) Clear()
- func (s *Custom[T]) Clone() *Custom[T]
- func (s *Custom[T]) Contains(e T) bool
- func (s *Custom[T]) Descend() iter.Seq2[int, T]
- func (s *Custom[T]) Difference(other *Custom[T]) *Custom[T]
- func (s *Custom[T]) Find(e T) (int, bool)
- func (s *Custom[T]) Intersect(other *Custom[T]) *Custom[T]
- func (s *Custom[T]) IsEmpty() bool
- func (s *Custom[T]) IsEqual(other *Custom[T]) bool
- func (s *Custom[T]) Items() []T
- func (s *Custom[T]) Max() T
- func (s *Custom[T]) MaxK(k int) []T
- func (s *Custom[T]) Min() T
- func (s *Custom[T]) MinK(k int) []T
- func (s1 *Custom[T]) Partition(s2 *Custom[T]) (d12, inter, d21 *Custom[T])
- func (s *Custom[T]) Remove(e T) bool
- func (s *Custom[T]) RemoveBefore(max T) int
- func (s *Custom[T]) RemoveBetween(min, max T) int
- func (s *Custom[T]) RemoveFrom(min T) int
- func (s *Custom[T]) Size() int
- func (s *Custom[T]) SymmetricDifference(other *Custom[T]) *Custom[T]
- func (s *Custom[T]) Union(other *Custom[T]) *Custom[T]
- type Ordered
- func (s *Ordered[T]) Add(e T) bool
- func (s *Ordered[T]) Ascend() iter.Seq2[int, T]
- func (s *Ordered[T]) At(i int) T
- func (s *Ordered[T]) BetweenAsc(min, max T) iter.Seq2[int, T]
- func (s *Ordered[T]) BetweenDesc(max, min T) iter.Seq2[int, T]
- func (s *Ordered[T]) Capacity() int
- func (s *Ordered[T]) Clear()
- func (s *Ordered[T]) Clone() *Ordered[T]
- func (s *Ordered[T]) Contains(e T) bool
- func (s *Ordered[T]) Descend() iter.Seq2[int, T]
- func (s *Ordered[T]) Difference(other *Ordered[T]) *Ordered[T]
- func (s *Ordered[T]) Find(e T) (int, bool)
- func (s *Ordered[T]) Intersect(other *Ordered[T]) *Ordered[T]
- func (s *Ordered[T]) IsEmpty() bool
- func (s *Ordered[T]) IsEqual(other *Ordered[T]) bool
- func (s *Ordered[T]) Items() []T
- func (s *Ordered[T]) Max() T
- func (s *Ordered[T]) MaxK(k int) []T
- func (s *Ordered[T]) Min() T
- func (s *Ordered[T]) MinK(k int) []T
- func (s1 *Ordered[T]) Partition(s2 *Ordered[T]) (d12, inter, d21 *Ordered[T])
- func (s *Ordered[T]) Remove(e T) bool
- func (s *Ordered[T]) RemoveBefore(max T) int
- func (s *Ordered[T]) RemoveBetween(min, max T) int
- func (s *Ordered[T]) RemoveFrom(min T) int
- func (s *Ordered[T]) Size() int
- func (s *Ordered[T]) SymmetricDifference(other *Ordered[T]) *Ordered[T]
- func (s *Ordered[T]) Union(other *Ordered[T]) *Ordered[T]
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Custom ¶ added in v0.2.0
type Custom[T any] struct { // contains filtered or unexported fields }
Custom is a slice-based set sorted in ascending order, as determined by the cmp function provided in the contructor. If T is an ordered type, you should use Ordered for better performance.
It's more performant that a map based approach for small collections (< 1000). The capacity of the set can dynamically grow, but the performance would start to deteriorate. Not safe for concurrent use.
func CustomFrom ¶ added in v0.4.3
CustomFrom returns an initialized set that contains the provided elements, sorted by the provided compare function cmp.
The cmp function allows two elements, a and b, to be compared, following a similar convention to that of the slices package. - cmp(a, b) < 0 if a < b - cmp(a, b) > 0 if a > b - cmp(a, b) == 0 if a = b (duplicates)
It panics if cmp is nil.
func IntersectCustom ¶ added in v0.4.0
IntersectCustom efficiently finds the common elements present in *all* provided Custom sets. The 'cmp' function defines the ordering for the resulting set, and *must* be the same as the comparison functions of all sets. It works by iteratively intersecting sets from the smallest to the biggest. It sorts the sets slice in place.
func MergeCustom ¶ added in v0.4.0
MergeCustom efficiently combines multiple Custom sets into a single new set with the specified comparison function cmp. This is significantly more efficient than chaining s1.Union(s2).Union(s3)... as it performs only a single sort and compact operation on the combined data.
func NewCustom ¶ added in v0.2.0
NewCustom returns an initialized set with the provided compare function and capacity.
The cmp function allows two elements, a and b, to be compared, following a similar convention to that of the slices package. - cmp(a, b) < 0 if a < b - cmp(a, b) > 0 if a > b - cmp(a, b) == 0 if a = b (duplicates)
It panics if the cmp function is nil or capacity is <= 0.
func (*Custom[T]) Add ¶ added in v0.2.0
Add an element and returns whether is was added (true), or was already present (false).
func (*Custom[T]) Ascend ¶ added in v0.2.0
Ascend returns an iterator over the set in ascending order.
func (*Custom[T]) BetweenAsc ¶ added in v0.2.0
BetweenAsc iterates CustomFrom min (inclusive) to max (exclusive) in ascending order. If min or max are not present in the set, iteration starts/ends at the position where they would appear in the sorted slice. Panics if max < min.
func (*Custom[T]) BetweenDesc ¶ added in v0.2.0
BetweenDesc iterates CustomFrom max (inclusive) down to min (exclusive) in descending order. If min or max are not present in the set, iteration starts/ends at the position where they would appear in the sorted slice. Panics if max < min.
func (*Custom[T]) Clear ¶ added in v0.2.0
func (s *Custom[T]) Clear()
Clear removes all elements from the set.
It zeroes out the elements to prevent memory leaks (releasing references) and resets the length to 0. The underlying array capacity is preserved to minimize allocations during future insertions.
func (*Custom[T]) Clone ¶ added in v0.2.0
Clone returns a clone of the set, that shares the cmp comparator function.
func (*Custom[T]) Contains ¶ added in v0.2.0
Contains returns whether the element is in the set. Operation is O(log(N))
func (*Custom[T]) Descend ¶ added in v0.2.0
Descend returns an iterator over the set in descending order.
func (*Custom[T]) Difference ¶ added in v0.2.0
Difference returns the difference between this set and other. The returned set will contain all elements of this set that are not elements of other. O(N+M) complexity. s1 and s2 must use the same (or equivalent) comparison functions.
func (*Custom[T]) Find ¶ added in v0.2.0
Find returns the index of an element, or the position where target would appear in the sort order. It also returns a bool saying whether the target is really found in the slice.
func (*Custom[T]) Intersect ¶ added in v0.2.0
Intersect returns the intersection of two sets, returning a NewCustom set containing only the common elements. O(N+M) complexity. s1 and s2 must use the same (or equivalent) comparison functions.
func (*Custom[T]) IsEqual ¶ added in v0.2.0
IsEqual returns whether the two sets have the same elements.
func (*Custom[T]) Items ¶ added in v0.2.0
func (s *Custom[T]) Items() []T
Items returns a copy of the internal slice of the set.
func (*Custom[T]) Max ¶ added in v0.2.0
func (s *Custom[T]) Max() T
Max returns the biggest element in the sets. It panics if the set is empty.
func (*Custom[T]) MaxK ¶ added in v0.2.0
MaxK returns the k biggest elements in s, sorted in ascending order. O(k) complexity. It panics if k is negative. If k is bigger than the set size, it returns all the items.
func (*Custom[T]) Min ¶ added in v0.2.0
func (s *Custom[T]) Min() T
Min returns the smallest element in the set. It panics if the set is empty.
func (*Custom[T]) MinK ¶ added in v0.2.0
MinK returns the k smallest elements in s, sorted in ascending order. O(k) complexity. It panics if k is negative. If k is bigger than the set size, it returns all the items.
func (*Custom[T]) Partition ¶ added in v0.2.0
Partition returns three NewCustom sets: - d12: elements in s1 not in s2 - inter: elements in both sets - d21: elements in s2 not in s1 O(N+M) complexity.
s1 and s2 must use the same (or equivalent) comparison functions.
func (*Custom[T]) Remove ¶ added in v0.2.0
Remove an element if present, and returns whether is was removed (true), or was never present (false).
func (*Custom[T]) RemoveBefore ¶ added in v0.3.0
RemoveBefore removes all elements e such that e < max. Returns num removed.
func (*Custom[T]) RemoveBetween ¶ added in v0.3.0
RemoveBetween removes all elements e such that min <= e < max. Returns num removed.
func (*Custom[T]) RemoveFrom ¶ added in v0.3.0
RemoveFrom removed all elements e such that e >= min. Returns num removed.
func (*Custom[T]) SymmetricDifference ¶ added in v0.2.0
SymmetricDifference returns a NewCustom set with all elements which are in either this set or the other set but not in both. O(N+M) complexity. s1 and s2 must use the same (or equivalent) comparison functions.
type Ordered ¶ added in v0.2.0
Ordered is a slice-based set sorted in ascending order. It's more performant that a map based approach for small collections (< 1000) of ordered types. The capacity of the set can dynamically grow, but the performance would start to deteriorate. Not safe for concurrent use.
func Intersect ¶ added in v0.4.0
Intersect efficiently finds the common elements present in *all* provided Ordered sets. It works by iteratively intersecting sets from the smallest to the biggest. It sorts the sets slice in place.
func Merge ¶ added in v0.4.0
Merge efficiently combines multiple Ordered sets into a single new set. This is significantly more efficient than chaining s1.Union(s2).Union(s3)... as it performs only a single sort and compact operation on the combined data.
func New ¶
New returns an initialized set with the provided capacity. It panics if the capacity is <= 0.
func (*Ordered[T]) Add ¶ added in v0.2.0
Add an element and returns whether is was added (true), or was already present (false).
func (*Ordered[T]) Ascend ¶ added in v0.2.0
Ascend returns an iterator over the set in ascending order.
func (*Ordered[T]) At ¶ added in v0.2.0
At returns the element at index i or panics if out of range.
func (*Ordered[T]) BetweenAsc ¶ added in v0.2.0
BetweenAsc iterates From min (inclusive) to max (exclusive) in ascending order. If min or max are not present in the set, iteration starts/ends at the position where they would appear in the sorted slice. Panics if max < min.
func (*Ordered[T]) BetweenDesc ¶ added in v0.2.0
BetweenDesc iterates From max (inclusive) down to min (exclusive) in descending order. If min or max are not present in the set, iteration starts/ends at the position where they would appear in the sorted slice. Panics if max < min.
func (*Ordered[T]) Capacity ¶ added in v0.4.2
Capacity returns the capacity of the underlying slice.
func (*Ordered[T]) Clear ¶ added in v0.2.0
func (s *Ordered[T]) Clear()
Clear removes all elements from the set.
It zeroes out the elements to prevent memory leaks (releasing references) and resets the length to 0. The underlying array capacity is preserved to minimize allocations during future insertions.
func (*Ordered[T]) Contains ¶ added in v0.2.0
Contains returns whether the element is in the set. Operation is O(log(N))
func (*Ordered[T]) Descend ¶ added in v0.2.0
Descend returns an iterator over the set in descending order.
func (*Ordered[T]) Difference ¶ added in v0.2.0
Difference returns the difference between this set and other. The returned set will contain all elements of this set that are not elements of other. O(N+M) complexity.
func (*Ordered[T]) Find ¶ added in v0.2.0
Find returns the index of an element, or the position where target would appear in the sort order. It also returns a bool saying whether the target is really found in the slice.
func (*Ordered[T]) Intersect ¶ added in v0.2.0
Intersect returns the intersection of two sets, returning a New set containing only the common elements. O(N+M) complexity.
func (*Ordered[T]) IsEqual ¶ added in v0.2.0
IsEqual returns whether the two sets have the same elements.
func (*Ordered[T]) Items ¶ added in v0.2.0
func (s *Ordered[T]) Items() []T
Items returns a copy of the internal slice of the set.
func (*Ordered[T]) Max ¶ added in v0.2.0
func (s *Ordered[T]) Max() T
Max returns the biggest element in the sets. It panics if the set is empty.
func (*Ordered[T]) MaxK ¶ added in v0.2.0
MaxK returns the k biggest elements in s, sorted in ascending order. O(k) complexity. It panics if k is negative. If k is bigger than the set size, it returns all the items.
func (*Ordered[T]) Min ¶ added in v0.2.0
func (s *Ordered[T]) Min() T
Min returns the smallest element in the set. It panics if the set is empty.
func (*Ordered[T]) MinK ¶ added in v0.2.0
MinK returns the k smallest elements in s, sorted in ascending order. O(k) complexity. It panics if k is negative. If k is bigger than the set size, it returns all the items.
func (*Ordered[T]) Partition ¶ added in v0.2.0
Partition returns three New sets: - d12: elements in s1 not in s2 - inter: elements in both sets - d21: elements in s2 not in s1 O(N+M) complexity.
func (*Ordered[T]) Remove ¶ added in v0.2.0
Remove an element if present, and returns whether is was removed (true), or was never present (false).
func (*Ordered[T]) RemoveBefore ¶ added in v0.3.0
RemoveBefore removes all elements e such that e < max. Returns num removed.
func (*Ordered[T]) RemoveBetween ¶ added in v0.3.0
RemoveBetween removes all elements e such that min <= e < max. Returns num removed.
func (*Ordered[T]) RemoveFrom ¶ added in v0.3.0
RemoveFrom removed all elements e such that e >= min. Returns num removed.
func (*Ordered[T]) SymmetricDifference ¶ added in v0.2.0
SymmetricDifference returns a New set with all elements which are in either this set or the other set but not in both. O(N+M) complexity.