golist

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Apr 3, 2022 License: MIT Imports: 4 Imported by: 2

README

Go List

A library for implementations of basic data structures in Golang.

Overview

Implementations include queue, stack and list. All implementations are thread-safe and use mutexes to handle a single process at a time.

A queue provides the following methods in its interface:

  • Enqueue: adds an element into the queue in FIFO structure (type-safe).
  • Dequeue: removes the first element that was enqueued in a FIFO structure (type-safe)
  • Peek: returns the element at the front of the queue, returning a pointer to the element and a boolean value to indicate if the queue is empty.
  • Length: returns the length of the queue as an integer.
  • UnmarshallJSON: overrides default unmarshal behaviour to allow unmarshalling array to queue
  • MarshalJSON: overrides default marshall behaviour to marshall into json array

A list provides the following methods in its interface:

  • Append: adds an element into the list in order of entry (type-safe).
  • RemoveAt: removes an element at a specified index, takes integer as an argument, returns data type of error.
  • Map: creates a map out of the list.
  • Filter: filters from a list using a bool function that satisfies a particular condition.
  • UnmarshallJSON: overrides default unmarshal behaviour to allow unmarshalling array to list
  • MarshalJSON: overrides default marshall behaviour to marshall into json array

A stack provides the following methods in its interface:

  • Push: adds an element into the stack in LIFO structure (type-safe).
  • Pop: removes an element from top of the stack in LIFO structure (type-safe).
  • Peek: returns the element at the top of the stack in LIFO structure, returning a pointer to the element and a boolean value to indicate if stack is empty.
  • Length: returns the length of the stack as an integer.
  • UnmarshallJSON: overrides default unmarshal behaviour to allow unmarshalling array to stack
  • MarshalJSON: overrides default marshall behaviour to marshall into json array

An error type provides certain error messages for indexes going out of bound, parsing and for internal errors.

Example Code

Import this library using the github URL in your import statements

  • Stacks
var stack implementations.Stack[string]
stack.Push("unicorn")
stack.Push("donkey")
stack.Push("bird")
stack.Push("horse")
stack.Pop() //LIFO
val2, ok2 := stack.Peek()
if !ok2 {
    fmt.Println("Stack is empty")
} else {
    fmt.Println("Value at top of stack is", *val2)
}
fmt.Println("Length of stack is", stack.Length())
  • Lists
var items implementations.List[int]
items.Append(8)
items.Append(33953)
items.Append(35)
x := items[0]
fmt.Println("Item at index 0 is ", x)
var evens = items.Filter(func(val int) bool {
    return val%2 == 0
})
fmt.Printf("Values that are even: %v\n", *evens)
err := items.RemoveAt(3)
if err != nil {
	fmt.Println("Error: tried to remove at index out of bounds. Operation failed.")
}
fmt.Println("Length of list is", items.Length())
  • Queues
var queue implementations.Queue[int]
queue.Enqueue(1)
queue.Enqueue(2)
queue.Enqueue(3)
queue.Dequeue() //FIFO
val, ok := queue.Peek()
if !ok {
    fmt.Println("Queue is empty")
} else {
	fmt.Println("Value at front of queue is", *val)
}
fmt.Println("Length of queue is", queue.Length())

Authors

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type List

type List[T any] []T

TypeDef List to array

func (*List[T]) Append added in v0.2.0

func (l *List[T]) Append(entry T) interfaces.IList[T]

func (*List[T]) Filter added in v0.2.0

func (l *List[T]) Filter(ex func(el T) bool) interfaces.IList[T]

func (*List[T]) Length added in v0.2.0

func (l *List[T]) Length() int

func (*List[T]) Map added in v0.2.0

func (l *List[T]) Map(ex func(T) any) interfaces.IList[any]

func (*List[T]) MarshalJSON added in v0.2.0

func (l *List[T]) MarshalJSON() ([]byte, error)

func (*List[T]) RemoveAt added in v0.2.0

func (l *List[T]) RemoveAt(index int) error

func (*List[T]) UnmarshalJSON added in v0.2.0

func (l *List[T]) UnmarshalJSON(data []byte) error

type Queue

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

func (*Queue[T]) Dequeue added in v0.2.0

func (q *Queue[T]) Dequeue() (*T, bool)

func (*Queue[T]) Enqueue added in v0.2.0

func (q *Queue[T]) Enqueue(entry T)

func (*Queue[T]) Length added in v0.2.0

func (q *Queue[T]) Length() int

func (*Queue[T]) MarshalJSON added in v0.2.0

func (q *Queue[T]) MarshalJSON() ([]byte, error)

func (*Queue[T]) Peek added in v0.2.0

func (q *Queue[T]) Peek() (*T, bool)

func (*Queue[T]) UnmarshalJSON added in v0.2.0

func (q *Queue[T]) UnmarshalJSON(data []byte) error

type Stack

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

func (*Stack[T]) Length added in v0.2.0

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

func (*Stack[T]) MarshalJSON added in v0.2.0

func (s *Stack[T]) MarshalJSON() ([]byte, error)

func (*Stack[T]) Peek added in v0.2.0

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

func (*Stack[T]) Pop added in v0.2.0

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

func (*Stack[T]) Push added in v0.2.0

func (s *Stack[T]) Push(entry T)

func (*Stack[T]) UnmarshalJSON added in v0.2.0

func (s *Stack[T]) UnmarshalJSON(data []byte) error

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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