dsa

package
v0.0.0-...-d67b42a Latest Latest
Warning

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

Go to latest
Published: Dec 23, 2022 License: MIT Imports: 1 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Abs

func Abs(n int) int

Get the absolute value of an integer

func GCD

func GCD(a, b int) int

Compute the Greatest Common Divisor (GCD) via Euclidean algorithm

func LCM

func LCM(a, b int, args ...int) int

Compute the Least Common Multiple using GCD recursively

func Mod

func Mod(n, m int) int

Compute the modulo as a positive integer always

Types

type Item

type Item[T any] struct {
	Value    T   // The value of the item; arbitrary.
	Priority int // The priority of the item in the queue (less is higher).
	// contains filtered or unexported fields
}

An Item is something we manage in a priority queue.

type Node

type Node[T comparable] struct {
	Value T
	Prev  *Node[T]
	Next  *Node[T]
}

A Node holds a value and forward and backward pointers

type PriorityQueue

type PriorityQueue[T any] []*Item[T]

A PriorityQueue implements heap.Interface and holds Items.

Note: please use the `container/heap` methods when pushing/popping items!

func (PriorityQueue[T]) Len

func (pq PriorityQueue[T]) Len() int

Return the size of the PriorityQueue

func (PriorityQueue[T]) Less

func (pq PriorityQueue[T]) Less(i, j int) bool

Less is used to sort the PriorityQueue

func (*PriorityQueue[T]) Pop

func (pq *PriorityQueue[T]) Pop() any

Pop the Item[T] with the lowest priority value

func (*PriorityQueue[T]) Push

func (pq *PriorityQueue[T]) Push(x any)

Add an Item[T] to the PriorityQueue

func (PriorityQueue[T]) Swap

func (pq PriorityQueue[T]) Swap(i, j int)

Swap two elements of the PriorityQueue using their indices

func (*PriorityQueue[T]) Update

func (pq *PriorityQueue[T]) Update(item *Item[T], value T, priority int)

Update modifies the priority and value of an Item[T] in the queue.

type Queue

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

A queue is internally represented with a slice

func NewQueue

func NewQueue[T any]() Queue[T]

Create a new Queue by initializing the fields

func (*Queue[T]) Add

func (queue *Queue[T]) Add(elem T)

Add a new element to the end of the Queue

func (Queue[T]) Peek

func (queue Queue[T]) Peek() T

Return the first element added to the Queue but do not remove it

func (*Queue[T]) Pop

func (queue *Queue[T]) Pop() T

Pop the first element added to the Queue

func (Queue[T]) Size

func (queue Queue[T]) Size() int

Return the size of the Queue

func (Queue[T]) Values

func (queue Queue[T]) Values() []T

Return a slice of the values of an Queue

type Ring

type Ring[T comparable] struct {
	Size int
	// contains filtered or unexported fields
}

A ring is a singly linked list with extra logic

func NewRing

func NewRing[T comparable]() Ring[T]

Create a new empty Ring

func (*Ring[T]) Append

func (ring *Ring[T]) Append(elem T)

Add a new item to the end of the Ring

func (Ring[T]) FindIndex

func (ring Ring[T]) FindIndex(elem T) int

Find the node index holding a given value

func (*Ring[T]) GetNode

func (ring *Ring[T]) GetNode(idx int) *Node[T]

Get the node at a given index of the ring

func (*Ring[T]) GetValue

func (ring *Ring[T]) GetValue(idx int) T

Get the value at a given index of the ring

func (*Ring[T]) MoveIdx

func (ring *Ring[T]) MoveIdx(idx, delta int)

Move a given index a certain number of steps forward

func (*Ring[T]) MoveNode

func (ring *Ring[T]) MoveNode(node *Node[T], delta int)

Move a given node a certain number of elements forward

func (Ring[T]) OriginalNodes

func (ring Ring[T]) OriginalNodes() []*Node[T]

Get the slice of nodes in the insertion order

func (Ring[T]) OriginalValues

func (ring Ring[T]) OriginalValues() []T

Get the list of values in the insertion order

func (Ring[T]) Values

func (ring Ring[T]) Values() []T

Get the slice of values in the current order

type Set

type Set[T comparable] struct {
	// contains filtered or unexported fields
}

A set is represented by a map of its values

func NewSet

func NewSet[T comparable]() Set[T]

Create a new Set and initialize all fields

func NewSetFrom

func NewSetFrom[T comparable](values []T) Set[T]

Create a new Set from a slice of values

func (Set[T]) Add

func (set Set[T]) Add(elem T)

Add a new element to the Set

func (Set[T]) Contains

func (set Set[T]) Contains(elem T) bool

Return true if the Set contains the element

func (Set[T]) Intersection

func (set Set[T]) Intersection(other Set[T]) Set[T]

Return the intersection of two given Sets

func (Set[T]) Remove

func (set Set[T]) Remove(elem T)

Remove an element from an Set

func (Set[T]) Size

func (set Set[T]) Size() int

Return the number of elements in the Set

func (Set[T]) Union

func (set Set[T]) Union(other Set[T]) Set[T]

Return the union of two given Sets

func (Set[T]) Values

func (set Set[T]) Values() []T

Return a slice of the values fo an Set

type Stack

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

A stack is represented internally with a slice

func NewStack

func NewStack[T any]() Stack[T]

Create a new Stack and initialize all fields

func (Stack[T]) Peek

func (stack Stack[T]) Peek() T

Return the top element of the Stack but do not remove it

func (*Stack[T]) Pop

func (stack *Stack[T]) Pop() T

Pop the last element pushed to the Stack

func (*Stack[T]) Push

func (stack *Stack[T]) Push(elem T)

Add a new element to the Stack

func (Stack[T]) Values

func (stack Stack[T]) Values() []T

Return a slice of the values of a Stack

Jump to

Keyboard shortcuts

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