Module Incremental__.Adjust_heights_heap
val sexp_of_t : t -> Ppx_sexp_conv_lib.Sexp.t
val create : max_height_allowed:int -> t
val length : t -> int
val max_height_allowed : t -> int
It 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_allowed
to change the maximum-allowed height.set_max_height_allowed t m
raises ifm < max_height_seen t
.
val set_max_height_allowed : t -> int -> unit
val max_height_seen : t -> int
max_height_seen t
returns the maximum height of any node ever created, not just nodes currently in use.
val set_height : t -> _ Incremental__.Types.Node.t -> int -> unit
set_height
must 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_height
raises ifnode.height > max_height_allowed t
.
val adjust_heights : t -> Incremental__.Types.Recompute_heap.t -> child:_ Incremental__.Types.Node.t -> parent:_ Incremental__.Types.Node.t -> unit
adjust_heights t recompute_heap ~child ~parent
is called whenparent
is added as a parent ofchild
andchild.height >= parent.height
. It restores the node height invariant:child.height < parent.height
(forparent
and all its ancestors).Pre and post-conditions:
t
is empty. Thus, for all nodesn
,n.height_in_adjust_heights_heap = -1
.- For all nodes
n
inrecompute_heap
,n.height = n.height_in_recompute_heap
.
adjust_heights
adds a noden
to the adjust-heights heap when it detects thatc.height >= n.height
for some childc
ofn
. It addsn
withn.height_in_adjust_heights_heap
set to the pre-adjusted height ofn
, and then setsn.height
toc.height + 1
.adjust_heights
then does not changen.height_in_adjust_heights_heap
untiln
is removed fromt
, at which point it is reset to-1
.adjust_heights
may increasen.height
further as it detects other childrenc
ofn
withc.height >= n.height
. A node'sheight_in_recompute_heap
changes at most once duringadjust_heights
, once the node's final adjusted height is known.Here is the algorithm.
while
t
is not empty: 1. remove ann
int
with minimumn.height_in_adjust_heights_heap
. 2.Recompute_heap.increase_height recompute_heap n
. 3. for all parentsp
ofn
, ifn.height >= p.height
, then ensurep
is int
and setp.height
ton.height + 1
andIf
adjust_heights
ever encounterschild
while visiting the ancestors ofparent
, then there is a cycle in the graph andadjust_heights
raises.adjust_heights
raises if a node's height needs to be increased beyondmax_height_allowed t
.