datastruct

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Apr 28, 2023 License: GPL-3.0 Imports: 4 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Deque

type Deque[V any] struct {
	Elems []V
	// contains filtered or unexported fields
}

Deque is a double end queue, contains a slice of any type

func NewDeque

func NewDeque[V any](slice []V) *Deque[V]

NewDeque creates a double end queue from a slice of any type

func (*Deque[V]) IsEmpty

func (d *Deque[V]) IsEmpty() bool

IsEmpty checks if the double end queue is empty

func (*Deque[V]) PeekFirst

func (d *Deque[V]) PeekFirst() V

PeekFirst returns the queue's first element

func (*Deque[V]) PeekLast

func (d *Deque[V]) PeekLast() V

PeekLast returns the queue's last element

func (*Deque[V]) PopFirst

func (d *Deque[V]) PopFirst() V

PopFirst returns the queue's first element and remove it from the queue

func (*Deque[V]) PopLast

func (d *Deque[V]) PopLast() V

PopLast returns the queue's last element and remove it from the queue

func (*Deque[V]) PushFirst

func (d *Deque[V]) PushFirst(elem V)

PushFirst inserts a new element of any type at the begining

func (*Deque[V]) PushLast

func (d *Deque[V]) PushLast(elem V)

PushLast appends a new element of any type at the end

func (*Deque[V]) Size

func (d *Deque[V]) Size() int

Size returns the length of double end queue

type DoublyLinkedList

type DoublyLinkedList struct {
	Head, Tail *DoublyLinkedNode
	Len        int
}

Doubly Linked List

func NewDoublyLinkedList

func NewDoublyLinkedList() *DoublyLinkedList

NewDoublyLinkedList initializes a DoublyLinkedList

func (*DoublyLinkedList) Delete

func (dl *DoublyLinkedList) Delete(node *DoublyLinkedNode)

Delete removes the chosen node from the list

func (*DoublyLinkedList) Pop

Pop moves out the node from the beginning of the list

func (*DoublyLinkedList) Push

func (dl *DoublyLinkedList) Push(node *DoublyLinkedNode)

Push appendes a node at the list's end

type DoublyLinkedNode

type DoublyLinkedNode struct {
	Key, Val   interface{}
	Prev, Next *DoublyLinkedNode
}

Doubly Linked Node

func NewDoublyLinkedNode

func NewDoublyLinkedNode(k, v interface{}) *DoublyLinkedNode

NewDoublyLinkedNode initializes a DoublyLinkedNode

type Elem

type Elem[T any, O constraints.Ordered] struct {
	// contains filtered or unexported fields
}

Elem contains a value, its priority and an index for heap.Interface methods

type GenericHeap

type GenericHeap[T any, O constraints.Ordered] interface {
	heap.Interface
	GetElems() []*Elem[T, O]
}

GenericHeap contains a heap.Interface and a GetElems method MaxHeap and MinHeap implement this interface

type Heap

type Heap[T any, O constraints.Ordered] []*Elem[T, O]

Heap implements heap.Interface's methods except Less(i, j int) bool

func (Heap[T, O]) Len

func (h Heap[T, O]) Len() int

Len implements sort/heap interface

func (*Heap[T, O]) Pop

func (h *Heap[T, O]) Pop() any

Pop implements heap interface

func (*Heap[T, O]) Push

func (h *Heap[T, O]) Push(item any)

Push implements heap interface

func (Heap[T, O]) Swap

func (h Heap[T, O]) Swap(i, j int)

Swap implements sort/heap interface

type LinkedHashmap

type LinkedHashmap struct {
	Map  map[interface{}]*DoublyLinkedNode
	List *DoublyLinkedList
}

Linked Hash Map

func NewLinkedHashmap

func NewLinkedHashmap() *LinkedHashmap

NewLinkedHashmap initializes a linked hashmap

func NewLinkedHashmapFromKV

func NewLinkedHashmapFromKV(kvList [][2]interface{}) *LinkedHashmap

NewLinkedHashmapFromKV initializes a linked hashmap from a list of key-value tuples

func (*LinkedHashmap) BecomeNewest

func (lm *LinkedHashmap) BecomeNewest(k interface{})

BecomeNewest moves the choosen node to the end of the list

func (*LinkedHashmap) Contains

func (lm *LinkedHashmap) Contains(k interface{}) bool

Contains checks if the linked hashmap contains a certain value

func (*LinkedHashmap) Delete

func (lm *LinkedHashmap) Delete(k interface{})

Delete removes the node if exist.

func (*LinkedHashmap) Get

func (lm *LinkedHashmap) Get(k interface{}) interface{}

Get returns the node's value if exist.

func (*LinkedHashmap) IntoIter

func (lm *LinkedHashmap) IntoIter() <-chan *DoublyLinkedNode

IntoIter returns an iterator to read through the ordered linked map

func (*LinkedHashmap) PopEldest

func (lm *LinkedHashmap) PopEldest() *DoublyLinkedNode

PopEldest moves out the node from the beginning of the list

func (*LinkedHashmap) Put

func (lm *LinkedHashmap) Put(k, v interface{})

Put adds a node to the list and registers to the dictionary if not exist yet. Update the value of exist one.

func (*LinkedHashmap) Size

func (lm *LinkedHashmap) Size() int

Size returns the linked hashmap's size

type LinkedList

type LinkedList[V any] struct {
	Head, Tail *LinkedListNode[V]
	// contains filtered or unexported fields
}

LinkedList contains two pointers indicating the linked list's Head and Tail position

func NewLinkedList

func NewLinkedList[V any](slice []V) *LinkedList[V]

