ds

package
v0.5.78 Latest Latest
Warning

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

Go to latest
Published: Mar 5, 2026 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package ds contains useful data structures.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewLookup

func NewLookup[T any, K comparable, V any](items []*T, entry func(*T) (K, V)) map[K]V

Create new lookup from list of ref items, using entry function

func NewLookupPlain added in v0.5.78

func NewLookupPlain[T any, K comparable, V any](items []T, entry func(T) (K, V)) map[K]V

Create new lookup from list of items, using entry function

Types

type CodeIDLookup

type CodeIDLookup = map[string]uint // Code => ID lookup

func NewCodeIDLookup

func NewCodeIDLookup[T identifiable](items []*T) CodeIDLookup

Create new CodeIDLookup from list of ref items

func NewCodeIDLookupPlain added in v0.5.78

func NewCodeIDLookupPlain[T identifiable](items []T) CodeIDLookup

Create new CodeIDLookup from list of items

type Coords

type Coords [2]int

func (Coords) Tuple

func (c Coords) Tuple() (int, int)

Unpack the coords values

type DLLNode

type DLLNode[T any] struct {
	Value T
	Next  *DLLNode[T]
	Prev  *DLLNode[T]
}

Doubly-Linked list node

func NewDLLNode

func NewDLLNode[T any](value T) *DLLNode[T]

Create new DLL node that contains value, Prev and Next pointers initialized to self

func (*DLLNode[T]) AddAfter

func (n *DLLNode[T]) AddAfter(value T) *DLLNode[T]

Add new node which contains value after current node

func (*DLLNode[T]) AddBefore

func (n *DLLNode[T]) AddBefore(value T) *DLLNode[T]

Add new node which contains value before current node

func (*DLLNode[T]) Remove

func (n *DLLNode[T]) Remove()

Remove the DLL node, link its neighbors to fill gap

type DataRows

type DataRows struct {
	Rows string
}

Struct for lines of data rows, separated by comma

func (DataRows) GetRows

func (d DataRows) GetRows() [][]string

Get lines from rows, and split each line by comma

type Edge

type Edge [2]Vertex

func NewDirectedEdge added in v0.5.29

func NewDirectedEdge(edge string) Edge

Create new directed Edge from v1->v2 string

func NewEdge

func NewEdge(edge string) Edge

Create new Edge from v1-v2 string

func (Edge) String

func (e Edge) String() string

Edge string representation

func (Edge) Tuple

func (e Edge) Tuple() (Vertex, Vertex)

Get edge endpoints

type EdgeSet

type EdgeSet = *Set[Edge]

type Graph

type Graph struct {
	Vertices     []Vertex
	Edges        []Edge
	IndexOf      map[Vertex]int
	EdgesOf      map[Vertex][]Edge
	NeighborsOf  map[Vertex]VertexSet
	EdgeWeightOf map[Edge]float64
}

func DirectedGraphFrom added in v0.5.29

func DirectedGraphFrom(vertices, edgePairs string) *Graph

Create directed graph from vertices (separated by whitespace), and edgePairs (v1->v2 edges separated by whitespace)

func GraphFrom

func GraphFrom(vertices, edgePairs string) *Graph

Create undirected graph from vertices (separated by whitespace), and edgePairs (v1-v2 edges separated by whitespace)

func NewGraph

func NewGraph() *Graph

Create new empty graph

func (Graph) ActiveNeighbors

func (g Graph) ActiveNeighbors(vertex Vertex, activeEdges EdgeSet) []Vertex

Get list of vertex neighbors, considering the active edge set

func (*Graph) AddDirectedEdge

func (g *Graph) AddDirectedEdge(vertex1, vertex2 Vertex)

Add directed edge to graph

func (*Graph) AddUndirectedEdge

func (g *Graph) AddUndirectedEdge(vertex1, vertex2 Vertex)

Add undirected edge to graph

func (*Graph) AddVertex

func (g *Graph) AddVertex(vertex Vertex)

Add vertex to graph

func (Graph) BFSTraversal

func (g Graph) BFSTraversal(start Vertex, activeEdges EdgeSet) []Vertex

Perform BFS traversal on the graph, starting at given vertex, considering the active edge set, return list of vertices visited

func (Graph) EdgeNames added in v0.5.45

func (g Graph) EdgeNames() []string

Get edge names

func (Graph) EdgeWeight

func (g Graph) EdgeWeight(edge Edge) (float64, bool)

Get edge weight

func (Graph) Neighbors

func (g Graph) Neighbors(vertex Vertex) []Vertex

Get list of vertex neighbors

type IDCodeLookup

type IDCodeLookup = map[uint]string // ID => Code lookup

func NewIDCodeLookup

func NewIDCodeLookup[T identifiable](items []*T) IDCodeLookup

Create new IDCodeLookup from list of ref items

func NewIDCodeLookupPlain added in v0.5.78

func NewIDCodeLookupPlain[T identifiable](items []T) IDCodeLookup

Create new IDCodeLookup from list of items

type List

type List[T any] struct {
	Items []T
	Count int
}

Struct for list of items

func NewList

func NewList[T any](items []T) *List[T]

Create new List from list of items

type ListWithLookup

type ListWithLookup[T, L any] struct {
	Items  []T
	Lookup map[string]L
	Count  int
}

