Documentation
¶
Overview ¶
Package collection provides thread-safe data structure implementations.
This package offers generic data structures with built-in synchronization using the common-library lock package.
Features:
- Thread-safe Queue (FIFO) with generics
- Thread-safe Deque (double-ended queue) with generics
- Automatic mutex-based synchronization
- Type-safe operations using Go generics
Example usage:
var d collection.Deque[string]
d.PushBack("hello")
back := d.Back()
d.PopBack()
Package collection provides thread-safe data structure implementations.
This package offers generic data structures with built-in synchronization using the common-library lock package.
Features:
- Thread-safe Queue (FIFO) with generics
- Thread-safe Deque (double-ended queue) with generics
- Automatic mutex-based synchronization
- Type-safe operations using Go generics
Example usage:
var q collection.Queue[int] q.Push(1) front := q.Front() q.Pop()
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Deque ¶
type Deque[T any] struct { // contains filtered or unexported fields }
Deque is struct that provides deque related methods.
func (*Deque[T]) Back ¶
func (d *Deque[T]) Back() T
Back returns the back element of the deque without removing it.
This is a thread-safe operation. The caller should ensure the deque is not empty before calling this method to avoid index out of range panics.
Returns the element at the back of the deque.
Example:
back := deque.Back()
func (*Deque[T]) Clear ¶
func (d *Deque[T]) Clear()
Clear removes all elements from the deque.
This is a thread-safe operation. After calling Clear, the deque will be empty and Size will return 0.
Example:
deque.Clear()
func (*Deque[T]) Empty ¶
Empty returns true if the deque contains no elements.
This is a thread-safe operation.
Returns true if the deque is empty, false otherwise.
Example:
if deque.Empty() {
fmt.Println("Deque is empty")
}
func (*Deque[T]) Front ¶
func (d *Deque[T]) Front() T
Front returns the front element of the deque without removing it.
This is a thread-safe operation. The caller should ensure the deque is not empty before calling this method to avoid index out of range panics.
Returns the element at the front of the deque.
Example:
front := deque.Front()
func (*Deque[T]) PopBack ¶
func (d *Deque[T]) PopBack()
PopBack removes the element at the back of the deque.
This is a thread-safe operation. If the deque is empty, this method does nothing. Elements can be removed from either end of the deque.
Example:
deque.PopBack()
func (*Deque[T]) PopFront ¶
func (d *Deque[T]) PopFront()
PopFront removes the element at the front of the deque.
This is a thread-safe operation. If the deque is empty, this method does nothing. Elements can be removed from either end of the deque.
Example:
deque.PopFront()
func (*Deque[T]) PushBack ¶
func (d *Deque[T]) PushBack(data T)
PushBack inserts an element at the back of the deque.
This is a thread-safe operation. Elements can be added to either end of the deque.
Parameters:
- data: the element to add to the back of the deque
Example:
deque.PushBack(42)
deque.PushBack("hello")
func (*Deque[T]) PushFront ¶
func (d *Deque[T]) PushFront(data T)
PushFront inserts an element at the front of the deque.
This is a thread-safe operation. Elements can be added to either end of the deque.
Parameters:
- data: the element to add to the front of the deque
Example:
deque.PushFront(42)
deque.PushFront("hello")
type Queue ¶
type Queue[T any] struct { // contains filtered or unexported fields }
Queue is struct that provides queue related methods.
func (*Queue[T]) Back ¶
func (q *Queue[T]) Back() T
Back returns the back element of the queue without removing it.
This is a thread-safe operation. The caller should ensure the queue is not empty before calling this method to avoid index out of range panics.
Returns the element at the back of the queue.
Example:
back := queue.Back()
func (*Queue[T]) Clear ¶
func (q *Queue[T]) Clear()
Clear removes all elements from the queue.
This is a thread-safe operation. After calling Clear, the queue will be empty and Size will return 0.
Example:
queue.Clear()
func (*Queue[T]) Empty ¶
Empty returns true if the queue contains no elements.
This is a thread-safe operation.
Returns true if the queue is empty, false otherwise.
Example:
if queue.Empty() {
fmt.Println("Queue is empty")
}
func (*Queue[T]) Front ¶
func (q *Queue[T]) Front() T
Front returns the front element of the queue without removing it.
This is a thread-safe operation. The caller should ensure the queue is not empty before calling this method to avoid index out of range panics.
Returns the element at the front of the queue.
Example:
front := queue.Front()
func (*Queue[T]) Pop ¶
func (q *Queue[T]) Pop()
Pop removes the element at the front of the queue.
This is a thread-safe operation. If the queue is empty, this method does nothing. Elements are removed from the front, implementing FIFO (First In First Out) behavior.
Example:
queue.Pop()
func (*Queue[T]) Push ¶
func (q *Queue[T]) Push(data T)
Push inserts an element at the back of the queue.
This is a thread-safe operation. Elements are added to the back and removed from the front, implementing FIFO (First In First Out) behavior.
Parameters:
- data: the element to add to the queue
Example:
queue.Push(42)
queue.Push("hello")