sortedlist

package
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: May 23, 2024 License: Apache-2.0 Imports: 5 Imported by: 0

Documentation

Overview

Package sortedlist provides a sorted list data structure.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func AscendOrder

func AscendOrder[T cmp.Ordered](a, b T) int

AscendOrder is a comparison function that returns the result of comparing two elements in ascending order.

func DescendOrder

func DescendOrder[T cmp.Ordered](a, b T) int

DescendOrder is a comparison function that returns the result of comparing two elements in descending order.

Types

type SortedList

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

SortedList is a type representing a sorted list data structure.

Example
l := NewSortedList(AscendOrder, 1, 3, 2, 5, 6, 9)
fmt.Println(l.ToArray())
Output:

[1 2 3 5 6 9]

func NewSortedList

func NewSortedList[T any](f func(a, b T) int, elements ...T) *SortedList[T]

NewSortedList creates and returns a new SortedList with the provided comparison function and initial elements.

func (SortedList[T]) At

func (l SortedList[T]) At(index int) (T, error)

At returns the element at the specified index in the sorted list, or an error if the index is out of range.

func (*SortedList[T]) Clear

func (l *SortedList[T]) Clear() *SortedList[T]

Clear removes all elements from the sorted list and returns the modified list.

func (SortedList[T]) Copy

func (l SortedList[T]) Copy() SortedList[T]

Copy creates a copy of the sorted list and returns it.

func (SortedList[T]) Count

func (l SortedList[T]) Count(element T) (count int)

Count returns the number of occurrences of the specified element in the sorted list.

func (SortedList[T]) Empty

func (l SortedList[T]) Empty() bool

Empty returns true if the sorted list is empty, otherwise false.

func (SortedList[T]) Equal

func (l SortedList[T]) Equal(another SortedList[T]) bool

Equal compares the sorted list with another sorted list and returns true if they are equal, otherwise false.

func (SortedList[T]) Every

func (l SortedList[T]) Every(condition func(T) bool) bool

Every checks if all elements in the list pass the condition implemented by the provided function.

func (SortedList[T]) Filter

func (l SortedList[T]) Filter(condition func(T) bool) SortedList[T]

Filter creates a new list with all elements that pass the condition implemented by the provided function.

func (SortedList[T]) Find

func (l SortedList[T]) Find(by func(T) bool) (T, bool)

Find returns the first element satisfying the given predicate function, along with a boolean indicating its existence.

func (SortedList[T]) FindIndex

func (l SortedList[T]) FindIndex(by func(T) bool) int

FindIndex returns the index of the first element satisfying the given predicate function, or -1 if not found.

func (SortedList[T]) FindLast

func (l SortedList[T]) FindLast(by func(T) bool) (T, bool)

FindLast returns the last element satisfying the given predicate function, along with a boolean indicating its existence.

func (SortedList[T]) FindLastIndex

func (l SortedList[T]) FindLastIndex(by func(T) bool) int

FindLastIndex returns the index of the last element satisfying the given predicate function, or -1 if not found.

func (SortedList[T]) Head

func (l SortedList[T]) Head() (T, error)

Head returns the first element of the sorted list, or an error if the list is empty.

func (SortedList[T]) Includes

func (l SortedList[T]) Includes(element T) bool

Includes checks if the specified element is present in the sorted list.

func (SortedList[T]) IndexOf

func (l SortedList[T]) IndexOf(element T) int

IndexOf returns the index of the first occurrence of the specified element in the sorted list, or -1 if the element is not found.

func (*SortedList[T]) Insert

func (l *SortedList[T]) Insert(element T) *SortedList[T]

Insert inserts an element into the sorted list at the appropriate position and returns the modified list.

Example
l := NewSortedList(AscendOrder, 1, 5, 8)
fmt.Println(l.ToArray())
l.Insert(3)
fmt.Println(l.ToArray())
Output:

[1 5 8]
[1 3 5 8]

func (SortedList[T]) LastIndexOf

func (l SortedList[T]) LastIndexOf(element T) int

