stackqueuebase

package
v0.0.6 Latest Latest
Warning

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

Go to latest
Published: Jan 21, 2024 License: MIT Imports: 2 Imported by: 0

Documentation

Overview

Package stackqueuebase implements core functionality fully generic stacks and queues.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Base

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

Base implements stack and queue functionality, given the appropriate |impl| helper.

func NewBase

func NewBase[T any](h SQ[T]) *Base[T]

NewBase creates a new Base object.

func NewBaseN

func NewBaseN[T any](size int, h SQ[T]) *Base[T]

NewBaseN creates a new Base object with |size| elements worth of preallocated memory.

func (*Base[T]) Empty

func (b *Base[T]) Empty() bool

Empty returns whether or not the stack or queue is empty.

func (*Base[T]) Join

func (b *Base[T]) Join(sep string) string

Join creates a string by combining each element from the stack or queue, with |sep| between each pair.

func (*Base[T]) Peek

func (b *Base[T]) Peek() (T, error)

Pop returns an item from the stack or queue without removing it. If the stack or queue is empty, an error is returned instead.

func (*Base[T]) PeekN

func (b *Base[T]) PeekN(n int) ([]T, error)

Pop returns N items from the stack or queue without removing them. If the stack or queue has fewer than N elements, an error is returned instead.

func (*Base[T]) Pop

func (b *Base[T]) Pop() (T, error)

Pop pops an item from the stack or queue. If the stack or queue is empty, an error is returned instead and the stack or queue remains unmodified.

func (*Base[T]) PopN

func (b *Base[T]) PopN(n int) ([]T, error)

Pop pops N items from the stack or queue. If the stack or queue has fewer than N elements, an error is returned instead and the stack or queue remains unmodified.

func (*Base[T]) Push

func (b *Base[T]) Push(v T)

Push pushes an item into the stack or queue.

func (*Base[T]) PushN

func (b *Base[T]) PushN(v ...T)

PushN pushes N items into the stack or queue.

func (*Base[T]) Size

func (b *Base[T]) Size() int

Size returns the number of elements in the stack or queue.

type Queue

type Queue[T any] struct{}

Queue implements SQ for a queue.

func (Queue[T]) NameLower

func (Queue[T]) NameLower() string

NameLower returns the name of this object ("queue").

func (Queue[T]) Nth

func (Queue[T]) Nth(impl []T, n int) T

Nth returns the Nth item from the slice.

func (Queue[T]) Rest

func (Queue[T]) Rest(impl []T) []T

Rest returns all but the first item from the slice.

type SQ

type SQ[T any] interface {
	// NameLower returns the name of this object ("stack" or "queue").
	NameLower() string

	// Nth returns the Nth item from the slice.
	Nth([]T, int) T

	// Rest returns all but the first item from the slice.
	Rest([]T) []T
}

SQ is an interface for the helper object that implements either stack-specific or queue-specific functionality.

type Stack

type Stack[T any] struct{}

Stack implements SQ for a stack.

func (Stack[T]) NameLower

func (Stack[T]) NameLower() string

NameLower returns the name of this object ("stack").

func (Stack[T]) Nth

func (Stack[T]) Nth(impl []T, n int) T

Nth returns the Nth item from the slice.

func (Stack[T]) Rest

func (Stack[T]) Rest(impl []T) []T

Rest returns all but the first item from the slice.

Jump to

Keyboard shortcuts

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