list

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Feb 7, 2024 License: MIT Imports: 1 Imported by: 0

Documentation

Overview

Package list implements a generic doubly linked list. It is re-implementation of container/list with generics.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func All

func All[T comparable](l *List[T], value T) bool

All returns whether all elements of l are value.

Example
package main

import (
	"fmt"

	"github.com/shogo82148/hi/list"
)

func main() {
	var l list.List[int]
	l.PushBack(5)
	l.PushBack(5)
	l.PushBack(5)
	l.PushBack(5)
	l.PushBack(5)

	fmt.Println(list.All(&l, 5))
	fmt.Println(list.Any(&l, 6))
}
Output:

true
false

func AllBy

func AllBy[T any](l *List[T], f func(int, T) bool) bool

AllBy returns whether f returns true for all elements in l.

Example
package main

import (
	"fmt"

	"github.com/shogo82148/hi/list"
)

func main() {
	var l list.List[int]
	l.PushBack(1)
	l.PushBack(2)
	l.PushBack(3)
	l.PushBack(4)
	l.PushBack(5)

	fmt.Println(list.AllBy(&l, func(_, v int) bool { return v > 0 }))
	fmt.Println(list.AllBy(&l, func(_, v int) bool { return v > 3 }))
}
Output:

true
false

func Any

func Any[T comparable](l *List[T], value T) bool

Any returns whether l has value at least one.

Example
package main

import (
	"fmt"

	"github.com/shogo82148/hi/list"
)

func main() {
	var l list.List[int]
	l.PushBack(1)
	l.PushBack(2)
	l.PushBack(3)
	l.PushBack(4)
	l.PushBack(5)

	fmt.Println(list.Any(&l, 5))
	fmt.Println(list.Any(&l, 6))
}
Output:

true
false

func AnyBy

func AnyBy[T any](l *List[T], f func(index int, value T) bool) bool

AnyBy returns whether l has an element for that f returns true.

Example
package main

import (
	"fmt"

	"github.com/shogo82148/hi/list"
)

func main() {
	var l list.List[int]
	l.PushBack(1)
	l.PushBack(2)
	l.PushBack(3)
	l.PushBack(4)
	l.PushBack(5)

	fmt.Println(list.AnyBy(&l, func(_, v int) bool { return v > 3 }))
	fmt.Println(list.AnyBy(&l, func(_, v int) bool { return v > 5 }))
}
Output:

true
false

func Count

func Count[T comparable](l *List[T], value T) int

Count counts the number of elements in the collection that compare equal to value.

Example
package main

import (
	"fmt"

	"github.com/shogo82148/hi/list"
)

func main() {
	var l list.List[int]
	l.PushBack(3)
	l.PushBack(1)
	l.PushBack(4)
	l.PushBack(1)
	l.PushBack(5)
	l.PushBack(9)
	l.PushBack(2)
	l.PushBack(6)
	l.PushBack(5)
	l.PushBack(3)
	l.PushBack(5)

	cnt := list.Count(&l, 5)
	fmt.Println(cnt)
}
Output:

3

func CountBy

func CountBy[T any](l *List[T], f func(index int, value T) bool) int

CountBy counts the number of elements that counter returns true.

Example
package main

import (
	"fmt"

	"github.com/shogo82148/hi/list"
)

func main() {
	var l list.List[int]
	l.PushBack(1)
	l.PushBack(2)
	l.PushBack(3)
	l.PushBack(4)
	l.PushBack(5)

	cnt := list.CountBy(&l, func(_, v int) bool { return v > 3 })
	fmt.Println(cnt)
}
Output:

2

func Reduce

func Reduce[T, U any](l *List[T], f func(i int, acc U, item T) U, init U) U

Reduce reduces l.

func Unzip2

func Unzip2[T1, T2 any](l *List[tuple.Tuple2[T1, T2]]) (*List[T1], *List[T2])

Unzip2 converts a list of 2-tuple to lists of each elements.

Example
package main

import (
	"fmt"

	"github.com/shogo82148/hi/list"
	"github.com/shogo82148/hi/tuple"
)

