1
Fork 0

BTreeMap: swap the names of NodeRef::new and Root::new_leaf

This commit is contained in:
Stein Somers 2020-11-22 12:29:48 +01:00
parent 20328b5323
commit b04abc433e
4 changed files with 14 additions and 14 deletions

View file

@ -67,7 +67,7 @@ impl<K, V> Root<K, V> {
// Push key-value pair and new right subtree. // Push key-value pair and new right subtree.
let tree_height = open_node.height() - 1; let tree_height = open_node.height() - 1;
let mut right_tree = Root::new_leaf(); let mut right_tree = Root::new();
for _ in 0..tree_height { for _ in 0..tree_height {
right_tree.push_internal_level(); right_tree.push_internal_level();
} }

View file

@ -9,7 +9,7 @@ use core::ops::{Index, RangeBounds};
use core::ptr; use core::ptr;
use super::borrow::DormantMutRef; use super::borrow::DormantMutRef;
use super::node::{self, marker, ForceResult::*, Handle, NodeRef}; use super::node::{self, marker, ForceResult::*, Handle, NodeRef, Root};
use super::search::{self, SearchResult::*}; use super::search::{self, SearchResult::*};
use super::unwrap_unchecked; use super::unwrap_unchecked;
@ -128,7 +128,7 @@ pub(super) const MIN_LEN: usize = node::MIN_LEN_AFTER_SPLIT;
/// ``` /// ```
#[stable(feature = "rust1", since = "1.0.0")] #[stable(feature = "rust1", since = "1.0.0")]
pub struct BTreeMap<K, V> { pub struct BTreeMap<K, V> {
root: Option<node::Root<K, V>>, root: Option<Root<K, V>>,
length: usize, length: usize,
} }
@ -145,7 +145,7 @@ unsafe impl<#[may_dangle] K, #[may_dangle] V> Drop for BTreeMap<K, V> {
impl<K: Clone, V: Clone> Clone for BTreeMap<K, V> { impl<K: Clone, V: Clone> Clone for BTreeMap<K, V> {
fn clone(&self) -> BTreeMap<K, V> { fn clone(&self) -> BTreeMap<K, V> {
fn clone_subtree<'a, K: Clone, V: Clone>( fn clone_subtree<'a, K: Clone, V: Clone>(
node: node::NodeRef<marker::Immut<'a>, K, V, marker::LeafOrInternal>, node: NodeRef<marker::Immut<'a>, K, V, marker::LeafOrInternal>,
) -> BTreeMap<K, V> ) -> BTreeMap<K, V>
where where
K: 'a, K: 'a,
@ -153,7 +153,7 @@ impl<K: Clone, V: Clone> Clone for BTreeMap<K, V> {
{ {
match node.force() { match node.force() {
Leaf(leaf) => { Leaf(leaf) => {
let mut out_tree = BTreeMap { root: Some(node::Root::new_leaf()), length: 0 }; let mut out_tree = BTreeMap { root: Some(Root::new()), length: 0 };
{ {
let root = out_tree.root.as_mut().unwrap(); // unwrap succeeds because we just wrapped let root = out_tree.root.as_mut().unwrap(); // unwrap succeeds because we just wrapped
@ -198,7 +198,7 @@ impl<K: Clone, V: Clone> Clone for BTreeMap<K, V> {
(root, length) (root, length)
}; };
out_node.push(k, v, subroot.unwrap_or_else(node::Root::new_leaf)); out_node.push(k, v, subroot.unwrap_or_else(Root::new));
out_tree.length += 1 + sublength; out_tree.length += 1 + sublength;
} }
} }
@ -1558,7 +1558,7 @@ pub(super) struct DrainFilterInner<'a, K: 'a, V: 'a> {
length: &'a mut usize, length: &'a mut usize,
/// Burried reference to the root field in the borrowed map. /// Burried reference to the root field in the borrowed map.
/// Wrapped in `Option` to allow drop handler to `take` it. /// Wrapped in `Option` to allow drop handler to `take` it.
dormant_root: Option<DormantMutRef<'a, node::Root<K, V>>>, dormant_root: Option<DormantMutRef<'a, Root<K, V>>>,
/// Contains a leaf edge preceding the next element to be returned, or the last leaf edge. /// Contains a leaf edge preceding the next element to be returned, or the last leaf edge.
/// Empty if the map has no root, if iteration went beyond the last leaf edge, /// Empty if the map has no root, if iteration went beyond the last leaf edge,
/// or if a panic occurred in the predicate. /// or if a panic occurred in the predicate.
@ -2160,8 +2160,8 @@ impl<K, V> BTreeMap<K, V> {
/// If the root node is the empty (non-allocated) root node, allocate our /// If the root node is the empty (non-allocated) root node, allocate our
/// own node. Is an associated function to avoid borrowing the entire BTreeMap. /// own node. Is an associated function to avoid borrowing the entire BTreeMap.
fn ensure_is_owned(root: &mut Option<node::Root<K, V>>) -> &mut node::Root<K, V> { fn ensure_is_owned(root: &mut Option<Root<K, V>>) -> &mut Root<K, V> {
root.get_or_insert_with(node::Root::new_leaf) root.get_or_insert_with(Root::new)
} }
} }

View file

@ -134,13 +134,13 @@ pub type Root<K, V> = NodeRef<marker::Owned, K, V, marker::LeafOrInternal>;
impl<K, V> Root<K, V> { impl<K, V> Root<K, V> {
/// Returns a new owned tree, with its own root node that is initially empty. /// Returns a new owned tree, with its own root node that is initially empty.
pub fn new_leaf() -> Self { pub fn new() -> Self {
NodeRef::new().forget_type() NodeRef::new_leaf().forget_type()
} }
} }
impl<K, V> NodeRef<marker::Owned, K, V, marker::Leaf> { impl<K, V> NodeRef<marker::Owned, K, V, marker::Leaf> {
fn new() -> Self { fn new_leaf() -> Self {
Self::from_new_leaf(Box::new(unsafe { LeafNode::new() })) Self::from_new_leaf(Box::new(unsafe { LeafNode::new() }))
} }

View file

@ -74,12 +74,12 @@ fn test_splitpoint() {
#[test] #[test]
fn test_partial_cmp_eq() { fn test_partial_cmp_eq() {
let mut root1 = NodeRef::new(); let mut root1 = NodeRef::new_leaf();
let mut leaf1 = root1.borrow_mut(); let mut leaf1 = root1.borrow_mut();
leaf1.push(1, ()); leaf1.push(1, ());
let mut root1 = root1.forget_type(); let mut root1 = root1.forget_type();
root1.push_internal_level(); root1.push_internal_level();
let root2 = Root::new_leaf(); let root2 = Root::new();
root1.reborrow().assert_back_pointers(); root1.reborrow().assert_back_pointers();
root2.reborrow().assert_back_pointers(); root2.reborrow().assert_back_pointers();