nbds

package module
v0.0.0-...-3ace436 Latest Latest
Warning

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

Go to latest
Published: Jul 11, 2022 License: MIT Imports: 2 Imported by: 0

README

NBDS - Node based data structures for Go

This repository consists of several node based data structures implemented in Go, mainly for educational / learning purposes using Go generics.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrIndexOutOfBounds = errors.New("index out of bounds")
	ErrNotFound         = errors.New("not found")
	ErrEmpty            = errors.New("no data in structure")
)

Functions

This section is empty.

Types

type List

type List[D comparable] struct {
	// contains filtered or unexported fields
}

List implements a double linked list using generic type D with should be a type that is comparable

func NewList

func NewList[D comparable]() *List[D]

NewList returns a new list of type D, which is a type that should be comparable.

Example:

// Creates a new list of type string
list := NewList[string]

// Creates a new list of type int
list := NewList[int]

func NewListFromSlice

func NewListFromSlice[D comparable](slice []D) *List[D]

NewListFromSlice returns a new list from slice. The newly created list will copy over the elements from the given slice and share its type Example:

// Creates a new list of type string
list := NewListFromSlice([]string{"a", "b", "c"})

func (*List[D]) AddHead

func (l *List[D]) AddHead(data D) *List[D]

AddHead adds a new element to head (beginning) of the List

Performance: O(1) (constant)

func (*List[D]) AddTail

func (l *List[D]) AddTail(data D) *List[D]

AddTail adds a new element to tail (end) of the List

Performance: O(1) (constant)

func (*List[D]) DeleteAt

func (l *List[D]) DeleteAt(index int) (D, error)

DeleteAt deletes the element from the list at the given index and returns the data stored for that element. If the given index is higher than the current length of the list ErrIndexOutOfBounds is returned

Performance (worst case): O(n) (linear)

func (*List[D]) Each

func (l *List[D]) Each(eachFunc func(data D))

Each invokes eachFunc on every element in the list

func (*List[D]) Filter

func (l *List[D]) Filter(filterFunc func(data D) bool) *List[D]

Filter returns a list with only the elements for which filterFunc returns true

func (*List[D]) Find

func (l *List[D]) Find(data D) (int, error)

Find returns the index for data of type D if present in List, or returns ErrNotFound if the given data could not be found in the list.

Performance (worst case): O(n) (linear)

func (*List[D]) HasMember

func (l *List[D]) HasMember(data D) bool

HasMember returns a bool indicating whether data is in the List

Performance (worst case): O(n) (linear)

func (*List[D]) InsertAt

func (l *List[D]) InsertAt(index int, data D) error

InxertAt inserts a new element to the given index. If the given index is higher than the current length of the list ErrIndexOutOfBounds is returned

Performance (worst case): O(n) (linear)

func (*List[D]) Map

func (l *List[D]) Map(mapFunc func(data D) D) *List[D]

Map applies mapFunc on every element in the list, and returns the list with the updated elements

func (*List[D]) PopHead

func (l *List[D]) PopHead() (D, error)

PopHead removes the first element of the list and returns it. If the list is empty ErrEmpty is returned

Performance: O(1) (constant)

func (*List[D]) PopTail

func (l *List[D]) PopTail() (D, error)

PopTail removes the last element of the list and returns it. If the list is empty ErrEmpty is returned

Performance: O(1) (constant)

func (*List[D]) ReadAt

func (l *List[D]) ReadAt(index int) (D, error)

ReadAt returns the data stored at the given index, or returns ErrIndexOutOfBounds when the index doesn't exist

Performance (worst case): O(n) (linear)

func (*List[D]) ToSlice

func (l *List[D]) ToSlice() []D

ToSlice returns a slice with a copy of all elements from the list

type Tree

type Tree[D constraints.Ordered] struct {
	// contains filtered or unexported fields
}

func (*Tree[D]) Inverse

func (t *Tree[D]) Inverse()

Jump to

Keyboard shortcuts

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