lists

package
v0.0.0-...-18275cc Latest Latest
Warning

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

Go to latest
Published: May 9, 2022 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package lists provides a doubly linked list and various functions useful with lists of any type.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Back

func Back[T any](l *List[T]) T

Back returns the last element in the list. It panics if the given list is empty. This function is O(1).

func Compact

func Compact[T comparable](l *List[T])

Compact replaces every consecutive group of equal elements with a single copy. This is like the uniq Unix command. This function is O(n), where n is length of the list.

func CompactFunc

func CompactFunc[T any](l *List[T], eq gcl.EqualFn[T, T])

CompactFunc is like Compact but it uses the `eq` function for comparison. This function is O(f * n), where n is length of the list and f is time complexity of the `eq` function.

func Compare

func Compare[T constraints.Ordered](l1, l2 *List[T]) int

Compare compares the elements of l1 and l2. The elements are compared sequentially from the beginning, until one element is not equal to the corresponding one. The result of comparing the first non-matching elements is returned. If both lists are equal until one of them ends, the shorter list is considered less than the longer one. The result is 0 if l1 == l2, -1 if l1 < l2, and +1 if l1 > l2. Comparisons involving floating point NaNs are ignored. This function is O(min(Len(l1), Len(l2))).

func CompareFunc

func CompareFunc[T1 any, T2 any](l1 *List[T1], l2 *List[T2], cmp gcl.CompareFn[T1, T2]) int

CompareFunc operates the same as Compare function but it uses the given `cmp` function for comparing each pair of elements. The result is the first non-zero result of cmp; if cmp always returns 0 the result is 0 if Len(l1) == Len(l2), -1 if Len(l1) < Len(l2), This function is O(f * min(Len(l1), Len(l2))), where f is the time complexity of `cmp` function.

func Contains

func Contains[T comparable](l *List[T], v T) bool

Contains tests whether the given list l has value v. This function is O(n), where n is length of the list.

func Equal

func Equal[T comparable](l1, l2 *List[T]) bool

Equal tests whether two lists are equal: the same length and all elements equal. Floating point NaNs are not considered equal. This function is O(min(Len(l1), Len(l2))).

func EqualFunc

func EqualFunc[T1 any, T2 any](l1 *List[T1], l2 *List[T2], eq gcl.EqualFn[T1, T2]) bool

EqualFunc tests whether two lists are equal using the given `eq` function. For each pair of elements, `eq` determines if they are equal or not. This function is O(f * min(Len(l1), Len(l2))), where f is the time complexity of `eq` function.

func Front

func Front[T any](l *List[T]) T

Front returns the first element in the list. It panics if the given list is empty. This function is O(1).

func Index

func Index[T comparable](l *List[T], v T) int

Index returns the index of the first occurrence of v in l, or -1 if not present. This function is O(n), where n is length of the list.

func IndexFunc

func IndexFunc[T comparable](l *List[T], pred func(T) bool) int

IndexFunc returns the index of the first element satisfying pred, or -1 if none do. This function is O(f * n), where n is length of the list and f is time complexity of the `pred` function.

func IsSorted

func IsSorted[T constraints.Ordered](l *List[T]) bool

IsSorted tests whether a list of any ordered type is sorted in ascending order. This function is O(n), where n is length of the list.

func IsSortedFunc

func IsSortedFunc[T any](l *List[T], less gcl.LessFn[T]) bool

IsSortedFunc tests whether a list type is sorted in ascending order, according to the `less` comparison function. This function is O(f * n), where n is length of the list and f is time complexity of the `less` function.

func Len

func Len[T any](l *List[T]) int

Len returns size of the given list. This function is O(1).

func PopBack

func PopBack[T any](l *List[T])

PopBack deletes the last element in the list. It requires the list to be non-empty, otherwise it panics. This function is O(1).

func PopFront

func PopFront[T any](l *List[T])

PopFront deletes the first element in the list. It requires the list to be non-empty, otherwise it panics. This function is O(1).

func PushBack

func PushBack[T any](l *List[T], elems ...T)

PushBack appends the given elements to the back of list `l`. This function is O(Len(elems)). So for a single element it would be O(1).

func PushFront

func PushFront[T any](l *List[T], elems ...T)

PushFront appends the given elements to the beginning of list `l`. This function is O(Len(elems)). So for a single element it would be O(1).

func Reverse

func Reverse[T any](l *List[T])

Reverse reverses the elements of the given list. This function is O(n), where n is length of the list.

func Sort

func Sort[T constraints.Ordered](l *List[T])

Sort sorts a list of any ordered type in ascending order. This function is O(n * log(n)), where n is length of the list.

