linear

package
v0.0.0-...-a6f5517 Latest Latest
Warning

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

Go to latest
Published: Dec 17, 2017 License: MIT Imports: 3 Imported by: 0

README

linear GoDoc

A collection of simple linear data structres, that are not in the standard Go lib.

Stack description

Basic stack (FILO) using the builtin linked list, can store any type, concurrency safe, no size limit, implements Stringer.

Queue description

Basic queue (FIFO) using the builtin linked list, can store any type, concurrency safe (optional mutex), no size limit, implements Stringer.

Documentation

Overview

Package linear contains a series of data structures based on lists.

Stack - O(1) FILO based on linked lists, any values interface{} Queue - O(1) FIFO based on linked lists, any values interface{}

Scenario 1: Faster, but not safe for concurrency. var listNotSafe := lists.NewStack(false) //Stack,Queue

Scenario 2: If you use goroutines create one using var listSafe := lists.NewStack(true) //Stack,Queue Most common error is "stack was empty", check Stack.Empty() or ignore it in highly-concurrent funcs. Because the state may change between the HasElement() call and Pop/Peek.

Scenario 3: Manual lock the struct, 100% reability, prune to mistakes/bugs var listNotSafe := lists.NewStack(false) //Stack,Queue listNotSafe.Lock() //do stuff with the list listNotSafe.Unlock()

For more details see the README and *_test.go

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ListCommon

type ListCommon interface {
	Len() int
	HasElement() bool
	IsEmpty() bool
}

ListCommon common methods for all list type based

type Queue

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

Queue FIFO list, uses Linked lists

func NewQueue

func NewQueue(autoMutexLock bool) (l *Queue)

NewQueue generates a new queue

func (*Queue) Dequeue

func (s *Queue) Dequeue() (item interface{}, ok bool)

Dequeue Removing (accessing) an element from the queue.

func (*Queue) Enqueue

func (s *Queue) Enqueue(item interface{}) (ok bool)

Enqueue (storing) an element on the queue.

func (*Queue) HasElement

func (s *Queue) HasElement() bool

HasElement Returns true if the list is NOT empty. Use this before Pop or Peek. Opposite of IsEmpty()

func (*Queue) IsEmpty

func (s *Queue) IsEmpty() bool

IsEmpty Returns true if the list is empty. Use this before Pop or Peek. Opposite of HasElement()

func (*Queue) Len

func (s *Queue) Len() int

Len get the current length of the list. The complexity is O(1).

func (*Queue) Peek

func (s *Queue) Peek() (item interface{}, ok bool)

Peek get the top data element of the stack, without removing it.

func (*Queue) String

func (s *Queue) String() string

String returns a string representation of the list

type Stack

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

Stack FILO list, uses Linked lists

func NewStack

func NewStack(autoMutexLock bool) (l *Stack)

NewStack generates a new stack

func (*Stack) HasElement

func (s *Stack) HasElement() bool

HasElement Returns true if the list is NOT empty. Use this before Pop or Peek. Opposite of IsEmpty()

func (*Stack) IsEmpty

func (s *Stack) IsEmpty() bool

IsEmpty Returns true if the list is empty. Use this before Pop or Peek. Opposite of HasElement()

func (*Stack) Len

func (s *Stack) Len() int

Len get the current length of the list. The complexity is O(1).

func (*Stack) Peek

func (s *Stack) Peek() (item interface{}, ok bool)

Peek get the top data element of the stack, without removing it.

func (*Stack) Pop

func (s *Stack) Pop() (item interface{}, ok bool)

Pop Removing (accessing) an element from the stack.

func (*Stack) Push

func (s *Stack) Push(item interface{}) (ok bool)

Push (storing) an element on the stack.

func (*Stack) String

func (s *Stack) String() string

String returns a string representation of the list

Jump to

Keyboard shortcuts

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