stack

package module
v1.0.5 Latest Latest
Warning

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

Go to latest
Published: Apr 11, 2021 License: BSD-3-Clause Imports: 3 Imported by: 1

README

stack

GoDoc Go Report Card Go Coverage

Lightweight, Simple, Quick, Thread-Safe Golang Stack Implementation

Purpose

Provide a fast, thread safe, and generic Golang Stack API with minimal external linkage and maximum performance and usability.

Installation

go get -d -v github.com/lossdev/stack

Example

package main

import (
    "github.com/lossdev/stack"
    "log"
    "fmt"
)

type foo struct {
    bar string
    baz bool
}

func main() {
    // declare a new Stack 's' with int type (stack.Int)
    s := stack.NewStack(stack.Int)
    if err := s.Push(1); err != nil {
        log.Println(err)
    }
    if recv, err := stack.ToInt(s.Peek()); err != nil {
        log.Println(err)
    } else {
        fmt.Println(recv)
    }
    // Adding a member of a different type than what s is declared as will error
    if err := s.Push("Hello, World!"); err != nil {
        log.Println(err)
    }
    gs := stack.NewGenericStack()
    f := foo{"Hello, World!", true}
    gs.Push(f)
    if recv, err := gs.Peek(); err != nil {
        log.Println(err)
    } else {
        // type assertion needed
        frecv := recv.(foo)
        fmt.Printf("GenericStack: {%s, %t}\n", frecv.bar, frecv.baz)
    }
}
$ go run example.go
1
2021/04/07 13:31:52 Push(): expected: [int]; received: [string]
GenericStack: {Hello, World!, true}

Documentation

Overview

Package stack aims to provide a fast, simple, thread safe, and user friendly stack library. Stacks that are composed of primitive types (int, float64, string, bool) inherit from their parent GenericStacks, which can be of any data type. GenericStacks must be type asserted when retrieving their values, however, primitive Stacks include wrapping functions that will easily return the intended types for you. This way, completeness and user friendliness (through avoiding user-side type assertions when possible) is achieved.

Index

Constants

View Source
const (
	// Int : stack.Int const represents integer stacks
	Int = iota
	// Float : stack.Float const represents float stacks
	Float
	// String : stack.String const represents string stacks
	String
	// Bool : stack.Bool const represent boolean stacks
	Bool
)

Variables

This section is empty.

Functions

func ToBool added in v1.0.4

func ToBool(memberReturned interface{}, err error) (bool, error)

ToBool behaves equivalently to ToInt, but operates with bool types

func ToFloat added in v1.0.4

func ToFloat(memberReturned interface{}, err error) (float64, error)

ToFloat behaves equivalently to ToInt, but operates with float64 types

func ToInt added in v1.0.4

func ToInt(memberReturned interface{}, err error) (int, error)

ToInt is intended to be a wrapping function around Pop() or Peek() so that the int variable can be explicitly returned to the user. Eliminates the need for a user side type assertion

func ToString added in v1.0.4

func ToString(memberReturned interface{}, err error) (string, error)

ToString behaves equivalently to ToInt, but operates with string types

Types

type GenericStack added in v1.0.4

type GenericStack struct {
	// contains filtered or unexported fields
}

GenericStack stores the lock and data members

func NewGenericStack added in v1.0.4

func NewGenericStack() *GenericStack

NewGenericStack is the constructor method that initializes a new, empty generic stack. Generic stacks can accept any data type, however, a type assertion will have to be included in a user-side program to retrieve values from the stack

func (*GenericStack) Drain added in v1.0.4

func (s *GenericStack) Drain()

Drain removes all elements that are currently in the stack.

func (*GenericStack) Peek added in v1.0.4

func (s *GenericStack) Peek() (interface{}, error)

Peek looks at, but does not remove, the top element of the stack. It returns an error if the stack is empty, or the value of the top element if it isn't.

func (*GenericStack) Pop added in v1.0.4

func (s *GenericStack) Pop() (interface{}, error)

Pop attempts to remove a value from the top of the stack. It will return an error if the stack is empty, or the value of the top element if it isn't.

func (*GenericStack) Push added in v1.0.4

func (s *GenericStack) Push(val interface{})

Push (GenericStack method) will add a new generic value to the top of the stack.

func (*GenericStack) Size added in v1.0.4

func (s *GenericStack) Size() int

Size returns the current size of the stack. If the stack is empty, it will simply return 0, not an error

type Stack

type Stack struct {
	GenericStack
	// contains filtered or unexported fields
}

Stack inherits GenericStack's previous fields, and adds a dataType field to store what primitive data type the stack should accept

func NewStack

func NewStack(dataType int) *Stack

NewStack is the constructor method that initializes a new, empty stack of a specific type and returns it

func (*Stack) Push

func (s *Stack) Push(val interface{}) error

Push (Stack method) will add a new value to the top of the stack, and also checks for type congruency. Returns an error if an attempted Push of a new element is not the same type as the declared Stack

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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