Module Incremental__.Adjust_heights_heap
val sexp_of_t : t -> Ppx_sexp_conv_lib.Sexp.t
val create : max_height_allowed:int -> tval length : t -> intval max_height_allowed : t -> intIt is required that all nodes have
n.height <= max_height_allowed t. Any attempt to set a node's height larger will raise.One can call
set_max_height_allowedto change the maximum-allowed height.set_max_height_allowed t mraises ifm < max_height_seen t.
val set_max_height_allowed : t -> int -> unitval max_height_seen : t -> intmax_height_seen treturns the maximum height of any node ever created, not just nodes currently in use.
val set_height : t -> _ Incremental__.Types.Node.t -> int -> unitset_heightmust be called to change the height of a node, except when clearing the height to-1. This allows the adjust-heights heap to track the maximum height of all nodes.set_heightraises ifnode.height > max_height_allowed t.
val adjust_heights : t -> Incremental__.Recompute_heap.t -> child:_ Incremental__.Types.Node.t -> parent:_ Incremental__.Types.Node.t -> unitadjust_heights t recompute_heap ~child ~parentis called whenparentis added as a parent ofchildandchild.height >= parent.height. It restores the node height invariant:child.height < parent.height(forparentand all its ancestors).Pre and post-conditions:
tis empty. Thus, for all nodesn,n.height_in_adjust_heights_heap = -1.- For all nodes
ninrecompute_heap,n.height = n.height_in_recompute_heap.
adjust_heightsadds a nodento the adjust-heights heap when it detects thatc.height >= n.heightfor some childcofn. It addsnwithn.height_in_adjust_heights_heapset to the pre-adjusted height ofn, and then setsn.heighttoc.height + 1.adjust_heightsthen does not changen.height_in_adjust_heights_heapuntilnis removed fromt, at which point it is reset to-1.adjust_heightsmay increasen.heightfurther as it detects other childrencofnwithc.height >= n.height. A node'sheight_in_recompute_heapchanges at most once duringadjust_heights, once the node's final adjusted height is known.Here is the algorithm.
while
tis not empty: 1. remove annintwith minimumn.height_in_adjust_heights_heap. 2.Recompute_heap.increase_height recompute_heap n. 3. for all parentspofn, ifn.height >= p.height, then ensurepis intand setp.heightton.height + 1andIf
adjust_heightsever encounterschildwhile visiting the ancestors ofparent, then there is a cycle in the graph andadjust_heightsraises.adjust_heightsraises if a node's height needs to be increased beyondmax_height_allowed t.