Commit 482fde78 by Jean-Christophe Filliâtre

binomial_heap: more efficient implementation (with rank now stored)

parent f5535c5e
 ... @@ -19,6 +19,7 @@ module BinomialHeap ... @@ -19,6 +19,7 @@ module BinomialHeap type tree = { type tree = { elem: elt; elem: elt; children: list tree; children: list tree; rank: int; } } (* [e] is no greater than the roots of the trees in [l] *) (* [e] is no greater than the roots of the trees in [l] *) ... @@ -41,14 +42,14 @@ module BinomialHeap ... @@ -41,14 +42,14 @@ module BinomialHeap lemma heaps_reverse: lemma heaps_reverse: forall h. heaps h -> heaps (reverse h) forall h. heaps h -> heaps (reverse h) function rank (t: tree) : int = (* function rank (t: tree) : int = *) length t.children (* length t.children *) function link (t1 t2: tree) : tree = function link (t1 t2: tree) : tree = if le t1.elem t2.elem then if le t1.elem t2.elem then { t1 with children = Cons t2 t1.children } { elem = t1.elem; rank = t1.rank + 1; children = Cons t2 t1.children } else else { t2 with children = Cons t1 t2.children } { elem = t2.elem; rank = t2.rank + 1; children = Cons t1 t2.children } function occ (x: elt) (l: list tree) : int = function occ (x: elt) (l: list tree) : int = match l with match l with ... @@ -86,7 +87,8 @@ module BinomialHeap ... @@ -86,7 +87,8 @@ module BinomialHeap end end predicate binomial_tree (t: tree) = predicate binomial_tree (t: tree) = has_order (rank t) t.children t.rank = length t.children && has_order t.rank t.children (* a binomial heap is a list of trees *) (* a binomial heap is a list of trees *) type heap = list tree type heap = list tree ... @@ -96,7 +98,7 @@ module BinomialHeap ... @@ -96,7 +98,7 @@ module BinomialHeap predicate inv (m: int) (h: heap) = predicate inv (m: int) (h: heap) = match h with match h with | Nil -> true | Nil -> true | Cons t r -> let k = rank t in m <= k && binomial_tree t && inv (k + 1) r | Cons t r -> let k = t.rank in m <= k && binomial_tree t && inv (k + 1) r end end lemma inv_reverse: lemma inv_reverse: ... @@ -166,7 +168,7 @@ module BinomialHeap ... @@ -166,7 +168,7 @@ module BinomialHeap ensures { occ x result = occ x h + 1 } ensures { occ x result = occ x h + 1 } ensures { forall e. e <> x -> occ e result = occ e h } ensures { forall e. e <> x -> occ e result = occ e h } = = add_tree { elem = x; children = Nil } h add_tree { elem = x; rank = 0; children = Nil } h let rec merge (ghost k: int) (h1 h2: heap) let rec merge (ghost k: int) (h1 h2: heap) requires { heaps h1 } requires { heaps h1 } ... ...
 ... @@ -6,28 +6,30 @@ ... @@ -6,28 +6,30 @@ ... @@ -50,16 +52,16 @@ ... @@ -50,16 +52,16 @@ ... @@ -78,60 +80,60 @@ ... @@ -78,60 +80,60 @@ ... @@ -163,6 +165,7 @@ ... @@ -163,6 +165,7 @@ ... @@ -170,25 +173,26 @@ ... @@ -170,25 +173,26 @@ ... @@ -197,19 +201,24 @@ ... @@ -197,19 +201,24 @@ ... @@ -218,25 +227,22 @@ ... @@ -218,25 +227,22 @@ ... @@ -245,7 +251,7 @@ ... @@ -245,7 +251,7 @@ ... @@ -256,15 +262,18 @@ ... @@ -256,15 +262,18 @@ ... @@ -274,20 +283,20 @@ ... @@ -274,20 +283,20 @@ ... @@ -296,40 +305,44 @@ ... @@ -296,40 +305,44 @@ ... @@ -348,16 +361,16 @@ ... @@ -348,16 +361,16 @@ ... @@ -368,10 +381,10 @@ ... @@ -368,10 +381,10 @@