Sorting

package
v0.3.37 Latest Latest
Warning

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

Go to latest
Published: Jul 10, 2024 License: MIT Imports: 3 Imported by: 1

Documentation

Index

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

func Sort[T any](S []T, sf SortFunc[T], isAsc bool)

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

func StableSort[T any](S []T, sf SortFunc[T], isAsc bool)

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

func NewSlice[T any](elems []T, sf SortFunc[T], isAsc bool) *Slice[T]

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

func (s *Slice[T]) Difference(other *Slice[T]) *Slice[T]

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

func (s *Slice[T]) Find(elem T) int

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

func (s *Slice[T]) FindSubsliceFrom(other *Slice[T], at int) int

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

func (s *Slice[T]) IndexOfDuplicate() int

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

func (s *Slice[T]) Insert(elem T, force bool) int

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

func (s *Slice[T]) MergeUnique(other *Slice[T]) *Slice[T]

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

func (s *Slice[T]) TryInsert(elem T) (int, bool)

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

type SortFunc[T any] func(e1, e2 T) int

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.

Jump to

Keyboard shortcuts

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