Struct for list of items, with associated lookup table, T = item type, L = lookup item type

func NewListWithLookup

func NewListWithLookup[T, L any](items []T) *ListWithLookup[T, L]

Create new ListWithLookup from given list of items, Initialize lookup to empty map

type LookupCode

type LookupCode[T any] = map[string]T // Code => Item lookup

func LookupCodeFromID

func LookupCodeFromID[T identifiable](idLookup LookupID[T], validCodes *Set[string]) LookupCode[T]

Create new LookupCode from given LookupID, Use nil set to skip code validation

func NewLookupCode

func NewLookupCode[T hasCode](items []*T) LookupCode[*T]

Create new LookupCode from list of ref items

func NewLookupCodePlain added in v0.5.78

func NewLookupCodePlain[T hasCode](items []T) LookupCode[T]

Create new LookupCode from list of items

type LookupID

type LookupID[T any] = map[uint]T // ID => Item lookup

func LookupIDFromCode

func LookupIDFromCode[T identifiable](codeLookup LookupCode[T], validIDs *Set[uint]) LookupID[T]

Create new LookupID from given LookupCode, Use nil set to skip id validation

func NewLookupID

func NewLookupID[T hasID](items []*T) LookupID[*T]

Create new LookupID from list of ref items

func NewLookupIDPlain added in v0.5.78

func NewLookupIDPlain[T hasID](items []T) LookupID[T]

Create new LookupID from list of items

type MapWithLookup

type MapWithLookup[K comparable, V, L any] struct {
	Items  map[K]V
	Lookup map[string]L
	Count  int
}

Struct for map of items, with associated lookup table, K, V = map key/value types, L = lookup item type

func NewMapWithLookup

func NewMapWithLookup[K comparable, V, L any](items map[K]V) *MapWithLookup[K, V, L]

Create new MapWithLookup from given map of items, Initialize lookup to empty map

type Queue

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

func NewQueue

func NewQueue[T any]() *Queue[T]

Create new empty queue

func QueueFrom

func QueueFrom[T any](items []T) *Queue[T]

Creates queue from given list of items

func (*Queue[T]) Dequeue

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

Remove item from front of queue, error if empty queue

func (*Queue[T]) Enqueue

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

Add item to end of queue

func (Queue[T]) Front

func (q Queue[T]) Front() (T, error)

Return item at front of queue without removing, error if empty queue

func (Queue[T]) IsEmpty

func (q Queue[T]) IsEmpty() bool

Checks if queue is empty

func (Queue[T]) Items

func (q Queue[T]) Items() []T

Return list of queue items

func (Queue[T]) Len

func (q Queue[T]) Len() int

Number of queue items left

func (Queue[T]) NotEmpty

func (q Queue[T]) NotEmpty() bool

Checks if queue is not empty

type Set

type Set[T comparable] struct {
	// contains filtered or unexported fields
}

func NewSet

func NewSet[T comparable]() *Set[T]

Create new empty set

func SetFrom

func SetFrom[T comparable](items []T) *Set[T]

Create set from given items

func (*Set[T]) Add

func (s *Set[T]) Add(item T)

Add item to set

func (*Set[T]) AddItems

func (s *Set[T]) AddItems(items []T)

Add items to set

func (*Set[T]) Delete

func (s *Set[T]) Delete(item T)

Delete item from set

func (Set[T]) Difference

func (s1 Set[T]) Difference(s2 *Set[T]) *Set[T]

Compute difference of two sets

func (Set[T]) Has

func (s Set[T]) Has(item T) bool

Check if set contains item

func (Set[T]) HasNo

func (s Set[T]) HasNo(item T) bool

Check if set doesn't contain item

func (Set[T]) Intersection

func (s1 Set[T]) Intersection(s2 *Set[T]) *Set[T]

Compute intersection of two sets

func (Set[T]) IsEmpty

func (s Set[T]) IsEmpty() bool

Check if set is empty

func (Set[T]) Items

func (s Set[T]) Items() []T

Return list of set items (random order)

func (Set[T]) Len

func (s Set[T]) Len() int

Number of unique set items

func (Set[T]) NotEmpty

func (s Set[T]) NotEmpty() bool

Check if set is not empty

func (Set[T]) Union

func (s1 Set[T]) Union(s2 *Set[T]) *Set[T]

Compute union of two sets

type Stack

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

func NewStack

func NewStack[T any]() *Stack[T]

Create new empty stack

func StackFrom

func StackFrom[T any](items []T) *Stack[T]

Create stack from given list of items (last item = stack top)

func (Stack[T]) IsEmpty

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

Check if stack is empty

func (Stack[T]) Items

func (s Stack[T]) Items() []T

Return list of stack items

func (Stack[T]) Len

func (s Stack[T]) Len() int

Number of stack items left

func (Stack[T]) NotEmpty

func (s Stack[T]) NotEmpty() bool

Check if stack is not empty

func (*Stack[T]) Pop

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

Remove item from top of stack, error if empty stack

func (*Stack[T]) Push

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

Push item onto stack

func (Stack[T]) Top

func (s Stack[T]) Top() (T, error)

Return item at top of stack without removing, error if empty stack

type Vertex

type Vertex = string

type VertexSet

type VertexSet = *Set[Vertex]

Jump to

Keyboard shortcuts

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