Created
May 12, 2017 12:43
-
-
Save yasuabe/9dc12ccf3e0b0af2cd564ec60bcf575b to your computer and use it in GitHub Desktop.
haskell implementation of simpath: CounterMap
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 Simpath.CounterMap where | |
import Data.Map (Map) | |
import qualified Data.Map as Map | |
import qualified Data.Foldable as Foldable | |
import Data.Function | |
import qualified Simpath.Frontier as F | |
import Simpath.Frontier (Frontier) | |
import Simpath.Border (Border) | |
type CounterMap = Map Frontier Integer | |
merge :: Integer -> CounterMap -> Frontier -> CounterMap | |
merge cnt cm fr = Map.insertWith (+) fr cnt cm | |
proceedAll :: Border -> CounterMap -> CounterMap | |
proceedAll b = Foldable.foldl f Map.empty . Map.toList | |
where f cm (fr, cnt) = on (.) recount lo hi cm | |
where (hi, lo) = F.proceed b fr | |
recount = flip $ Foldable.foldl $ merge cnt | |
countPaths :: [Border] -> Integer | |
countPaths = headCount . foldl (flip proceedAll) initialMap | |
where initialMap = Map.singleton F.initial 1 | |
headCount = head . Map.elems |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment