Documentation
¶
Index ¶
- func CopyBucketSet[K comparable, E any](bs map[K][]E) map[K][]E
- func DoBuckets[K comparable, E any](bs map[K][]E, f func(K, []E) error) error
- func GetBucket[K comparable, E any](bs map[K][]E, key K) []E
- func IteratorOfBucketSet[K comparable, E any](bs map[K][]E) uc.Iterater[E]
- func KeepIfBuckets[K comparable, E any](bs map[K][]E, f us.PredicateFilter[E]) map[K][]E
- func MakeBucketSet[K comparable, E any](elems []E, f func(E) K) map[K][]E
- func Sort[T any](S []T, sf SortFunc[T], isAsc bool)
- func SortBucketSet[K comparable, E any](bs map[K][]E, sf SortFunc[E], isAsc bool)
- func StableSort[T any](S []T, sf SortFunc[T], isAsc bool)
- func ViewTopNBuckets[K comparable, E any](bs map[K][]E, n int) map[K][]E
- type Slice
- func (s *Slice[T]) Difference(other *Slice[T]) *Slice[T]
- func (s *Slice[T]) Find(elem T) int
- func (s *Slice[T]) FindSubsliceFrom(other *Slice[T], at int) int
- func (s *Slice[T]) IndexOfDuplicate() int
- func (s *Slice[T]) Insert(elem T, force bool) int
- func (s *Slice[T]) MergeUnique(other *Slice[T]) *Slice[T]
- func (s *Slice[T]) TryInsert(elem T) (int, bool)
- func (s *Slice[T]) Uniquefy()
- type SortFunc
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CopyBucketSet ¶ added in v0.3.35
func CopyBucketSet[K comparable, E any](bs map[K][]E) map[K][]E
CopyBucketSet copies the given map of buckets.
Parameters:
- bs: map of buckets to copy.
Returns:
- map[int]*Bucket: the copied map of buckets.
Behaviors:
- If the given map is nil, an empty map is returned.
func DoBuckets ¶ added in v0.3.35
func DoBuckets[K comparable, E any](bs map[K][]E, f func(K, []E) error) error
DoBuckets applies the given function to each bucket.
Parameters:
- bs: map of buckets.
- f: function to apply to each bucket.
Returns:
- error: the first error encountered.
Behaviors:
- If the given map is nil or the function is nil, nothing is done.
func GetBucket ¶ added in v0.3.35
func GetBucket[K comparable, E any](bs map[K][]E, key K) []E
GetBucket returns the bucket with the given key.
Parameters:
- bs: map of buckets.
- key: key of the bucket to return.
Returns:
- *Bucket: the bucket with the given key.
Behaviors:
- If the bucket does not exist, nil is returned.
- If the given map is nil, nil is returned.
func IteratorOfBucketSet ¶ added in v0.3.35
func IteratorOfBucketSet[K comparable, E any](bs map[K][]E) uc.Iterater[E]
IteratorOfBucketSet is a function type that creates an iterator for the given bucket.
Parameters:
- bs: the bucket set.
Returns:
- common.Iterater[E]: the iterator.
Behaviors:
- If the given bucket set is nil, an empty iterator is returned.
func KeepIfBuckets ¶ added in v0.3.35
func KeepIfBuckets[K comparable, E any](bs map[K][]E, f us.PredicateFilter[E]) map[K][]E
KeepIfBuckets keeps the elements in the buckets that satisfy the given predicate filter.
Parameters:
- bs: map of buckets to filter.
- f: predicate filter to use.
Returns:
- map[int]*Bucket: the map of buckets.
Behaviors:
- If a bucket is empty after filtering, it is removed.
- WARNING: This can modify the original map.
- If the given map is nil, an empty map is returned.
- If the given filter is nil, the original map is returned (no copy).
func MakeBucketSet ¶ added in v0.3.22
func MakeBucketSet[K comparable, E any](elems []E, f func(E) K) map[K][]E
MakeBucketSet creates a map of buckets from the given elements using the given function.
Parameters:
- elems: elements to add to the buckets.
- f: function to use to determine the size of the buckets.
Returns:
- map[int]*Bucket: the map of buckets.
Behaviors:
- If the given slice of elements is empty or the function is nil, an empty map is returned.
func Sort ¶ added in v0.3.20
Sort is a function that sorts a slice of elements in ascending order.
Parameters:
- slice: The slice to sort.
- sf: A function that compares two elements.
- isAsc: A flag indicating if the sort is in ascending order.
Behaviors:
- This function uses the Quick Sort algorithm to sort the slice.
- The elements in the slice must implement the Comparer interface.
func SortBucketSet ¶ added in v0.3.35
func SortBucketSet[K comparable, E any](bs map[K][]E, sf SortFunc[E], isAsc bool)
SortBucketSet sorts the buckets using the given function in-place.
Parameters:
- bs: map of buckets to sort.
- sf: comparison function to use.
- isAsc: flag indicating if the sort is in ascending order.
Behaviors:
- If the given map is nil or the function is nil, nothing is done.
func StableSort ¶ added in v0.3.20
StableSort is a function that sorts a slice of elements in ascending order while preserving the order of equal elements.
Parameters:
- slice: The slice to sort.
- sf: A function that compares two elements.
- isAsc: A flag indicating if the sort is in ascending order.
Behaviors:
- This function uses the Merge Sort algorithm to sort the slice.
- The elements in the slice must implement the Comparer interface.
func ViewTopNBuckets ¶ added in v0.3.35
func ViewTopNBuckets[K comparable, E any](bs map[K][]E, n int) map[K][]E
ViewTopNBuckets returns the top N buckets.
Parameters:
- bs: map of buckets to filter.
- n: number of buckets to return.
Returns:
- map[int]*Bucket: the map of buckets.
Behaviors:
- If the given map is nil or the number is less than or equal to 0, an empty map is returned.
Types ¶
type Slice ¶
type Slice[T any] struct { // contains filtered or unexported fields }
Slice is a slice that uses the Compare method to compare elements.
func NewSlice ¶
NewSlice creates a new sorted slice.
Parameters:
- elems: The elements to add to the sorted slice.
Returns:
- *Slice[T]: The new sorted slice.
Behaviors:
- Returns nil if the sort function is nil.
func (*Slice[T]) Difference ¶
Difference returns the elements that are in s but not in other.
Parameters:
- other: The slice to compare with.
Returns:
- *Slice[T]: The slice of elements that are in s but not in other.
func (*Slice[T]) Find ¶
Find is the same as Find but uses the Compare method of the elements.
Parameters:
- elem: element to find.
Returns:
- int: index of the first occurrence of the element or -1 if not found.
Behaviors:
- The values must be sorted in ascending order for the Compare method to work.
func (*Slice[T]) FindSubsliceFrom ¶
FindSubBytesFrom finds the first occurrence of a subslice in a byte slice starting from a given index.
Parameters:
- S: The byte slice to search in.
- subS: The byte slice to search for.
- at: The index to start searching from.
Returns:
- int: The index of the first occurrence of the subslice.
Behavior:
- The function uses the Knuth-Morris-Pratt algorithm to find the subslice.
- If S or subS is empty, the function returns -1.
- If the subslice is not found, the function returns -1.
- If at is negative, it is set to 0.
func (*Slice[T]) IndexOfDuplicate ¶
IndexOfDuplicate returns the index of the first duplicate element in the slice.
Returns:
- int: index of the first duplicate element.
Behaviors:
- The function returns -1 if no duplicates are found.
func (*Slice[T]) Insert ¶
Insert inserts an element into the sorted slice.
Parameters:
- elem: The element to insert.
- force: If true, the element is inserted even if it is already in the slice.
Returns:
- int: The index where the element was inserted.
Behaviors:
- The function inserts the element in the correct position to maintain the order.
func (*Slice[T]) MergeUnique ¶
MergeUnique merges two slices and removes duplicate elements.
Parameters:
- S1: first slice of elements.
- S2: second slice of elements.
Returns:
- []T: slice of elements with duplicates removed.
Behaviors:
- The function does preserve the order of the elements in the slice.
func (*Slice[T]) TryInsert ¶
TryInsert tries to insert an element into the sorted slice.
Parameters:
- elem: The element to insert.
Returns:
- int: The index where the element would be inserted.
- bool: True if the element is already in the slice, false otherwise.
func (*Slice[T]) Uniquefy ¶
func (s *Slice[T]) Uniquefy()
Uniquefy is the same as Uniquefy but uses the Compare method of the elements.
Parameters:
- S: slice of elements.
Returns:
- []T: slice of elements with duplicates removed.
Behavior:
- The function preserves the order of the elements in the slice.
- The values must be sorted in ascending order for the Compare method to work.
type SortFunc ¶ added in v0.3.20
SortFunc is a function type that defines a comparison function for elements.
Parameters:
- e1: The first element to compare.
- e2: The second element to compare.
Returns:
- int: A negative value if the first element is less than the second element, 0 if they are equal, and a positive value if the first element is greater than the second element.