Red-black trees #
This module implements a type RBMap α β cmp
which is a functional data structure for
storing a key-value store in a binary search tree.
It is built on the simpler RBSet α cmp
type, which stores a set of values of type α
using the function cmp : α → α → Ordering
for determining the ordering relation.
The tree will never store two elements that compare .eq
under the cmp
function,
but the function does not have to satisfy cmp x y = .eq → x = y
, and in the map case
α
is a key-value pair and the cmp
function only compares the keys.
In a red-black tree, every node has a color which is either "red" or "black" (this particular choice of colors is conventional). A nil node is considered black.
- red: Batteries.RBColor
A red node is required to have black children.
- black: Batteries.RBColor
Every path from the root to a leaf must pass through the same number of black nodes.
Instances For
Equations
- Batteries.instReprRBColor = { reprPrec := Batteries.reprRBColor✝ }
A red-black tree. (This is an internal implementation detail of the RBSet
type,
which includes the invariants of the tree.) This is a binary search tree augmented with
a "color" field which is either red or black for each node and used to implement
the re-balancing operations.
See: Red–black tree
- nil: {α : Type u} → Batteries.RBNode α
An empty tree.
- node: {α : Type u} → Batteries.RBColor → Batteries.RBNode α → α → Batteries.RBNode α → Batteries.RBNode α
Instances For
Equations
- Batteries.instReprRBNode = { reprPrec := Batteries.reprRBNode✝ }
Equations
- Batteries.RBNode.instEmptyCollection = { emptyCollection := Batteries.RBNode.nil }
The minimum element of a tree is the left-most value.
Equations
- Batteries.RBNode.nil.min? = none
- (Batteries.RBNode.node c Batteries.RBNode.nil v r).min? = some v
- (Batteries.RBNode.node c l v r).min? = l.min?
Instances For
The maximum element of a tree is the right-most value.
Equations
- Batteries.RBNode.nil.max? = none
- (Batteries.RBNode.node c l v Batteries.RBNode.nil).max? = some v
- (Batteries.RBNode.node c l v r).max? = r.max?
Instances For
Alias of Batteries.RBNode.min?
.
The minimum element of a tree is the left-most value.
Equations
Instances For
Alias of Batteries.RBNode.max?
.
The maximum element of a tree is the right-most value.
Equations
Instances For
Fold a function in tree order along the nodes. v₀
is used at nil
nodes and
f
is used to combine results at branching nodes.
Equations
- Batteries.RBNode.fold v₀ f Batteries.RBNode.nil = v₀
- Batteries.RBNode.fold v₀ f (Batteries.RBNode.node a a_1 a_2 a_3) = f (Batteries.RBNode.fold v₀ f a_1) a_2 (Batteries.RBNode.fold v₀ f a_3)
Instances For
Fold a function on the values from left to right (in increasing order).
Equations
- Batteries.RBNode.foldl f x Batteries.RBNode.nil = x
- Batteries.RBNode.foldl f x (Batteries.RBNode.node c l v r) = Batteries.RBNode.foldl f (f (Batteries.RBNode.foldl f x l) v) r
Instances For
Fold a function on the values from right to left (in decreasing order).
Equations
- Batteries.RBNode.foldr f Batteries.RBNode.nil x = x
- Batteries.RBNode.foldr f (Batteries.RBNode.node c l v r) x = Batteries.RBNode.foldr f l (f v (Batteries.RBNode.foldr f r x))
Instances For
O(n)
. Convert the tree to a list in ascending order.
Equations
- t.toList = Batteries.RBNode.foldr (fun (x1 : α) (x2 : List α) => x1 :: x2) t []
Instances For
Run monadic function f
on each element of the tree (in increasing order).
Equations
- Batteries.RBNode.forM f Batteries.RBNode.nil = pure PUnit.unit
- Batteries.RBNode.forM f (Batteries.RBNode.node a a_1 a_2 a_3) = do Batteries.RBNode.forM f a_1 f a_2 Batteries.RBNode.forM f a_3
Instances For
Fold a monadic function on the values from left to right (in increasing order).
Equations
- Batteries.RBNode.foldlM f x Batteries.RBNode.nil = pure x
- Batteries.RBNode.foldlM f x (Batteries.RBNode.node c l v r) = do let __do_lift ← Batteries.RBNode.foldlM f x l let __do_lift ← f __do_lift v Batteries.RBNode.foldlM f __do_lift r
Instances For
Implementation of for x in t
loops over a RBNode
(in increasing order).
Equations
- as.forIn init f = ForInStep.run <$> Batteries.RBNode.forIn.visit f as init
Instances For
Inner loop of forIn
.
Equations
- One or more equations did not get rendered due to their size.
- Batteries.RBNode.forIn.visit f Batteries.RBNode.nil x = pure (ForInStep.yield x)
Instances For
An auxiliary data structure (an iterator) over an RBNode
which lazily
pulls elements from the left.
- nil: {α : Type u_1} → Batteries.RBNode.Stream α
The stream is empty.
- cons: {α : Type u_1} → α → Batteries.RBNode α → Batteries.RBNode.Stream α → Batteries.RBNode.Stream α
We are ready to deliver element
v
with right childr
, and wheretail
represents all the subtrees we have yet to destructure.
Instances For
O(log n)
. Turn a node into a stream, by descending along the left spine.
Equations
- Batteries.RBNode.nil.toStream x = x
- (Batteries.RBNode.node c l v r).toStream x = l.toStream (Batteries.RBNode.Stream.cons v r x)
Instances For
O(1)
amortized, O(log n)
worst case: Get the next element from the stream.
Equations
- Batteries.RBNode.Stream.nil.next? = none
- (Batteries.RBNode.Stream.cons v r tail).next? = some (v, r.toStream tail)
Instances For
Fold a function on the values from left to right (in increasing order).
Equations
- Batteries.RBNode.Stream.foldl f x Batteries.RBNode.Stream.nil = x
- Batteries.RBNode.Stream.foldl f x (Batteries.RBNode.Stream.cons v r tail) = Batteries.RBNode.Stream.foldl f (Batteries.RBNode.foldl f (f x v) r) tail
Instances For
Fold a function on the values from right to left (in decreasing order).
Equations
- Batteries.RBNode.Stream.foldr f Batteries.RBNode.Stream.nil x = x
- Batteries.RBNode.Stream.foldr f (Batteries.RBNode.Stream.cons v r tail) x = f v (Batteries.RBNode.foldr f r (Batteries.RBNode.Stream.foldr f tail x))
Instances For
O(n)
. Convert the stream to a list in ascending order.
Equations
- t.toList = Batteries.RBNode.Stream.foldr (fun (x1 : α) (x2 : List α) => x1 :: x2) t []
Instances For
Equations
- Batteries.RBNode.instToStreamStream = { toStream := fun (x : Batteries.RBNode α) => x.toStream }
Equations
- Batteries.RBNode.instStreamStream = { next? := Batteries.RBNode.Stream.next? }
Returns true
iff every element of the tree satisfies p
.
Equations
- Batteries.RBNode.all p Batteries.RBNode.nil = true
- Batteries.RBNode.all p (Batteries.RBNode.node a a_1 a_2 a_3) = (p a_2 && Batteries.RBNode.all p a_1 && Batteries.RBNode.all p a_3)
Instances For
Returns true
iff any element of the tree satisfies p
.
Equations
- Batteries.RBNode.any p Batteries.RBNode.nil = false
- Batteries.RBNode.any p (Batteries.RBNode.node a a_1 a_2 a_3) = (p a_2 || Batteries.RBNode.any p a_1 || Batteries.RBNode.any p a_3)
Instances For
Asserts that p
holds on every element of the tree.
Equations
- Batteries.RBNode.All p Batteries.RBNode.nil = True
- Batteries.RBNode.All p (Batteries.RBNode.node a a_1 a_2 a_3) = (p a_2 ∧ Batteries.RBNode.All p a_1 ∧ Batteries.RBNode.All p a_3)
Instances For
Equations
- Batteries.RBNode.instDecidableAllOfDecidablePred = decidable_of_iff (Batteries.RBNode.all (fun (b : α) => decide (p b)) t = true) ⋯
Asserts that p
holds on some element of the tree.
Equations
- Batteries.RBNode.Any p Batteries.RBNode.nil = False
- Batteries.RBNode.Any p (Batteries.RBNode.node a a_1 a_2 a_3) = (p a_2 ∨ Batteries.RBNode.Any p a_1 ∨ Batteries.RBNode.Any p a_3)
Instances For
Equations
- Batteries.RBNode.instDecidableAnyOfDecidablePred = decidable_of_iff (Batteries.RBNode.any (fun (b : α) => decide (p b)) t = true) ⋯
True if x
is an element of t
"exactly", i.e. up to equality, not the cmp
relation.
Equations
- Batteries.RBNode.EMem x t = Batteries.RBNode.Any (fun (x_1 : α) => x = x_1) t
Instances For
Equations
- Batteries.RBNode.instMembership = { mem := fun (t : Batteries.RBNode α) (x : α) => Batteries.RBNode.EMem x t }
True if the specified cut
matches at least one element of of t
.
Equations
- Batteries.RBNode.MemP cut t = Batteries.RBNode.Any (fun (x : α) => cut x = Ordering.eq) t
Instances For
True if x
is equivalent to an element of t
.
Equations
- Batteries.RBNode.Mem cmp x t = Batteries.RBNode.MemP (cmp x) t
Instances For
Equations
- Batteries.RBNode.Slow.instDecidableEMem = inferInstanceAs (Decidable (Batteries.RBNode.Any (fun (x_1 : α) => x = x_1) t))
Instances For
Equations
- Batteries.RBNode.Slow.instDecidableMemP = inferInstanceAs (Decidable (Batteries.RBNode.Any (fun (x : α) => cut x = Ordering.eq) t))
Instances For
Equations
- Batteries.RBNode.Slow.instDecidableMem = inferInstanceAs (Decidable (Batteries.RBNode.Any (fun (x_1 : α) => cmp x x_1 = Ordering.eq) t))
Instances For
Asserts that t₁
and t₂
have the same number of elements in the same order,
and R
holds pairwise between them. The tree structure is ignored.
Equations
- One or more equations did not get rendered due to their size.
Instances For
Equations
- Batteries.RBNode.instBEq = { beq := fun (a b : Batteries.RBNode α) => Batteries.RBNode.all₂ (fun (x1 x2 : α) => x1 == x2) a b }
We say that x < y
under the comparator cmp
if cmp x y = .lt
.
- In order to avoid assuming the comparator is always lawful, we use a
local
∀ [TransCmp cmp]
binder in the relation so that the ordering properties of the tree only need to hold if the comparator is lawful. - The
Nonempty
wrapper is a no-op because this is already a proposition, but it prevents the[TransCmp cmp]
binder from being introduced when we don't want it.
Equations
- Batteries.RBNode.cmpLT cmp x y = Nonempty (∀ [inst : Batteries.TransCmp cmp], cmp x y = Ordering.lt)
Instances For
Equations
- Batteries.RBNode.instDecidableCmpLTOfTransCmp cmp = decidable_of_iff' (cmp x y = Ordering.lt) ⋯
We say that x ≈ y
under the comparator cmp
if cmp x y = .eq
. See also cmpLT
.
Equations
- Batteries.RBNode.cmpEq cmp x y = Nonempty (∀ [inst : Batteries.TransCmp cmp], cmp x y = Ordering.eq)
Instances For
Equations
- Batteries.RBNode.instDecidableCmpEqOfTransCmp cmp = decidable_of_iff' (cmp x y = Ordering.eq) ⋯
O(n)
. Verifies an ordering relation on the nodes of the tree.
Equations
- Batteries.RBNode.isOrdered cmp Batteries.RBNode.nil (some l_2) (some r_2) = decide (cmp l_2 r_2 = Ordering.lt)
- Batteries.RBNode.isOrdered cmp Batteries.RBNode.nil l r = true
- Batteries.RBNode.isOrdered cmp (Batteries.RBNode.node a a_1 a_2 a_3) l r = (Batteries.RBNode.isOrdered cmp a_1 l (some a_2) && Batteries.RBNode.isOrdered cmp a_3 (some a_2) r)
Instances For
The first half of Okasaki's balance
, concerning red-red sequences in the left child.
Equations
- One or more equations did not get rendered due to their size.
- x✝¹.balance1 x✝ x = Batteries.RBNode.node Batteries.RBColor.black x✝¹ x✝ x
Instances For
The second half of Okasaki's balance
, concerning red-red sequences in the right child.
Equations
- One or more equations did not get rendered due to their size.
- x✝¹.balance2 x✝ x = Batteries.RBNode.node Batteries.RBColor.black x✝¹ x✝ x
Instances For
Returns red
if the node is red, otherwise black
. (Nil nodes are treated as black
.)
Equations
- (Batteries.RBNode.node c l v r).isRed = c
- x.isRed = Batteries.RBColor.black
Instances For
Returns black
if the node is black, otherwise red
.
(Nil nodes are treated as red
, which is not the usual convention but useful for deletion.)
Equations
- (Batteries.RBNode.node c l v r).isBlack = c
- x.isBlack = Batteries.RBColor.red
Instances For
Changes the color of the root to black
.
Equations
- Batteries.RBNode.nil.setBlack = Batteries.RBNode.nil
- (Batteries.RBNode.node a a_1 a_2 a_3).setBlack = Batteries.RBNode.node Batteries.RBColor.black a_1 a_2 a_3
Instances For
O(n)
. Reverses the ordering of the tree without any rebalancing.
Equations
- Batteries.RBNode.nil.reverse = Batteries.RBNode.nil
- (Batteries.RBNode.node a a_1 a_2 a_3).reverse = Batteries.RBNode.node a a_3.reverse a_2 a_1.reverse
Instances For
The core of the insert
function. This adds an element x
to a balanced red-black tree.
Importantly, the result of calling ins
is not a proper red-black tree,
because it has a broken balance invariant.
(See Balanced.ins
for the balance invariant of ins
.)
The insert
function does the final fixup needed to restore the invariant.
Equations
- One or more equations did not get rendered due to their size.
- Batteries.RBNode.ins cmp x Batteries.RBNode.nil = Batteries.RBNode.node Batteries.RBColor.red Batteries.RBNode.nil x Batteries.RBNode.nil
Instances For
insert cmp t v
inserts element v
into the tree, using the provided comparator
cmp
to put it in the right place and automatically rebalancing the tree as necessary.
Equations
- Batteries.RBNode.insert cmp t v = match t.isRed with | Batteries.RBColor.red => (Batteries.RBNode.ins cmp v t).setBlack | Batteries.RBColor.black => Batteries.RBNode.ins cmp v t
Instances For
Recolor the root of the tree to red
if possible.
Equations
- (Batteries.RBNode.node c a v b).setRed = Batteries.RBNode.node Batteries.RBColor.red a v b
- Batteries.RBNode.nil.setRed = Batteries.RBNode.nil
Instances For
Rebalancing a tree which has shrunk on the left.
Equations
- One or more equations did not get rendered due to their size.
- (Batteries.RBNode.node Batteries.RBColor.red a x b).balLeft v r = Batteries.RBNode.node Batteries.RBColor.red (Batteries.RBNode.node Batteries.RBColor.black a x b) v r
- l.balLeft v (Batteries.RBNode.node Batteries.RBColor.black a y b) = l.balance2 v (Batteries.RBNode.node Batteries.RBColor.red a y b)
- l.balLeft v r = Batteries.RBNode.node Batteries.RBColor.red l v r
Instances For
Rebalancing a tree which has shrunk on the right.
Equations
- One or more equations did not get rendered due to their size.
- l.balRight v (Batteries.RBNode.node Batteries.RBColor.red a x b) = Batteries.RBNode.node Batteries.RBColor.red l v (Batteries.RBNode.node Batteries.RBColor.black a x b)
- (Batteries.RBNode.node Batteries.RBColor.black a x_1 b).balRight v r = (Batteries.RBNode.node Batteries.RBColor.red a x_1 b).balance1 v r
- l.balRight v r = Batteries.RBNode.node Batteries.RBColor.red l v r
Instances For
The number of nodes in the tree.
Equations
- Batteries.RBNode.nil.size = 0
- (Batteries.RBNode.node a a_1 a_2 a_3).size = a_1.size + a_3.size + 1
Instances For
Concatenate two trees with the same black-height.
Equations
Instances For
erase #
The core of the erase
function. The tree returned from this function has a broken invariant,
which is restored in erase
.
Equations
- One or more equations did not get rendered due to their size.
- Batteries.RBNode.del cut Batteries.RBNode.nil = Batteries.RBNode.nil
Instances For
The erase cut t
function removes an element from the tree t
.
The cut
function is used to locate an element in the tree:
it returns .gt
if we go too high and .lt
if we go too low;
if it returns .eq
we will remove the element.
(The function cmp k
for some key k
is a valid cut function, but we can also use cuts that
are not of this form as long as they are suitably monotonic.)
Equations
- Batteries.RBNode.erase cut t = (Batteries.RBNode.del cut t).setBlack
Instances For
Finds an element in the tree satisfying the cut
function.
Equations
- One or more equations did not get rendered due to their size.
- Batteries.RBNode.find? cut Batteries.RBNode.nil = none
Instances For
upperBound? cut
retrieves the smallest entry larger than or equal to cut
, if it exists.
Equations
- One or more equations did not get rendered due to their size.
- Batteries.RBNode.upperBound? cut Batteries.RBNode.nil x = x
Instances For
lowerBound? cut
retrieves the largest entry smaller than or equal to cut
, if it exists.
Equations
- One or more equations did not get rendered due to their size.
- Batteries.RBNode.lowerBound? cut Batteries.RBNode.nil x = x
Instances For
Returns the root of the tree, if any.
Equations
- Batteries.RBNode.nil.root? = none
- (Batteries.RBNode.node a a_1 a_2 a_3).root? = some a_2
Instances For
O(n)
. Map a function on every value in the tree.
This requires IsMonotone
on the function in order to preserve the order invariant.
Equations
- Batteries.RBNode.map f Batteries.RBNode.nil = Batteries.RBNode.nil
- Batteries.RBNode.map f (Batteries.RBNode.node a a_1 a_2 a_3) = Batteries.RBNode.node a (Batteries.RBNode.map f a_1) (f a_2) (Batteries.RBNode.map f a_3)
Instances For
Converts the tree into an array in increasing sorted order.
Equations
- n.toArray = Batteries.RBNode.foldl (fun (x1 : Array α) (x2 : α) => x1.push x2) #[] n
Instances For
A RBNode.Path α
is a "cursor" into an RBNode
which represents the path
from the root to a subtree. Note that the path goes from the target subtree
up to the root, which is reversed from the normal way data is stored in the tree.
See Zipper for more information.
- root: {α : Type u} → Batteries.RBNode.Path α
The root of the tree, which is the end of the path of parents.
- left: {α : Type u} → Batteries.RBColor → Batteries.RBNode.Path α → α → Batteries.RBNode α → Batteries.RBNode.Path α
A path that goes down the left subtree.
- right: {α : Type u} → Batteries.RBColor → Batteries.RBNode α → α → Batteries.RBNode.Path α → Batteries.RBNode.Path α
A path that goes down the right subtree.
Instances For
Fills the Path
with a subtree.
Equations
- Batteries.RBNode.Path.root.fill x = x
- (Batteries.RBNode.Path.left c parent y b).fill x = parent.fill (Batteries.RBNode.node c x y b)
- (Batteries.RBNode.Path.right c a y parent).fill x = parent.fill (Batteries.RBNode.node c a y x)
Instances For
Like find?
, but instead of just returning the element, it returns the entire subtree
at the element and a path back to the root for reconstructing the tree.
Equations
- One or more equations did not get rendered due to their size.
- Batteries.RBNode.zoom cut Batteries.RBNode.nil x = (Batteries.RBNode.nil, x)
Instances For
This function does the second part of RBNode.ins
,
which unwinds the stack and rebuilds the tree.
Equations
- Batteries.RBNode.Path.root.ins x = x.setBlack
- (Batteries.RBNode.Path.left Batteries.RBColor.red parent y b).ins x = parent.ins (Batteries.RBNode.node Batteries.RBColor.red x y b)
- (Batteries.RBNode.Path.right Batteries.RBColor.red a y parent).ins x = parent.ins (Batteries.RBNode.node Batteries.RBColor.red a y x)
- (Batteries.RBNode.Path.left Batteries.RBColor.black parent y b).ins x = parent.ins (x.balance1 y b)
- (Batteries.RBNode.Path.right Batteries.RBColor.black a y parent).ins x = parent.ins (a.balance2 y x)
Instances For
path.insertNew v
inserts element v
into the tree, assuming that path
is zoomed in
on a nil
node such that inserting a new element at this position is valid.
Equations
- path.insertNew v = path.ins (Batteries.RBNode.node Batteries.RBColor.red Batteries.RBNode.nil v Batteries.RBNode.nil)
Instances For
path.insert t v
inserts element v
into the tree, assuming that (t, path)
was the result of a
previous zoom
operation (so either the root of t
is equivalent to v
or it is empty).
Equations
- path.insert Batteries.RBNode.nil v = path.insertNew v
- path.insert (Batteries.RBNode.node a a_1 a_2 a_3) v = path.fill (Batteries.RBNode.node a a_1 v a_3)
Instances For
path.del t c
does the second part of RBNode.del
, which unwinds the stack
and rebuilds the tree. The c
argument is the color of the node before the deletion
(we used t₀.isBlack
for this in RBNode.del
but the original tree is no longer
available in this formulation).
Equations
- Batteries.RBNode.Path.root.del x✝ x = x✝.setBlack
- (Batteries.RBNode.Path.left c parent y b).del x Batteries.RBColor.red = parent.del (Batteries.RBNode.node Batteries.RBColor.red x y b) c
- (Batteries.RBNode.Path.right c a y parent).del x Batteries.RBColor.red = parent.del (Batteries.RBNode.node Batteries.RBColor.red a y x) c
- (Batteries.RBNode.Path.left c parent y b).del x Batteries.RBColor.black = parent.del (x.balLeft y b) c
- (Batteries.RBNode.Path.right c a y parent).del x Batteries.RBColor.black = parent.del (a.balRight y x) c
Instances For
path.erase t v
removes the root element of t
from the tree, assuming that (t, path)
was
the result of a previous zoom
operation.
Equations
- path.erase Batteries.RBNode.nil = path.fill Batteries.RBNode.nil
- path.erase (Batteries.RBNode.node a a_1 a_2 a_3) = path.del (a_1.append a_3) a
Instances For
modify cut f t
uses cut
to find an element,
then modifies the element using f
and reinserts it into the tree.
Because the tree structure is not modified,
f
must not modify the ordering properties of the element.
The element in t
is used linearly if t
is unshared.
Equations
- One or more equations did not get rendered due to their size.
Instances For
alter cut f t
simultaneously handles inserting, erasing and replacing an element
using a function f : Option α → Option α
. It is passed the result of t.find? 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.
Equations
- One or more equations did not get rendered due to their size.
Instances For
The ordering invariant of a red-black tree, which is a binary search tree.
This says that every element of a left subtree is less than the root, and
every element in the right subtree is greater than the root, where the
less than relation x < y
is understood to mean cmp x y = .lt
.
Because we do not assume that cmp
is lawful when stating this property,
we write it in such a way that if cmp
is not lawful then the condition holds trivially.
That way we can prove the ordering invariants without assuming cmp
is lawful.
Equations
- One or more equations did not get rendered due to their size.
- Batteries.RBNode.Ordered cmp Batteries.RBNode.nil = True
Instances For
Equations
- One or more equations did not get rendered due to their size.
- Batteries.RBNode.Slow.instDecidableOrdered cmp Batteries.RBNode.nil = inferInstanceAs (Decidable True)
Instances For
The red-black balance invariant. Balanced t c n
says that the color of the root node is c
,
and the black-height (the number of black nodes on any path from the root) of the tree is n
.
Additionally, every red node must have black children.
- nil: ∀ {α : Type u_1}, Batteries.RBNode.nil.Balanced Batteries.RBColor.black 0
A nil node is balanced with black-height 0, and it is considered black.
- red: ∀ {α : Type u_1} {x : Batteries.RBNode α} {n : Nat} {y : Batteries.RBNode α} {v : α},
x.Balanced Batteries.RBColor.black n →
y.Balanced Batteries.RBColor.black n →
(Batteries.RBNode.node Batteries.RBColor.red x v y).Balanced Batteries.RBColor.red n
A red node is balanced with black-height
n
if its children are both black with with black-heightn
. - black: ∀ {α : Type u_1} {x : Batteries.RBNode α} {c₁ : Batteries.RBColor} {n : Nat} {y : Batteries.RBNode α}
{c₂ : Batteries.RBColor} {v : α},
x.Balanced c₁ n →
y.Balanced c₂ n → (Batteries.RBNode.node Batteries.RBColor.black x v y).Balanced Batteries.RBColor.black (n + 1)
A black node is balanced with black-height
n + 1
if its children both have black-heightn
.
Instances For
The well-formedness invariant for a red-black tree. The first constructor is the real invariant,
and the others allow us to "cheat" in this file and define insert
and erase
,
which have more complex proofs that are delayed to Batteries.Data.RBMap.Lemmas
.
- mk: ∀ {α : Type u_1} {cmp : α → α → Ordering} {t : Batteries.RBNode α} {c : Batteries.RBColor} {n : Nat},
Batteries.RBNode.Ordered cmp t → t.Balanced c n → Batteries.RBNode.WF cmp t
The actual well-formedness invariant: a red-black tree has the ordering and balance invariants.
- insert: ∀ {α : Type u_1} {cmp : α → α → Ordering} {t : Batteries.RBNode α} {a : α}, Batteries.RBNode.WF cmp t → Batteries.RBNode.WF cmp (Batteries.RBNode.insert cmp t a)
- erase: ∀ {α : Type u_1} {cmp : α → α → Ordering} {t : Batteries.RBNode α} {cut : α → Ordering}, Batteries.RBNode.WF cmp t → Batteries.RBNode.WF cmp (Batteries.RBNode.erase cut t)
Instances For
An RBSet
is a self-balancing binary search tree.
The cmp
function is the comparator that will be used for performing searches;
it should satisfy the requirements of TransCmp
for it to have sensible behavior.
Equations
- Batteries.RBSet α cmp = { t : Batteries.RBNode α // Batteries.RBNode.WF cmp t }
Instances For
O(1)
. Construct a new empty tree.
Equations
- Batteries.mkRBSet α cmp = ⟨Batteries.RBNode.nil, ⋯⟩
Instances For
O(1)
. Construct a new empty tree.
Equations
- Batteries.RBSet.empty = Batteries.mkRBSet α cmp
Instances For
Equations
- Batteries.RBSet.instEmptyCollection α cmp = { emptyCollection := Batteries.RBSet.empty }
Equations
- Batteries.RBSet.instInhabited α cmp = { default := ∅ }
O(1)
. Construct a new tree with one element v
.
Equations
- Batteries.RBSet.single v = ⟨Batteries.RBNode.node Batteries.RBColor.red Batteries.RBNode.nil v Batteries.RBNode.nil, ⋯⟩
Instances For
O(n)
. Fold a function on the values from left to right (in increasing order).
Equations
- Batteries.RBSet.foldl f init t = Batteries.RBNode.foldl f init t.val
Instances For
O(n)
. Fold a function on the values from right to left (in decreasing order).
Equations
- Batteries.RBSet.foldr f init t = Batteries.RBNode.foldr f t.val init
Instances For
O(n)
. Fold a monadic function on the values from left to right (in increasing order).
Equations
- Batteries.RBSet.foldlM f init t = Batteries.RBNode.foldlM f init t.val
Instances For
O(n)
. Run monadic function f
on each element of the tree (in increasing order).
Equations
- Batteries.RBSet.forM f t = Batteries.RBNode.forM f t.val
Instances For
Equations
- Batteries.RBSet.instForIn = { forIn := fun {β : Type ?u.32} [Monad m] (t : Batteries.RBSet α cmp) => t.val.forIn }
Equations
- Batteries.RBSet.instToStreamStream = { toStream := fun (x : Batteries.RBSet α cmp) => x.val.toStream }
O(1)
. Is the tree empty?
Equations
- Batteries.RBSet.isEmpty ⟨Batteries.RBNode.nil, property⟩ = true
- x.isEmpty = false
Instances For
O(n)
. Convert the tree to a list in ascending order.
Equations
- t.toList = t.val.toList
Instances For
O(log n)
. Returns the entry a
such that a ≤ k
for all keys in the RBSet.
Equations
- t.min? = t.val.min?
Instances For
O(log n)
. Returns the entry a
such that a ≥ k
for all keys in the RBSet.
Equations
- t.max? = t.val.max?
Instances For
Alias of Batteries.RBSet.min?
.
O(log n)
. Returns the entry a
such that a ≤ k
for all keys in the RBSet.
Equations
Instances For
Alias of Batteries.RBSet.max?
.
O(log n)
. Returns the entry a
such that a ≥ k
for all keys in the RBSet.
Equations
Instances For
Equations
- Batteries.RBSet.instRepr = { reprPrec := fun (m : Batteries.RBSet α cmp) (prec : Nat) => Repr.addAppParen (Std.Format.text "RBSet.ofList " ++ repr m.toList) prec }
O(log n)
. Insert element v
into the tree.
Equations
- t.insert v = ⟨Batteries.RBNode.insert cmp t.val v, ⋯⟩
Instances For
Insert all elements from a collection into a RBSet α cmp
.
Equations
- One or more equations did not get rendered due to their size.
Instances For
O(log n)
. Remove an element from the tree using a cut function.
The cut
function is used to locate an element in the tree:
it returns .gt
if we go too high and .lt
if we go too low;
if it returns .eq
we will remove the element.
(The function cmp k
for some key k
is a valid cut function, but we can also use cuts that
are not of this form as long as they are suitably monotonic.)
Equations
- t.erase cut = ⟨Batteries.RBNode.erase cut t.val, ⋯⟩
Instances For
O(log n)
. Find an element in the tree using a cut function.
Equations
- t.findP? cut = Batteries.RBNode.find? cut t.val
Instances For
O(log n)
. Returns an element in the tree equivalent to x
if one exists.
Equations
- t.find? x = Batteries.RBNode.find? (cmp x) t.val
Instances For
O(log n)
. Find an element in the tree, or return a default value v₀
.
Equations
- t.findPD cut v₀ = (t.findP? cut).getD v₀
Instances For
O(log n)
. upperBoundP cut
retrieves the smallest entry comparing gt
or eq
under cut
,
if it exists. If multiple keys in the map return eq
under cut
, any of them may be returned.
Equations
- t.upperBoundP? cut = Batteries.RBNode.upperBound? cut t.val
Instances For
O(log n)
. upperBound? k
retrieves the largest entry smaller than or equal to k
,
if it exists.
Equations
- t.upperBound? k = t.upperBoundP? (cmp k)
Instances For
O(log n)
. lowerBoundP cut
retrieves the largest entry comparing lt
or eq
under cut
,
if it exists. If multiple keys in the map return eq
under cut
, any of them may be returned.
Equations
- t.lowerBoundP? cut = Batteries.RBNode.lowerBound? cut t.val
Instances For
O(log n)
. lowerBound? k
retrieves the largest entry smaller than or equal to k
,
if it exists.
Equations
- t.lowerBound? k = t.lowerBoundP? (cmp k)
Instances For
O(log n)
. Returns true if the given cut returns eq
for something in the RBSet.
Equations
- t.containsP cut = (t.findP? cut).isSome
Instances For
O(log n)
. Returns true if the given key a
is in the RBSet.
Equations
- t.contains a = (t.find? a).isSome
Instances For
O(n log n)
. Build a tree from an unsorted list by inserting them one at a time.
Equations
- Batteries.RBSet.ofList l cmp = List.foldl (fun (r : Batteries.RBSet α cmp) (p : α) => r.insert p) (Batteries.mkRBSet α cmp) l
Instances For
O(n log n)
. Build a tree from an unsorted array by inserting them one at a time.
Equations
- Batteries.RBSet.ofArray l cmp = Array.foldl (fun (r : Batteries.RBSet α cmp) (p : α) => r.insert p) (Batteries.mkRBSet α cmp) l
Instances For
O(n)
. Returns true if the given predicate is true for all items in the RBSet.
Equations
- t.all p = Batteries.RBNode.all p t.val
Instances For
O(n)
. Returns true if the given predicate is true for any item in the RBSet.
Equations
- t.any p = Batteries.RBNode.any p t.val
Instances For
Asserts that t₁
and t₂
have the same number of elements in the same order,
and R
holds pairwise between them. The tree structure is ignored.
Equations
- Batteries.RBSet.all₂ R t₁ t₂ = Batteries.RBNode.all₂ R t₁.val t₂.val
Instances For
True if x
is an element of t
"exactly", i.e. up to equality, not the cmp
relation.
Equations
- Batteries.RBSet.EMem x t = Batteries.RBNode.EMem x t.val
Instances For
True if the specified cut
matches at least one element of of t
.
Equations
- Batteries.RBSet.MemP cut t = Batteries.RBNode.MemP cut t.val
Instances For
True if x
is equivalent to an element of t
.
Equations
- Batteries.RBSet.Mem x t = Batteries.RBSet.MemP (cmp x) t
Instances For
Equations
- Batteries.RBSet.instMembership = { mem := fun (t : Batteries.RBSet α cmp) (x : α) => Batteries.RBSet.Mem x t }
Equations
- Batteries.RBSet.Slow.instDecidableEMem = inferInstanceAs (Decidable (Batteries.RBNode.Any (fun (x_1 : α) => x = x_1) t.val))
Instances For
Equations
- Batteries.RBSet.Slow.instDecidableMemP = inferInstanceAs (Decidable (Batteries.RBNode.Any (fun (x : α) => cut x = Ordering.eq) t.val))
Instances For
Equations
- Batteries.RBSet.Slow.instDecidableMem = inferInstanceAs (Decidable (Batteries.RBNode.Any (fun (x_1 : α) => cmp x x_1 = Ordering.eq) t.val))
Instances For
Returns true if t₁
and t₂
are equal as sets (assuming cmp
and ==
are compatible),
ignoring the internal tree structure.
Equations
- Batteries.RBSet.instBEq = { beq := fun (a b : Batteries.RBSet α cmp) => Batteries.RBSet.all₂ (fun (x1 x2 : α) => x1 == x2) a b }
O(log n)
. Returns the minimum element of the tree, or panics if the tree is empty.
Equations
- t.min! = t.min?.getD (panicWithPosWithDecl "Batteries.Data.RBMap.Basic" "Batteries.RBSet.min!" 798 71 "tree is empty")
Instances For
O(log n)
. Returns the maximum element of the tree, or panics if the tree is empty.
Equations
- t.max! = t.max?.getD (panicWithPosWithDecl "Batteries.Data.RBMap.Basic" "Batteries.RBSet.max!" 801 71 "tree is empty")
Instances For
O(log n)
. Attempts to find the value with key k : α
in t
and panics if there is no such key.
Equations
- t.findP! cut = (t.findP? cut).getD (panicWithPosWithDecl "Batteries.Data.RBMap.Basic" "Batteries.RBSet.findP!" 807 23 "key is not in the tree")
Instances For
O(log n)
. Attempts to find the value with key k : α
in t
and panics if there is no such key.
Equations
- t.find! k = (t.find? k).getD (panicWithPosWithDecl "Batteries.Data.RBMap.Basic" "Batteries.RBSet.find!" 813 20 "key is not in the tree")
Instances For
The predicate asserting that the result of modifyP
is safe to construct.
- wf : Batteries.RBNode.WF cmp (Batteries.RBNode.modify cut f t.val)
The resulting tree is well formed.
Instances
The resulting tree is well formed.
O(log n)
. In-place replace an element found by cut
.
This takes the element out of the tree while f
runs,
so it uses the element linearly if t
is unshared.
The ModifyWF
assumption is required because f
may change
the ordering properties of the element, which would break the invariants.
Equations
- t.modifyP cut f = ⟨Batteries.RBNode.modify cut f t.val, ⋯⟩
Instances For
The predicate asserting that the result of alterP
is safe to construct.
- wf : Batteries.RBNode.WF cmp (Batteries.RBNode.alter cut f t.val)
The resulting tree is well formed.
Instances
The resulting tree is well formed.
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.alterP cut f = ⟨Batteries.RBNode.alter cut f t.val, ⋯⟩
Instances For
O(n₂ * log (n₁ + n₂))
. Merges the maps t₁
and t₂
.
If equal keys exist in both, the key from t₂
is preferred.
Equations
- t₁.union t₂ = Batteries.RBSet.foldl Batteries.RBSet.insert t₁ t₂
Instances For
Equations
- Batteries.RBSet.instUnion = { union := Batteries.RBSet.union }
O(n₂ * log (n₁ + n₂))
. Merges the maps t₁
and t₂
. If equal keys exist in both,
then use mergeFn a₁ a₂
to produce the new merged value.
Equations
- One or more equations did not get rendered due to their size.
Instances For
O(n₁ * log (n₁ + n₂))
. Intersects the maps t₁
and t₂
using mergeFn a b
to produce the new value.
Equations
- One or more equations did not get rendered due to their size.
Instances For
O(n * log n)
. Constructs the set of all elements satisfying p
.
Equations
- t.filter p = Batteries.RBSet.foldl (fun (acc : Batteries.RBSet α cmp) (a : α) => bif p a then acc.insert a else acc) ∅ t
Instances For
O(n * log n)
. Map a function on every value in the set.
If the function is monotone, consider using the more efficient RBSet.mapMonotone
instead.
Equations
- t.map f = Batteries.RBSet.foldl (fun (acc : Batteries.RBSet β cmpβ) (a : α) => acc.insert (f a)) ∅ t
Instances For
O(n₁ * (log n₁ + log n₂))
. Constructs the set of all elements of t₁
that are not in t₂
.
Instances For
Equations
- Batteries.RBSet.instSDiff = { sdiff := Batteries.RBSet.sdiff }
An RBMap
is a self-balancing binary search tree, used to store a key-value map.
The cmp
function is the comparator that will be used for performing searches;
it should satisfy the requirements of TransCmp
for it to have sensible behavior.
Equations
- Batteries.RBMap α β cmp = Batteries.RBSet (α × β) (Ordering.byKey Prod.fst cmp)
Instances For
O(1)
. Construct a new empty map.
Equations
- Batteries.mkRBMap α β cmp = Batteries.mkRBSet (α × β) (Ordering.byKey Prod.fst cmp)
Instances For
O(1)
. Construct a new empty map.
Equations
- Batteries.RBMap.empty = Batteries.mkRBMap α β cmp
Instances For
Equations
- Batteries.instEmptyCollectionRBMap α β cmp = { emptyCollection := Batteries.RBMap.empty }
Equations
- Batteries.instInhabitedRBMap α β cmp = { default := ∅ }
O(1)
. Construct a new tree with one key-value pair k, v
.
Equations
- Batteries.RBMap.single k v = Batteries.RBSet.single (k, v)
Instances For
O(n)
. Fold a function on the values from left to right (in increasing order).
Equations
- Batteries.RBMap.foldl f x ⟨t, property⟩ = Batteries.RBNode.foldl (fun (s : σ) (x : α × β) => match x with | (a, b) => f s a b) x t
Instances For
O(n)
. Fold a function on the values from right to left (in decreasing order).
Equations
- Batteries.RBMap.foldr f x ⟨t, property⟩ = Batteries.RBNode.foldr (fun (x : α × β) (s : σ) => match x with | (a, b) => f a b s) t x
Instances For
O(n)
. Fold a monadic function on the values from left to right (in increasing order).
Equations
- Batteries.RBMap.foldlM f x ⟨t, property⟩ = Batteries.RBNode.foldlM (fun (s : σ) (x : α × β) => match x with | (a, b) => f s a b) x t
Instances For
O(n)
. Run monadic function f
on each element of the tree (in increasing order).
Equations
- Batteries.RBMap.forM f t = Batteries.RBNode.forM (fun (x : α × β) => match x with | (a, b) => f a b) t.val
Instances For
Equations
- Batteries.RBMap.instForInProd = inferInstanceAs (ForIn m (Batteries.RBSet (α × β) (Ordering.byKey Prod.fst cmp)) (α × β))
Equations
- Batteries.RBMap.instToStreamStreamProd = inferInstanceAs (ToStream (Batteries.RBSet (α × β) (Ordering.byKey Prod.fst cmp)) (Batteries.RBNode.Stream (α × β)))
O(n)
. Constructs the array of keys of the map.
Equations
- t.keysArray = Batteries.RBNode.foldl (fun (x1 : Array α) (x2 : α × β) => x1.push x2.fst) #[] t.val
Instances For
O(n)
. Constructs the list of keys of the map.
Equations
- t.keysList = Batteries.RBNode.foldr (fun (x1 : α × β) (x2 : List α) => x1.fst :: x2) t.val []
Instances For
An "iterator" over the keys of the map. This is a trivial wrapper over the underlying map,
but it comes with a small API to use it in a for
loop or convert it to an array or list.
Equations
- Batteries.RBMap.Keys α β cmp = Batteries.RBMap α β cmp
Instances For
The keys of the map. This is an O(1)
wrapper operation, which
can be used in for
loops or converted to an array or list.
Equations
- t.keys = t
Instances For
O(n)
. Constructs the array of keys of the map.
Instances For
O(n)
. Constructs the list of keys of the map.
Instances For
Equations
- Batteries.RBMap.instCoeHeadKeysArray = { coe := Batteries.RBMap.keysArray }
Equations
- Batteries.RBMap.instCoeHeadKeysList = { coe := Batteries.RBMap.keysList }
Equations
- One or more equations did not get rendered due to their size.
Equations
- Batteries.RBMap.instForMKeys = { forM := fun [Monad m] (t : Batteries.RBMap.Keys α β cmp) (f : α → m PUnit) => Batteries.RBNode.forM (fun (x : α × β) => f x.fst) t.val }
The result of toStream
on a Keys
.
Equations
- Batteries.RBMap.Keys.Stream α β = Batteries.RBNode.Stream (α × β)
Instances For
A stream over the iterator.
Equations
- t.toStream = t.val.toStream
Instances For
O(1)
amortized, O(log n)
worst case: Get the next element from the stream.
Equations
- t.next? = match inline (Batteries.RBNode.Stream.next? t) with | none => none | some ((a, snd), t) => some (a, t)
Instances For
Equations
- Batteries.RBMap.instToStreamKeysStream = { toStream := Batteries.RBMap.Keys.toStream }
Equations
- Batteries.RBMap.instStreamStream = { next? := Batteries.RBMap.Keys.Stream.next? }
O(n)
. Constructs the array of values of the map.
Equations
- t.valuesArray = Batteries.RBNode.foldl (fun (x1 : Array β) (x2 : α × β) => x1.push x2.snd) #[] t.val
Instances For
O(n)
. Constructs the list of values of the map.
Equations
- t.valuesList = Batteries.RBNode.foldr (fun (x1 : α × β) (x2 : List β) => x1.snd :: x2) t.val []
Instances For
An "iterator" over the values of the map. This is a trivial wrapper over the underlying map,
but it comes with a small API to use it in a for
loop or convert it to an array or list.
Equations
- Batteries.RBMap.Values α β cmp = Batteries.RBMap α β cmp
Instances For
The "keys" of the map. This is an O(1)
wrapper operation, which
can be used in for
loops or converted to an array or list.
Equations
- t.values = t
Instances For
O(n)
. Constructs the array of values of the map.
Instances For
O(n)
. Constructs the list of values of the map.
Instances For
Equations
- Batteries.RBMap.instCoeHeadValuesArray = { coe := Batteries.RBMap.valuesArray }
Equations
- Batteries.RBMap.instCoeHeadValuesList = { coe := Batteries.RBMap.valuesList }
Equations
- One or more equations did not get rendered due to their size.
Equations
- Batteries.RBMap.instForMValues = { forM := fun [Monad m] (t : Batteries.RBMap.Values α β cmp) (f : β → m PUnit) => Batteries.RBNode.forM (fun (x : α × β) => f x.snd) t.val }
A stream over the iterator.
Equations
- t.toStream = t.val.toStream
Instances For
O(1)
amortized, O(log n)
worst case: Get the next element from the stream.
Equations
- t.next? = match inline (Batteries.RBNode.Stream.next? t) with | none => none | some ((fst, b), t) => some (b, t)
Instances For
Equations
- Batteries.RBMap.instToStreamValuesStream = { toStream := Batteries.RBMap.Values.toStream }
Equations
- Batteries.RBMap.instStreamStream_1 = { next? := Batteries.RBMap.Values.Stream.next? }
O(1)
. Is the tree empty?
Equations
- Batteries.RBMap.isEmpty = Batteries.RBSet.isEmpty
Instances For
O(n)
. Convert the tree to a list in ascending order.
Equations
- Batteries.RBMap.toList = Batteries.RBSet.toList
Instances For
O(log n)
. Returns the key-value pair (a, b)
such that a ≤ k
for all keys in the RBMap.
Equations
- Batteries.RBMap.min? = Batteries.RBSet.min?
Instances For
O(log n)
. Returns the key-value pair (a, b)
such that a ≥ k
for all keys in the RBMap.
Equations
- Batteries.RBMap.max? = Batteries.RBSet.max?
Instances For
Alias of Batteries.RBMap.min?
.
O(log n)
. Returns the key-value pair (a, b)
such that a ≤ k
for all keys in the RBMap.
Equations
Instances For
Alias of Batteries.RBMap.max?
.
O(log n)
. Returns the key-value pair (a, b)
such that a ≥ k
for all keys in the RBMap.
Equations
Instances For
Equations
- Batteries.RBMap.instRepr = { reprPrec := fun (m : Batteries.RBMap α β cmp) (prec : Nat) => Repr.addAppParen (Std.Format.text "RBMap.ofList " ++ repr m.toList) prec }
O(log n)
. Insert key-value pair (k, v)
into the tree.
Equations
- t.insert k v = Batteries.RBSet.insert t (k, v)
Instances For
O(log n)
. Remove an element k
from the map.
Equations
- t.erase k = Batteries.RBSet.erase t fun (x : α × β) => cmp k x.fst
Instances For
O(n log n)
. Build a tree from an unsorted list by inserting them one at a time.
Equations
- Batteries.RBMap.ofList l cmp = Batteries.RBSet.ofList l (Ordering.byKey Prod.fst cmp)
Instances For
O(n log n)
. Build a tree from an unsorted array by inserting them one at a time.
Equations
- Batteries.RBMap.ofArray l cmp = Batteries.RBSet.ofArray l (Ordering.byKey Prod.fst cmp)
Instances For
O(log n)
. Find an entry in the tree with key equal to k
.
Equations
- t.findEntry? k = Batteries.RBSet.findP? t fun (x : α × β) => cmp k x.fst
Instances For
O(log n)
. Find the value corresponding to key k
.
Equations
- t.find? k = Option.map (fun (x : α × β) => x.snd) (t.findEntry? k)
Instances For
O(log n)
. Find the value corresponding to key k
, or return v₀
if it is not in the map.
Equations
- t.findD k v₀ = (t.find? k).getD v₀
Instances For
O(log n)
. lowerBound? k
retrieves the key-value pair of the largest key
smaller than or equal to k
, if it exists.
Equations
- t.lowerBound? k = Batteries.RBSet.lowerBoundP? t fun (x : α × β) => cmp k x.fst
Instances For
O(log n)
. Returns true if the given key a
is in the RBMap.
Equations
- t.contains a = (t.findEntry? a).isSome
Instances For
O(n)
. Returns true if the given predicate is true for all items in the RBMap.
Equations
- t.all p = Batteries.RBSet.all t fun (x : α × β) => match x with | (a, b) => p a b
Instances For
O(n)
. Returns true if the given predicate is true for any item in the RBMap.
Equations
- t.any p = Batteries.RBSet.any t fun (x : α × β) => match x with | (a, b) => p a b
Instances For
Asserts that t₁
and t₂
have the same number of elements in the same order,
and R
holds pairwise between them. The tree structure is ignored.
Equations
- Batteries.RBMap.all₂ R t₁ t₂ = Batteries.RBSet.all₂ R t₁ t₂
Instances For
Asserts that t₁
and t₂
have the same set of keys (up to equality).
Equations
- t₁.eqKeys t₂ = Batteries.RBMap.all₂ (fun (x1 : α × β) (x2 : α × γ) => decide (cmp x1.fst x2.fst = Ordering.eq)) t₁ t₂
Instances For
Returns true if t₁
and t₂
have the same keys and values
(assuming cmp
and ==
are compatible), ignoring the internal tree structure.
Equations
- Batteries.RBMap.instBEq = inferInstanceAs (BEq (Batteries.RBSet (α × β) (Ordering.byKey Prod.fst cmp)))
O(n)
. The number of items in the RBMap.
Equations
- Batteries.RBMap.size = Batteries.RBSet.size
Instances For
O(log n)
. Returns the minimum element of the map, or panics if the map is empty.
Equations
- Batteries.RBMap.min! = Batteries.RBSet.min!
Instances For
O(log n)
. Returns the maximum element of the map, or panics if the map is empty.
Equations
- Batteries.RBMap.max! = Batteries.RBSet.max!
Instances For
Attempts to find the value with key k : α
in t
and panics if there is no such key.
Equations
- t.find! k = (t.find? k).getD (panicWithPosWithDecl "Batteries.Data.RBMap.Basic" "Batteries.RBMap.find!" 1131 20 "key is not in the map")
Instances For
O(n₂ * log (n₁ + n₂))
. Merges the maps t₁
and t₂
, if a key a : α
exists in both,
then use mergeFn a b₁ b₂
to produce the new merged value.
Equations
- One or more equations did not get rendered due to their size.
Instances For
O(n₁ * log (n₁ + n₂))
. Intersects the maps t₁
and t₂
using mergeFn a b
to produce the new value.
Equations
- One or more equations did not get rendered due to their size.
Instances For
O(n * log n)
. Constructs the set of all elements satisfying p
.
Equations
- t.filter p = Batteries.RBSet.filter t fun (x : α × β) => match x with | (a, b) => p a b
Instances For
O(n₁ * (log n₁ + log n₂))
. Constructs the set of all elements of t₁
that are not in t₂
.
Instances For
O(n log n)
. Build a tree from an unsorted list by inserting them one at a time.
Equations
- l.toRBMap cmp = Batteries.RBMap.ofList l cmp