collection

package module
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Jan 3, 2025 License: Unlicense Imports: 3 Imported by: 1

README

Collection

Collection is a Go library aim to implement some basic data structure such as List, Queue, Stack, Heap and more.

Documentation

Overview

Collection is a Go library aim to implement some basic data structure such as List, Queue, Stack, Heap and more.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ErrIndexOutOfRange

type ErrIndexOutOfRange struct {
	// contains filtered or unexported fields
}

func (*ErrIndexOutOfRange) Error

func (err *ErrIndexOutOfRange) Error() string

type ErrIsEmpty

type ErrIsEmpty struct {
	// contains filtered or unexported fields
}

func (*ErrIsEmpty) Error

func (err *ErrIsEmpty) Error() string

type ErrNotFound

type ErrNotFound struct {
	// contains filtered or unexported fields
}

func (*ErrNotFound) Error

func (err *ErrNotFound) Error() string

type List

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

List is a doubly linked list implementation. All operation on List should be thread-safe, because it only allow one goroutine at a time to access it data.

func NewList

func NewList[T any](values ...T) *List[T]

NewList creates a new doubly linked List. All operation on List should be thread-safe, because it only allow one goroutine at a time to access it data.

emptyList := New[int]()
initializedList := New(1, 2, 3, 4, 5)

func (*List[T]) All

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

All return an iterator of elements in list going from head to tail. The iterator returns the index and value of the node.

for idx, val := range list.All() {
   // code goes here
}

func (*List[T]) Append

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

Append adds new nodes to at the end of the list

func (*List[T]) Backward

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

Backward return an iterator of elements in list going from tail to head. The iterator returns the index and value of the node.

for idx, val := range list.Backward() {
   // code goes here
}

func (*List[T]) Dequeue

func (l *List[T]) Dequeue() (T, error)

Dequeue removes and returns the first element of the list. If the list is empty then return ErrIsEmpty as an error.

func (*List[T]) Index

func (l *List[T]) Index(at int) (T, error)

Index gets value at the specified index. If the index is out of range, it will return ErrIndexOutOfRange as error.

func (*List[T]) Insert

func (l *List[T]) Insert(value T, at int) error

Insert adds a new node after the node at a specified index. If the index is less than zero or greater than or equal the current length of the list, then this function will return an ErrIndexOutOfRange error. If you want to insert at the start of the list use List.Prepend instead.

func (*List[T]) Length

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

Length returns the number of node in the list.

func (*List[T]) Pop

func (l *List[T]) Pop() (T, error)

Pop removes and returns the last element of the list. If the list is empty then return ErrIsEmpty as an error.

func (*List[T]) Prepend

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

Prepend adds a new node at the start of the list.

func (*List[T]) Remove

func (l *List[T]) Remove(at int) (T, error)

Remove removes and returns the element at the specified index of the list. If the index is out of range then return ErrIndexOutOfRange as an error. Or if the list is empty then return ErrIsEmpty as an error.

func (*List[T]) Search

func (l *List[T]) Search(target T, equal func(value, target T) bool) (int, error)

Search searches for a value in the list. It takes the target to search for and the equal function. The equal function takes two arguments value and target, it should return true if the two arguments is considered to be equal.

It returns an index greater or equal to zero and a nil error if the value existed inside the list, else return the index of -1 and error of ErrNotFound.

 type user struct {
   name string
 }

 func main() {
	  user1 := user{name: "User 1"}
	  user2 := user{name: "User 2"}
	  users := NewList(user1, user2)

	  equal := func (value, target user) bool {
	    return value.name == target.name
	  }
	  idx, err := users.Search(user1, equal)
	  // code goes here
 }

Jump to

Keyboard shortcuts

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