stack

package
v0.11.0 Latest Latest
Warning

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

Go to latest
Published: Jun 13, 2022 License: MIT Imports: 0 Imported by: 0

Documentation

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.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Rings

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

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".

Example
package main

import (
	"fmt"
	"simonwaldherr.de/go/golibs/stack"
)

func main() {
	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

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

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

Example
package main

import (
	"fmt"
	"simonwaldherr.de/go/golibs/as"
	"simonwaldherr.de/go/golibs/stack"
)

func main() {
	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
)

Jump to

Keyboard shortcuts

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