Commit a38eacf3 authored by Hans-Peter Deifel's avatar Hans-Peter Deifel 🐢
Browse files

Move predecessor calculation to init function

Previously, this was already computed in Encoding.new. But since
`pred` requires knowledge about the size of the target set, this is
much better done in the init phase.
parent 3e4c4887
......@@ -19,16 +19,23 @@ data AlgoState s h = AlgoState
, lastW :: MVector s (STRef s (RI.Weight h))
, functor :: h -- TODO Shouldn't be needed
, encoding :: Encoding (RI.Label h) (RI.H1 h)
, pred :: Vector [EdgeRef]
-- refineable partition
}
initialize :: RefinementInterface h => h -> Encoding (RI.Label h) (RI.H1 h) -> ST s (AlgoState s h)
initialize functor encoding = do
-- nextSize is the node count of the target set. All target indices in the given
-- edge set must fall into this range.
initialize :: RefinementInterface h => h -> Encoding (RI.Label h) (RI.H1 h) -> Int -> ST s (AlgoState s h)
initialize functor encoding nextSize = do
toSub <- VM.replicate (size encoding) []
lastW <- VM.new (length (edges encoding))
predMutable <- VM.replicate nextSize []
iforM_ (edges encoding) $ \i (Edge x _ _) -> do
iforM_ (edges encoding) $ \i (Edge x _ y) -> do
VM.modify toSub (EdgeRef i:) x
VM.modify predMutable (EdgeRef i:) y
pred <- V.freeze predMutable
forM_ (elements encoding) $ \x -> do
outgoingLabels <- map (label . graph encoding) <$> VM.read toSub x
......
......@@ -5,18 +5,13 @@ module Data.MorphismEncoding
, Edge(..)
, new
, size
, outgoingSize
, elements
, typeOf
, graph
) where
import Data.Maybe (fromMaybe)
import Data.Vector (Vector)
import qualified Data.Vector as V
import Data.HashMap.Strict (HashMap)
import qualified Data.HashMap.Strict as HashMap
newtype EdgeRef = EdgeRef Int
deriving (Eq, Ord, Show)
......@@ -30,29 +25,12 @@ data Edge a = Edge
data Encoding a h1 = Encoding
{ structure :: Vector h1
, outgoingSize :: Int
, edges :: Vector (Edge a)
-- same as edges, but reverse and without label
, predecessors :: Vector [EdgeRef]
}
deriving (Show)
new :: Vector h1 -> Vector (Edge a) -> Encoding a h1
new structure edges =
let
-- FIXME Check that each outnode is mentioned
-- TODO Make efficient
outnodes :: HashMap Int [EdgeRef]
outnodes = HashMap.fromListWith (++) $
V.toList $ V.imap (\idx e -> (to e, [EdgeRef idx])) edges
outgoingSize = HashMap.size outnodes
predecessors = V.generate outgoingSize $ \i ->
fromMaybe [] (HashMap.lookup i outnodes)
in
Encoding {..}
new structure edges = Encoding {..}
size :: Encoding a h1 -> Int
size = length . structure
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment