singlylinkedlist

package
v0.0.0-...-0e264d2 Latest Latest
Warning

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

Go to latest
Published: Sep 29, 2022 License: BSD-2-Clause, ISC Imports: 7 Imported by: 0

Documentation

Overview

Package singlylinkedlist implements the singly-linked list.

Structure is not thread safe.

Reference: https://en.wikipedia.org/wiki/List_%28abstract_data_type%29

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Iterator

type Iterator[T constraints.Ordered] struct {
	// contains filtered or unexported fields
}

Iterator holding the iterator's state

func (*Iterator[T]) Begin

func (iterator *Iterator[T]) Begin()

Begin resets the iterator to its initial state (one-before-first) Call Next() to fetch the first element if any.

func (*Iterator[T]) First

func (iterator *Iterator[T]) First() bool

First moves the iterator to the first element and returns true if there was a first element in the container. If First() returns true, then first element's index and value can be retrieved by Index() and Value(). Modifies the state of the iterator.

func (*Iterator[T]) Index

func (iterator *Iterator[T]) Index() int

Index returns the current element's index. Does not modify the state of the iterator.

func (*Iterator[T]) Next

func (iterator *Iterator[T]) Next() bool

Next moves the iterator to the next element and returns true if there was a next element in the container. If Next() returns true, then next element's index and value can be retrieved by Index() and Value(). If Next() was called for the first time, then it will point the iterator to the first element if it exists. Modifies the state of the iterator.

func (*Iterator[T]) NextTo

func (iterator *Iterator[T]) NextTo(f func(index int, value T) bool) bool

NextTo moves the iterator to the next element from current position that satisfies the condition given by the passed function, and returns true if there was a next element in the container. If NextTo() returns true, then next element's index and value can be retrieved by Index() and Value(). Modifies the state of the iterator.

func (*Iterator[T]) Value

func (iterator *Iterator[T]) Value() T

Value returns the current element's value. Does not modify the state of the iterator.

type List

type List[T constraints.Ordered] struct {
	// contains filtered or unexported fields
}

List holds the elements, where each element points to the next element

func New

func New[T constraints.Ordered](values ...T) *List[T]

New instantiates a new list and adds the passed values, if any, to the list

func (*List[T]) Add

func (list *List[T]) Add(values ...T)

Add appends a value (one or more) at the end of the list (same as Append())

func (*List[T]) All

func (list *List[T]) All(f func(index int, value T) bool) bool

All passes each element of the container to the given function and returns true if the function returns true for all elements.

func (*List[T]) Any

func (list *List[T]) Any(f func(index int, value T) bool) bool

Any passes each element of the container to the given function and returns true if the function ever returns true for any element.

func (*List[T]) Append

func (list *List[T]) Append(values ...T)

Append appends a value (one or more) at the end of the list (same as Add())

func (*List[T]) Clear

func (list *List[T]) Clear()

Clear removes all elements from the list.

func (*List[T]) Contains

func (list *List[T]) Contains(values ...T) bool

Contains checks if values (one or more) are present in the set. All values have to be present in the set for the method to return true. Performance time complexity of n^2. Returns true if no arguments are passed at all, i.e. set is always super-set of empty set.

func (*List[T]) Each

func (list *List[T]) Each(f func(index int, value T))

Each calls the given function once for each element, passing that element's index and value.

func (*List[T]) Empty

func (list *List[T]) Empty() bool

Empty returns true if list does not contain any elements.

func (*List[T]) Find

func (list *List[T]) Find(f func(index int, value T) bool) (index int, value T)

Find passes each element of the container to the given function and returns the first (index,value) for which the function is true or -1,nil otherwise if no element matches the criteria.

func (*List[T]) FromJSON

func (list *List[T]) FromJSON(data []byte) error

FromJSON populates list's elements from the input JSON representation.

func (*List[T]) Get

func (list *List[T]) Get(index int) (T, bool)

Get returns the element at index. Second return parameter is true if index is within bounds of the array and array is not empty, otherwise false.

func (*List[T]) IndexOf

func (list *List[T]) IndexOf(value T) int

IndexOf returns index of provided element

func (*List[T]) Insert

func (list *List[T]) Insert(index int, values ...T)

Insert inserts values at specified index position shifting the value at that position (if any) and any subsequent elements to the right. Does not do anything if position is negative or bigger than list's size Note: position equal to list's size is valid, i.e. append.

func (*List[T]) Iterator

func (list *List[T]) Iterator() Iterator[T]

Iterator returns a stateful iterator whose values can be fetched by an index.

func (*List[T]) Map

func (list *List[T]) Map(f func(index int, value T) T) *List[T]

Map invokes the given function once for each element and returns a container containing the values returned by the given function.

func (*List[T]) MarshalJSON

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

MarshalJSON @implements json.Marshaler

func (*List[T]) Prepend

func (list *List[T]) Prepend(values ...T)

Prepend prepends a values (or more)

func (*List[T]) Remove

func (list *List[T]) Remove(index int)

Remove removes the element at the given index from the list.

func (*List[T]) Select

func (list *List[T]) Select(f func(index int, value T) bool) *List[T]

Select returns a new container containing all elements for which the given function returns a true value.

func (*List[T]) Set

func (list *List[T]) Set(index int, value T)

Set value at specified index Does not do anything if position is negative or bigger than list's size Note: position equal to list's size is valid, i.e. append.

func (*List[T]) Size

func (list *List[T]) Size() int

Size returns number of elements within the list.

func (*List[T]) Sort

func (list *List[T]) Sort(comparator utils.Comparator[T])

Sort sort values (in-place) using.

func (*List[T]) String

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

String returns a string representation of container

func (*List[T]) Swap

func (list *List[T]) Swap(i, j int)

Swap swaps values of two elements at the given indices.

func (*List[T]) ToJSON

func (list *List[T]) ToJSON() ([]byte, error)

ToJSON outputs the JSON representation of list's elements.

func (*List[T]) UnmarshalJSON

func (list *List[T]) UnmarshalJSON(bytes []byte) error

UnmarshalJSON @implements json.Unmarshaler

func (*List[T]) Values

func (list *List[T]) Values() []T

Values returns all elements in the list.

Jump to

Keyboard shortcuts

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