func SortFunc

func SortFunc[T any](l *List[T], less gcl.LessFn[T])

SortFunc sorts the given list in ascending order as determined by the `less` function. This function is O(f * n * log(n)), where n is length of the list and f is time complexity of the `less` function.

Types

type FrwIter

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

FrwIter is a list forward iterator.

func Iter

func Iter[T any](l *List[T]) *FrwIter[T]

Iter returns an forward iterator to the beginning. Initially, the returned iterator is located at one step before the first element (one-before-first).

func Pos

func Pos[T comparable](l *List[T], v T) (*FrwIter[T], bool)

Pos returns an iterator pointing to the first occurrence of v in l. The returned boolean value indicates if v is present in the list. This function is O(n), where n is length of the list.

func (*FrwIter[T]) HasNext

func (it *FrwIter[T]) HasNext() bool

func (*FrwIter[T]) Next

func (it *FrwIter[T]) Next() T

type FrwIterMut

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

FrwIterMut is a mutable forward iterator for lists. It allows mutations by returning pointers to the list elements. FrwIterMut is an iterator over pointers of type T. In other words, FrwIterMut[T] implements iters.Iterator[*T].

func IterMut

func IterMut[T any](l *List[T]) *FrwIterMut[T]

IterMut returns a forward iterator to the beginning with mutable pointers. Initially, the returned iterator is located at one step before the first element (one-before-first).

func (*FrwIterMut[T]) Delete

func (it *FrwIterMut[T]) Delete()

Delete deletes the element that the iterator it is pointing to. Delete requires the iterator it to point to an actual element in the list. For example, it's not possible to call Delete on the IterMut iterator (in its inital state) because this iterator is located at one step before the first element and this is not an actual list element. This function is O(1).

func (*FrwIterMut[T]) HasNext

func (it *FrwIterMut[T]) HasNext() bool

func (*FrwIterMut[T]) Insert

func (it *FrwIterMut[T]) Insert(elems ...T)

Insert inserts the given values next after the iterator it. This function is O(len(elems)). So inserting a single element would be O(1).

func (*FrwIterMut[T]) Next

func (it *FrwIterMut[T]) Next() *T

type List

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

List is doubly linked list.

func Clone

func Clone[T any](l *List[T]) *List[T]

Clone returns a copy of the given list. The elements are copied using assignment, so this is a shallow clone. This function is O(n), where n is length of the list.

func FromIter

func FromIter[T any](it iters.Iterator[T]) *List[T]

FromIter builds a new list from the given iterator.

func New

func New[T any](elems ...T) *List[T]

New creates a new linked list and returns a pointer to it.

func (*List[T]) MarshalJSON

func (l *List[T]) MarshalJSON() ([]byte, error)

func (*List[T]) String

func (l *List[T]) String() string

func (*List[T]) UnmarshalJSON

func (l *List[T]) UnmarshalJSON(b []byte) error

type RevIter

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

RevIter is a list reverse iterator.

func RIter

func RIter[T any](l *List[T]) *RevIter[T]

RIter returns a reverse iterator going from the end to the beginning. Initially, the returned iterator is located at one step past the last element (one-past-last).

func (*RevIter[T]) HasNext

func (it *RevIter[T]) HasNext() bool

func (*RevIter[T]) Next

func (it *RevIter[T]) Next() T

type RevIterMut

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

RevIterMut is a mutable reverse iterator for lists. It allows mutations by returning pointers to the list elements. RevIterMut is an iterator over pointers of type T. In other words, RevIterMut[T] implements iters.Iterator[*T].

func RIterMut

func RIterMut[T any](l *List[T]) *RevIterMut[T]

RIterMut returns a reverse iterator going from the end to the beginning with mutable pointers. Initially, the returned iterator is located at one step past the last element (one-past-last).

func (*RevIterMut[T]) Delete

func (it *RevIterMut[T]) Delete()

Delete deletes the element that the iterator it is pointing to. Delete requires the iterator it to point to an actual element in the list. For example, it's not possible to call Delete on the RIterMut iterator (in its inital state) because this iterator is located at one step past the last element and this is not an actual list element. This function is O(1).

func (*RevIterMut[T]) HasNext

func (it *RevIterMut[T]) HasNext() bool

func (*RevIterMut[T]) Insert

func (it *RevIterMut[T]) Insert(elems ...T)

Insert inserts the given values next after (in a reverse direction) the iterator it. This function is O(len(elems)). So inserting a single element would be O(1).

func (*RevIterMut[T]) Next

func (it *RevIterMut[T]) Next() *T

Jump to

Keyboard shortcuts

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