ds

package
v2.2.3 Latest Latest
Warning

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

Go to latest
Published: Apr 22, 2025 License: MIT Imports: 1 Imported by: 0

Documentation

Overview

Package ds provides generic data structures for common use cases. This file implements a FIFO queue.

Package ds provides generic data structure implementations including stacks, queues, sets, priority queues, and linked lists. These implementations are designed to be type-safe and reusable across different data types using Go's generics.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewRingBuffer

func NewRingBuffer[T any](cap int) *ringBuffer[T]

Types

type HashSet

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

func NewSet

func NewSet[T comparable]() *HashSet[T]

func (*HashSet[T]) Add

func (s *HashSet[T]) Add(value T)

func (*HashSet[T]) Has

func (s *HashSet[T]) Has(value T) bool

func (*HashSet[T]) Items

func (s *HashSet[T]) Items() []T

func (*HashSet[T]) Len

func (s *HashSet[T]) Len() int

func (*HashSet[T]) Remove

func (s *HashSet[T]) Remove(value T)

type HeapQueue

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

func NewPriorityQueue

func NewPriorityQueue[T any]() *HeapQueue[T]

func (*HeapQueue[T]) Len

func (hq *HeapQueue[T]) Len() int

func (*HeapQueue[T]) Peek

func (hq *HeapQueue[T]) Peek() (T, bool)

func (*HeapQueue[T]) Pop

func (hq *HeapQueue[T]) Pop() (T, bool)

func (*HeapQueue[T]) Push

func (hq *HeapQueue[T]) Push(value T, priority int)

type LinkedList

type LinkedList[T any] interface {
	// Insert adds a value to the end of the list.
	Insert(value T)

	// InsertAt adds a value at the specified position.
	// Returns true if successful, false if position is invalid.
	InsertAt(value T, pos int) bool

	// DeleteAt removes the value at the specified position.
	// Returns true if successful, false if position is invalid.
	DeleteAt(pos int) bool

	// Get retrieves the value at the specified position.
	// Returns the value and true if successful, zero value and false if position is invalid.
	Get(pos int) (T, bool)

	// Len returns the number of elements in the list.
	Len() int
}

LinkedList is a generic interface for a singly linked list implementation.

type PriorityQueue

type PriorityQueue[T any] interface {
	// Push adds an item with the given priority to the queue.
	Push(item T, priority int)

	// Pop removes and returns the highest priority item.
	// Returns the value and true if successful, zero value and false if empty.
	Pop() (T, bool)

	// Peek returns the highest priority item without removing it.
	// Returns the value and true if successful, zero value and false if empty.
	Peek() (T, bool)

	// Len returns the number of elements in the queue.
	Len() int
}

PriorityQueue is a generic interface for a priority-based queue where elements are dequeued based on their priority values.

type Queue

type Queue[T any] interface {
	// Enqueue adds a new value to the end of the queue.
	Enqueue(value T)

	// Dequeue removes and returns the first value from the queue.
	// Returns the value and true if successful, zero value and false if empty.
	Dequeue() (T, bool)

	// Peek returns the first value without removing it.
	// Returns the value and true if successful, zero value and false if empty.
	Peek() (T, bool)

	// Len returns the number of elements in the queue.
	Len() int

	// IsEmpty returns true if the queue has no elements.
	IsEmpty() bool
}

Queue is a generic FIFO (First-In-First-Out) queue interface that can store elements of any type. It provides basic queue operations including enqueue and dequeue.

type RingBuffer

type RingBuffer[T any] interface {
	Push(value T)
	Pop() (T, bool)
	Peek() (T, bool)
	Len() int
	Cap() int
}

type Set

type Set[T comparable] interface {
	// Add inserts a value into the set.
	Add(value T)

	// Remove deletes a value from the set.
	Remove(value T)

	// Has checks if a value exists in the set.
	Has(value T) bool

	// Len returns the number of elements in the set.
	Len() int

	// Items return a slice containing all elements in the set.
	Items() []T
}

Set is a generic interface for a collection of unique elements.

type SinglyLinkedList

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

func NewLinkedList

func NewLinkedList[T any]() *SinglyLinkedList[T]

func (*SinglyLinkedList[T]) DeleteAt

func (l *SinglyLinkedList[T]) DeleteAt(pos int) bool

func (*SinglyLinkedList[T]) Get

func (l *SinglyLinkedList[T]) Get(pos int) (T, bool)

func (*SinglyLinkedList[T]) Insert

func (l *SinglyLinkedList[T]) Insert(value T)

func (*SinglyLinkedList[T]) InsertAt

func (l *SinglyLinkedList[T]) InsertAt(value T, pos int) bool

func (*SinglyLinkedList[T]) Len

func (l *SinglyLinkedList[T]) Len() int

type SliceQueue

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

SliceQueue is a slice-based implementation of the Queue interface.

func NewQueue

func NewQueue[T any]() *SliceQueue[T]

NewQueue returns an empty queue.

func (*SliceQueue[T]) Dequeue

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

Dequeue removes and returns the first value from the queue.

func (*SliceQueue[T]) Enqueue

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

Enqueue adds a new value to the end of the queue.

func (*SliceQueue[T]) IsEmpty

func (q *SliceQueue[T]) IsEmpty() bool

IsEmpty returns true if the queue has no elements.

func (*SliceQueue[T]) Len

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

Len returns the number of elements in the queue.

func (*SliceQueue[T]) Peek

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

Peek returns the first value without removing it.

type SliceStack

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

SliceStack is a slice-based implementation of the Stack interface. It provides an efficient, dynamic-size stack implementation.

func NewStack

func NewStack[T any]() *SliceStack[T]

NewStack creates and returns an empty stack.

func (*SliceStack[T]) IsEmpty

func (s *SliceStack[T]) IsEmpty() bool

IsEmpty returns true if the stack has no elements.

func (*SliceStack[T]) Len

func (s *SliceStack[T]) Len() int

Len returns the number of elements in the stack.

func (*SliceStack[T]) Peek

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

Peek returns the top value without removing it.

func (*SliceStack[T]) Pop

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

Pop removes and returns the top value of the stack.

func (*SliceStack[T]) Push

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

Push adds a new value to the top of the stack.

type Stack

type Stack[T any] interface {
	// Push adds a new value to the top of the stack.
	Push(value T)

	// Pop removes and returns the top value of the stack.
	// Returns the value and true if successful, zero value and false if empty.
	Pop() (T, bool)

	// Peek returns the top value without removing it.
	// Returns the value and true if successful, zero value and false if empty.
	Peek() (T, bool)

	// Len returns the number of elements in the stack.
	Len() int

	// IsEmpty returns true if the stack has no elements.
	IsEmpty() bool
}

Stack is a generic LIFO (Last-In-First-Out) stack interface that can store elements of any type. It provides basic stack operations including push, pop, and peek.

Jump to

Keyboard shortcuts

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