stack

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Oct 8, 2022 License: MIT Imports: 2 Imported by: 0

README

AtomicGo | stack

Downloads Latest Release Tests Coverage Unit test count Go report


Get The Module | Documentation | Contributing | Code of Conduct


AtomicGo


-----------------------------------------------------------------------------------------------------

go get atomicgo.dev/stack


-----------------------------------------------------------------------------------------------------

stack

import "atomicgo.dev/stack"

Package stack is a simple implemntation of a stack data structure. It uses generics to make it type safe.

Index

type Stack

Stack is a simple implementation of a stack data structure.

type Stack[T any] struct {
    // contains filtered or unexported fields
}
func New
func New[T any]() Stack[T]

New returns a new stack.

Example

package main

import (
	"atomicgo.dev/stack"
)

func main() {
	stack.New[string]()
}

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

Clear removes all items from the stack.

Example

package main

import (
	"fmt"

	"atomicgo.dev/stack"
)

func main() {
	s := stack.New[string]()
	s.Push("Hello")
	s.Push("World")

	s.Clear()

	fmt.Println(s)

}
Output
[]

func (*Stack[T]) Contains
func (s *Stack[T]) Contains(item T) bool

Contains returns true if the stack contains the item.

Example

package main

import (
	"fmt"

	"atomicgo.dev/stack"
)

func main() {
	s := stack.New[string]()
	s.Push("Hello")
	s.Push("World")

	fmt.Println(s.Contains("Hello"))
	fmt.Println(s.Contains("Foo"))

}
Output
true
false

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

IsEmpty returns true if the stack is empty.

Example

package main

import (
	"fmt"

	"atomicgo.dev/stack"
)

func main() {
	s := stack.New[string]()
	s.Push("Hello")
	s.Push("World")

	fmt.Println(s.IsEmpty())

	s.Clear()

	fmt.Println(s.IsEmpty())

}
Output
false
true

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

Peek returns the top item of the stack without removing it.

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

Pop removes an item from the stack and returns it. Panics if the stack is empty. Use PopSafe for safer access to the Stack.

Example

package main

import (
	"fmt"

	"atomicgo.dev/stack"
)

func main() {
	s := stack.New[string]()
	s.Push("Hello")
	s.Push("World")

	fmt.Println(s.Pop())
	fmt.Println(s.Pop())

}
Output
World
Hello

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

PopSafe removes an item from the stack and returns it. Returns the zero value of the type if the stack is empty. To make this function safe, it uses reflection and is therefore slower than Pop.

Example

package main

import (
	"fmt"

	"atomicgo.dev/stack"
)

func main() {
	s := stack.New[string]()
	s.Push("Hello")
	s.Push("World")

	fmt.Println(s.PopSafe())
	fmt.Println(s.PopSafe())
	fmt.Println(s.PopSafe())

}
Output
World
Hello

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

Push adds items to a stack.

Example

package main

import (
	"fmt"

	"atomicgo.dev/stack"
)

func main() {
	s := stack.New[string]()
	s.Push("Hello")
	s.Push("World")

	fmt.Println(s)

}
Output
[Hello World]

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

Size returns the size of the stack.

Example

package main

import (
	"fmt"

	"atomicgo.dev/stack"
)

func main() {
	s := stack.New[string]()
	s.Push("Hello")
	s.Push("World")

	fmt.Println(s.Size())

}
Output
2

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

package main

import (
	"fmt"

	"atomicgo.dev/stack"
)

func main() {
	s := stack.New[string]()
	s.Push("Hello")
	s.Push("World")

	fmt.Println(s.String())

}
Output
[Hello World]

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

Values returns the values of the stack as a slice.

Example

package main

import (
	"fmt"

	"atomicgo.dev/stack"
)

func main() {
	s := stack.New[string]()
	s.Push("Hello")
	s.Push("World")

	fmt.Println(s.Values())

}
Output
[Hello World]

Generated by gomarkdoc


