Sorting

package
v0.3.17 Latest Latest
Warning

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

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

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Slice

type Slice[T uc.Comparer] struct {
	// contains filtered or unexported fields
}

Slice is a slice that uses the Compare method to compare elements.

func NewSlice

func NewSlice[T uc.Comparer](elems []T) *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.

func (*Slice[T]) Difference

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

Difference returns the elements that are in S1 but not in S2.

Parameters:

  • S1: The first slice of elements.
  • S2: The second slice of elements.

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, error)

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.
  • error: An error of type *uc.ErrNotComparable if the elements are not comparable.

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, error)

IndexOfDuplicate returns the index of the first duplicate element in the slice.

Parameters:

  • S: slice of elements.

Returns:

  • int: index of the first duplicate element or -1 if there are no duplicates.
  • error: error of type *uc.ErrNotComparable if the elements are not comparable.

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 not preserve the order of the elements in the slices.

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() *Slice[T]

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.

Jump to

Keyboard shortcuts

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