LastIndexOf returns the index of the last occurrence of the specified element in the sorted list, or -1 if the element is not found.

func (SortedList[T]) Len

func (l SortedList[T]) Len() int

Len returns the number of elements in the sorted list.

func (SortedList[T]) Max

func (l SortedList[T]) Max() (element T, err error)

Max returns the maximum element in the sorted list, along with an error if the list is empty.

Example
l := NewSortedList(AscendOrder, 1, 2, 3, 2, 4)
m, _ := l.Max()
fmt.Println(m)
l.Reverse()
m, _ = l.Max()
fmt.Println(m)
Output:

4
1

func (SortedList[T]) Min

func (l SortedList[T]) Min() (element T, err error)

Min returns the minimum element in the sorted list, along with an error if the list is empty.

Example
l := NewSortedList(AscendOrder, 1, 2, 3, 2, 4)
m, _ := l.Min()
fmt.Println(m)
l.Reverse()
m, _ = l.Min()
fmt.Println(m)
Output:

1
4

func (*SortedList[T]) Pop

func (l *SortedList[T]) Pop(indexes ...int) (element T, err error)

Pop removes and returns the element at the specified indexes from the sorted list.

func (SortedList[T]) Reduce

func (l SortedList[T]) Reduce(handler func(T, T) T, initial ...T) (T, error)

Reduce applies a function against an accumulator and each element in the list (from left to right) to reduce it to a single value.

func (SortedList[T]) ReduceRight

func (l SortedList[T]) ReduceRight(handler func(T, T) T, initial ...T) (T, error)

ReduceRight applies a function against an accumulator and each element in the list (from right to left) to reduce it to a single value.

func (*SortedList[T]) Remove

func (l *SortedList[T]) Remove(element T, counts ...int) *SortedList[T]

Remove removes the specified element from the sorted list, according to the given count, and returns the modified list.

func (*SortedList[T]) RemoveIf

func (l *SortedList[T]) RemoveIf(condition func(T) bool, counts ...int) SortedList[T]

RemoveIf removes elements from the sorted list based on the specified condition and returns a new sorted list.

func (*SortedList[T]) RemoveRange

func (l *SortedList[T]) RemoveRange(start, end int) SortedList[T]

RemoveRange removes a range of elements from the sorted list and returns a new sorted list.

Example
l := NewSortedList(AscendOrder, 2, 3, 1, 4, 5)
fmt.Println(l.ToArray())
removed := l.RemoveRange(1, 3)
fmt.Println(removed.ToArray())
fmt.Println(l.ToArray())
Output:

[1 2 3 4 5]
[2 3]
[1 4 5]

func (*SortedList[T]) RemoveRightIf

func (l *SortedList[T]) RemoveRightIf(condition func(T) bool, counts ...int) SortedList[T]

RemoveRightIf removes elements from the end of the sorted list based on the specified condition and returns a new sorted list.

func (*SortedList[T]) Reverse

func (l *SortedList[T]) Reverse() *SortedList[T]

Reverse reverses the order of elements in the sorted list and returns the modified list.

func (SortedList[T]) Slice

func (l SortedList[T]) Slice(args ...int) SortedList[T]

Slice returns a new sorted list that is a subset of the original sorted list based on the provided slice parameters.

func (SortedList[T]) Some

func (l SortedList[T]) Some(condition func(T) bool) bool

Some checks if at least one element in the list passes the condition implemented by the provided function.

func (SortedList[T]) Tail

func (l SortedList[T]) Tail() (T, error)

Tail returns the last element of the sorted list, or an error if the list is empty.

func (SortedList[T]) ToArray

func (l SortedList[T]) ToArray() []T

ToArray returns a copy of the elements in the sorted list as a regular slice.

func (SortedList[T]) ToList

func (l SortedList[T]) ToList() arraylist.ArrayList[T]

ToList returns a copy of the sorted list's underlying arraylist.

func (SortedList[T]) ToReversed

func (l SortedList[T]) ToReversed() SortedList[T]

ToReversed returns a new sorted list with the elements in reversed order.

Jump to

Keyboard shortcuts

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