mirror of
https://github.com/rust-lang/rust.git
synced 2025-06-05 11:48:30 +00:00
BTreeMap: relax the explicit borrow rule to make code shorter and safer
This commit is contained in:
parent
d23e084483
commit
29114ff0fb
@ -248,7 +248,7 @@ where
|
|||||||
let (map, dormant_map) = DormantMutRef::new(self);
|
let (map, dormant_map) = DormantMutRef::new(self);
|
||||||
let root_node = Self::ensure_is_owned(&mut map.root).borrow_mut();
|
let root_node = Self::ensure_is_owned(&mut map.root).borrow_mut();
|
||||||
match search::search_tree::<marker::Mut<'_>, K, (), K>(root_node, &key) {
|
match search::search_tree::<marker::Mut<'_>, K, (), K>(root_node, &key) {
|
||||||
Found(handle) => Some(mem::replace(handle.into_key_mut(), key)),
|
Found(mut kv) => Some(mem::replace(kv.key_mut(), key)),
|
||||||
GoDown(handle) => {
|
GoDown(handle) => {
|
||||||
VacantEntry { key, handle, dormant_map, _marker: PhantomData }.insert(());
|
VacantEntry { key, handle, dormant_map, _marker: PhantomData }.insert(());
|
||||||
None
|
None
|
||||||
|
@ -239,17 +239,16 @@ impl<K, V> NodeRef<marker::Owned, K, V, marker::LeafOrInternal> {
|
|||||||
/// - We cannot get implicit coercion from say `Mut<'a>` to `Immut<'a>`.
|
/// - We cannot get implicit coercion from say `Mut<'a>` to `Immut<'a>`.
|
||||||
/// Therefore, we have to explicitly call `reborrow` on a more powerfull
|
/// Therefore, we have to explicitly call `reborrow` on a more powerfull
|
||||||
/// `NodeRef` in order to reach a method like `key_at`.
|
/// `NodeRef` in order to reach a method like `key_at`.
|
||||||
/// - All methods on `NodeRef` that return some kind of reference, except
|
///
|
||||||
/// `reborrow` and `reborrow_mut`, take `self` by value and not by reference.
|
/// All methods on `NodeRef` that return some kind of reference, either:
|
||||||
/// This avoids silently returning a second reference somewhere in the tree.
|
/// - Take `self` by value, and return the lifetime carried by `BorrowType`.
|
||||||
/// That is irrelevant when `BorrowType` is `Immut<'a>`, but the rule does
|
/// Sometimes, to invoke such a method, we need to call `reborrow_mut`.
|
||||||
/// no harm because we make those `NodeRef` implicitly `Copy`.
|
/// - Take `self` by reference, and (implicitly) return that reference's
|
||||||
/// The rule also avoids implicitly returning the lifetime of `&self`,
|
/// lifetime, instead of the lifetime carried by `BorrowType`. That way,
|
||||||
/// instead of the lifetime carried by `BorrowType`.
|
/// the borrow checker guarantees that the `NodeRef` remains borrowed as long
|
||||||
/// An exception to this rule are the insert functions.
|
/// as the returned reference is used.
|
||||||
/// - Given the above, we need a `reborrow_mut` to explicitly copy a `Mut<'a>`
|
/// The methods supporting insert bend this rule by returning a raw pointer,
|
||||||
/// `NodeRef` whenever we want to invoke a method returning an extra reference
|
/// i.e., a reference without any lifetime.
|
||||||
/// somewhere in the tree.
|
|
||||||
pub struct NodeRef<BorrowType, K, V, Type> {
|
pub struct NodeRef<BorrowType, K, V, Type> {
|
||||||
/// The number of levels that the node and the level of leaves are apart, a
|
/// The number of levels that the node and the level of leaves are apart, a
|
||||||
/// constant of the node that cannot be entirely described by `Type`, and that
|
/// constant of the node that cannot be entirely described by `Type`, and that
|
||||||
@ -305,9 +304,9 @@ impl<'a, K, V> NodeRef<marker::Immut<'a>, K, V, marker::Internal> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl<'a, K, V> NodeRef<marker::Mut<'a>, K, V, marker::Internal> {
|
impl<'a, K, V> NodeRef<marker::Mut<'a>, K, V, marker::Internal> {
|
||||||
/// Offers exclusive access to the data of an internal node.
|
/// Borrows exclusive access to the data of an internal node.
|
||||||
fn as_internal_mut(this: &mut Self) -> &'a mut InternalNode<K, V> {
|
fn as_internal_mut(&mut self) -> &mut InternalNode<K, V> {
|
||||||
let ptr = Self::as_internal_ptr(this);
|
let ptr = Self::as_internal_ptr(self);
|
||||||
unsafe { &mut *ptr }
|
unsafe { &mut *ptr }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -355,7 +354,7 @@ impl<'a, K: 'a, V: 'a, Type> NodeRef<marker::Immut<'a>, K, V, Type> {
|
|||||||
/// The node has more than `idx` initialized elements.
|
/// The node has more than `idx` initialized elements.
|
||||||
pub unsafe fn key_at(self, idx: usize) -> &'a K {
|
pub unsafe fn key_at(self, idx: usize) -> &'a K {
|
||||||
debug_assert!(idx < self.len());
|
debug_assert!(idx < self.len());
|
||||||
unsafe { Self::as_leaf(&self).keys.get_unchecked(idx).assume_init_ref() }
|
unsafe { self.into_leaf().keys.get_unchecked(idx).assume_init_ref() }
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Exposes one of the values stored in the node.
|
/// Exposes one of the values stored in the node.
|
||||||
@ -364,7 +363,7 @@ impl<'a, K: 'a, V: 'a, Type> NodeRef<marker::Immut<'a>, K, V, Type> {
|
|||||||
/// The node has more than `idx` initialized elements.
|
/// The node has more than `idx` initialized elements.
|
||||||
unsafe fn val_at(self, idx: usize) -> &'a V {
|
unsafe fn val_at(self, idx: usize) -> &'a V {
|
||||||
debug_assert!(idx < self.len());
|
debug_assert!(idx < self.len());
|
||||||
unsafe { Self::as_leaf(&self).vals.get_unchecked(idx).assume_init_ref() }
|
unsafe { self.into_leaf().vals.get_unchecked(idx).assume_init_ref() }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -431,8 +430,8 @@ impl<BorrowType, K, V, Type> NodeRef<BorrowType, K, V, Type> {
|
|||||||
|
|
||||||
impl<'a, K: 'a, V: 'a, Type> NodeRef<marker::Immut<'a>, K, V, Type> {
|
impl<'a, K: 'a, V: 'a, Type> NodeRef<marker::Immut<'a>, K, V, Type> {
|
||||||
/// Exposes the leaf portion of any leaf or internal node in an immutable tree.
|
/// Exposes the leaf portion of any leaf or internal node in an immutable tree.
|
||||||
fn as_leaf(this: &Self) -> &'a LeafNode<K, V> {
|
fn into_leaf(self) -> &'a LeafNode<K, V> {
|
||||||
let ptr = Self::as_leaf_ptr(this);
|
let ptr = Self::as_leaf_ptr(&self);
|
||||||
// SAFETY: there can be no mutable references into this tree borrowed as `Immut`.
|
// SAFETY: there can be no mutable references into this tree borrowed as `Immut`.
|
||||||
unsafe { &*ptr }
|
unsafe { &*ptr }
|
||||||
}
|
}
|
||||||
@ -489,42 +488,49 @@ impl<'a, K, V, Type> NodeRef<marker::Mut<'a>, K, V, Type> {
|
|||||||
NodeRef { height: self.height, node: self.node, _marker: PhantomData }
|
NodeRef { height: self.height, node: self.node, _marker: PhantomData }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Borrows exclusive access to the leaf portion of any leaf or internal node.
|
||||||
|
fn as_leaf_mut(&mut self) -> &mut LeafNode<K, V> {
|
||||||
|
let ptr = Self::as_leaf_ptr(self);
|
||||||
|
// SAFETY: we have exclusive access to the entire node.
|
||||||
|
unsafe { &mut *ptr }
|
||||||
|
}
|
||||||
|
|
||||||
/// Offers exclusive access to the leaf portion of any leaf or internal node.
|
/// Offers exclusive access to the leaf portion of any leaf or internal node.
|
||||||
fn as_leaf_mut(this: &mut Self) -> &'a mut LeafNode<K, V> {
|
fn into_leaf_mut(mut self) -> &'a mut LeafNode<K, V> {
|
||||||
let ptr = Self::as_leaf_ptr(this);
|
let ptr = Self::as_leaf_ptr(&mut self);
|
||||||
// SAFETY: we have exclusive access to the entire node.
|
// SAFETY: we have exclusive access to the entire node.
|
||||||
unsafe { &mut *ptr }
|
unsafe { &mut *ptr }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a, K: 'a, V: 'a, Type> NodeRef<marker::Mut<'a>, K, V, Type> {
|
impl<'a, K: 'a, V: 'a, Type> NodeRef<marker::Mut<'a>, K, V, Type> {
|
||||||
/// Offers exclusive access to a part of the key storage area.
|
/// Borrows exclusive access to an element of the key storage area.
|
||||||
///
|
///
|
||||||
/// # Safety
|
/// # Safety
|
||||||
/// The node has more than `idx` initialized elements.
|
/// The node has more than `idx` initialized elements.
|
||||||
unsafe fn into_key_area_mut_at(mut self, idx: usize) -> &'a mut MaybeUninit<K> {
|
unsafe fn key_area_mut_at(&mut self, idx: usize) -> &mut MaybeUninit<K> {
|
||||||
debug_assert!(idx < self.len());
|
debug_assert!(idx < self.len());
|
||||||
unsafe { Self::as_leaf_mut(&mut self).keys.get_unchecked_mut(idx) }
|
unsafe { self.as_leaf_mut().keys.get_unchecked_mut(idx) }
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Offers exclusive access to a part of the value storage area.
|
/// Borrows exclusive access to an element of the value storage area.
|
||||||
///
|
///
|
||||||
/// # Safety
|
/// # Safety
|
||||||
/// The node has more than `idx` initialized elements.
|
/// The node has more than `idx` initialized elements.
|
||||||
unsafe fn into_val_area_mut_at(mut self, idx: usize) -> &'a mut MaybeUninit<V> {
|
unsafe fn val_area_mut_at(&mut self, idx: usize) -> &mut MaybeUninit<V> {
|
||||||
debug_assert!(idx < self.len());
|
debug_assert!(idx < self.len());
|
||||||
unsafe { Self::as_leaf_mut(&mut self).vals.get_unchecked_mut(idx) }
|
unsafe { self.as_leaf_mut().vals.get_unchecked_mut(idx) }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a, K: 'a, V: 'a> NodeRef<marker::Mut<'a>, K, V, marker::Internal> {
|
impl<'a, K: 'a, V: 'a> NodeRef<marker::Mut<'a>, K, V, marker::Internal> {
|
||||||
/// Offers exclusive access to a part of the storage area for edge contents.
|
/// Borrows exclusive access to an element of the storage area for edge contents.
|
||||||
///
|
///
|
||||||
/// # Safety
|
/// # Safety
|
||||||
/// The node has at least `idx` initialized elements.
|
/// The node has at least `idx` initialized elements.
|
||||||
unsafe fn into_edge_area_mut_at(mut self, idx: usize) -> &'a mut MaybeUninit<BoxedNode<K, V>> {
|
unsafe fn edge_area_mut_at(&mut self, idx: usize) -> &mut MaybeUninit<BoxedNode<K, V>> {
|
||||||
debug_assert!(idx <= self.len());
|
debug_assert!(idx <= self.len());
|
||||||
unsafe { Self::as_internal_mut(&mut self).edges.get_unchecked_mut(idx) }
|
unsafe { self.as_internal_mut().edges.get_unchecked_mut(idx) }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -533,14 +539,14 @@ impl<'a, K: 'a, V: 'a, Type> NodeRef<marker::Immut<'a>, K, V, Type> {
|
|||||||
/// regardless of the node's current length,
|
/// regardless of the node's current length,
|
||||||
/// having exclusive access to the entire node.
|
/// having exclusive access to the entire node.
|
||||||
unsafe fn key_area(self) -> &'a [MaybeUninit<K>] {
|
unsafe fn key_area(self) -> &'a [MaybeUninit<K>] {
|
||||||
Self::as_leaf(&self).keys.as_slice()
|
self.into_leaf().keys.as_slice()
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Exposes the entire value storage area in the node,
|
/// Exposes the entire value storage area in the node,
|
||||||
/// regardless of the node's current length,
|
/// regardless of the node's current length,
|
||||||
/// having exclusive access to the entire node.
|
/// having exclusive access to the entire node.
|
||||||
unsafe fn val_area(self) -> &'a [MaybeUninit<V>] {
|
unsafe fn val_area(self) -> &'a [MaybeUninit<V>] {
|
||||||
Self::as_leaf(&self).vals.as_slice()
|
self.into_leaf().vals.as_slice()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -554,33 +560,33 @@ impl<'a, K: 'a, V: 'a> NodeRef<marker::Immut<'a>, K, V, marker::Internal> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl<'a, K: 'a, V: 'a, Type> NodeRef<marker::Mut<'a>, K, V, Type> {
|
impl<'a, K: 'a, V: 'a, Type> NodeRef<marker::Mut<'a>, K, V, Type> {
|
||||||
/// Offers exclusive access to a sized slice of key storage area in the node.
|
/// Borrows exclusive access to a sized slice of key storage area in the node.
|
||||||
unsafe fn into_key_area_slice(mut self) -> &'a mut [MaybeUninit<K>] {
|
unsafe fn key_area_slice(&mut self) -> &mut [MaybeUninit<K>] {
|
||||||
let len = self.len();
|
let len = self.len();
|
||||||
// SAFETY: the caller will not be able to call further methods on self
|
// SAFETY: the caller will not be able to call further methods on self
|
||||||
// until the key slice reference is dropped, as we have unique access
|
// until the key slice reference is dropped, as we have unique access
|
||||||
// for the lifetime of the borrow.
|
// for the lifetime of the borrow.
|
||||||
unsafe { Self::as_leaf_mut(&mut self).keys.get_unchecked_mut(..len) }
|
unsafe { self.as_leaf_mut().keys.get_unchecked_mut(..len) }
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Offers exclusive access to a sized slice of value storage area in the node.
|
/// Borrows exclusive access to a sized slice of value storage area in the node.
|
||||||
unsafe fn into_val_area_slice(mut self) -> &'a mut [MaybeUninit<V>] {
|
unsafe fn val_area_slice(&mut self) -> &mut [MaybeUninit<V>] {
|
||||||
let len = self.len();
|
let len = self.len();
|
||||||
// SAFETY: the caller will not be able to call further methods on self
|
// SAFETY: the caller will not be able to call further methods on self
|
||||||
// until the value slice reference is dropped, as we have unique access
|
// until the value slice reference is dropped, as we have unique access
|
||||||
// for the lifetime of the borrow.
|
// for the lifetime of the borrow.
|
||||||
unsafe { Self::as_leaf_mut(&mut self).vals.get_unchecked_mut(..len) }
|
unsafe { self.as_leaf_mut().vals.get_unchecked_mut(..len) }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a, K: 'a, V: 'a> NodeRef<marker::Mut<'a>, K, V, marker::Internal> {
|
impl<'a, K: 'a, V: 'a> NodeRef<marker::Mut<'a>, K, V, marker::Internal> {
|
||||||
/// Offers exclusive access to a sized slice of storage area for edge contents in the node.
|
/// Borrows exclusive access to a sized slice of storage area for edge contents in the node.
|
||||||
unsafe fn into_edge_area_slice(mut self) -> &'a mut [MaybeUninit<BoxedNode<K, V>>] {
|
unsafe fn edge_area_slice(&mut self) -> &mut [MaybeUninit<BoxedNode<K, V>>] {
|
||||||
let len = self.len();
|
let len = self.len();
|
||||||
// SAFETY: the caller will not be able to call further methods on self
|
// SAFETY: the caller will not be able to call further methods on self
|
||||||
// until the edge slice reference is dropped, as we have unique access
|
// until the edge slice reference is dropped, as we have unique access
|
||||||
// for the lifetime of the borrow.
|
// for the lifetime of the borrow.
|
||||||
unsafe { Self::as_internal_mut(&mut self).edges.get_unchecked_mut(..len + 1) }
|
unsafe { self.as_internal_mut().edges.get_unchecked_mut(..len + 1) }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -604,9 +610,9 @@ impl<'a, K, V, Type> NodeRef<marker::ValMut<'a>, K, V, Type> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl<'a, K: 'a, V: 'a, Type> NodeRef<marker::Mut<'a>, K, V, Type> {
|
impl<'a, K: 'a, V: 'a, Type> NodeRef<marker::Mut<'a>, K, V, Type> {
|
||||||
/// Exposes exclusive access to the length of the node.
|
/// Borrows exclusive access to the length of the node.
|
||||||
pub fn into_len_mut(mut self) -> &'a mut u16 {
|
pub fn len_mut(&mut self) -> &mut u16 {
|
||||||
&mut (*Self::as_leaf_mut(&mut self)).len
|
&mut self.as_leaf_mut().len
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -623,7 +629,8 @@ impl<'a, K: 'a, V: 'a> NodeRef<marker::Mut<'a>, K, V, marker::LeafOrInternal> {
|
|||||||
impl<K, V> NodeRef<marker::Owned, K, V, marker::LeafOrInternal> {
|
impl<K, V> NodeRef<marker::Owned, K, V, marker::LeafOrInternal> {
|
||||||
/// Clears the root's link to its parent edge.
|
/// Clears the root's link to its parent edge.
|
||||||
fn clear_parent_link(&mut self) {
|
fn clear_parent_link(&mut self) {
|
||||||
let leaf = NodeRef::as_leaf_mut(&mut self.borrow_mut());
|
let mut root_node = self.borrow_mut();
|
||||||
|
let leaf = root_node.as_leaf_mut();
|
||||||
leaf.parent = None;
|
leaf.parent = None;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -631,13 +638,13 @@ impl<K, V> NodeRef<marker::Owned, K, V, marker::LeafOrInternal> {
|
|||||||
impl<'a, K: 'a, V: 'a> NodeRef<marker::Mut<'a>, K, V, marker::Leaf> {
|
impl<'a, K: 'a, V: 'a> NodeRef<marker::Mut<'a>, K, V, marker::Leaf> {
|
||||||
/// Adds a key-value pair to the end of the node.
|
/// Adds a key-value pair to the end of the node.
|
||||||
pub fn push(&mut self, key: K, val: V) {
|
pub fn push(&mut self, key: K, val: V) {
|
||||||
let len = unsafe { self.reborrow_mut().into_len_mut() };
|
let len = self.len_mut();
|
||||||
let idx = usize::from(*len);
|
let idx = usize::from(*len);
|
||||||
assert!(idx < CAPACITY);
|
assert!(idx < CAPACITY);
|
||||||
*len += 1;
|
*len += 1;
|
||||||
unsafe {
|
unsafe {
|
||||||
self.reborrow_mut().into_key_area_mut_at(idx).write(key);
|
self.key_area_mut_at(idx).write(key);
|
||||||
self.reborrow_mut().into_val_area_mut_at(idx).write(val);
|
self.val_area_mut_at(idx).write(val);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -646,9 +653,9 @@ impl<'a, K: 'a, V: 'a> NodeRef<marker::Mut<'a>, K, V, marker::Leaf> {
|
|||||||
assert!(self.len() < CAPACITY);
|
assert!(self.len() < CAPACITY);
|
||||||
|
|
||||||
unsafe {
|
unsafe {
|
||||||
*self.reborrow_mut().into_len_mut() += 1;
|
*self.len_mut() += 1;
|
||||||
slice_insert(self.reborrow_mut().into_key_area_slice(), 0, key);
|
slice_insert(self.key_area_slice(), 0, key);
|
||||||
slice_insert(self.reborrow_mut().into_val_area_slice(), 0, val);
|
slice_insert(self.val_area_slice(), 0, val);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -675,14 +682,14 @@ impl<'a, K: 'a, V: 'a> NodeRef<marker::Mut<'a>, K, V, marker::Internal> {
|
|||||||
pub fn push(&mut self, key: K, val: V, edge: Root<K, V>) {
|
pub fn push(&mut self, key: K, val: V, edge: Root<K, V>) {
|
||||||
assert!(edge.height == self.height - 1);
|
assert!(edge.height == self.height - 1);
|
||||||
|
|
||||||
let len = unsafe { self.reborrow_mut().into_len_mut() };
|
let len = self.len_mut();
|
||||||
let idx = usize::from(*len);
|
let idx = usize::from(*len);
|
||||||
assert!(idx < CAPACITY);
|
assert!(idx < CAPACITY);
|
||||||
*len += 1;
|
*len += 1;
|
||||||
unsafe {
|
unsafe {
|
||||||
self.reborrow_mut().into_key_area_mut_at(idx).write(key);
|
self.key_area_mut_at(idx).write(key);
|
||||||
self.reborrow_mut().into_val_area_mut_at(idx).write(val);
|
self.val_area_mut_at(idx).write(val);
|
||||||
self.reborrow_mut().into_edge_area_mut_at(idx + 1).write(edge.node);
|
self.edge_area_mut_at(idx + 1).write(edge.node);
|
||||||
Handle::new_edge(self.reborrow_mut(), idx + 1).correct_parent_link();
|
Handle::new_edge(self.reborrow_mut(), idx + 1).correct_parent_link();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -694,10 +701,10 @@ impl<'a, K: 'a, V: 'a> NodeRef<marker::Mut<'a>, K, V, marker::Internal> {
|
|||||||
assert!(self.len() < CAPACITY);
|
assert!(self.len() < CAPACITY);
|
||||||
|
|
||||||
unsafe {
|
unsafe {
|
||||||
*self.reborrow_mut().into_len_mut() += 1;
|
*self.len_mut() += 1;
|
||||||
slice_insert(self.reborrow_mut().into_key_area_slice(), 0, key);
|
slice_insert(self.key_area_slice(), 0, key);
|
||||||
slice_insert(self.reborrow_mut().into_val_area_slice(), 0, val);
|
slice_insert(self.val_area_slice(), 0, val);
|
||||||
slice_insert(self.reborrow_mut().into_edge_area_slice(), 0, edge.node);
|
slice_insert(self.edge_area_slice(), 0, edge.node);
|
||||||
}
|
}
|
||||||
|
|
||||||
self.correct_all_childrens_parent_links();
|
self.correct_all_childrens_parent_links();
|
||||||
@ -728,7 +735,7 @@ impl<'a, K: 'a, V: 'a> NodeRef<marker::Mut<'a>, K, V, marker::LeafOrInternal> {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
*self.reborrow_mut().into_len_mut() -= 1;
|
*self.len_mut() -= 1;
|
||||||
(key, val, edge)
|
(key, val, edge)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -742,12 +749,12 @@ impl<'a, K: 'a, V: 'a> NodeRef<marker::Mut<'a>, K, V, marker::LeafOrInternal> {
|
|||||||
let old_len = self.len();
|
let old_len = self.len();
|
||||||
|
|
||||||
unsafe {
|
unsafe {
|
||||||
let key = slice_remove(self.reborrow_mut().into_key_area_slice(), 0);
|
let key = slice_remove(self.key_area_slice(), 0);
|
||||||
let val = slice_remove(self.reborrow_mut().into_val_area_slice(), 0);
|
let val = slice_remove(self.val_area_slice(), 0);
|
||||||
let edge = match self.reborrow_mut().force() {
|
let edge = match self.reborrow_mut().force() {
|
||||||
ForceResult::Leaf(_) => None,
|
ForceResult::Leaf(_) => None,
|
||||||
ForceResult::Internal(mut internal) => {
|
ForceResult::Internal(mut internal) => {
|
||||||
let node = slice_remove(internal.reborrow_mut().into_edge_area_slice(), 0);
|
let node = slice_remove(internal.edge_area_slice(), 0);
|
||||||
let mut edge = Root { node, height: internal.height - 1, _marker: PhantomData };
|
let mut edge = Root { node, height: internal.height - 1, _marker: PhantomData };
|
||||||
// Currently, clearing the parent link is superfluous, because we will
|
// Currently, clearing the parent link is superfluous, because we will
|
||||||
// insert the node elsewhere and set its parent link again.
|
// insert the node elsewhere and set its parent link again.
|
||||||
@ -759,14 +766,14 @@ impl<'a, K: 'a, V: 'a> NodeRef<marker::Mut<'a>, K, V, marker::LeafOrInternal> {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
*self.reborrow_mut().into_len_mut() -= 1;
|
*self.len_mut() -= 1;
|
||||||
|
|
||||||
(key, val, edge)
|
(key, val, edge)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn into_kv_pointers_mut(mut self) -> (*mut K, *mut V) {
|
fn into_kv_pointers_mut(mut self) -> (*mut K, *mut V) {
|
||||||
let leaf = Self::as_leaf_mut(&mut self);
|
let leaf = self.as_leaf_mut();
|
||||||
let keys = MaybeUninit::slice_as_mut_ptr(&mut leaf.keys);
|
let keys = MaybeUninit::slice_as_mut_ptr(&mut leaf.keys);
|
||||||
let vals = MaybeUninit::slice_as_mut_ptr(&mut leaf.vals);
|
let vals = MaybeUninit::slice_as_mut_ptr(&mut leaf.vals);
|
||||||
(keys, vals)
|
(keys, vals)
|
||||||
@ -970,11 +977,11 @@ impl<'a, K: 'a, V: 'a> Handle<NodeRef<marker::Mut<'a>, K, V, marker::Leaf>, mark
|
|||||||
debug_assert!(self.node.len() < CAPACITY);
|
debug_assert!(self.node.len() < CAPACITY);
|
||||||
|
|
||||||
unsafe {
|
unsafe {
|
||||||
*self.node.reborrow_mut().into_len_mut() += 1;
|
*self.node.len_mut() += 1;
|
||||||
slice_insert(self.node.reborrow_mut().into_key_area_slice(), self.idx, key);
|
slice_insert(self.node.key_area_slice(), self.idx, key);
|
||||||
slice_insert(self.node.reborrow_mut().into_val_area_slice(), self.idx, val);
|
slice_insert(self.node.val_area_slice(), self.idx, val);
|
||||||
|
|
||||||
self.node.reborrow_mut().into_val_area_mut_at(self.idx).assume_init_mut()
|
self.node.val_area_mut_at(self.idx).assume_init_mut()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -1028,10 +1035,10 @@ impl<'a, K: 'a, V: 'a> Handle<NodeRef<marker::Mut<'a>, K, V, marker::Internal>,
|
|||||||
debug_assert!(edge.height == self.node.height - 1);
|
debug_assert!(edge.height == self.node.height - 1);
|
||||||
|
|
||||||
unsafe {
|
unsafe {
|
||||||
*self.node.reborrow_mut().into_len_mut() += 1;
|
*self.node.len_mut() += 1;
|
||||||
slice_insert(self.node.reborrow_mut().into_key_area_slice(), self.idx, key);
|
slice_insert(self.node.key_area_slice(), self.idx, key);
|
||||||
slice_insert(self.node.reborrow_mut().into_val_area_slice(), self.idx, val);
|
slice_insert(self.node.val_area_slice(), self.idx, val);
|
||||||
slice_insert(self.node.reborrow_mut().into_edge_area_slice(), self.idx + 1, edge.node);
|
slice_insert(self.node.edge_area_slice(), self.idx + 1, edge.node);
|
||||||
|
|
||||||
self.node.correct_childrens_parent_links((self.idx + 1)..=self.node.len());
|
self.node.correct_childrens_parent_links((self.idx + 1)..=self.node.len());
|
||||||
}
|
}
|
||||||
@ -1134,12 +1141,13 @@ impl<'a, K: 'a, V: 'a, NodeType> Handle<NodeRef<marker::Immut<'a>, K, V, NodeTyp
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl<'a, K: 'a, V: 'a, NodeType> Handle<NodeRef<marker::Mut<'a>, K, V, NodeType>, marker::KV> {
|
impl<'a, K: 'a, V: 'a, NodeType> Handle<NodeRef<marker::Mut<'a>, K, V, NodeType>, marker::KV> {
|
||||||
pub fn into_key_mut(self) -> &'a mut K {
|
pub fn key_mut(&mut self) -> &mut K {
|
||||||
unsafe { self.node.into_key_area_mut_at(self.idx).assume_init_mut() }
|
unsafe { self.node.key_area_mut_at(self.idx).assume_init_mut() }
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn into_val_mut(self) -> &'a mut V {
|
pub fn into_val_mut(self) -> &'a mut V {
|
||||||
unsafe { self.node.into_val_area_mut_at(self.idx).assume_init_mut() }
|
let leaf = self.node.into_leaf_mut();
|
||||||
|
unsafe { leaf.vals.get_unchecked_mut(self.idx).assume_init_mut() }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1154,7 +1162,7 @@ impl<'a, K: 'a, V: 'a, NodeType> Handle<NodeRef<marker::Mut<'a>, K, V, NodeType>
|
|||||||
// We cannot call separate key and value methods, because calling the second one
|
// We cannot call separate key and value methods, because calling the second one
|
||||||
// invalidates the reference returned by the first.
|
// invalidates the reference returned by the first.
|
||||||
unsafe {
|
unsafe {
|
||||||
let leaf = NodeRef::as_leaf_mut(&mut self.node.reborrow_mut());
|
let leaf = self.node.as_leaf_mut();
|
||||||
let key = leaf.keys.get_unchecked_mut(self.idx).assume_init_mut();
|
let key = leaf.keys.get_unchecked_mut(self.idx).assume_init_mut();
|
||||||
let val = leaf.vals.get_unchecked_mut(self.idx).assume_init_mut();
|
let val = leaf.vals.get_unchecked_mut(self.idx).assume_init_mut();
|
||||||
(key, val)
|
(key, val)
|
||||||
@ -1196,7 +1204,7 @@ impl<'a, K: 'a, V: 'a, NodeType> Handle<NodeRef<marker::Mut<'a>, K, V, NodeType>
|
|||||||
new_len,
|
new_len,
|
||||||
);
|
);
|
||||||
|
|
||||||
*self.node.reborrow_mut().into_len_mut() = self.idx as u16;
|
*self.node.len_mut() = self.idx as u16;
|
||||||
(k, v)
|
(k, v)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -1227,9 +1235,9 @@ impl<'a, K: 'a, V: 'a> Handle<NodeRef<marker::Mut<'a>, K, V, marker::Leaf>, mark
|
|||||||
mut self,
|
mut self,
|
||||||
) -> ((K, V), Handle<NodeRef<marker::Mut<'a>, K, V, marker::Leaf>, marker::Edge>) {
|
) -> ((K, V), Handle<NodeRef<marker::Mut<'a>, K, V, marker::Leaf>, marker::Edge>) {
|
||||||
unsafe {
|
unsafe {
|
||||||
let k = slice_remove(self.node.reborrow_mut().into_key_area_slice(), self.idx);
|
let k = slice_remove(self.node.key_area_slice(), self.idx);
|
||||||
let v = slice_remove(self.node.reborrow_mut().into_val_area_slice(), self.idx);
|
let v = slice_remove(self.node.val_area_slice(), self.idx);
|
||||||
*self.node.reborrow_mut().into_len_mut() -= 1;
|
*self.node.len_mut() -= 1;
|
||||||
((k, v), self.left_edge())
|
((k, v), self.left_edge())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -1374,29 +1382,27 @@ impl<'a, K: 'a, V: 'a> BalancingContext<'a, K, V> {
|
|||||||
});
|
});
|
||||||
|
|
||||||
unsafe {
|
unsafe {
|
||||||
*left_node.reborrow_mut().into_len_mut() = new_left_len as u16;
|
*left_node.len_mut() = new_left_len as u16;
|
||||||
|
|
||||||
let parent_key =
|
let parent_key = slice_remove(parent_node.key_area_slice(), parent_idx);
|
||||||
slice_remove(parent_node.reborrow_mut().into_key_area_slice(), parent_idx);
|
left_node.key_area_mut_at(old_left_len).write(parent_key);
|
||||||
left_node.reborrow_mut().into_key_area_mut_at(old_left_len).write(parent_key);
|
|
||||||
ptr::copy_nonoverlapping(
|
ptr::copy_nonoverlapping(
|
||||||
right_node.reborrow().key_area().as_ptr(),
|
right_node.reborrow().key_area().as_ptr(),
|
||||||
left_node.reborrow_mut().into_key_area_slice().as_mut_ptr().add(old_left_len + 1),
|
left_node.key_area_slice().as_mut_ptr().add(old_left_len + 1),
|
||||||
right_len,
|
right_len,
|
||||||
);
|
);
|
||||||
|
|
||||||
let parent_val =
|
let parent_val = slice_remove(parent_node.val_area_slice(), parent_idx);
|
||||||
slice_remove(parent_node.reborrow_mut().into_val_area_slice(), parent_idx);
|
left_node.val_area_mut_at(old_left_len).write(parent_val);
|
||||||
left_node.reborrow_mut().into_val_area_mut_at(old_left_len).write(parent_val);
|
|
||||||
ptr::copy_nonoverlapping(
|
ptr::copy_nonoverlapping(
|
||||||
right_node.reborrow().val_area().as_ptr(),
|
right_node.reborrow().val_area().as_ptr(),
|
||||||
left_node.reborrow_mut().into_val_area_slice().as_mut_ptr().add(old_left_len + 1),
|
left_node.val_area_slice().as_mut_ptr().add(old_left_len + 1),
|
||||||
right_len,
|
right_len,
|
||||||
);
|
);
|
||||||
|
|
||||||
slice_remove(&mut parent_node.reborrow_mut().into_edge_area_slice(), parent_idx + 1);
|
slice_remove(&mut parent_node.edge_area_slice(), parent_idx + 1);
|
||||||
parent_node.correct_childrens_parent_links(parent_idx + 1..old_parent_len);
|
parent_node.correct_childrens_parent_links(parent_idx + 1..old_parent_len);
|
||||||
*parent_node.reborrow_mut().into_len_mut() -= 1;
|
*parent_node.len_mut() -= 1;
|
||||||
|
|
||||||
if parent_node.height > 1 {
|
if parent_node.height > 1 {
|
||||||
// SAFETY: the height of the nodes being merged is one below the height
|
// SAFETY: the height of the nodes being merged is one below the height
|
||||||
@ -1405,11 +1411,7 @@ impl<'a, K: 'a, V: 'a> BalancingContext<'a, K, V> {
|
|||||||
let right_node = right_node.cast_to_internal_unchecked();
|
let right_node = right_node.cast_to_internal_unchecked();
|
||||||
ptr::copy_nonoverlapping(
|
ptr::copy_nonoverlapping(
|
||||||
right_node.reborrow().edge_area().as_ptr(),
|
right_node.reborrow().edge_area().as_ptr(),
|
||||||
left_node
|
left_node.edge_area_slice().as_mut_ptr().add(old_left_len + 1),
|
||||||
.reborrow_mut()
|
|
||||||
.into_edge_area_slice()
|
|
||||||
.as_mut_ptr()
|
|
||||||
.add(old_left_len + 1),
|
|
||||||
right_len + 1,
|
right_len + 1,
|
||||||
);
|
);
|
||||||
|
|
||||||
@ -1511,14 +1513,14 @@ impl<'a, K: 'a, V: 'a> BalancingContext<'a, K, V> {
|
|||||||
move_kv(left_kv, new_left_len, parent_kv, 0, 1);
|
move_kv(left_kv, new_left_len, parent_kv, 0, 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
*left_node.reborrow_mut().into_len_mut() -= count as u16;
|
*left_node.len_mut() -= count as u16;
|
||||||
*right_node.reborrow_mut().into_len_mut() += count as u16;
|
*right_node.len_mut() += count as u16;
|
||||||
|
|
||||||
match (left_node.reborrow_mut().force(), right_node.reborrow_mut().force()) {
|
match (left_node.reborrow_mut().force(), right_node.reborrow_mut().force()) {
|
||||||
(ForceResult::Internal(left), ForceResult::Internal(mut right)) => {
|
(ForceResult::Internal(left), ForceResult::Internal(mut right)) => {
|
||||||
// Make room for stolen edges.
|
// Make room for stolen edges.
|
||||||
let left = left.reborrow();
|
let left = left.reborrow();
|
||||||
let right_edges = right.reborrow_mut().into_edge_area_slice().as_mut_ptr();
|
let right_edges = right.edge_area_slice().as_mut_ptr();
|
||||||
ptr::copy(right_edges, right_edges.add(count), old_right_len + 1);
|
ptr::copy(right_edges, right_edges.add(count), old_right_len + 1);
|
||||||
right.correct_childrens_parent_links(count..count + old_right_len + 1);
|
right.correct_childrens_parent_links(count..count + old_right_len + 1);
|
||||||
|
|
||||||
@ -1569,8 +1571,8 @@ impl<'a, K: 'a, V: 'a> BalancingContext<'a, K, V> {
|
|||||||
ptr::copy(right_kv.1.add(count), right_kv.1, new_right_len);
|
ptr::copy(right_kv.1.add(count), right_kv.1, new_right_len);
|
||||||
}
|
}
|
||||||
|
|
||||||
*left_node.reborrow_mut().into_len_mut() += count as u16;
|
*left_node.len_mut() += count as u16;
|
||||||
*right_node.reborrow_mut().into_len_mut() -= count as u16;
|
*right_node.len_mut() -= count as u16;
|
||||||
|
|
||||||
match (left_node.reborrow_mut().force(), right_node.reborrow_mut().force()) {
|
match (left_node.reborrow_mut().force(), right_node.reborrow_mut().force()) {
|
||||||
(ForceResult::Internal(left), ForceResult::Internal(mut right)) => {
|
(ForceResult::Internal(left), ForceResult::Internal(mut right)) => {
|
||||||
@ -1578,7 +1580,7 @@ impl<'a, K: 'a, V: 'a> BalancingContext<'a, K, V> {
|
|||||||
move_edges(right.reborrow(), 0, left, old_left_len + 1, count);
|
move_edges(right.reborrow(), 0, left, old_left_len + 1, count);
|
||||||
|
|
||||||
// Fill gap where stolen edges used to be.
|
// Fill gap where stolen edges used to be.
|
||||||
let right_edges = right.reborrow_mut().into_edge_area_slice().as_mut_ptr();
|
let right_edges = right.edge_area_slice().as_mut_ptr();
|
||||||
ptr::copy(right_edges.add(count), right_edges, new_right_len + 1);
|
ptr::copy(right_edges.add(count), right_edges, new_right_len + 1);
|
||||||
right.correct_childrens_parent_links(0..=new_right_len);
|
right.correct_childrens_parent_links(0..=new_right_len);
|
||||||
}
|
}
|
||||||
@ -1612,7 +1614,7 @@ unsafe fn move_edges<'a, K: 'a, V: 'a>(
|
|||||||
) {
|
) {
|
||||||
unsafe {
|
unsafe {
|
||||||
let source_ptr = source.edge_area().as_ptr();
|
let source_ptr = source.edge_area().as_ptr();
|
||||||
let dest_ptr = dest.reborrow_mut().into_edge_area_slice().as_mut_ptr();
|
let dest_ptr = dest.edge_area_slice().as_mut_ptr();
|
||||||
ptr::copy_nonoverlapping(source_ptr.add(source_offset), dest_ptr.add(dest_offset), count);
|
ptr::copy_nonoverlapping(source_ptr.add(source_offset), dest_ptr.add(dest_offset), count);
|
||||||
dest.correct_childrens_parent_links(dest_offset..dest_offset + count);
|
dest.correct_childrens_parent_links(dest_offset..dest_offset + count);
|
||||||
}
|
}
|
||||||
@ -1708,8 +1710,8 @@ impl<'a, K, V> Handle<NodeRef<marker::Mut<'a>, K, V, marker::LeafOrInternal>, ma
|
|||||||
|
|
||||||
move_kv(left_kv, new_left_len, right_kv, 0, new_right_len);
|
move_kv(left_kv, new_left_len, right_kv, 0, new_right_len);
|
||||||
|
|
||||||
*left_node.reborrow_mut().into_len_mut() = new_left_len as u16;
|
*left_node.len_mut() = new_left_len as u16;
|
||||||
*right_node.reborrow_mut().into_len_mut() = new_right_len as u16;
|
*right_node.len_mut() = new_right_len as u16;
|
||||||
|
|
||||||
match (left_node.force(), right_node.force()) {
|
match (left_node.force(), right_node.force()) {
|
||||||
(ForceResult::Internal(left), ForceResult::Internal(right)) => {
|
(ForceResult::Internal(left), ForceResult::Internal(right)) => {
|
||||||
|
Loading…
Reference in New Issue
Block a user