Sorting

package
v0.3.30 Latest Latest
Warning

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

Go to latest
Published: Jun 19, 2024 License: MIT Imports: 5 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 Bucket added in v0.3.20

type Bucket[T any] struct {
	// contains filtered or unexported fields
}

Bucket represents a bucket of elements.

func NewBucket added in v0.3.20

func NewBucket[T any](elements []T) *Bucket[T]

NewBucket creates a new bucket of elements.

Parameters:

  • elements: elements to add to the bucket.

Returns:

  • *Bucket: the new bucket.

func (*Bucket[T]) Add added in v0.3.20

func (b *Bucket[T]) Add(element T)

Add adds a element to the bucket.

Parameters:

  • element: element to add.

func (*Bucket[T]) Copy added in v0.3.20

func (b *Bucket[T]) Copy() uc.Copier

Copy implements the common.Copier interface.

func (*Bucket[T]) GetSize added in v0.3.20

func (b *Bucket[T]) GetSize() int

GetSize returns the size of the bucket.

Returns:

  • int: the size of the bucket.

func (*Bucket[T]) Iterator added in v0.3.20

func (b *Bucket[T]) Iterator() ui.Iterater[T]

Iterator implements the Iterators.Iterable interface.

func (*Bucket[T]) Limit added in v0.3.20

func (b *Bucket[T]) Limit(n int)

Limit limits the number of elements in the bucket.

Parameters:

  • n: number of elements to keep.

Behaviors:

  • If the n is less than or equal to 0, then the bucket will be empty.
  • If the n is greater then the size of the bucket, then the bucket will remain the same.

func (*Bucket[T]) LinearKeep added in v0.3.20

func (b *Bucket[T]) LinearKeep(f us.PredicateFilter[T])

LinearKeep keeps the elements in the bucket that satisfy the given predicate filter.

Parameters:

  • f: predicate filter to use.

func (*Bucket[T]) Slice added in v0.3.21

func (b *Bucket[T]) Slice() []T

Slice returns the elements of the bucket.

If possible, use only Iterator() instead of this function.

Returns:

  • []T: the elements of the bucket.

func (*Bucket[T]) Sort added in v0.3.20

func (b *Bucket[T]) Sort(sf SortFunc[T], isAsc bool)

Sort sorts the elements in the bucket using the given comparison function.

Parameters:

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

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, *Bucket[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) *Bucket[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]*Bucket[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 ErrNotComparable added in v0.3.20

type ErrNotComparable[A, B any] struct {
	// First is the first value that is not comparable.
	First A

	// Second is the second value that is not comparable.
	Second B
}

ErrNotComparable is an error type that is returned when two values are not comparable.

func NewErrNotComparable added in v0.3.20

func NewErrNotComparable[A, B any](first A, second B) *ErrNotComparable[A, B]

NewErrNotComparable creates a new ErrNotComparable error with the provided values.

Parameters:

  • first: The first value that is not comparable.
  • second: The second value that is not comparable.

Returns:

  • *ErrNotComparable: A pointer to the new error.

func (*ErrNotComparable[A, B]) Error added in v0.3.20

func (e *ErrNotComparable[A, B]) Error() string

Error returns the error message: "values <First> and <Second> are not comparable".

Returns:

  • string: The error message.

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