func main() {
	var l list.List[tuple.Tuple2[int, string]]
	l.PushBack(tuple.New2(1, "one"))
	l.PushBack(tuple.New2(2, "two"))
	l.PushBack(tuple.New2(3, "three"))

	l1, l2 := list.Unzip2(&l)
	for e := l1.Front(); e != nil; e = e.Next() {
		fmt.Println(e.Value)
	}
	for e := l2.Front(); e != nil; e = e.Next() {
		fmt.Println(e.Value)
	}
}
Output:

1
2
3
one
two
three

func Unzip3

func Unzip3[T1, T2, T3 any](l *List[tuple.Tuple3[T1, T2, T3]]) (*List[T1], *List[T2], *List[T3])

Unzip3 converts a list of 3-tuple to lists of each elements.

func Unzip4

func Unzip4[T1, T2, T3, T4 any](l *List[tuple.Tuple4[T1, T2, T3, T4]]) (*List[T1], *List[T2], *List[T3], *List[T4])

Unzip4 converts a list of 4-tuple to lists of each elements.

func Unzip5

func Unzip5[T1, T2, T3, T4, T5 any](l *List[tuple.Tuple5[T1, T2, T3, T4, T5]]) (*List[T1], *List[T2], *List[T3], *List[T4], *List[T5])

Unzip5 converts a list of 5-tuple to lists of each elements.

func Unzip6

func Unzip6[T1, T2, T3, T4, T5, T6 any](l *List[tuple.Tuple6[T1, T2, T3, T4, T5, T6]]) (*List[T1], *List[T2], *List[T3], *List[T4], *List[T5], *List[T6])

Unzip6 converts a list of 6-tuple to lists of each elements.

func Unzip7

func Unzip7[T1, T2, T3, T4, T5, T6, T7 any](l *List[tuple.Tuple7[T1, T2, T3, T4, T5, T6, T7]]) (*List[T1], *List[T2], *List[T3], *List[T4], *List[T5], *List[T6], *List[T7])

Unzip7 converts a list of 7-tuple to lists of each elements.

func Unzip8

func Unzip8[T1, T2, T3, T4, T5, T6, T7, T8 any](l *List[tuple.Tuple8[T1, T2, T3, T4, T5, T6, T7, T8]]) (*List[T1], *List[T2], *List[T3], *List[T4], *List[T5], *List[T6], *List[T7], *List[T8])

Unzip8 converts a list of 8-tuple to lists of each elements.

func Unzip9

func Unzip9[T1, T2, T3, T4, T5, T6, T7, T8, T9 any](l *List[tuple.Tuple9[T1, T2, T3, T4, T5, T6, T7, T8, T9]]) (*List[T1], *List[T2], *List[T3], *List[T4], *List[T5], *List[T6], *List[T7], *List[T8], *List[T9])

Unzip9 converts a list of 9-tuple to lists of each elements.

func Unzip10

func Unzip10[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10 any](l *List[tuple.Tuple10[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10]]) (*List[T1], *List[T2], *List[T3], *List[T4], *List[T5], *List[T6], *List[T7], *List[T8], *List[T9], *List[T10])

Unzip10 converts a list of 10-tuple to lists of each elements.

func Unzip11

func Unzip11[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11 any](l *List[tuple.Tuple11[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11]]) (*List[T1], *List[T2], *List[T3], *List[T4], *List[T5], *List[T6], *List[T7], *List[T8], *List[T9], *List[T10], *List[T11])

Unzip11 converts a list of 11-tuple to lists of each elements.

func Unzip12

func Unzip12[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12 any](l *List[tuple.Tuple12[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12]]) (*List[T1], *List[T2], *List[T3], *List[T4], *List[T5], *List[T6], *List[T7], *List[T8], *List[T9], *List[T10], *List[T11], *List[T12])

Unzip12 converts a list of 12-tuple to lists of each elements.

func Unzip13

