haggle-0.1.0.0: A graph library offering mutable, immutable, and inductive graphs

Safe HaskellNone
LanguageHaskell2010

Data.Graph.Haggle.VertexMap

Contents

Description

This is a simple module to handle a common pattern: constructing graphs where vertex labels map uniquely to vertices.

The primary functions in this module are vertexForLabel and vertexForLabelRef, which take a vertex label and return the Vertex for that label (allocating a new Vertex) if necessary. The first of those functions explicitly threads the mapping as inputs and outputs. The second manages a mutable ref side-by-side with the underlying graph.

After the graph is fully constructed, this mapping is often still useful.

Synopsis

Pure interface

data VertexMap nl Source #

A simple mapping from labels to their Vertex

Instances
NFData nl => NFData (VertexMap nl) Source # 
Instance details

Defined in Data.Graph.Haggle.VertexMap

Methods

rnf :: VertexMap nl -> () #

vertexForLabel :: (MLabeledVertex g, Ord (MVertexLabel g), PrimMonad m, MonadRef m) => g m -> VertexMap (MVertexLabel g) -> MVertexLabel g -> m (Vertex, VertexMap (MVertexLabel g)) Source #

(v, m') <- vertexForLabel g m lbl

Looks up the Vertex for lbl in g. If no Vertex in g has that label, a new Vertex is allocated and returned. The updated vertex mapping m' is returned, too.

lookupVertexForLabel :: Ord nl => nl -> VertexMap nl -> Maybe Vertex Source #

A pure lookup to convert a Vertex label into a Vertex. If the label is not in the graph, returns Nothing.

Ref interface

data VertexMapRef nl m Source #

A VertexMap wrapped up in a mutable ref for possibly easier access in vertexMapFromRef.

newVertexMapRef :: (PrimMonad m, MonadRef m) => m (VertexMapRef nl m) Source #

Allocate a new VertexMap buried in a mutable ref.

vertexForLabelRef :: (MLabeledVertex g, Ord (MVertexLabel g), PrimMonad m, MonadRef m) => g m -> VertexMapRef (MVertexLabel g) m -> MVertexLabel g -> m Vertex Source #

Just like vertexForLabel, but holding the mapping in a ref instead of threading it. Usage is simpler:

v <- vertexForLabelRef g m lbl

vertexMapFromRef :: (PrimMonad m, MonadRef m) => VertexMapRef nl m -> m (VertexMap nl) Source #

Extract the pure VertexMap from the mutable ref. This is useful to retain the mapping after the graph is fully constructed.