list

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Feb 27, 2025 License: MIT Imports: 1 Imported by: 0

Documentation

Overview

Package list provides common two-linked list with iterators

Can store any comparable value. No pointers and type-checks. Able to be used with 'range' and some functions of 'slices' package

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type List

type List[LT comparable] struct {
	// contains filtered or unexported fields
}

List Two-side linked list

func New

func New[LT comparable](data ...LT) *List[LT]

New Create a new instance of List. Can be filled through initialization with direct order. l := New(1, 2, 3, 4, 5) returns List: 1 <-> 2 <-> 3 <-> 4 <-> 5

func (*List[T]) Add

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

Add Add value to the end of the list. Same as PushBack.

func (*List[T]) AddAfterIndex

func (l *List[T]) AddAfterIndex(index int, values ...T) bool

AddAfterIndex Add value to the specific position. If list is 1 <-> 2 <-> 3 <-> 4 then you call this function with index 2 (for example with value 9). You will get 1 <-> 2 <-> 3 <-> 9 <-> 4

func (*List[T]) AddBeforeIndex

func (l *List[T]) AddBeforeIndex(index int, values ...T) bool

AddBeforeIndex Add value to the specific position. If list is 1 <-> 2 <-> 3 <-> 4 then you call this function with index 2 (for example with value 9). You will get 1 <-> 2 <-> 9 <-> 3 <-> 4

func (*List[T]) Back

func (l *List[T]) Back() (val T, exists bool)

Back returns the last element of the list

func (*List[LT]) Clear

func (l *List[LT]) Clear()

Clear Carefully remove all elements from the List. Prevent memory leaks and clear all links between elements. O(N)

func (*List[T]) Clone

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

Clone creates a new list with equal values with the same order.

func (*List[T]) Contains

func (l *List[T]) Contains(value T) bool

Contains returns true if list has at least one element. In other case returns false.

func (*List[T]) Delete

func (l *List[T]) Delete(value T) bool

Delete removes the first found element

func (*List[T]) Equal

func (l *List[T]) Equal(other *List[T]) bool

Equal checks is 'other' list has equal values with the same order.

func (*List[T]) Find

func (l *List[T]) Find(value T) []int

Find returns all indexes of equal elements.

func (*List[T]) Front

func (l *List[T]) Front() (val T, exists bool)

Front returns the first element of the list

func (*List[T]) Index

func (l *List[T]) Index(value T) int

Index returns the first index of equal element (from the top).

func (*List[LT]) Len

func (l *List[LT]) Len() int

Len Returns count of elements in the list.

func (*List[T]) MoveAfter

func (l *List[T]) MoveAfter(from, to int) bool

MoveAfter moves one element from the index 'from' after index 'to'.

func (*List[T]) MoveBefore

func (l *List[T]) MoveBefore(from, to int) bool

MoveBefore moves one element from the index 'from' before index 'to'.

func (*List[LT]) PeakAt

func (l *List[LT]) PeakAt(index int) (val LT, exists bool)

PeakAt returns an element at the specific of the list and 'true'. If there is no element on this position the default value will be returned and 'false' as the second argument.

func (*List[T]) PopAt

func (l *List[T]) PopAt(index int) (val T, exists bool)

PopAt removes an element from the list and return them, the second argument will be 'true'. If there is no element on this position the default value will be returned and 'false' as the second argument.

func (*List[T]) PopBack

func (l *List[T]) PopBack() (val T, exists bool)

PopBack removes the last element from the list and return them, the second argument will be 'true'. If there is no element default value will be returned and 'false' as the second argument.

func (*List[T]) PopFront

func (l *List[T]) PopFront() (val T, exists bool)

PopFront removes the first element from the list and return them, the second argument will be 'true'. If there is no element default value will be returned and 'false' as the second argument.

func (*List[T]) PushBack

func (l *List[T]) PushBack(values ...T)

PushBack Add value to the end of the list.

func (*List[T]) PushFront

func (l *List[T]) PushFront(values ...T)

PushFront Add value to the start of the list.

func (*List[T]) RIndex

func (l *List[T]) RIndex(value T) int

RIndex returns the first index of equal element (from the end).

func (*List[LT]) ReversedSeq

func (l *List[LT]) ReversedSeq() iter.Seq[LT]

ReversedSeq Return function for value-only sequence but reversed order. Can be used in slices library and range.

Example
lst := New(1, 2, 3, 4)
for num := range lst.ReversedSeq() {
	fmt.Println(num)
}
Output:

4
3
2
1

func (*List[LT]) ReversedSeq2

func (l *List[LT]) ReversedSeq2() iter.Seq2[int, LT]

ReversedSeq2 Return function for int-value sequence (element number and value), but reversed order. Can be used in slices library and range.

Example
lst := New(1, 2, 3, 4)
for i, num := range lst.ReversedSeq2() {
	fmt.Printf("%d: %d\n", i, num)
}

fmt.Println()

// If you need indexes from 0 to 3
reverseIdx := func(seq2 iter.Seq2[int, int]) iter.Seq2[int, int] {
	return func(yield func(int, int) bool) {
		i := 0
		for _, v := range seq2 {
			if !yield(i, v) {
				return
			}
			i++
		}
	}
}

for i, num := range reverseIdx(lst.ReversedSeq2()) {
	fmt.Printf("%d: %d\n", i, num)
}
Output:

3: 4
2: 3
1: 2
0: 1

0: 4
1: 3
2: 2
3: 1

func (*List[LT]) Seq

func (l *List[LT]) Seq() iter.Seq[LT]

Seq Return function for value-only sequence. Can be used in slices library and range.

Example
lst := New(1, 2, 3, 4)
for num := range lst.Seq() {
	fmt.Println(num)
}
Output:

1
2
3
4

func (*List[LT]) Seq2

func (l *List[LT]) Seq2() iter.Seq2[int, LT]

Seq2 Return function for int-value sequence (element number and value). Can be used in slices library and range.

Example
lst := New(1, 2, 3, 4)
for i, num := range lst.Seq2() {
	fmt.Printf("%d: %d\n", i, num)
}
Output:

0: 1
1: 2
2: 3
3: 4

Jump to

Keyboard shortcuts

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