Documentation ¶
Overview ¶
Package dagger is a collection of generic, concurrency safe datastructures including a Directed Acyclic Graph and others. Datastructures are implemented using generics in Go 1.18.
Supported Datastructures:
DAG: thread safe directed acyclic graph
Queue: unbounded thread safe fifo queue
Stack: unbounded thread safe lifo stack
BoundedQueue: bounded thread safe fifo queue with a fixed capacity
PriorityQueue: thread safe priority queue
HashMap: thread safe hashmap
Set: thread safe set
ChannelGroup: thread safe group of channels for broadcasting 1 value to N channels
MultiContext: thread safe context for coordinating the cancellation of multiple contexts
Borrower: thread safe object ownership manager
Index ¶
- func UniqueID(prefix string) string
- type BoundedQueue
- func (q *BoundedQueue[T]) Close()
- func (q *BoundedQueue[T]) Len() int
- func (q *BoundedQueue[T]) Pop() (T, bool)
- func (q *BoundedQueue[T]) PopContext(ctx context.Context) (T, bool)
- func (q *BoundedQueue[T]) Push(val T) bool
- func (q *BoundedQueue[T]) PushContext(ctx context.Context, val T) bool
- func (q *BoundedQueue[T]) Range(fn func(element T) bool)
- func (q *BoundedQueue[T]) RangeContext(ctx context.Context, fn func(element T) bool)
- type DAG
- func (g *DAG[T]) Acyclic() bool
- func (g *DAG[T]) BFS(ctx context.Context, reverse bool, start *GraphNode[T], ...) error
- func (g *DAG[T]) DFS(ctx context.Context, reverse bool, start *GraphNode[T], fn GraphSearchFunc[T]) error
- func (g *DAG[T]) GetEdge(id string) (*GraphEdge[T], bool)
- func (g *DAG[T]) GetEdges() []*GraphEdge[T]
- func (g *DAG[T]) GetNode(id string) (*GraphNode[T], bool)
- func (g *DAG[T]) GetNodes() []*GraphNode[T]
- func (g *DAG[T]) GraphViz() (image.Image, error)
- func (g *DAG[T]) HasEdge(id string) bool
- func (g *DAG[T]) HasNode(id string) bool
- func (g *DAG[T]) RangeEdges(fn func(e *GraphEdge[T]) bool)
- func (g *DAG[T]) RangeNodes(fn func(n *GraphNode[T]) bool)
- func (g *DAG[T]) SetNode(node Node) *GraphNode[T]
- func (g *DAG[T]) Size() (int, int)
- func (g *DAG[T]) TopologicalSort(reverse bool) ([]*GraphNode[T], error)
- type DagOpt
- type GraphEdge
- type GraphNode
- func (n *GraphNode[T]) Ancestors(fn func(node *GraphNode[T]) bool)
- func (n *GraphNode[T]) BFS(ctx context.Context, reverse bool, fn GraphSearchFunc[T]) error
- func (n *GraphNode[T]) DFS(ctx context.Context, reverse bool, fn GraphSearchFunc[T]) error
- func (n *GraphNode[T]) Descendants(fn func(node *GraphNode[T]) bool)
- func (n *GraphNode[T]) EdgesFrom(relationship string, fn func(e *GraphEdge[T]) bool)
- func (n *GraphNode[T]) EdgesTo(relationship string, fn func(e *GraphEdge[T]) bool)
- func (n *GraphNode[T]) Graph() *DAG[T]
- func (n *GraphNode[T]) IsConnectedTo(node *GraphNode[T]) bool
- func (n *GraphNode[T]) Remove() error
- func (n *GraphNode[T]) RemoveEdge(edgeID string)
- func (n *GraphNode[T]) SetEdge(relationship string, toNode Node, metadata map[string]string) (*GraphEdge[T], error)
- type GraphSearchFunc
- type HashMap
- func (n *HashMap[K, V]) Clear()
- func (n *HashMap[K, V]) Delete(key K)
- func (n *HashMap[K, V]) Exists(key K) bool
- func (n *HashMap[K, V]) Filter(f func(key K, value V) bool) *HashMap[K, V]
- func (n *HashMap[K, V]) Get(key K) (V, bool)
- func (n *HashMap[K, V]) Keys() []K
- func (n *HashMap[K, V]) Len() int
- func (n *HashMap[K, V]) Map() map[K]V
- func (n *HashMap[K, V]) Range(f func(key K, value V) bool)
- func (n *HashMap[K, V]) Set(key K, value V)
- func (n *HashMap[K, V]) Values() []V
- type Node
- type PriorityQueue
- type Queue
- type Set
- type Stack
- func (s *Stack[T]) Clear()
- func (s *Stack[T]) Len() int
- func (s *Stack[T]) Peek() (T, bool)
- func (s *Stack[T]) Pop() (T, bool)
- func (s *Stack[T]) Push(f T)
- func (s *Stack[T]) Range(fn func(element T) bool)
- func (s *Stack[T]) RangeUntil(fn func(element T) bool, done chan struct{})
- func (s *Stack[T]) Sort(lessFunc func(i T, j T) bool) []T
- func (s *Stack[T]) Values() []T
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type BoundedQueue ¶
type BoundedQueue[T any] struct { // contains filtered or unexported fields }
BoundedQueue is a basic FIFO BoundedQueue based on a buffered channel
func NewBoundedQueue ¶
func NewBoundedQueue[T any](maxSize int) *BoundedQueue[T]
NewBoundedQueue returns a new BoundedQueue with the given max size. When the max size is reached, the queue will block until a value is removed. If maxSize is 0, the queue will always block until a value is removed. The BoundedQueue is concurrent-safe.
func (*BoundedQueue[T]) Close ¶
func (q *BoundedQueue[T]) Close()
Close closes the BoundedQueue channel.
func (*BoundedQueue[T]) Len ¶
func (q *BoundedQueue[T]) Len() int
Len returns the number of elements in the BoundedQueue.
func (*BoundedQueue[T]) Pop ¶
func (q *BoundedQueue[T]) Pop() (T, bool)
Pop removes and returns an element from the beginning of the BoundedQueue.
func (*BoundedQueue[T]) PopContext ¶ added in v3.1.6
func (q *BoundedQueue[T]) PopContext(ctx context.Context) (T, bool)
PopContext removes and returns an element from the beginning of the BoundedQueue. If no element is available, it will block until an element is available or the context is cancelled.
func (*BoundedQueue[T]) Push ¶
func (q *BoundedQueue[T]) Push(val T) bool
Push adds an element to the end of the BoundedQueue and returns a channel that will block until the element is added. If the queue is full, it will block until an element is removed.
func (*BoundedQueue[T]) PushContext ¶ added in v3.1.6
func (q *BoundedQueue[T]) PushContext(ctx context.Context, val T) bool
PushContext adds an element to the end of the BoundedQueue and returns a channel that will block until the element is added. If the queue is full, it will block until an element is removed or the context is cancelled.
func (*BoundedQueue[T]) Range ¶
func (q *BoundedQueue[T]) Range(fn func(element T) bool)
Range executes a provided function once for each BoundedQueue element until it returns false.
func (*BoundedQueue[T]) RangeContext ¶ added in v3.1.6
func (q *BoundedQueue[T]) RangeContext(ctx context.Context, fn func(element T) bool)
RangeContext executes a provided function once for each BoundedQueue element until it returns false or a value is sent to the done channel. Use this function when you want to continuously process items from the queue until a done signal is received.
type DAG ¶
type DAG[T Node] struct { // contains filtered or unexported fields }
DAG is a concurrency safe, mutable, in-memory directed graph
func (*DAG[T]) BFS ¶
func (g *DAG[T]) BFS(ctx context.Context, reverse bool, start *GraphNode[T], search GraphSearchFunc[T]) error
BFS executes a depth first search on the graph starting from the current node. The reverse parameter determines whether the search is reversed or not. The fn parameter is a function that is called on each node in the graph. If the function returns false, the search is stopped.
func (*DAG[T]) DFS ¶
func (g *DAG[T]) DFS(ctx context.Context, reverse bool, start *GraphNode[T], fn GraphSearchFunc[T]) error
DFS executes a depth first search on the graph starting from the current node. The reverse parameter determines whether the search is reversed or not. The fn parameter is a function that is called on each node in the graph. If the function returns false, the search is stopped.
func (*DAG[T]) RangeEdges ¶
RangeEdges iterates over all edges in the graph
func (*DAG[T]) RangeNodes ¶
RangeNodes iterates over all nodes in the graph
func (*DAG[T]) SetNode ¶
SetNode sets a node in the graph - it will use the node's ID as the key and overwrite any existing node with the same ID
type DagOpt ¶
type DagOpt func(*dagOpts)
DagOpt is an option for configuring a DAG
func WithVizualization ¶
func WithVizualization() DagOpt
WithVizualization enables graphviz visualization on the DAG
type GraphEdge ¶
type GraphEdge[T Node] struct { // contains filtered or unexported fields }
GraphEdge is a relationship between two nodes
func (*GraphEdge[T]) Relationship ¶
Relationship returns the relationship between the two nodes
func (*GraphEdge[T]) SetMetadata ¶ added in v3.1.4
SetMetadata sets the metadata of the node
type GraphNode ¶
GraphNode is a node in the graph. It can be connected to other nodes via edges.
func (*GraphNode[T]) BFS ¶
BFS performs a breadth-first search on the graph starting from the current node
func (*GraphNode[T]) DFS ¶
DFS performs a depth-first search on the graph starting from the current node
func (*GraphNode[T]) Descendants ¶
Descendants returns the descendants of the current node
func (*GraphNode[T]) EdgesFrom ¶
EdgesFrom iterates over the edges from the current node to other nodes with the given relationship. If the relationship is empty, all relationships will be iterated over.
func (*GraphNode[T]) EdgesTo ¶
EdgesTo iterates over the edges from other nodes to the current node with the given relationship. If the relationship is empty, all relationships will be iterated over.
func (*GraphNode[T]) IsConnectedTo ¶
IsConnectedTo returns true if the current node is connected to the given node in any direction
func (*GraphNode[T]) RemoveEdge ¶
RemoveEdge removes an edge from the current node by edgeID
func (*GraphNode[T]) SetEdge ¶
func (n *GraphNode[T]) SetEdge(relationship string, toNode Node, metadata map[string]string) (*GraphEdge[T], error)
SetEdge sets an edge from the current node to the node with the given nodeID. If the nodeID does not exist, an error is returned. If the edgeID is empty, a unique id will be generated. If the metadata is nil, an empty map will be used.
type GraphSearchFunc ¶
type GraphSearchFunc[T Node] func(ctx context.Context, relationship string, node *GraphNode[T]) bool
GraphSearchFunc is a function that is called on each node in the graph during a search
type HashMap ¶
type HashMap[K comparable, V any] struct { // contains filtered or unexported fields }
HashMap is a thread safe map
func NewHashMap ¶
func NewHashMap[K comparable, V any]() *HashMap[K, V]
NewHashMap creates a new generic hash map
func (*HashMap[K, V]) Delete ¶
func (n *HashMap[K, V]) Delete(key K)
Delete deletes the key from the map
func (*HashMap[K, V]) Filter ¶ added in v3.1.2
Filter returns a new hashmap with the values that return true from the function
func (*HashMap[K, V]) Keys ¶
func (n *HashMap[K, V]) Keys() []K
Keys returns a copy of the keys in the map as a slice
func (*HashMap[K, V]) Map ¶ added in v3.1.2
func (n *HashMap[K, V]) Map() map[K]V
Map returns a copy of the hashmap as a map[string]T
type Node ¶ added in v3.1.5
type Node interface { // ID returns the unique identifier of the node ID() string // Metadata returns the metadata of the node Metadata() map[string]string // SetMetadata sets the metadata of the node SetMetadata(metadata map[string]string) }
Node is a node in the graph. It can be connected to other nodes via edges.
type PriorityQueue ¶
type PriorityQueue[T any] struct { // contains filtered or unexported fields }
PriorityQueue is a thread safe priority queue
func NewPriorityQueue ¶
func NewPriorityQueue[T any]() *PriorityQueue[T]
NewPriorityQueue creates a new priority queue
func (*PriorityQueue[T]) Len ¶
func (q *PriorityQueue[T]) Len() int
Len returns the length of the queue
func (*PriorityQueue[T]) Peek ¶
func (q *PriorityQueue[T]) Peek() (T, bool)
Peek returns the next item in the queue without removing it
func (*PriorityQueue[T]) Pop ¶
func (q *PriorityQueue[T]) Pop() (T, bool)
Pop pops an item off the queue
func (*PriorityQueue[T]) Push ¶
func (q *PriorityQueue[T]) Push(item T, weight float64)
Push pushes an item onto the queue
func (*PriorityQueue[T]) UpdatePriority ¶
func (q *PriorityQueue[T]) UpdatePriority(value T, priority float64)
type Queue ¶
type Queue[T any] struct { // contains filtered or unexported fields }
Queue is a thread safe non-blocking queue
func (*Queue[T]) Range ¶
Range executes a provided function once for each Queue element until it returns false or the Queue is empty.
func (*Queue[T]) RangeUntil ¶
RangeUntil executes a provided function once for each Queue element until it returns false or a value is sent on the done channel. Use this function when you want to continuously process items from the queue until a done signal is received.
type Set ¶
type Set[T comparable] struct { // contains filtered or unexported fields }
Set is a basic thread-safe Set implementation.
func NewSet ¶
func NewSet[T comparable]() *Set[T]
NewSet returns a new Set with the given initial size.
func (*Set[T]) Range ¶
Range executes a provided function once for each Set element until it returns false.
type Stack ¶
type Stack[T any] struct { // contains filtered or unexported fields }
Stack is a basic LIFO Stack
func (*Stack[T]) Peek ¶
Peek returns the top element of the Stack without removing it. Return false if Stack is empty.
func (*Stack[T]) Range ¶
Range executes a provided function once for each Stack element until it returns false.
func (*Stack[T]) RangeUntil ¶
RangeUntil executes a provided function once after calling Pop on the stack until the function returns false or a value is sent on the done channel. Use this function when you want to continuously process items from the stack until a done signal is received.