module Math.Grads.Graph
( EdgeList
, Graph (..)
, GraphEdge
, changeIndsEdge
, changeTypeEdge
, edgeType
) where
import Data.List (nub)
type GraphEdge e = (Int, Int, e)
type EdgeList e = [GraphEdge e]
edgeType :: GraphEdge e -> e
edgeType (_, _, t) = t
changeTypeEdge :: (e1 -> e2) -> GraphEdge e1 -> GraphEdge e2
changeTypeEdge f (a, b, t) = (a, b, f t)
changeIndsEdge :: (Int -> Int) -> GraphEdge e -> GraphEdge e
changeIndsEdge f (a, b, t) = (f a, f b, t)
class Graph g where
fromList :: (Ord v, Eq v) => ([v], [GraphEdge e]) -> g v e
toList :: (Ord v, Eq v) => g v e -> ([v], [GraphEdge e])
vCount :: g v e -> Int
infixl 9 !>
(!>) :: (Ord v, Eq v) => g v e -> v -> [(v, e)]
infixl 9 !.
(!.) :: g v e -> Int -> [(Int, e)]
infixl 9 ?>
(?>) :: (Ord v, Eq v) => g v e -> v -> Maybe [(v, e)]
infixl 9 ?.
(?.) :: g v e -> Int -> Maybe [(Int, e)]
incident :: (Ord v, Eq v) => g v e -> v -> [(v, v, e)]
incident gr at = (\(a, b) -> (at, a, b)) <$> gr !> at
safeIncident :: (Ord v, Eq v) => g v e -> v -> Maybe [(v, v, e)]
safeIncident gr at = map (\(a, b) -> (at, a, b)) <$> gr ?> at
incidentIdx :: (Eq e) => g v e -> Int -> [GraphEdge e]
incidentIdx gr idx = nub ((\(a, b) -> (min idx a, max idx a, b)) <$> gr !. idx)
safeIncidentIdx :: (Eq e) => g v e -> Int -> Maybe [GraphEdge e]
safeIncidentIdx gr idx = nub <$> (map (\(a, b) -> (min idx a, max idx a, b)) <$> gr ?. idx)