func Unzip13[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13 any](l *List[tuple.Tuple13[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13]]) (*List[T1], *List[T2], *List[T3], *List[T4], *List[T5], *List[T6], *List[T7], *List[T8], *List[T9], *List[T10], *List[T11], *List[T12], *List[T13])

Unzip13 converts a list of 13-tuple to lists of each elements.

func Unzip14

func Unzip14[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14 any](l *List[tuple.Tuple14[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14]]) (*List[T1], *List[T2], *List[T3], *List[T4], *List[T5], *List[T6], *List[T7], *List[T8], *List[T9], *List[T10], *List[T11], *List[T12], *List[T13], *List[T14])

Unzip14 converts a list of 14-tuple to lists of each elements.

func Unzip15

func Unzip15[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15 any](l *List[tuple.Tuple15[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15]]) (*List[T1], *List[T2], *List[T3], *List[T4], *List[T5], *List[T6], *List[T7], *List[T8], *List[T9], *List[T10], *List[T11], *List[T12], *List[T13], *List[T14], *List[T15])

Unzip15 converts a list of 15-tuple to lists of each elements.

func Unzip16

func Unzip16[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16 any](l *List[tuple.Tuple16[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16]]) (*List[T1], *List[T2], *List[T3], *List[T4], *List[T5], *List[T6], *List[T7], *List[T8], *List[T9], *List[T10], *List[T11], *List[T12], *List[T13], *List[T14], *List[T15], *List[T16])

Unzip16 converts a list of 16-tuple to lists of each elements.

Types

type Element

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

func (*Element[T]) Next

func (e *Element[T]) Next() *Element[T]

Next returns the next list element or nil.

func (*Element[T]) Prev

func (e *Element[T]) Prev() *Element[T]

Prev returns the previous list element or nil.

type List

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

func Filter

func Filter[T any](l *List[T], f func(index int, value T) bool) *List[T]

Filter iterates over elements of collection, returning a list of all elements predicate returns true for.

Example
package main

import (
	"fmt"

	"github.com/shogo82148/hi/list"
)

func main() {
	var l list.List[int]
	l.PushBack(1)
	l.PushBack(2)
	l.PushBack(3)
	l.PushBack(4)
	l.PushBack(5)

	m := list.Filter(&l, func(_, v int) bool { return v > 3 })
	for e := m.Front(); e != nil; e = e.Next() {
		fmt.Println(e.Value)
	}
}
Output:

4
5

func FilterFalse

func FilterFalse[T any](l *List[T], f func(index int, value T) bool) *List[T]

FilterFalse iterates over elements of collection, returning a list of all elements predicate returns false for.

func Map

func Map[T, U any](l *List[T], f func(int, T) U) *List[U]

Map

func New

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

New returns an initialized list.

func Zip2

func Zip2[T1, T2 any](l1 *List[T1], l2 *List[T2]) *List[tuple.Tuple2[T1, T2]]

Zip2 returns a list of 2-tuples. The returned list have the length of the shortest list.

Example
package main

import (
	"fmt"

	"github.com/shogo82148/hi/list"
)

func main() {
	var l1 list.List[int]
	var l2 list.List[string]
	l1.PushBack(1)
	l1.PushBack(2)
	l1.PushBack(3)
	l2.PushBack("one")
	l2.PushBack("two")
	l2.PushBack("three")

	l := list.Zip2(&l1, &l2)
	for e := l.Front(); e != nil; e = e.Next() {
		fmt.Println(e.Value)
	}
}
Output:

(1, one)
(2, two)
(3, three)

func Zip3

func Zip3[T1, T2, T3 any](l1 *List[T1], l2 *List[T2], l3 *List[T3]) *List[tuple.Tuple3[T1, T2, T3]]

Zip3 returns a list of 3-tuples. The returned list have the length of the shortest list.

func Zip4

func Zip4[T1, T2, T3, T4 any](l1 *List[T1], l2 *List[T2], l3 *List[T3], l4 *List[T4]) *List[tuple.Tuple4[T1, T2, T3, T4]]

Zip4 returns a list of 4-tuples. The returned list have the length of the shortest list.

func Zip5

func Zip5[T1, T2, T3, T4, T5 any](l1 *List[T1], l2 *List[T2], l3 *List[T3], l4 *List[T4], l5 *List[T5]) *List[tuple.Tuple5[T1, T2, T3, T4, T5]]

Zip5 returns a list of 5-tuples. The returned list have the length of the shortest list.

func Zip6

func Zip6[T1, T2, T3, T4, T5, T6 any](l1 *List[T1], l2 *List[T2], l3 *List[T3], l4 *List[T4], l5 *List[T5], l6 *List[T6]) *List[tuple.Tuple6[T1, T2, T3, T4, T5, T6]]

Zip6 returns a list of 6-tuples. The returned list have the length of the shortest list.

func Zip7

func Zip7[T1, T2, T3, T4, T5, T6, T7 any](l1 *List[T1], l2 *List[T2], l3 *List[T3], l4 *List[T4], l5 *List[T5], l6 *List[T6], l7 *List[T7]) *List[tuple.Tuple7[T1, T2, T3, T4, T5, T6, T7]]

Zip7 returns a list of 7-tuples. The returned list have the length of the shortest list.

func Zip8

func Zip8[T1, T2, T3, T4, T5, T6, T7, T8 any](l1 *List[T1], l2 *List[T2], l3 *List[T3], l4 *List[T4], l5 *List[T5], l6 *List[T6], l7 *List[T7], l8 *List[T8]) *List[tuple.Tuple8[T1, T2, T3, T4, T5, T6, T7, T8]]

Zip8 returns a list of 8-tuples. The returned list have the length of the shortest list.

func Zip9

func Zip9[T1, T2, T3, T4, T5, T6, T7, T8, T9 any](l1 *List[T1], l2 *List[T2], l3 *List[T3], l4 *List[T4], l5 *List[T5], l6 *List[T6], l7 *List[T7], l8 *List[T8], l9 *List[T9]) *List[tuple.Tuple9[T1, T2, T3, T4, T5, T6, T7, T8, T9]]

Zip9 returns a list of 9-tuples. The returned list have the length of the shortest list.

func Zip10

func Zip10[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10 any](l1 *List[T1], l2 *List[T2], l3 *List[T3], l4 *List[T4], l5 *List[T5], l6 *List[T6], l7 *List[T7], l8 *List[T8], l9 *List[T9], l10 *List[T10]) *List[tuple.Tuple10[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10]]

Zip10 returns a list of 10-tuples. The returned list have the length of the shortest list.

func Zip11

func Zip11[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11 any](l1 *List[T1], l2 *List[T2], l3 *List[T3], l4 *List[T4], l5 *List[T5], l6 *List[T6], l7 *List[T7], l8 *List[T8], l9 *List[T9], l10 *List[T10], l11 *List[T11]) *List[tuple.Tuple11[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11]]

Zip11 returns a list of 11-tuples. The returned list have the length of the shortest list.

func Zip12

func Zip12[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12 any](l1 *List[T1], l2 *List[T2], l3 *List[T3], l4 *List[T4], l5 *List[T5], l6 *List[T6], l7 *List[T7], l8 *List[T8], l9 *List[T9], l10 *List[T10], l11 *List[T11], l12 *List[T12]) *List[tuple.Tuple12[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12]]

Zip12 returns a list of 12-tuples. The returned list have the length of the shortest list.

func Zip13

func Zip13[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13 any](l1 *List[T1], l2 *List[T2], l3 *List[T3], l4 *List[T4], l5 *List[T5], l6 *List[T6], l7 *List[T7], l8 *List[T8], l9 *List[T9], l10 *List[T10], l11 *List[T11], l12 *List[T12], l13 *List[T13]) *List[tuple.Tuple13[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13]]

Zip13 returns a list of 13-tuples. The returned list have the length of the shortest list.

func Zip14

func Zip14[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14 any](l1 *List[T1], l2 *List[T2], l3 *List[T3], l4 *List[T4], l5 *List[T5], l6 *List[T6], l7 *List[T7], l8 *List[T8], l9 *List[T9], l10 *List[T10], l11 *List[T11], l12 *List[T12], l13 *List[T13], l14 *List[T14]) *List[tuple.Tuple14[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14]]

Zip14 returns a list of 14-tuples. The returned list have the length of the shortest list.

func Zip15

func Zip15[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15 any](l1 *List[T1], l2 *List[T2], l3 *List[T3], l4 *List[T4], l5 *List[T5], l6 *List[T6], l7 *List[T7], l8 *List[T8], l9 *List[T9], l10 *List[T10], l11 *List[T11], l12 *List[T12], l13 *List[T13], l14 *List[T14], l15 *List[T15]) *List[tuple.Tuple15[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15]]

Zip15 returns a list of 15-tuples. The returned list have the length of the shortest list.

func Zip16

func Zip16[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16 any](l1 *List[T1], l2 *List[T2], l3 *List[T3], l4 *List[T4], l5 *List[T5], l6 *List[T6], l7 *List[T7], l8 *List[T8], l9 *List[T9], l10 *List[T10], l11 *List[T11], l12 *List[T12], l13 *List[T13], l14 *List[T14], l15 *List[T15], l16 *List[T16]) *List[tuple.Tuple16[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16]]

Zip16 returns a list of 16-tuples. The returned list have the length of the shortest list.

func (*List[T]) Back

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

Back returns the last element of list l or nil if the list is empty.

func (*List[T]) Front

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

Front returns the first element of list l or nil if the list is empty.

func (*List[T]) Init

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

Init initializes or clears list l.

func (*List[T]) InsertAfter

func (l *List[T]) InsertAfter(v T, mark *Element[T]) *Element[T]

InsertAfter inserts a new element e with value v immediately after mark and returns e. If mark is not an element of l, the list is not modified. The mark must not be nil.

func (*List[T]) InsertBefore

func (l *List[T]) InsertBefore(v T, mark *Element[T]) *Element[T]

InsertBefore inserts a new element e with value v immediately before mark and returns e. If mark is not an element of l, the list is not modified. The mark must not be nil.

func (*List[T]) Len

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

Len returns the number of elements of list l. The complexity is O(1).

func (*List[T]) MoveAfter

func (l *List[T]) MoveAfter(e, mark *Element[T])

MoveAfter moves element e to its new position after mark. If e or mark is not an element of l, or e == mark, the list is not modified. The element and mark must not be nil.

func (*List[T]) MoveBefore

func (l *List[T]) MoveBefore(e, mark *Element[T])

MoveBefore moves element e to its new position before mark. If e or mark is not an element of l, or e == mark, the list is not modified. The element and mark must not be nil.

func (*List[T]) MoveToBack

func (l *List[T]) MoveToBack(e *Element[T])

MoveToBack moves element e to the back of list l. If e is not an element of l, the list is not modified. The element must not be nil.

func (*List[T]) MoveToFront

func (l *List[T]) MoveToFront(e *Element[T])

MoveToFront moves element e to the front of list l. If e is not an element of l, the list is not modified. The element must not be nil.

func (*List[T]) PushBack

func (l *List[T]) PushBack(v T) *Element[T]

PushBack inserts a new element e with value v at the back of list l and returns e.

func (*List[T]) PushBackList

func (l *List[T]) PushBackList(other *List[T])

PushBackList inserts a copy of another list at the back of list l. The lists l and other may be the same. They must not be nil.

func (*List[T]) PushFront

func (l *List[T]) PushFront(v T) *Element[T]

PushFront inserts a new element e with value v at the front of list l and returns e.

func (*List[T]) PushFrontList

func (l *List[T]) PushFrontList(other *List[T])

PushFrontList inserts a copy of another list at the front of list l. The lists l and other may be the same. They must not be nil.

func (*List[T]) Remove

func (l *List[T]) Remove(e *Element[T]) T

Remove removes e from l if e is an element of list l. It returns the element value e.Value. The element must not be nil.

Jump to

Keyboard shortcuts

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