...

Package stack

import "simonwaldherr.de/go/golibs/stack"
Overview
Index
Examples

Overview ▾

stack implements a stack with "last in, first out" functionality. it also provides a ring memory type which overrides itself after n write ops.

type Rings

struct Rings contains nodes as slice of strings, count for the current ring position, xcount for the total amount of added entries and size for the maximum size of the "Ring".

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

Example

Code:

ring := stack.Ring()
ring.Init(4)

for i := 0; i < 16; i++ {
    ring.Push(fmt.Sprintf("%v", i))
}

vals := ring.Get(2)
for _, v := range vals {
    fmt.Println(v)
}

Output:

12
13
14

func Ring

func Ring() *Rings

Ring returns a pointer to a new ring.

func (*Rings) Get

func (r *Rings) Get(from int) []string

Get returns a slice of strings from the given to the current position

func (*Rings) GetSize

func (r *Rings) GetSize() int

GetSize returns the max size of the Ring.

func (*Rings) Init

func (r *Rings) Init(i int)

Init sets the default values of the Ring.

func (*Rings) Pos

func (r *Rings) Pos() (int, int)

Pos returns the current position and the number of overall added values

func (*Rings) Push

func (r *Rings) Push(n string) int

Push adds a string to the Ring and returns it position

func (*Rings) SetSize

func (r *Rings) SetSize(i int)

SetSize sets the maximum size of the Ring, this size must be greater then the current counter.

type Stack

struct Stack contains nodes as slice of interfaces and a counter for the current position.

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

Example

Code:

array := stack.Lifo()
array.Push("1")
array.Push(float64(13.37))
array.Push(false)
array.Push("Lorem Ipsum Dolor sit Amet")

for array.Len() > 0 {
    fmt.Println(as.String(array.Pop()))
}

Output:

Lorem Ipsum Dolor sit Amet
false
13.37
1

func Fifo

func Fifo() *Stack

func Lifo

func Lifo() *Stack

Lifo returns a pointer to a new stack.

func (*Stack) Add

func (s *Stack) Add(n interface{})

func (*Stack) Get

func (s *Stack) Get() interface{}

func (*Stack) IsEmpty

func (s *Stack) IsEmpty() bool

func (*Stack) Len

func (s *Stack) Len() int

Len returns the current position in the Stack.

func (*Stack) Pop

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

Pop returns the last added value and decrease the position counter.

func (*Stack) Push

func (s *Stack) Push(n interface{})

Push adds a value to the Stack

func (*Stack) ToFifo

func (s *Stack) ToFifo() *Stack

func (*Stack) ToLifo

func (s *Stack) ToLifo() *Stack

func (*Stack) Unset

func (s *Stack) Unset()

func (*Stack) Val

func (s *Stack) Val() []interface{}

type Stype

type Stype int
const (
    NIL Stype = iota
    LiFo
    FiFo
)