Sorting

package
v0.3.31 Latest Latest
Warning

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

Go to latest
Published: Jun 20, 2024 License: MIT Imports: 4 Imported by: 1

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

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 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.

Types

type BucketSet added in v0.3.20

type BucketSet[K comparable, E any] struct {
	// contains filtered or unexported fields
}

BucketSet is a type that represents a set of buckets.

func MakeBucketSet added in v0.3.22

func MakeBucketSet[K comparable, E any](elems []E, f func(E) K) *BucketSet[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.

func NewBucketSet added in v0.3.20

func NewBucketSet[K comparable, E any](m map[K][]E) *BucketSet[K, E]

NewBucketSet creates a new bucket set from the given map of elements.

Parameters:

  • m: map of elements to add to the buckets.

Returns:

  • *BucketSet: the new bucket set.

func (*BucketSet[K, E]) Copy added in v0.3.22

func (bs *BucketSet[K, E]) Copy() uc.Copier

Copy implements the common.Copier interface.

func (*BucketSet[K, E]) DoBuckets added in v0.3.21

func (bs *BucketSet[K, E]) DoBuckets(f func(K, []E) error) error

DoBuckets applies the given function to each bucket.

Parameters:

  • f: function to apply to each bucket.

Returns:

  • error: the first error encountered.

func (*BucketSet[K, E]) GetBucket added in v0.3.21

func (bs *BucketSet[K, E]) GetBucket(size K) []E

GetBucket returns the bucket with the given size.

Parameters:

  • size: size of the bucket to return.

Returns:

  • *Bucket: the bucket with the given size.

Behaviors:

  • If the bucket does not exist, nil is returned.

func (*BucketSet[K, E]) GetBuckets added in v0.3.21

func (bs *BucketSet[K, E]) GetBuckets() map[K][]E

GetBuckets returns the map of buckets.

Returns:

  • map[int]*Bucket: the map of buckets.

func (*BucketSet[K, E]) Iterator added in v0.3.21

func (bs *BucketSet[K, E]) Iterator() ui.Iterater[E]

Iterator implements the Iterators.Iterable interface.

func (*BucketSet[K, E]) KeepIfBuckets added in v0.3.20

func (bs *BucketSet[K, E]) KeepIfBuckets(f us.PredicateFilter[E])

KeepIfBuckets keeps the elements in the buckets that satisfy the given predicate filter.

Parameters:

  • f: predicate filter to use.

func (*BucketSet[K, E]) Sort added in v0.3.20

func (bs *BucketSet[K, E]) Sort(sf SortFunc[E], isAsc bool)

Sort sorts the buckets using the given function.

Parameters:

  • buckets: buckets to sort.
  • sf: comparison function to use.
  • isAsc: flag indicating if the sort is in ascending order.

func (*BucketSet[K, E]) ViewTopNBuckets added in v0.3.20

func (bs *BucketSet[K, E]) ViewTopNBuckets(n int)

ViewTopNBuckets returns the top N buckets.

Parameters:

  • n: number of buckets to return.

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