AtomicGo.dev  ·  with ❤️ by @MarvinJWendt | MarvinJWendt.com

Documentation

Overview

Package stack is a simple implemntation of a stack data structure. It uses generics to make it type safe.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Stack

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

Stack is a simple implementation of a stack data structure.

func New

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

New returns a new stack.

Example
package main

import (
	"atomicgo.dev/stack"
)

func main() {
	stack.New[string]()
}
Output:

func (*Stack[T]) Clear

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

Clear removes all items from the stack.

Example
package main

import (
	"fmt"

	"atomicgo.dev/stack"
)

func main() {
	s := stack.New[string]()
	s.Push("Hello")
	s.Push("World")

	s.Clear()

	fmt.Println(s)

}
Output:

[]

func (*Stack[T]) Contains

func (s *Stack[T]) Contains(item T) bool

Contains returns true if the stack contains the item.

Example
package main

import (
	"fmt"

	"atomicgo.dev/stack"
)

func main() {
	s := stack.New[string]()
	s.Push("Hello")
	s.Push("World")

	fmt.Println(s.Contains("Hello"))
	fmt.Println(s.Contains("Foo"))

}
Output:

true
false

func (*Stack[T]) IsEmpty

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

IsEmpty returns true if the stack is empty.

Example
package main

import (
	"fmt"

	"atomicgo.dev/stack"
)

func main() {
	s := stack.New[string]()
	s.Push("Hello")
	s.Push("World")

	fmt.Println(s.IsEmpty())

	s.Clear()

	fmt.Println(s.IsEmpty())

}
Output:

false
true

func (*Stack[T]) Peek

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

Peek returns the top item of the stack without removing it.

func (*Stack[T]) Pop

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

Pop removes an item from the stack and returns it. Panics if the stack is empty. Use PopSafe for safer access to the Stack.

Example
package main

import (
	"fmt"

	"atomicgo.dev/stack"
)

func main() {
	s := stack.New[string]()
	s.Push("Hello")
	s.Push("World")

	fmt.Println(s.Pop())
	fmt.Println(s.Pop())

}
Output:

World
Hello

func (*Stack[T]) PopSafe

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

PopSafe removes an item from the stack and returns it. Returns the zero value of the type if the stack is empty. To make this function safe, it uses reflection and is therefore slower than Pop.

Example
package main

import (
	"fmt"

	"atomicgo.dev/stack"
)

func main() {
	s := stack.New[string]()
	s.Push("Hello")
	s.Push("World")

	fmt.Println(s.PopSafe())
	fmt.Println(s.PopSafe())
	fmt.Println(s.PopSafe())

}
Output:

World
Hello

func (*Stack[T]) Push

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

Push adds items to a stack.

Example
package main

import (
	"fmt"

	"atomicgo.dev/stack"
)

func main() {
	s := stack.New[string]()
	s.Push("Hello")
	s.Push("World")

	fmt.Println(s)

}
Output:

[Hello World]

func (*Stack[T]) Size

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

Size returns the size of the stack.

Example
package main

import (
	"fmt"

	"atomicgo.dev/stack"
)

func main() {
	s := stack.New[string]()
	s.Push("Hello")
	s.Push("World")

	fmt.Println(s.Size())

}
Output:

2

func (Stack[T]) String

func (s Stack[T]) String() string
Example
package main

import (
	"fmt"

	"atomicgo.dev/stack"
)

func main() {
	s := stack.New[string]()
	s.Push("Hello")
	s.Push("World")

	fmt.Println(s.String())

}
Output:

[Hello World]

func (*Stack[T]) Values

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

Values returns the values of the stack as a slice.

Example
package main

import (
	"fmt"

	"atomicgo.dev/stack"
)

func main() {
	s := stack.New[string]()
	s.Push("Hello")
	s.Push("World")

	fmt.Println(s.Values())

}
Output:

[Hello World]

Jump to

Keyboard shortcuts

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