Created
November 19, 2018 19:21
-
-
Save Solonarv/d24fff46e0748670bc7b8fa2dcf71a8f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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