lists

package
v4.3.1 Latest Latest
Warning

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

Go to latest
Published: Oct 21, 2024 License: MIT Imports: 1 Imported by: 10

Documentation

Overview

Package lists contains a linked list implementation and common list-like data structures, such as a Queue, a Stack, and a Ring type.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Element

type Element[T any] struct {

	// The value stored with this element.
	Value T
	// contains filtered or unexported fields
}

Element is an element of a linked list.

func (*Element[T]) Next

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

Next returns the next list element or nil.

func (*Element[T]) Prev

func (l *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
}

List represents a doubly linked list. The zero value for List is an empty list ready to use.

This list is a fork of the official Go implementation, with the change of making it generic.

Example
package main

import (
	"fmt"

	"gopkg.in/typ.v4/lists"
)

func main() {
	// Create a new list and put some numbers in it.
	l := lists.New[int]()
	e4 := l.PushBack(4)
	e1 := l.PushFront(1)
	l.InsertBefore(3, e4)
	l.InsertAfter(2, e1)

	// Iterate through list and print its contents.
	for e := l.Front(); e != nil; e = e.Next() {
		fmt.Println(e.Value)
	}

}
Output:

1
2
3
4

func New

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

New returns an initialized 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.

type Queue

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

Queue is a first-in-first-out collection.

The implementation is done via a linked-list.

Example
package main

import (
	"fmt"

	"gopkg.in/typ.v4/lists"
)

func main() {
	var q lists.Queue[string]
	q.Enqueue("tripp")
	q.Enqueue("trapp")
	q.Enqueue("trull")

	fmt.Println(q.Len())     // 3
	fmt.Println(q.Dequeue()) // tripp, true
	fmt.Println(q.Dequeue()) // trapp, true
	fmt.Println(q.Dequeue()) // trull, true
	fmt.Println(q.Dequeue()) // "", false

}
Output:

3
tripp true
trapp true
trull true
 false

func (*Queue[T]) Dequeue

func (q *Queue[T]) Dequeue() (T, bool)

Dequeue removes and returns a value from the end of the queue.

func (*Queue[T]) Enqueue

func (q *Queue[T]) Enqueue(value T)

Enqueue adds a value to the start of the queue.

func (*Queue[T]) Len

func (q *Queue[T]) Len() int

Len returns the number of elements in the queue.

func (*Queue[T]) Peek

func (q *Queue[T]) Peek() (T, bool)

Peek returns (but does not remove) a value from the end of the queue.

type Ring

type Ring[T any] struct {
	Value T // for use by client; untouched by this library
	// contains filtered or unexported fields
}

A Ring is an element of a circular list, or ring. Rings do not have a beginning or end; a pointer to any ring element serves as reference to the entire ring. Empty rings are represented as nil Ring pointers. The zero value for a Ring is a one-element ring with a nil Value.

This list is a fork of the official Go implementation, with the change of making it generic.

func NewRing

func NewRing[T any](n int) *Ring[T]

NewRing creates a ring of n elements.

func (*Ring[T]) Do

func (r *Ring[T]) Do(f func(T))

Do calls function f on each element of the ring, in forward order. The behavior of Do is undefined if f changes *r.

Example
package main

import (
	"fmt"

	"gopkg.in/typ.v4/lists"
)

func main() {
	// Create a new ring of size 5
	r := lists.NewRing[int](5)

	// Get the length of the ring
	n := r.Len()

	// Initialize the ring with some integer values
	for i := 0; i < n; i++ {
		r.Value = i
		r = r.Next()
	}

	// Iterate through the ring and print its contents
	r.Do(func(p int) {
		fmt.Println(p)
	})

}
Output:

0
1
2
3
4

func (*Ring[T]) Len

func (r *Ring[T]) Len() int

Len computes the number of elements in ring r. It executes in time proportional to the number of elements.

Example
package main

import (
	"fmt"

	"gopkg.in/typ.v4/lists"
)

func main() {
	// Create a new ring of size 4
	r := lists.NewRing[int](4)

	// Print out its length
	fmt.Println(r.Len())

}
Output:

