-generalBuildHeapGraph limit _ _ _ _ | limit <= 0 = error "buildHeapGraph: limit has to be positive"
-generalBuildHeapGraph limit defD knownEntries newIndices initialBoxes = do
- let initialState = (knownEntries, newIndices, [])
- (is, hg) <- runWriterT (evalStateT run initialState)
- return (HeapGraph hg, is)
+generalBuildHeapGraph limit _ _ | limit <= 0 = error "buildHeapGraph: limit has to be positive"
+generalBuildHeapGraph limit (HeapGraph hg) addBoxes = do
+ -- First collect all live boxes from the existing heap graph
+ boxList <- catMaybes <$> do
+ forM (M.toList hg) $ \(i, hge) -> do
+ mbBox <- derefWeakBox (hgeBox hge)
+ return $ (\b -> (b,i)) <$> mbBox
+
+ let indices | M.null hg = [0..]
+ | otherwise = [1 + fst (M.findMax hg)..]
+
+ initialState = (boxList, indices, [])
+ -- It is ok to use the Monoid (IntMap a) instance here, because
+ -- we will, besides the first time, use 'tell' only to add singletons not
+ -- already there
+ (is, hg') <- runWriterT (evalStateT run initialState)
+ -- Now add the annotations of the root values
+ let hg'' = foldr (uncurry annotateHeapGraph) (HeapGraph hg') is
+ return (hg'', is)