stackgo

package module
v1.1.1 Latest Latest
Warning

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

Go to latest
Published: Nov 24, 2017 License: MIT Imports: 0 Imported by: 5

README

Stackgo Build Status Coverage Status

Stackgo is a slice-based implementation of a simple stack in Go. It uses a pre-alloc pagination strategy which adds little memory overhead to the stack allocation but makes push operations faster than with a classic Stack implementation.

Please NOTE that the current implementation is NOT thread-safe.

Getting started

Import

You can either import this package directly:

import "github.com/alediaferia/stackgo"

or through gopkg.in

import "gopkg.in/alediaferia/stackgo.v1"

Currently only version 1 has been released.

Usage

Using it is pretty straightforward

package main

import (
  "github.com/alediaferia/stackgo"
  "fmt"
)

func main() {
  stack := stackgo.NewStack()

  // Stack supports any type
  // so we just push whatever
  // we want here
  stack.Push(75)
  stack.Push(124)
  stack.Push("Hello World!")

  for stack.Size() > 0 {
    fmt.Printf("Just popped %v\n", stack.Pop())
  }

}

Performance

Check the implementation details here.

Contribute

I'd really appreciate contributions, otherwise I wouldn't have made this open 😃. Also, if you have suggestions on how to make this perform even faster I'd be really happy to hear about them.

License

This code is released under the MIT License term, included in this project tree. Copyright © 2015, Alessandro Diaferia alediaferia@gmail.com

Documentation

Overview

All you need to work with stackgo stacks

For an example usage visit http://github.com/alediaferia/stackgo

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Stack

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

func NewStack

func NewStack() *Stack

NewStack Creates a new Stack object with an underlying default block allocation size. The current default allocation size is one page. If you want to use a different block size use

NewStackWithCapacity()

func NewStackWithCapacity

func NewStackWithCapacity(cap int) *Stack

NewStackWithCapacity makes it easy to specify a custom block size for inner slice backing the stack

func (*Stack) Pop

func (s *Stack) Pop() (elem interface{})

Pop pops the top element from the stack If the stack is empty it returns nil

func (*Stack) Push

func (s *Stack) Push(elem ...interface{})

Push pushes a new element to the stack

func (*Stack) Size

func (s *Stack) Size() int

The current size of the stack

func (*Stack) Top

func (s *Stack) Top() (elem interface{})

Jump to

Keyboard shortcuts

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