genericstack

package module
v1.0.5 Latest Latest
Warning

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

Go to latest
Published: Jul 8, 2024 License: CC0-1.0 Imports: 2 Imported by: 1

README

Generic stack

Generic stack data structure.

Installation

go get github.com/Matej-Chmel/go-generic-stack@v1.0.4

Example

An example illustrates the following features

  • Construction
  • Push and pop operations
  • Copy of the top
  • Changing top in-place using a pointer
  • Reading information about the stack (empty, length, capacity)
  • Converting the stack into a string using formatting options
  • Clear operation
package main

import (
	"fmt"

	gs "github.com/Matej-Chmel/go-generic-stack"
)

func main() {
	stack := gs.New[float32]()
	stack.PushItems(.15, 1.5, 3)

	fmt.Println(stack.Top()) // 3
	*stack.TopPointer() = 3.14159

	fmt.Println(stack.PopAndReturn()) // 3.14159
	stack.Pop()

	fmt.Println(stack.Empty())    // false
	fmt.Println(stack.HasItems()) // true
	fmt.Println(stack.Len())      // 1
	fmt.Println(stack.Cap())      // 4

	stack.Push(10.56)
	stack.Push(20.99)

	fmt.Println(stack) // [20.99 10.56 0.15]

	format := gs.NewFormat[float32]()
	format.Conversion = func(item *float32) string {
		return fmt.Sprintf("%.1f", *item)
	}
	format.Start = "("
	format.Sep = ", "
	format.End = ")"

	fmt.Println(stack.Format(format)) // (21.0, 10.6, 0.2)

	stack.Clear()
	fmt.Println(stack) // []
}

Documentation

Overview

Package for generic LIFO stack data structure

Index

Constants

View Source
const (
	// Default symbol to write after the last item
	DefaultEnd string = "]"
	// Default symbol to write between two items
	DefaultSep string = " "
	// Default symbol to write before the first item
	DefaultStart string = "["
	// Default direction of formatting
	DefaultTopFirst bool = true
)

Variables

This section is empty.

Functions

func DefaultConversion added in v1.0.4

func DefaultConversion[T any](val *T) string

Default conversion function converts each item to its default Go format

Types

type Format added in v1.0.4

type Format[T any] struct {
	// Function that converts each item of the stack to a string
	Conversion func(*T) string
	// Symbol to write after the last item
	End string
	// Symbol to write between two items
	Sep string
	// Symbol to write before the first item
	Start string
	// If true, items are written from top to bottom,
	// if false, the direction is reversed
	TopFirst bool
}

Formatting options for a Stack[T]

func NewFormat added in v1.0.4

func NewFormat[T any]() *Format[T]

Constructs new formatting options

type Stack

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

Generic LIFO stack, Last In, First Out

func New added in v1.0.4

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

Constructs a new stack

func (*Stack[T]) Cap

func (s *Stack[T]) Cap() int

Returns the capacity of the the underlying slic

func (*Stack[T]) Clear

func (s *Stack[T]) Clear()

Removes all items, capacity remains unchanged

func (*Stack[T]) ClearWithCap added in v1.0.4

func (s *Stack[T]) ClearWithCap(c int)

Removes all items and sets capacity to c

func (*Stack[T]) Empty

func (s *Stack[T]) Empty() bool

Returns a flag indicating whether there are no items in the stack

func (*Stack[T]) Format added in v1.0.4

func (s *Stack[T]) Format(f *Format[T]) string

Format stack into a string according to Options

func (*Stack[T]) HasItems

func (s *Stack[T]) HasItems() bool

Returns a flag indicating whether there is at least one item in the stack

func (*Stack[T]) Len

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

Returns numnber of items in the stack.

func (*Stack[T]) Pop

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

Removes the top item. Panics if the stack is empty.

func (*Stack[T]) PopAndReturn added in v1.0.4

func (s *Stack[T]) PopAndReturn() T

Removes the top item and returns a shallow copy of it. Panics if the stack is empty.

func (*Stack[T]) Push

func (s *Stack[T]) Push(item T)

Adds an item as the new top of the stack.

func (*Stack[T]) PushItems added in v1.0.4

func (s *Stack[T]) PushItems(items ...T)

Adds items on the stack in order. Last item becomes the new top.

func (*Stack[T]) PushSlice

func (s *Stack[T]) PushSlice(items []T)

Adds a slice of items on the stack in order. Last item becomes the new top.

func (Stack[T]) String

func (s Stack[T]) String() string

Returns default string representation in form [top ... ...]

func (*Stack[T]) Top

func (s *Stack[T]) Top() T

Returns a shallow copy of the top item without removing it from the stack. Panics if the stack is empty.

func (*Stack[T]) TopPointer added in v1.0.4

func (s *Stack[T]) TopPointer() *T

Returns a pointer to the top item without removing it from the stack. Panics if the stack is empty.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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