4
func (r *Ring[T]) Link(s *Ring[T]) *Ring[T]

Link connects ring r with ring s such that r.Next() becomes s and returns the original value for r.Next(). r must not be empty.

If r and s point to the same ring, linking them removes the elements between r and s from the ring. The removed elements form a subring and the result is a reference to that subring (if no elements were removed, the result is still the original value for r.Next(), and not nil).

If r and s point to different rings, linking them creates a single ring with the elements of s inserted after r. The result points to the element following the last element of s after insertion.

func (*Ring[T]) Move

func (r *Ring[T]) Move(n int) *Ring[T]

Move moves n % r.Len() elements backward (n < 0) or forward (n >= 0) in the ring and returns that ring element. r must not be empty.

Example
package main

import (
	"fmt"

	"gopkg.in/typ.v4/lists"
)

func main() {
	// Create a new ring of size 5
	r := lists.NewRing[int](5)

	// Get the length of the ring
	n := r.Len()

	// Initialize the ring with some integer values
	for i := 0; i < n; i++ {
		r.Value = i
		r = r.Next()
	}

	// Move the pointer forward by three steps
	r = r.Move(3)

	// Iterate through the ring and print its contents
	r.Do(func(p int) {
		fmt.Println(p)
	})

}
Output:

3
4
0
1
2

func (*Ring[T]) Next

func (r *Ring[T]) Next() *Ring[T]

Next returns the next ring element. r must not be empty.

Example
package main

import (
	"fmt"

	"gopkg.in/typ.v4/lists"
)

func main() {
	// Create a new ring of size 5
	r := lists.NewRing[int](5)

	// Get the length of the ring
	n := r.Len()

	// Initialize the ring with some integer values
	for i := 0; i < n; i++ {
		r.Value = i
		r = r.Next()
	}

	// Iterate through the ring and print its contents
	for j := 0; j < n; j++ {
		fmt.Println(r.Value)
		r = r.Next()
	}

}
Output:

0
1
2
3
4

func (*Ring[T]) Prev

func (r *Ring[T]) Prev() *Ring[T]

Prev returns the previous ring element. r must not be empty.

Example
package main

import (
	"fmt"

	"gopkg.in/typ.v4/lists"
)

func main() {
	// Create a new ring of size 5
	r := lists.NewRing[int](5)

	// Get the length of the ring
	n := r.Len()

	// Initialize the ring with some integer values
	for i := 0; i < n; i++ {
		r.Value = i
		r = r.Next()
	}

	// Iterate through the ring backwards and print its contents
	for j := 0; j < n; j++ {
		r = r.Prev()
		fmt.Println(r.Value)
	}

}
Output:

4
3
2
1
0
func (r *Ring[T]) Unlink(n int) *Ring[T]

Unlink removes n % r.Len() elements from the ring r, starting at r.Next(). If n % r.Len() == 0, r remains unchanged. The result is the removed subring. r must not be empty.

type Stack

type Stack[T any] []T

Stack is a first-in-last-out collection.

Example
package main

import (
	"fmt"

	"gopkg.in/typ.v4/lists"
)

func main() {
	var s lists.Stack[string]
	s.Push("tripp")
	s.Push("trapp")
	s.Push("trull")

	fmt.Println(len(s))  // 3
	fmt.Println(s.Pop()) // trull, true
	fmt.Println(s.Pop()) // trapp, true
	fmt.Println(s.Pop()) // tripp, true
	fmt.Println(s.Pop()) // "", false

}
Output:

3
trull true
trapp true
tripp true
 false

func (*Stack[T]) Peek

func (s *Stack[T]) Peek() (T, bool)

Peek returns (but does not remove) the value on top of the stack, or false if the stack is empty.

func (*Stack[T]) Pop

func (s *Stack[T]) Pop() (T, bool)

Pop removes and returns the value on top of the stack, or false if the stack is empty.

func (*Stack[T]) Push

func (s *Stack[T]) Push(value T)

Push adds a value to the top of the stack.

Jump to

Keyboard shortcuts

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