NewLinkedList creates a linked list from a slice of any type

func (*LinkedList[V]) PrintAll

func (l *LinkedList[V]) PrintAll()

PrintAll prints all elements' Value in the linked list, comma separated.

func (*LinkedList[V]) Push

func (l *LinkedList[V]) Push(v V)

Push appends a new element to the linked list's Tail

func (*LinkedList[V]) ToSlice

func (l *LinkedList[V]) ToSlice() []V

ToSlice converts the linked list back to a slice of the same element Value's type

type LinkedListNode

type LinkedListNode[V any] struct {
	Val  V
	Next *LinkedListNode[V]
}

LinkedListNode is the basic element in LinkedList contains a Value and a pointer to the Next element

type MaxHeap

type MaxHeap[T any, O constraints.Ordered] struct {
	*Heap[T, O]
}

MaxHeap embeds Heap and provides a Less method, thus implements heap.Interface it also provides a GetElems method, to satisfy the GenericHeap interface

func (MaxHeap[T, O]) GetElems

func (h MaxHeap[T, O]) GetElems() []*Elem[T, O]

GetElems returns elements store in the heap

func (MaxHeap[T, O]) Less

func (h MaxHeap[T, O]) Less(i, j int) bool

Less Implement sort/heap interface for MaxHeap

type MinHeap

type MinHeap[T any, O constraints.Ordered] struct {
	*Heap[T, O]
}

MinHeap embeds Heap and provides a Less method, thus implements heap.Interface it also provides a GetElems method, to satisfy the GenericHeap interface

func (MinHeap[T, O]) GetElems

func (h MinHeap[T, O]) GetElems() []*Elem[T, O]

GetElems returns elements store in the heap

func (MinHeap[T, O]) Less

func (h MinHeap[T, O]) Less(i, j int) bool

Less Implement sort/heap interface for MinHeap

type MonotonicQueue

type MonotonicQueue[V constraints.Ordered] struct {
	Deque *Deque[V]
	// contains filtered or unexported fields
}

Monotonic queue contains a double end queue of any type All the elements stored in the Dequeue is in descending order

func NewMonotonicQueue

func NewMonotonicQueue[V constraints.Ordered]() *MonotonicQueue[V]

NewMonotonicQueue creates an empty double end queue of any type

func (*MonotonicQueue[V]) IsEmpty

func (mq *MonotonicQueue[V]) IsEmpty() bool

IsEmpty checks if the monotonic queue is empty

func (*MonotonicQueue[V]) Max

func (mq *MonotonicQueue[V]) Max() V

Max returns the max element of the queue, which is the first element of the underlying Deque

func (*MonotonicQueue[V]) Pop

func (mq *MonotonicQueue[V]) Pop(elem V)

Pop tries to remove the given element if this element is the max of the queue if not, this action is ignored

func (*MonotonicQueue[V]) Push

func (mq *MonotonicQueue[V]) Push(elem V)

Push pops up all elements smaller than the current element from the end of the Dequeue and inserts the current element at the end of the underlying Dequeue

func (*MonotonicQueue[V]) Size

func (mq *MonotonicQueue[V]) Size() int

Size returns the length of monotonic queue

type PriorityQueue

type PriorityQueue[T any, O constraints.Ordered] struct {
	// contains filtered or unexported fields
}

PriorityQueue adds the thread safe to the GenericHeap

func NewPQ

func NewPQ[T any, O constraints.Ordered](values []T, prios []O, popLowest bool) *PriorityQueue[T, O]

NewPQ builds a priority queue with a slice of value and priority popLowest decides its a min heap or max heap

func (*PriorityQueue[T, O]) Peek

func (pq *PriorityQueue[T, O]) Peek() T

Peek checks the min/max value of the priority queue

func (*PriorityQueue[T, O]) Pop

func (pq *PriorityQueue[T, O]) Pop() T

Pop returns the min/max value of the priority queue

func (*PriorityQueue[T, O]) PopWithPriority

func (pq *PriorityQueue[T, O]) PopWithPriority() (T, O)

PopWithPriority returns the min/max value of the priority queue along with its priority

func (*PriorityQueue[T, O]) Push

func (pq *PriorityQueue[T, O]) Push(value T, prio O)

Push adds a element to the queue

func (*PriorityQueue[T, O]) Size

func (pq *PriorityQueue[T, O]) Size() int

Size returns the number of elements in the priority queue

type Queue

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

Queue contains a slice of any type

func NewQueue

func NewQueue[V any](slice []V) *Queue[V]

NewQueue creates a queue from a slice of any type

func (*Queue[V]) IsEmpty

func (q *Queue[V]) IsEmpty() bool

IsEmpty checks if the queue is empty

func (*Queue[V]) Peek

func (q *Queue[V]) Peek() V

Peek returns the queue's head

func (*Queue[V]) Pop

func (q *Queue[V]) Pop() V

Pop returns the queue's head and remove it from the queue

func (*Queue[V]) Push

func (q *Queue[V]) Push(elem V)

Push appends a new element of any type to the end

func (*Queue[V]) Size

func (q *Queue[V]) Size() int

Size returns the length of queue

type Stack

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

func NewStack

func NewStack[V any](slice []V) *Stack[V]

func (*Stack[V]) IsEmpty

func (s *Stack[V]) IsEmpty() bool

func (*Stack[V]) Peek

func (s *Stack[V]) Peek() V

func (*Stack[V]) Pop

func (s *Stack[V]) Pop() V

func (*Stack[V]) Push

func (s *Stack[V]) Push(elem V)

func (*Stack[V]) Size

func (s *Stack[V]) Size() int

Jump to

Keyboard shortcuts

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