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 ¶
Types ¶
type HashSet ¶
type HashSet[T comparable] struct { // contains filtered or unexported fields }
func NewSet ¶
func NewSet[T comparable]() *HashSet[T]
type HeapQueue ¶
type HeapQueue[T any] struct { // contains filtered or unexported fields }
func NewPriorityQueue ¶
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 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 (*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 (*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.