From faa30f4cca45fa140c8434f12c865f83bf78e8d0 Mon Sep 17 00:00:00 2001 From: Srinivas Reddy Thatiparthy Date: Thu, 22 Dec 2016 07:55:29 +0530 Subject: [PATCH] run rustfmt on btree folder --- src/libcollections/btree/map.rs | 60 +-- src/libcollections/btree/node.rs | 636 ++++++++++++----------------- src/libcollections/btree/search.rs | 62 +-- src/libcollections/btree/set.rs | 12 +- 4 files changed, 331 insertions(+), 439 deletions(-) diff --git a/src/libcollections/btree/map.rs b/src/libcollections/btree/map.rs index 4755f8a4c55a4..a014180e6b18a 100644 --- a/src/libcollections/btree/map.rs +++ b/src/libcollections/btree/map.rs @@ -235,12 +235,12 @@ impl super::Recover for BTreeMap match search::search_tree(self.root.as_mut(), key) { Found(handle) => { Some(OccupiedEntry { - handle: handle, - length: &mut self.length, - _marker: PhantomData, - } - .remove_kv() - .0) + handle: handle, + length: &mut self.length, + _marker: PhantomData, + } + .remove_kv() + .0) } GoDown(_) => None, } @@ -251,12 +251,12 @@ impl super::Recover for BTreeMap Found(handle) => Some(mem::replace(handle.into_kv_mut().0, key)), GoDown(handle) => { VacantEntry { - key: key, - handle: handle, - length: &mut self.length, - _marker: PhantomData, - } - .insert(()); + key: key, + handle: handle, + length: &mut self.length, + _marker: PhantomData, + } + .insert(()); None } } @@ -340,12 +340,16 @@ pub enum Entry<'a, K: 'a, V: 'a> { impl<'a, K: 'a + Debug + Ord, V: 'a + Debug> Debug for Entry<'a, K, V> { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { match *self { - Vacant(ref v) => f.debug_tuple("Entry") - .field(v) - .finish(), - Occupied(ref o) => f.debug_tuple("Entry") - .field(o) - .finish(), + Vacant(ref v) => { + f.debug_tuple("Entry") + .field(v) + .finish() + } + Occupied(ref o) => { + f.debug_tuple("Entry") + .field(o) + .finish() + } } } } @@ -367,8 +371,8 @@ pub struct VacantEntry<'a, K: 'a, V: 'a> { impl<'a, K: 'a + Debug + Ord, V: 'a> Debug for VacantEntry<'a, K, V> { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { f.debug_tuple("VacantEntry") - .field(self.key()) - .finish() + .field(self.key()) + .finish() } } @@ -389,9 +393,9 @@ pub struct OccupiedEntry<'a, K: 'a, V: 'a> { impl<'a, K: 'a + Debug + Ord, V: 'a + Debug> Debug for OccupiedEntry<'a, K, V> { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { f.debug_struct("OccupiedEntry") - .field("key", self.key()) - .field("value", self.get()) - .finish() + .field("key", self.key()) + .field("value", self.get()) + .finish() } } @@ -591,11 +595,11 @@ impl BTreeMap { match search::search_tree(self.root.as_mut(), key) { Found(handle) => { Some(OccupiedEntry { - handle: handle, - length: &mut self.length, - _marker: PhantomData, - } - .remove()) + handle: handle, + length: &mut self.length, + _marker: PhantomData, + } + .remove()) } GoDown(_) => None, } diff --git a/src/libcollections/btree/node.rs b/src/libcollections/btree/node.rs index e9bc29118d508..27159ee26de4e 100644 --- a/src/libcollections/btree/node.rs +++ b/src/libcollections/btree/node.rs @@ -95,7 +95,7 @@ impl LeafNode { vals: mem::uninitialized(), parent: ptr::null(), parent_idx: mem::uninitialized(), - len: 0 + len: 0, } } } @@ -125,7 +125,7 @@ impl InternalNode { unsafe fn new() -> Self { InternalNode { data: LeafNode::new(), - edges: mem::uninitialized() + edges: mem::uninitialized(), } } } @@ -135,20 +135,16 @@ impl InternalNode { /// of nodes is acutally behind the box, and, partially due to this lack of information, has no /// destructor. struct BoxedNode { - ptr: Unique> + ptr: Unique>, } impl BoxedNode { fn from_leaf(node: Box>) -> Self { - unsafe { - BoxedNode { ptr: Unique::new(Box::into_raw(node)) } - } + unsafe { BoxedNode { ptr: Unique::new(Box::into_raw(node)) } } } fn from_internal(node: Box>) -> Self { - unsafe { - BoxedNode { ptr: Unique::new(Box::into_raw(node) as *mut LeafNode) } - } + unsafe { BoxedNode { ptr: Unique::new(Box::into_raw(node) as *mut LeafNode) } } } unsafe fn from_ptr(ptr: NonZero<*const LeafNode>) -> Self { @@ -156,9 +152,7 @@ impl BoxedNode { } fn as_ptr(&self) -> NonZero<*const LeafNode> { - unsafe { - NonZero::new(*self.ptr as *const LeafNode) - } + unsafe { NonZero::new(*self.ptr as *const LeafNode) } } } @@ -166,22 +160,21 @@ impl BoxedNode { /// and must be cleaned up manually. pub struct Root { node: BoxedNode, - height: usize + height: usize, } -unsafe impl Sync for Root { } -unsafe impl Send for Root { } +unsafe impl Sync for Root {} +unsafe impl Send for Root {} impl Root { pub fn new_leaf() -> Self { Root { node: BoxedNode::from_leaf(Box::new(unsafe { LeafNode::new() })), - height: 0 + height: 0, } } - pub fn as_ref(&self) - -> NodeRef { + pub fn as_ref(&self) -> NodeRef { NodeRef { height: self.height, node: self.node.as_ptr(), @@ -190,8 +183,7 @@ impl Root { } } - pub fn as_mut(&mut self) - -> NodeRef { + pub fn as_mut(&mut self) -> NodeRef { NodeRef { height: self.height, node: self.node.as_ptr(), @@ -200,8 +192,7 @@ impl Root { } } - pub fn into_ref(self) - -> NodeRef { + pub fn into_ref(self) -> NodeRef { NodeRef { height: self.height, node: self.node.as_ptr(), @@ -212,8 +203,7 @@ impl Root { /// Adds a new internal node with a single edge, pointing to the previous root, and make that /// new node the root. This increases the height by 1 and is the opposite of `pop_level`. - pub fn push_level(&mut self) - -> NodeRef { + pub fn push_level(&mut self) -> NodeRef { let mut new_node = Box::new(unsafe { InternalNode::new() }); new_node.edges[0] = unsafe { BoxedNode::from_ptr(self.node.as_ptr()) }; @@ -224,7 +214,7 @@ impl Root { height: self.height, node: self.node.as_ptr(), root: self as *mut _, - _marker: PhantomData + _marker: PhantomData, }; unsafe { @@ -245,20 +235,18 @@ impl Root { self.node = unsafe { BoxedNode::from_ptr(self.as_mut() - .cast_unchecked::() - .first_edge() - .descend() - .node) + .cast_unchecked::() + .first_edge() + .descend() + .node) }; self.height -= 1; self.as_mut().as_leaf_mut().parent = ptr::null(); unsafe { - heap::deallocate( - top, - mem::size_of::>(), - mem::align_of::>() - ); + heap::deallocate(top, + mem::size_of::>(), + mem::align_of::>()); } } } @@ -285,39 +273,31 @@ pub struct NodeRef { node: NonZero<*const LeafNode>, // This is null unless the borrow type is `Mut` root: *const Root, - _marker: PhantomData<(BorrowType, Type)> + _marker: PhantomData<(BorrowType, Type)>, } -impl<'a, K: 'a, V: 'a, Type> Copy for NodeRef, K, V, Type> { } +impl<'a, K: 'a, V: 'a, Type> Copy for NodeRef, K, V, Type> {} impl<'a, K: 'a, V: 'a, Type> Clone for NodeRef, K, V, Type> { fn clone(&self) -> Self { *self } } -unsafe impl Sync - for NodeRef { } +unsafe impl Sync for NodeRef {} -unsafe impl<'a, K: Sync + 'a, V: Sync + 'a, Type> Send - for NodeRef, K, V, Type> { } -unsafe impl<'a, K: Send + 'a, V: Send + 'a, Type> Send - for NodeRef, K, V, Type> { } -unsafe impl Send - for NodeRef { } +unsafe impl<'a, K: Sync + 'a, V: Sync + 'a, Type> Send for NodeRef, K, V, Type> {} +unsafe impl<'a, K: Send + 'a, V: Send + 'a, Type> Send for NodeRef, K, V, Type> {} +unsafe impl Send for NodeRef {} impl NodeRef { fn as_internal(&self) -> &InternalNode { - unsafe { - &*(*self.node as *const InternalNode) - } + unsafe { &*(*self.node as *const InternalNode) } } } impl<'a, K, V> NodeRef, K, V, marker::Internal> { fn as_internal_mut(&mut self) -> &mut InternalNode { - unsafe { - &mut *(*self.node as *mut InternalNode) - } + unsafe { &mut *(*self.node as *mut InternalNode) } } } @@ -342,7 +322,7 @@ impl NodeRef { height: self.height, node: self.node, root: self.root, - _marker: PhantomData + _marker: PhantomData, } } @@ -352,14 +332,12 @@ impl NodeRef { height: self.height, node: self.node, root: self.root, - _marker: PhantomData + _marker: PhantomData, } } fn as_leaf(&self) -> &LeafNode { - unsafe { - &**self.node - } + unsafe { &**self.node } } pub fn keys(&self) -> &[K] { @@ -377,31 +355,21 @@ impl NodeRef { /// /// `edge.descend().ascend().unwrap()` and `node.ascend().unwrap().descend()` should /// both, upon success, do nothing. - pub fn ascend(self) -> Result< - Handle< - NodeRef< - BorrowType, - K, V, - marker::Internal - >, - marker::Edge - >, - Self - > { + pub fn ascend + (self) + -> Result, marker::Edge>, Self> { if self.as_leaf().parent.is_null() { Err(self) } else { Ok(Handle { node: NodeRef { height: self.height + 1, - node: unsafe { - NonZero::new(self.as_leaf().parent as *mut LeafNode) - }, + node: unsafe { NonZero::new(self.as_leaf().parent as *mut LeafNode) }, root: self.root, - _marker: PhantomData + _marker: PhantomData, }, idx: self.as_leaf().parent_idx as usize, - _marker: PhantomData + _marker: PhantomData, }) } } @@ -433,19 +401,14 @@ impl NodeRef { /// Similar to `ascend`, gets a reference to a node's parent node, but also /// deallocate the current node in the process. This is unsafe because the /// current node will still be accessible despite being deallocated. - pub unsafe fn deallocate_and_ascend(self) -> Option< - Handle< - NodeRef< - marker::Owned, - K, V, - marker::Internal - >, - marker::Edge - > - > { + pub unsafe fn deallocate_and_ascend + (self) + -> Option, marker::Edge>> { let ptr = self.as_leaf() as *const LeafNode as *const u8 as *mut u8; let ret = self.ascend().ok(); - heap::deallocate(ptr, mem::size_of::>(), mem::align_of::>()); + heap::deallocate(ptr, + mem::size_of::>(), + mem::align_of::>()); ret } } @@ -454,23 +417,14 @@ impl NodeRef { /// Similar to `ascend`, gets a reference to a node's parent node, but also /// deallocate the current node in the process. This is unsafe because the /// current node will still be accessible despite being deallocated. - pub unsafe fn deallocate_and_ascend(self) -> Option< - Handle< - NodeRef< - marker::Owned, - K, V, - marker::Internal - >, - marker::Edge - > - > { + pub unsafe fn deallocate_and_ascend + (self) + -> Option, marker::Edge>> { let ptr = self.as_internal() as *const InternalNode as *const u8 as *mut u8; let ret = self.ascend().ok(); - heap::deallocate( - ptr, - mem::size_of::>(), - mem::align_of::>() - ); + heap::deallocate(ptr, + mem::size_of::>(), + mem::align_of::>()); ret } } @@ -478,14 +432,13 @@ impl NodeRef { impl<'a, K, V, Type> NodeRef, K, V, Type> { /// Unsafely asserts to the compiler some static information about whether this /// node is a `Leaf`. - unsafe fn cast_unchecked(&mut self) - -> NodeRef { + unsafe fn cast_unchecked(&mut self) -> NodeRef { NodeRef { height: self.height, node: self.node, root: self.root, - _marker: PhantomData + _marker: PhantomData, } } @@ -504,14 +457,12 @@ impl<'a, K, V, Type> NodeRef, K, V, Type> { height: self.height, node: self.node, root: self.root, - _marker: PhantomData + _marker: PhantomData, } } fn as_leaf_mut(&mut self) -> &mut LeafNode { - unsafe { - &mut *(*self.node as *mut LeafNode) - } + unsafe { &mut *(*self.node as *mut LeafNode) } } pub fn keys_mut(&mut self) -> &mut [K] { @@ -526,16 +477,8 @@ impl<'a, K, V, Type> NodeRef, K, V, Type> { impl<'a, K: 'a, V: 'a, Type> NodeRef, K, V, Type> { pub fn into_slices(self) -> (&'a [K], &'a [V]) { unsafe { - ( - slice::from_raw_parts( - self.as_leaf().keys.as_ptr(), - self.len() - ), - slice::from_raw_parts( - self.as_leaf().vals.as_ptr(), - self.len() - ) - ) + (slice::from_raw_parts(self.as_leaf().keys.as_ptr(), self.len()), + slice::from_raw_parts(self.as_leaf().vals.as_ptr(), self.len())) } } } @@ -544,23 +487,15 @@ impl<'a, K: 'a, V: 'a, Type> NodeRef, K, V, Type> { /// Gets a mutable reference to the root itself. This is useful primarily when the /// height of the tree needs to be adjusted. Never call this on a reborrowed pointer. pub fn into_root_mut(self) -> &'a mut Root { - unsafe { - &mut *(self.root as *mut Root) - } + unsafe { &mut *(self.root as *mut Root) } } pub fn into_slices_mut(mut self) -> (&'a mut [K], &'a mut [V]) { unsafe { - ( - slice::from_raw_parts_mut( - &mut self.as_leaf_mut().keys as *mut [K] as *mut K, - self.len() - ), - slice::from_raw_parts_mut( - &mut self.as_leaf_mut().vals as *mut [V] as *mut V, - self.len() - ) - ) + (slice::from_raw_parts_mut(&mut self.as_leaf_mut().keys as *mut [K] as *mut K, + self.len()), + slice::from_raw_parts_mut(&mut self.as_leaf_mut().vals as *mut [V] as *mut V, + self.len())) } } } @@ -608,7 +543,8 @@ impl<'a, K, V> NodeRef, K, V, marker::Internal> { unsafe { ptr::write(self.keys_mut().get_unchecked_mut(idx), key); ptr::write(self.vals_mut().get_unchecked_mut(idx), val); - ptr::write(self.as_internal_mut().edges.get_unchecked_mut(idx + 1), edge.node); + ptr::write(self.as_internal_mut().edges.get_unchecked_mut(idx + 1), + edge.node); self.as_leaf_mut().len += 1; @@ -637,14 +573,10 @@ impl<'a, K, V> NodeRef, K, V, marker::Internal> { unsafe { slice_insert(self.keys_mut(), 0, key); slice_insert(self.vals_mut(), 0, val); - slice_insert( - slice::from_raw_parts_mut( - self.as_internal_mut().edges.as_mut_ptr(), - self.len()+1 - ), - 0, - edge.node - ); + slice_insert(slice::from_raw_parts_mut(self.as_internal_mut().edges.as_mut_ptr(), + self.len() + 1), + 0, + edge.node); self.as_leaf_mut().len += 1; @@ -669,7 +601,10 @@ impl<'a, K, V> NodeRef, K, V, marker::LeafOrInternal> { ForceResult::Leaf(_) => None, ForceResult::Internal(internal) => { let edge = ptr::read(internal.as_internal().edges.get_unchecked(idx + 1)); - let mut new_root = Root { node: edge, height: internal.height - 1 }; + let mut new_root = Root { + node: edge, + height: internal.height - 1, + }; new_root.as_mut().as_leaf_mut().parent = ptr::null(); Some(new_root) } @@ -694,15 +629,16 @@ impl<'a, K, V> NodeRef, K, V, marker::LeafOrInternal> { let edge = match self.reborrow_mut().force() { ForceResult::Leaf(_) => None, ForceResult::Internal(mut internal) => { - let edge = slice_remove( - slice::from_raw_parts_mut( - internal.as_internal_mut().edges.as_mut_ptr(), - old_len+1 - ), - 0 - ); - - let mut new_root = Root { node: edge, height: internal.height - 1 }; + let edge = slice_remove(slice::from_raw_parts_mut(internal.as_internal_mut() + .edges + .as_mut_ptr(), + old_len + 1), + 0); + + let mut new_root = Root { + node: edge, + height: internal.height - 1, + }; new_root.as_mut().as_leaf_mut().parent = ptr::null(); for i in 0..old_len { @@ -720,32 +656,28 @@ impl<'a, K, V> NodeRef, K, V, marker::LeafOrInternal> { } fn into_kv_pointers_mut(mut self) -> (*mut K, *mut V) { - ( - self.keys_mut().as_mut_ptr(), - self.vals_mut().as_mut_ptr() - ) + (self.keys_mut().as_mut_ptr(), self.vals_mut().as_mut_ptr()) } } impl NodeRef { /// Checks whether a node is an `Internal` node or a `Leaf` node. - pub fn force(self) -> ForceResult< - NodeRef, - NodeRef - > { + pub fn force(self) + -> ForceResult, + NodeRef> { if self.height == 0 { ForceResult::Leaf(NodeRef { height: self.height, node: self.node, root: self.root, - _marker: PhantomData + _marker: PhantomData, }) } else { ForceResult::Internal(NodeRef { height: self.height, node: self.node, root: self.root, - _marker: PhantomData + _marker: PhantomData, }) } } @@ -762,10 +694,10 @@ impl NodeRef { pub struct Handle { node: Node, idx: usize, - _marker: PhantomData + _marker: PhantomData, } -impl Copy for Handle { } +impl Copy for Handle {} // We don't need the full generality of `#[derive(Clone)]`, as the only time `Node` will be // `Clone`able is when it is an immutable reference and therefore `Copy`. impl Clone for Handle { @@ -790,7 +722,7 @@ impl Handle, mar Handle { node: node, idx: idx, - _marker: PhantomData + _marker: PhantomData, } } @@ -804,32 +736,27 @@ impl Handle, mar } impl PartialEq - for Handle, HandleType> { - + for Handle, HandleType> { fn eq(&self, other: &Self) -> bool { self.node.node == other.node.node && self.idx == other.idx } } -impl - Handle, HandleType> { - +impl Handle, + HandleType> { /// Temporarily takes out another, immutable handle on the same location. - pub fn reborrow(&self) - -> Handle, HandleType> { + pub fn reborrow(&self) -> Handle, HandleType> { // We can't use Handle::new_kv or Handle::new_edge because we don't know our type Handle { node: self.node.reborrow(), idx: self.idx, - _marker: PhantomData + _marker: PhantomData, } } } -impl<'a, K, V, NodeType, HandleType> - Handle, K, V, NodeType>, HandleType> { - +impl<'a, K, V, NodeType, HandleType> Handle, K, V, NodeType>, HandleType> { /// Temporarily takes out another, mutable handle on the same location. Beware, as /// this method is very dangerous, doubly so since it may not immediately appear /// dangerous. @@ -841,20 +768,18 @@ impl<'a, K, V, NodeType, HandleType> // FIXME(@gereeter) consider adding yet another type parameter to `NodeRef` that restricts // the use of `ascend` and `into_root_mut` on reborrowed pointers, preventing this unsafety. pub unsafe fn reborrow_mut(&mut self) - -> Handle, HandleType> { + -> Handle, HandleType> { // We can't use Handle::new_kv or Handle::new_edge because we don't know our type Handle { node: self.node.reborrow_mut(), idx: self.idx, - _marker: PhantomData + _marker: PhantomData, } } } -impl - Handle, marker::Edge> { - +impl Handle, marker::Edge> { /// Creates a new handle to an edge in `node`. `idx` must be less than or equal to /// `node.len()`. pub fn new_edge(node: NodeRef, idx: usize) -> Self { @@ -864,12 +789,11 @@ impl Handle { node: node, idx: idx, - _marker: PhantomData + _marker: PhantomData, } } - pub fn left_kv(self) - -> Result, marker::KV>, Self> { + pub fn left_kv(self) -> Result, marker::KV>, Self> { if self.idx > 0 { Ok(Handle::new_kv(self.node, self.idx - 1)) @@ -878,8 +802,7 @@ impl } } - pub fn right_kv(self) - -> Result, marker::KV>, Self> { + pub fn right_kv(self) -> Result, marker::KV>, Self> { if self.idx < self.node.len() { Ok(Handle::new_kv(self.node, self.idx)) @@ -913,8 +836,7 @@ impl<'a, K, V> Handle, K, V, marker::Leaf>, marker::Edge /// this edge. This method splits the node if there isn't enough room. /// /// The returned pointer points to the inserted value. - pub fn insert(mut self, key: K, val: V) - -> (InsertResult<'a, K, V, marker::Leaf>, *mut V) { + pub fn insert(mut self, key: K, val: V) -> (InsertResult<'a, K, V, marker::Leaf>, *mut V) { if self.node.len() < CAPACITY { let ptr = self.insert_fit(key, val); @@ -923,15 +845,12 @@ impl<'a, K, V> Handle, K, V, marker::Leaf>, marker::Edge let middle = Handle::new_kv(self.node, B); let (mut left, k, v, mut right) = middle.split(); let ptr = if self.idx <= B { - unsafe { - Handle::new_edge(left.reborrow_mut(), self.idx).insert_fit(key, val) - } + unsafe { Handle::new_edge(left.reborrow_mut(), self.idx).insert_fit(key, val) } } else { unsafe { - Handle::new_edge( - right.as_mut().cast_unchecked::(), - self.idx - (B + 1) - ).insert_fit(key, val) + Handle::new_edge(right.as_mut().cast_unchecked::(), + self.idx - (B + 1)) + .insert_fit(key, val) } }; (InsertResult::Split(left, k, v, right), ptr) @@ -952,8 +871,9 @@ impl<'a, K, V> Handle, K, V, marker::Internal>, marker:: /// Unsafely asserts to the compiler some static information about whether the underlying /// node of this handle is a `Leaf`. - unsafe fn cast_unchecked(&mut self) - -> Handle, marker::Edge> { + unsafe fn cast_unchecked + (&mut self) + -> Handle, marker::Edge> { Handle::new_edge(self.node.cast_unchecked(), self.idx) } @@ -970,16 +890,12 @@ impl<'a, K, V> Handle, K, V, marker::Internal>, marker:: // This cast is a lie, but it allows us to reuse the key/value insertion logic. self.cast_unchecked::().insert_fit(key, val); - slice_insert( - slice::from_raw_parts_mut( - self.node.as_internal_mut().edges.as_mut_ptr(), - self.node.len() - ), - self.idx + 1, - edge.node - ); + slice_insert(slice::from_raw_parts_mut(self.node.as_internal_mut().edges.as_mut_ptr(), + self.node.len()), + self.idx + 1, + edge.node); - for i in (self.idx+1)..(self.node.len()+1) { + for i in (self.idx + 1)..(self.node.len() + 1) { Handle::new_edge(self.node.reborrow_mut(), i).correct_parent_link(); } } @@ -988,8 +904,11 @@ impl<'a, K, V> Handle, K, V, marker::Internal>, marker:: /// Inserts a new key/value pair and an edge that will go to the right of that new pair /// between this edge and the key/value pair to the right of this edge. This method splits /// the node if there isn't enough room. - pub fn insert(mut self, key: K, val: V, edge: Root) - -> InsertResult<'a, K, V, marker::Internal> { + pub fn insert(mut self, + key: K, + val: V, + edge: Root) + -> InsertResult<'a, K, V, marker::Internal> { // Necessary for correctness, but this is an internal module debug_assert!(edge.height == self.node.height - 1); @@ -1006,10 +925,9 @@ impl<'a, K, V> Handle, K, V, marker::Internal>, marker:: } } else { unsafe { - Handle::new_edge( - right.as_mut().cast_unchecked::(), - self.idx - (B + 1) - ).insert_fit(key, val, edge); + Handle::new_edge(right.as_mut().cast_unchecked::(), + self.idx - (B + 1)) + .insert_fit(key, val, edge); } } InsertResult::Split(left, k, v, right) @@ -1017,9 +935,7 @@ impl<'a, K, V> Handle, K, V, marker::Internal>, marker:: } } -impl - Handle, marker::Edge> { - +impl Handle, marker::Edge> { /// Finds the node pointed to by this edge. /// /// `edge.descend().ascend().unwrap()` and `node.ascend().unwrap().descend()` should @@ -1029,30 +945,22 @@ impl height: self.node.height - 1, node: unsafe { self.node.as_internal().edges.get_unchecked(self.idx).as_ptr() }, root: self.node.root, - _marker: PhantomData + _marker: PhantomData, } } } -impl<'a, K: 'a, V: 'a, NodeType> - Handle, K, V, NodeType>, marker::KV> { - +impl<'a, K: 'a, V: 'a, NodeType> Handle, K, V, NodeType>, marker::KV> { pub fn into_kv(self) -> (&'a K, &'a V) { let (keys, vals) = self.node.into_slices(); - unsafe { - (keys.get_unchecked(self.idx), vals.get_unchecked(self.idx)) - } + unsafe { (keys.get_unchecked(self.idx), vals.get_unchecked(self.idx)) } } } -impl<'a, K: 'a, V: 'a, NodeType> - Handle, K, V, NodeType>, marker::KV> { - +impl<'a, K: 'a, V: 'a, NodeType> Handle, K, V, NodeType>, marker::KV> { pub fn into_kv_mut(self) -> (&'a mut K, &'a mut V) { let (mut keys, mut vals) = self.node.into_slices_mut(); - unsafe { - (keys.get_unchecked_mut(self.idx), vals.get_unchecked_mut(self.idx)) - } + unsafe { (keys.get_unchecked_mut(self.idx), vals.get_unchecked_mut(self.idx)) } } } @@ -1073,8 +981,7 @@ impl<'a, K, V> Handle, K, V, marker::Leaf>, marker::KV> /// - The key and value pointed to by this handle and extracted. /// - All the key/value pairs to the right of this handle are put into a newly /// allocated node. - pub fn split(mut self) - -> (NodeRef, K, V, marker::Leaf>, K, V, Root) { + pub fn split(mut self) -> (NodeRef, K, V, marker::Leaf>, K, V, Root) { unsafe { let mut new_node = Box::new(LeafNode::new()); @@ -1083,35 +990,30 @@ impl<'a, K, V> Handle, K, V, marker::Leaf>, marker::KV> let new_len = self.node.len() - self.idx - 1; - ptr::copy_nonoverlapping( - self.node.keys().as_ptr().offset(self.idx as isize + 1), - new_node.keys.as_mut_ptr(), - new_len - ); - ptr::copy_nonoverlapping( - self.node.vals().as_ptr().offset(self.idx as isize + 1), - new_node.vals.as_mut_ptr(), - new_len - ); + ptr::copy_nonoverlapping(self.node.keys().as_ptr().offset(self.idx as isize + 1), + new_node.keys.as_mut_ptr(), + new_len); + ptr::copy_nonoverlapping(self.node.vals().as_ptr().offset(self.idx as isize + 1), + new_node.vals.as_mut_ptr(), + new_len); self.node.as_leaf_mut().len = self.idx as u16; new_node.len = new_len as u16; - ( - self.node, - k, v, - Root { - node: BoxedNode::from_leaf(new_node), - height: 0 - } - ) + (self.node, + k, + v, + Root { + node: BoxedNode::from_leaf(new_node), + height: 0, + }) } } /// Removes the key/value pair pointed to by this handle, returning the edge between the /// now adjacent key/value pairs to the left and right of this handle. pub fn remove(mut self) - -> (Handle, K, V, marker::Leaf>, marker::Edge>, K, V) { + -> (Handle, K, V, marker::Leaf>, marker::Edge>, K, V) { unsafe { let k = slice_remove(self.node.keys_mut(), self.idx); let v = slice_remove(self.node.vals_mut(), self.idx); @@ -1129,8 +1031,7 @@ impl<'a, K, V> Handle, K, V, marker::Internal>, marker:: /// - The key and value pointed to by this handle and extracted. /// - All the edges and key/value pairs to the right of this handle are put into /// a newly allocated node. - pub fn split(mut self) - -> (NodeRef, K, V, marker::Internal>, K, V, Root) { + pub fn split(mut self) -> (NodeRef, K, V, marker::Internal>, K, V, Root) { unsafe { let mut new_node = Box::new(InternalNode::new()); @@ -1140,39 +1041,33 @@ impl<'a, K, V> Handle, K, V, marker::Internal>, marker:: let height = self.node.height; let new_len = self.node.len() - self.idx - 1; - ptr::copy_nonoverlapping( - self.node.keys().as_ptr().offset(self.idx as isize + 1), - new_node.data.keys.as_mut_ptr(), - new_len - ); - ptr::copy_nonoverlapping( - self.node.vals().as_ptr().offset(self.idx as isize + 1), - new_node.data.vals.as_mut_ptr(), - new_len - ); - ptr::copy_nonoverlapping( - self.node.as_internal().edges.as_ptr().offset(self.idx as isize + 1), - new_node.edges.as_mut_ptr(), - new_len + 1 - ); + ptr::copy_nonoverlapping(self.node.keys().as_ptr().offset(self.idx as isize + 1), + new_node.data.keys.as_mut_ptr(), + new_len); + ptr::copy_nonoverlapping(self.node.vals().as_ptr().offset(self.idx as isize + 1), + new_node.data.vals.as_mut_ptr(), + new_len); + ptr::copy_nonoverlapping(self.node + .as_internal() + .edges + .as_ptr() + .offset(self.idx as isize + 1), + new_node.edges.as_mut_ptr(), + new_len + 1); self.node.as_leaf_mut().len = self.idx as u16; new_node.data.len = new_len as u16; let mut new_root = Root { node: BoxedNode::from_internal(new_node), - height: height + height: height, }; - for i in 0..(new_len+1) { + for i in 0..(new_len + 1) { Handle::new_edge(new_root.as_mut().cast_unchecked(), i).correct_parent_link(); } - ( - self.node, - k, v, - new_root - ) + (self.node, k, v, new_root) } } @@ -1180,17 +1075,14 @@ impl<'a, K, V> Handle, K, V, marker::Internal>, marker:: /// a node to hold the combination of the nodes to the left and right of this handle along /// with the key/value pair at this handle. pub fn can_merge(&self) -> bool { - ( - self.reborrow() - .left_edge() - .descend() - .len() - + self.reborrow() - .right_edge() - .descend() - .len() - + 1 - ) <= CAPACITY + (self.reborrow() + .left_edge() + .descend() + .len() + + self.reborrow() + .right_edge() + .descend() + .len() + 1) <= CAPACITY } /// Combines the node immediately to the left of this handle, the key/value pair pointed @@ -1199,7 +1091,7 @@ impl<'a, K, V> Handle, K, V, marker::Internal>, marker:: /// /// Assumes that this edge `.can_merge()`. pub fn merge(mut self) - -> Handle, K, V, marker::Internal>, marker::Edge> { + -> Handle, K, V, marker::Internal>, marker::Edge> { let self1 = unsafe { ptr::read(&self) }; let self2 = unsafe { ptr::read(&self) }; let mut left_node = self1.left_edge().descend(); @@ -1213,21 +1105,21 @@ impl<'a, K, V> Handle, K, V, marker::Internal>, marker:: unsafe { ptr::write(left_node.keys_mut().get_unchecked_mut(left_len), slice_remove(self.node.keys_mut(), self.idx)); - ptr::copy_nonoverlapping( - right_node.keys().as_ptr(), - left_node.keys_mut().as_mut_ptr().offset(left_len as isize + 1), - right_len - ); + ptr::copy_nonoverlapping(right_node.keys().as_ptr(), + left_node.keys_mut() + .as_mut_ptr() + .offset(left_len as isize + 1), + right_len); ptr::write(left_node.vals_mut().get_unchecked_mut(left_len), slice_remove(self.node.vals_mut(), self.idx)); - ptr::copy_nonoverlapping( - right_node.vals().as_ptr(), - left_node.vals_mut().as_mut_ptr().offset(left_len as isize + 1), - right_len - ); + ptr::copy_nonoverlapping(right_node.vals().as_ptr(), + left_node.vals_mut() + .as_mut_ptr() + .offset(left_len as isize + 1), + right_len); slice_remove(&mut self.node.as_internal_mut().edges, self.idx + 1); - for i in self.idx+1..self.node.len() { + for i in self.idx + 1..self.node.len() { Handle::new_edge(self.node.reborrow_mut(), i).correct_parent_link(); } self.node.as_leaf_mut().len -= 1; @@ -1235,34 +1127,26 @@ impl<'a, K, V> Handle, K, V, marker::Internal>, marker:: left_node.as_leaf_mut().len += right_len as u16 + 1; if self.node.height > 1 { - ptr::copy_nonoverlapping( - right_node.cast_unchecked().as_internal().edges.as_ptr(), - left_node.cast_unchecked() - .as_internal_mut() - .edges - .as_mut_ptr() - .offset(left_len as isize + 1), - right_len + 1 - ); - - for i in left_len+1..left_len+right_len+2 { - Handle::new_edge( - left_node.cast_unchecked().reborrow_mut(), - i - ).correct_parent_link(); + ptr::copy_nonoverlapping(right_node.cast_unchecked().as_internal().edges.as_ptr(), + left_node.cast_unchecked() + .as_internal_mut() + .edges + .as_mut_ptr() + .offset(left_len as isize + 1), + right_len + 1); + + for i in left_len + 1..left_len + right_len + 2 { + Handle::new_edge(left_node.cast_unchecked().reborrow_mut(), i) + .correct_parent_link(); } - heap::deallocate( - *right_node.node as *mut u8, - mem::size_of::>(), - mem::align_of::>() - ); + heap::deallocate(*right_node.node as *mut u8, + mem::size_of::>(), + mem::align_of::>()); } else { - heap::deallocate( - *right_node.node as *mut u8, - mem::size_of::>(), - mem::align_of::>() - ); + heap::deallocate(*right_node.node as *mut u8, + mem::size_of::>(), + mem::align_of::>()); } Handle::new_edge(self.node, self.idx) @@ -1281,7 +1165,7 @@ impl<'a, K, V> Handle, K, V, marker::Internal>, marker:: match self.reborrow_mut().right_edge().descend().force() { ForceResult::Leaf(mut leaf) => leaf.push_front(k, v), - ForceResult::Internal(mut internal) => internal.push_front(k, v, edge.unwrap()) + ForceResult::Internal(mut internal) => internal.push_front(k, v, edge.unwrap()), } } } @@ -1298,7 +1182,7 @@ impl<'a, K, V> Handle, K, V, marker::Internal>, marker:: match self.reborrow_mut().left_edge().descend().force() { ForceResult::Leaf(mut leaf) => leaf.push(k, v), - ForceResult::Internal(mut internal) => internal.push(k, v, edge.unwrap()) + ForceResult::Internal(mut internal) => internal.push(k, v, edge.unwrap()), } } } @@ -1327,12 +1211,8 @@ impl<'a, K, V> Handle, K, V, marker::Internal>, marker:: }; // Make room for stolen elements in the right child. - ptr::copy(right_kv.0, - right_kv.0.offset(count as isize), - right_len); - ptr::copy(right_kv.1, - right_kv.1.offset(count as isize), - right_len); + ptr::copy(right_kv.0, right_kv.0.offset(count as isize), right_len); + ptr::copy(right_kv.1, right_kv.1.offset(count as isize), right_len); // Move elements from the left child to the right one. move_kv(left_kv, new_left_len + 1, right_kv, 0, count - 1); @@ -1357,9 +1237,11 @@ impl<'a, K, V> Handle, K, V, marker::Internal>, marker:: right.correct_childrens_parent_links(count, count + right_len + 1); move_edges(left, new_left_len + 1, right, 0, count); - }, - (ForceResult::Leaf(_), ForceResult::Leaf(_)) => { } - _ => { unreachable!(); } + } + (ForceResult::Leaf(_), ForceResult::Leaf(_)) => {} + _ => { + unreachable!(); + } } } } @@ -1397,12 +1279,8 @@ impl<'a, K, V> Handle, K, V, marker::Internal>, marker:: move_kv(right_kv, count - 1, parent_kv, 0, 1); // Fix right indexing - ptr::copy(right_kv.0.offset(count as isize), - right_kv.0, - new_right_len); - ptr::copy(right_kv.1.offset(count as isize), - right_kv.1, - new_right_len); + ptr::copy(right_kv.0.offset(count as isize), right_kv.0, new_right_len); + ptr::copy(right_kv.1.offset(count as isize), right_kv.1, new_right_len); } left_node.reborrow_mut().as_leaf_mut().len += count as u16; @@ -1418,19 +1296,21 @@ impl<'a, K, V> Handle, K, V, marker::Internal>, marker:: right_edges, new_right_len + 1); right.correct_childrens_parent_links(0, new_right_len + 1); - }, - (ForceResult::Leaf(_), ForceResult::Leaf(_)) => { } - _ => { unreachable!(); } + } + (ForceResult::Leaf(_), ForceResult::Leaf(_)) => {} + _ => { + unreachable!(); + } } } } } -unsafe fn move_kv( - source: (*mut K, *mut V), source_offset: usize, - dest: (*mut K, *mut V), dest_offset: usize, - count: usize) -{ +unsafe fn move_kv(source: (*mut K, *mut V), + source_offset: usize, + dest: (*mut K, *mut V), + dest_offset: usize, + count: usize) { ptr::copy_nonoverlapping(source.0.offset(source_offset as isize), dest.0.offset(dest_offset as isize), count); @@ -1440,11 +1320,11 @@ unsafe fn move_kv( } // Source and destination must have the same height. -unsafe fn move_edges( - mut source: NodeRef, source_offset: usize, - mut dest: NodeRef, dest_offset: usize, - count: usize) -{ +unsafe fn move_edges(mut source: NodeRef, + source_offset: usize, + mut dest: NodeRef, + dest_offset: usize, + count: usize) { let source_ptr = source.as_internal_mut().edges.as_mut_ptr(); let dest_ptr = dest.as_internal_mut().edges.as_mut_ptr(); ptr::copy_nonoverlapping(source_ptr.offset(source_offset as isize), @@ -1453,25 +1333,27 @@ unsafe fn move_edges( dest.correct_childrens_parent_links(dest_offset, dest_offset + count); } -impl - Handle, HandleType> { - +impl Handle, + HandleType> { /// Check whether the underlying node is an `Internal` node or a `Leaf` node. - pub fn force(self) -> ForceResult< - Handle, HandleType>, - Handle, HandleType> - > { + pub fn force(self) + -> ForceResult, HandleType>, + Handle, HandleType>> { match self.node.force() { - ForceResult::Leaf(node) => ForceResult::Leaf(Handle { - node: node, - idx: self.idx, - _marker: PhantomData - }), - ForceResult::Internal(node) => ForceResult::Internal(Handle { - node: node, - idx: self.idx, - _marker: PhantomData - }) + ForceResult::Leaf(node) => { + ForceResult::Leaf(Handle { + node: node, + idx: self.idx, + _marker: PhantomData, + }) + } + ForceResult::Internal(node) => { + ForceResult::Internal(Handle { + node: node, + idx: self.idx, + _marker: PhantomData, + }) + } } } } @@ -1480,7 +1362,7 @@ impl<'a, K, V> Handle, K, V, marker::LeafOrInternal>, ma /// Move the suffix after `self` from one node to another one. `right` must be empty. /// The first edge of `right` remains unchanged. pub fn move_suffix(&mut self, - right: &mut NodeRef, K, V, marker::LeafOrInternal>) { + right: &mut NodeRef, K, V, marker::LeafOrInternal>) { unsafe { let left_new_len = self.idx; let mut left_node = self.reborrow_mut().into_node(); @@ -1503,9 +1385,11 @@ impl<'a, K, V> Handle, K, V, marker::LeafOrInternal>, ma match (left_node.force(), right_node.force()) { (ForceResult::Internal(left), ForceResult::Internal(right)) => { move_edges(left, left_new_len + 1, right, 1, right_new_len); - }, - (ForceResult::Leaf(_), ForceResult::Leaf(_)) => { } - _ => { unreachable!(); } + } + (ForceResult::Leaf(_), ForceResult::Leaf(_)) => {} + _ => { + unreachable!(); + } } } } @@ -1513,12 +1397,12 @@ impl<'a, K, V> Handle, K, V, marker::LeafOrInternal>, ma pub enum ForceResult { Leaf(Leaf), - Internal(Internal) + Internal(Internal), } pub enum InsertResult<'a, K, V, Type> { Fit(Handle, K, V, Type>, marker::KV>), - Split(NodeRef, K, V, Type>, K, V, Root) + Split(NodeRef, K, V, Type>, K, V, Root), } pub mod marker { @@ -1537,20 +1421,16 @@ pub mod marker { } unsafe fn slice_insert(slice: &mut [T], idx: usize, val: T) { - ptr::copy( - slice.as_ptr().offset(idx as isize), - slice.as_mut_ptr().offset(idx as isize + 1), - slice.len() - idx - ); + ptr::copy(slice.as_ptr().offset(idx as isize), + slice.as_mut_ptr().offset(idx as isize + 1), + slice.len() - idx); ptr::write(slice.get_unchecked_mut(idx), val); } unsafe fn slice_remove(slice: &mut [T], idx: usize) -> T { let ret = ptr::read(slice.get_unchecked(idx)); - ptr::copy( - slice.as_ptr().offset(idx as isize + 1), - slice.as_mut_ptr().offset(idx as isize), - slice.len() - idx - 1 - ); + ptr::copy(slice.as_ptr().offset(idx as isize + 1), + slice.as_mut_ptr().offset(idx as isize), + slice.len() - idx - 1); ret } diff --git a/src/libcollections/btree/search.rs b/src/libcollections/btree/search.rs index c94b570bfed8b..a04fec4a30e2e 100644 --- a/src/libcollections/btree/search.rs +++ b/src/libcollections/btree/search.rs @@ -19,58 +19,60 @@ use self::SearchResult::*; pub enum SearchResult { Found(Handle, marker::KV>), - GoDown(Handle, marker::Edge>) + GoDown(Handle, marker::Edge>), } -pub fn search_tree( - mut node: NodeRef, - key: &Q -) -> SearchResult - where Q: Ord, K: Borrow { +pub fn search_tree + (mut node: NodeRef, + key: &Q) + -> SearchResult + where Q: Ord, + K: Borrow +{ loop { match search_node(node, key) { Found(handle) => return Found(handle), - GoDown(handle) => match handle.force() { - Leaf(leaf) => return GoDown(leaf), - Internal(internal) => { - node = internal.descend(); - continue; + GoDown(handle) => { + match handle.force() { + Leaf(leaf) => return GoDown(leaf), + Internal(internal) => { + node = internal.descend(); + continue; + } } } } } } -pub fn search_node( - node: NodeRef, - key: &Q -) -> SearchResult - where Q: Ord, K: Borrow { +pub fn search_node + (node: NodeRef, + key: &Q) + -> SearchResult + where Q: Ord, + K: Borrow +{ match search_linear(&node, key) { - (idx, true) => Found( - Handle::new_kv(node, idx) - ), - (idx, false) => SearchResult::GoDown( - Handle::new_edge(node, idx) - ) + (idx, true) => Found(Handle::new_kv(node, idx)), + (idx, false) => SearchResult::GoDown(Handle::new_edge(node, idx)), } } -fn search_linear( - node: &NodeRef, - key: &Q -) -> (usize, bool) - where Q: Ord, K: Borrow { +fn search_linear(node: &NodeRef, + key: &Q) + -> (usize, bool) + where Q: Ord, + K: Borrow +{ for (i, k) in node.keys().iter().enumerate() { match key.cmp(k.borrow()) { - Ordering::Greater => {}, + Ordering::Greater => {} Ordering::Equal => return (i, true), - Ordering::Less => return (i, false) + Ordering::Less => return (i, false), } } (node.keys().len(), false) } - diff --git a/src/libcollections/btree/set.rs b/src/libcollections/btree/set.rs index f006ba9537161..5ee3b4ca2556f 100644 --- a/src/libcollections/btree/set.rs +++ b/src/libcollections/btree/set.rs @@ -655,7 +655,9 @@ impl BTreeSet { /// assert_eq!(b[&41], "e"); /// ``` #[stable(feature = "btree_split_off", since = "1.11.0")] - pub fn split_off(&mut self, key: &Q) -> Self where T: Borrow { + pub fn split_off(&mut self, key: &Q) -> Self + where T: Borrow + { BTreeSet { map: self.map.split_off(key) } } } @@ -850,7 +852,9 @@ impl<'a, T> DoubleEndedIterator for Iter<'a, T> { } #[stable(feature = "rust1", since = "1.0.0")] impl<'a, T> ExactSizeIterator for Iter<'a, T> { - fn len(&self) -> usize { self.iter.len() } + fn len(&self) -> usize { + self.iter.len() + } } #[unstable(feature = "fused", issue = "35602")] @@ -875,7 +879,9 @@ impl DoubleEndedIterator for IntoIter { } #[stable(feature = "rust1", since = "1.0.0")] impl ExactSizeIterator for IntoIter { - fn len(&self) -> usize { self.iter.len() } + fn len(&self) -> usize { + self.iter.len() + } } #[unstable(feature = "fused", issue = "35602")]