Skip to content

Instantly share code, notes, and snippets.

@Solonarv
Created November 19, 2018 19:21
Show Gist options
  • Save Solonarv/d24fff46e0748670bc7b8fa2dcf71a8f to your computer and use it in GitHub Desktop.
Save Solonarv/d24fff46e0748670bc7b8fa2dcf71a8f to your computer and use it in GitHub Desktop.
module DAG where
import Data.IntMap (IntMap)
import qualified Data.IntMap as IntMap
import Data.IntSet (IntSet)
import qualified Data.IntSet as IntSet
data DAG a = DAG { dagValues :: IntMap a, dagEdges :: IntMap IntSet }
type NodeIndex = Int
type NodeIndexes = IntSet
nodeChildren :: DAG -> NodeIndex -> NodeIndexes
nodeChildren DAG{dagEdges = edges} v = case edges `IntMap.lookup` v of
Nothing -> IntSet.empty
Just relatives -> IntSet.map (\o -> o + v) $ IntSet.filter (> 0) relatives
nodeParents :: DAG -> NodeIndex -> NodeIndexes
nodeParents DAG{dagEdges = edges} v = case edges `IntMap.lookup` v of
Nothing -> IntSet.empty
Just relatives -> IntSet.map (\o -> o + v) $ IntSet.filter (< 0) relatives
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment