collection

package module
v0.5.1 Latest Latest
Warning

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

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

README

Collection

Go Reference codecov CI

Collection is a Go library that aims to implement basic data structures such as List, Queue, Stack, Heap, and more.

Data Structures

  • Linked list is implemented as a doubly linked list.
  • Stack is implemented by using linked list as the base.
  • Queue is implemented by using linked list as the base.
  • Heap is implemented by using slice as the base.

Caches

  • LRU implemeted cache with LRU eviction policy.
  • MRU implemeted cache with MRU eviction policy.

Documentation

Overview

Collection is a Go library that aims to implement basic data structures such as List, Queue, Stack, Heap, and more.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrIsEmpty         error = fmt.Errorf("is empty")
	ErrNotFound        error = fmt.Errorf("not found")
	ErrIndexOutOfRange error = fmt.Errorf("index is out of range")
)

Functions

func GreaterThan added in v0.4.0

func GreaterThan[T Orderable](current, other T) bool

Greater than, can be use as cmp function of Heap to create a max heap.

func GreaterThanOrEqual added in v0.4.0

func GreaterThanOrEqual[T Orderable](current, other T) bool

Greater than or equal, can be use as cmp function of Heap to create a max heap.

func LessThan added in v0.4.0

func LessThan[T Orderable](current, other T) bool

Less than, can be use as cmp function of Heap to create a min heap.

func LessThanOrEqual added in v0.4.0

func LessThanOrEqual[T Orderable](current, other T) bool

Less than or equal, can be use as cmp function of Heap to create a min heap.

func Must added in v0.4.0

func Must[T any](f func() (T, error)) T

Ensure f must return a nil error else this will panic.

Types

type Heap added in v0.4.0

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

A Heap implemented using slice, a dynamic array implementation, as the base. Heap is thread-safe, because it only allow one goroutine at a time to access it data.

func MustNewHeap added in v0.4.0

func MustNewHeap[T any](cmp func(current, other T) bool) *Heap[T]

Like NewHeap but will panic if cmp is nil.

func NewHeap added in v0.4.0

func NewHeap[T any](cmp func(current, other T) bool) (*Heap[T], error)

NewHeap creates a new Heap. It takes a function that compares two values. During swim/heapify-up and sink/heapify-down operation if the function returns true, then the nodes will be swapped with eachother. This will return an error if cmp is nil, if you want to panic instead use MustNewHeap.

For example a max Heap[int] would need:

func maxHeapCmp(current, other int) {
	return current >= other
}

A min Heap[int] would need:

func mimHeapCmp(current, other int) {
	return current <= other
}

func (*Heap[T]) IsEmpty added in v0.4.0

func (h *Heap[T]) IsEmpty() bool

IsEmpty returns true if the heap does not hold any value.

func (*Heap[T]) Pop added in v0.4.0

func (h *Heap[T]) Pop() (T, error)

Get a value at the root node, and remove it from the Heap. Returns ErrIsEmpty if the Heap is empty.

func (*Heap[T]) Push added in v0.4.0

func (h *Heap[T]) Push(values ...T)

Push values into the Heap.

func (*Heap[T]) PushPop added in v0.4.0

func (h *Heap[T]) PushPop(value T) (T, error)

Push a value into the heap and then pop the root node. This function is equivalent to call a Heap.Push followed by a Heap.Pop, but have a more efficient implementation. Returns ErrIsEmpty if the Heap is empty.

func (*Heap[T]) Top added in v0.4.0

func (h *Heap[T]) Top() (T, error)

Peek at the value at the root node without removing it from the Heap. Returns ErrIsEmpty if the Heap is empty.

type List

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

List is a doubly linked list implementation. All operation on List is 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]) Head added in v0.5.0

func (l *List[T]) Head() *internal.Node[T]

Return the head node of the List.

BUG(trviph): This function is needed by the cache package, but is leaks internal.Node to the users. We could either find a way to remove this or could just ignore this.

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, after 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
 }

type Orderable added in v0.4.0

type Orderable interface {
	int | int8 | int16 | int32 | int64 |
		uint | uint8 | uint16 | uint32 | uint64 |
		float32 | float64 | string
}

Orderable are types that support the following operations

  • Equal
  • Not equal
  • Less than
  • Less than or equal
  • Greater than
  • Greater than or equal

type Queue added in v0.3.0

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

A first-in-first-out Queue implemented by using List as the base. Since List is thread-safe, Queue should also be thread-safe.

func NewQueue added in v0.3.0

func NewQueue[T any](values ...T) *Queue[T]

NewQueue creates a new Queue of type T.

func (*Queue[T]) Dequeue added in v0.3.0

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

Dequeue get the value from the front of the queue, and remove it from the queue. If the queue is empty return ErrIsEmpty as an error.

func (*Queue[T]) Front added in v0.3.0

func (q *Queue[T]) Front() (T, error)

Front get the value from the front but does not remove it from the queue. If the queue is empty return ErrIsEmpty as an error.

func (*Queue[T]) Length added in v0.3.0

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

Length returns the number of values current in the queue.

func (*Queue[T]) Push added in v0.3.0

func (q *Queue[T]) Push(values ...T)

Push a list of values in to the queue, starting from left to right.

func (*Queue[T]) Rear added in v0.3.0

func (q *Queue[T]) Rear() (T, error)

Rear get the value from the rear/end but does not remove it from the queue. If the queue is empty return ErrIsEmpty as an error.

type Stack added in v0.2.0

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

A first-in-last-out Stack implemented by using List as the base. Since List is thread-safe, Stack should also be thread-safe.

func NewStack added in v0.2.0

func NewStack[T any](values ...T) *Stack[T]

NewStack creates a new Stack of type T.

func (*Stack[T]) Length added in v0.2.0

func (s *Stack[T]) Length() int

Length returns the number of values current in the stack.

func (*Stack[T]) Pop added in v0.2.0

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

Pop get the value of the last push, and remove the value from the stack. If the stack is empty return ErrIsEmpty as an error.

func (*Stack[T]) Push added in v0.2.0

func (s *Stack[T]) Push(values ...T)

Push a list of values in to the stack, starting from left to right.

func (*Stack[T]) Top added in v0.2.0

func (s *Stack[T]) Top() (T, error)

Top get the value of the last push but does not remove the value from the stack. If the stack is empty return ErrIsEmpty as an error.

Notes

Bugs

  • This function is needed by the cache package, but is leaks internal.Node to the users. We could either find a way to remove this or could just ignore this.

Directories

Path Synopsis
Package cache implemented some basic in memory caching strategies.
Package cache implemented some basic in memory caching strategies.

Jump to

Keyboard shortcuts

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