Path operations; modify and alter #
This develops the necessary theorems to construct the modify and alter functions on RBSet
using path operations for in-place modification of an RBTree.
path balance #
Asserts that property p holds on the root of the tree, if any.
Equations
- Batteries.RBNode.OnRoot p Batteries.RBNode.nil = True
- Batteries.RBNode.OnRoot p (Batteries.RBNode.node c l x_1 r) = p x_1
Instances For
The balance invariant for a path. path.Balanced c₀ n₀ c n means that path is a red-black tree
with balance invariant c₀, n₀, but it has a "hole" where a tree with balance invariant c, n
has been removed. The defining property is Balanced.fill: if path.Balanced c₀ n₀ c n and you
fill the hole with a tree satisfying t.Balanced c n, then (path.fill t).Balanced c₀ n₀ .
- root
{c₀ : RBColor}
{n₀ : Nat}
{α : Type u_1}
: Path.Balanced c₀ n₀ root c₀ n₀
The root of the tree is
c₀, n₀-balanced by assumption. - redL
{c₀ : RBColor}
{n₀ : Nat}
{α : Type u_1}
{y : RBNode α}
{n : Nat}
{parent : Path α}
{v : α}
: y.Balanced RBColor.black n →
Path.Balanced c₀ n₀ parent RBColor.red n → Path.Balanced c₀ n₀ (left RBColor.red parent v y) RBColor.black n
Descend into the left subtree of a red node.
- redR
{c₀ : RBColor}
{n₀ : Nat}
{α : Type u_1}
{x : RBNode α}
{n : Nat}
{v : α}
{parent : Path α}
: x.Balanced RBColor.black n →
Path.Balanced c₀ n₀ parent RBColor.red n → Path.Balanced c₀ n₀ (right RBColor.red x v parent) RBColor.black n
Descend into the right subtree of a red node.
- blackL
{c₀ : RBColor}
{n₀ : Nat}
{α : Type u_1}
{y : RBNode α}
{c₂ : RBColor}
{n : Nat}
{parent : Path α}
{v : α}
{c₁ : RBColor}
: y.Balanced c₂ n →
Path.Balanced c₀ n₀ parent RBColor.black (n + 1) → Path.Balanced c₀ n₀ (left RBColor.black parent v y) c₁ n
Descend into the left subtree of a black node.
- blackR
{c₀ : RBColor}
{n₀ : Nat}
{α : Type u_1}
{x : RBNode α}
{c₁ : RBColor}
{n : Nat}
{v : α}
{parent : Path α}
{c₂ : RBColor}
: x.Balanced c₁ n →
Path.Balanced c₀ n₀ parent RBColor.black (n + 1) → Path.Balanced c₀ n₀ (right RBColor.black x v parent) c₂ n
Descend into the right subtree of a black node.
Instances For
The defining property of a balanced path: If path is a c₀,n₀ tree with a c,n hole,
then filling the hole with a c,n tree yields a c₀,n₀ tree.
The property of a path returned by t.zoom cut. Each of the parents visited along the path have
the appropriate ordering relation to the cut.
Equations
- Batteries.RBNode.Path.Zoomed cut Batteries.RBNode.Path.root = True
- Batteries.RBNode.Path.Zoomed cut (Batteries.RBNode.Path.left c parent x_1 r) = (cut x_1 = Ordering.lt ∧ Batteries.RBNode.Path.Zoomed cut parent)
- Batteries.RBNode.Path.Zoomed cut (Batteries.RBNode.Path.right c l x_1 parent) = (cut x_1 = Ordering.gt ∧ Batteries.RBNode.Path.Zoomed cut parent)
Instances For
path.RootOrdered cmp v is true if v would be able to fit into the hole
without violating the ordering invariant.
Equations
- Batteries.RBNode.Path.RootOrdered cmp Batteries.RBNode.Path.root x✝ = True
- Batteries.RBNode.Path.RootOrdered cmp (Batteries.RBNode.Path.left c parent x_2 r) x✝ = (Batteries.RBNode.cmpLT cmp x✝ x_2 ∧ Batteries.RBNode.Path.RootOrdered cmp parent x✝)
- Batteries.RBNode.Path.RootOrdered cmp (Batteries.RBNode.Path.right c l x_2 parent) x✝ = (Batteries.RBNode.cmpLT cmp x_2 x✝ ∧ Batteries.RBNode.Path.RootOrdered cmp parent x✝)
Instances For
The ordering invariant for a Path.
Equations
- One or more equations did not get rendered due to their size.
- Batteries.RBNode.Path.Ordered cmp Batteries.RBNode.Path.root = True
Instances For
alter #
The alter function preserves the ordering invariants.
A sufficient condition for ModifyWF is that the new element compares equal to the original.
O(log n). In-place replace the corresponding to key k.
This takes the element out of the tree while f runs,
so it uses the element linearly if t is unshared.
Equations
Instances For
O(log n). alterP cut f t simultaneously handles inserting, erasing and replacing an element
using a function f : Option α → Option α. It is passed the result of t.findP? cut
and can either return none to remove the element or some a to replace/insert
the element with a (which must have the same ordering properties as the original element).
The element is used linearly if t is unshared.
The AlterWF assumption is required because f may change
the ordering properties of the element, which would break the invariants.
Equations
- t.alter k f = Batteries.RBSet.alterP t (fun (x : α × β) => cmp k x.fst) (Batteries.RBMap.alter.adapt k f)