Skip to content

Instantly share code, notes, and snippets.

@EduardoRFS
Last active March 18, 2022 03:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save EduardoRFS/e61246ee3128e00e05db0c740bef081b to your computer and use it in GitHub Desktop.
Save EduardoRFS/e61246ee3128e00e05db0c740bef081b to your computer and use it in GitHub Desktop.
type value =
| Block of block
| Value of int
and block = {
mutable marked : bool;
fields : value array;
}
(* this doesn't work, fails when there is cycle and no progress *)
(* The idea here is that on each step we will be always traversing unmarked
nodes and marking only one of the branches, then starting again until
all the reachable nodes are marked *)
let rec mark_block_step block =
let fields = block.fields in
let length = Array.length fields in
let rec loop index =
let next () = loop (index + 1) in
if index >= length - 1 then
block.marked <- true
else
let field = Array.unsafe_get fields index in
match field with
| Block { marked = true; fields = _ }
| Value _ ->
next ()
| Block block -> mark_block_step block in
loop 0
let rec mark_block block =
if not block.marked then (
mark_block_step block;
mark_block block)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment