Documentation ¶
Index ¶
- 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, error)
- func (s *Slice[T]) IndexOfDuplicate() (int, error)
- 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() *Slice[T]
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Slice ¶
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.
func (*Slice[T]) Difference ¶
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 ¶
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.
- 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 ¶
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 ¶
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 not preserve the order of the elements in the slices.
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 ¶
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.