Commit 488169d9 authored by Bastian Kauschke's avatar Bastian Kauschke
Browse files

remove redundant calls to `statesOfBlock` in `Minimize`

parent 224c0632
......@@ -67,18 +67,22 @@ combineEdges encoding sorts part edges =
-- All relevant groups of edges which we have to `merge`.
let groupedEdges =
NonEmpty.groupBy (\a b -> from a == from b && to a == to b) $
sortEdges (mapMaybe (updateEdge part) edges)
sortEdges (mapMaybe (updateEdge firstStates part) edges)
in V.fromList . List.concat $ map mergeEdges groupedEdges
where
-- The functor sort of the given state.
--
-- As we have already updated the states here, we have to first look
-- at the original `state` which is the first state of the relevant block.
partitionSort state = fromEnum (sortedSort $ typeOf encoding (head $ statesOfBlock part (Block state))) - 1
-- The first state of each block.
firstStates = runST $ do
v <- MV.replicate (numBlocks part) (-1)
forM_ (states encoding) $ \s ->
MV.modify v (\old -> if old < 0 then s else old) (fromBlock $ blockOfState part s)
V.freeze v
-- The functor sort of the given block.
partitionSort block = fromEnum (sortedSort $ typeOf encoding (firstStates V.! block)) - 1
mergeEdges :: NonEmpty (Edge (Label (Desorted f))) -> [Edge (Label (Desorted f))]
mergeEdges (e:|ll) = map
-- Edges in a group only differ in their `Label`, so we can reuse the first edge
-- Edges in a group only differ in their `label`, so we can reuse the first edge
-- to rebuild after unwrapping the `label` of each element.
(\v -> Edge (from e) (mapDesortedLabel @f @f (const v) $ label e) (to e))
(merge (sorts V.! (partitionSort $ from e)) (NonEmpty.map (desortedLabel @f . label) (e:|ll)))
......@@ -88,10 +92,10 @@ sortEdges = List.sortBy (\a b -> compare (from a, to a) (from b, to b))
-- | Removes all edges which are not at the front of a block and updates `from` and `to` for the
-- remaining ones.
updateEdge :: Partition -> Edge f -> Maybe (Edge f)
updateEdge part e =
updateEdge :: V.Vector Int -> Partition -> Edge f -> Maybe (Edge f)
updateEdge firstStates part e =
let block = blockOfState part $ from e
in if head (statesOfBlock part block) == from e then
in if firstStates V.! (fromBlock block) == from e then
Just (Edge (fromBlock block) (label e) (fromBlock (blockOfState part $ to e))) else Nothing
-- | Merges the symbols of all states in a given block.
......
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