Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
int
Rust
提交
92acdc8b
R
Rust
项目概览
int
/
Rust
大约 1 年 前同步成功
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
R
Rust
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
92acdc8b
编写于
1月 02, 2020
作者:
S
Stein Somers
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Tweak and extend internal documentation, including debug asserts.
Co-Authored-By:
N
Robin Kruppe
<
robin.kruppe@gmail.com
>
上级
766fba3f
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
39 addition
and
15 deletion
+39
-15
src/liballoc/collections/btree/map.rs
src/liballoc/collections/btree/map.rs
+2
-2
src/liballoc/collections/btree/node.rs
src/liballoc/collections/btree/node.rs
+37
-13
未找到文件。
src/liballoc/collections/btree/map.rs
浏览文件 @
92acdc8b
...
...
@@ -493,7 +493,7 @@ pub fn new() -> BTreeMap<K, V> {
BTreeMap
{
root
:
node
::
Root
::
shared_empty_root
(),
length
:
0
}
}
/// Clears the map, removing all
value
s.
/// Clears the map, removing all
element
s.
///
/// # Examples
///
...
...
@@ -2605,7 +2605,7 @@ fn remove_kv(self) -> (K, V) {
// Handle underflow
let
mut
cur_node
=
small_leaf
.forget_type
();
while
cur_node
.len
()
<
node
::
CAPACITY
/
2
{
while
cur_node
.len
()
<
node
::
MIN_LEN
{
match
handle_underfull_node
(
cur_node
)
{
AtRoot
=>
break
,
EmptyParent
(
_
)
=>
unreachable!
(),
...
...
src/liballoc/collections/btree/node.rs
浏览文件 @
92acdc8b
...
...
@@ -308,15 +308,15 @@ pub fn pop_level(&mut self) {
/// `Leaf`, the `NodeRef` points to a leaf node, when this is `Internal` the
/// `NodeRef` points to an internal node, and when this is `LeafOrInternal` the
/// `NodeRef` could be pointing to either type of node.
/// Note that in case of a leaf node, this might still be the shared root!
Only turn
///
this into a `LeafNode` reference if you know it is not a root! Shared references
///
must be dereferencable *for the entire size of their pointee*, so `&InternalNode`
///
pointing to the shared root is UB
.
/// Turning this into a `NodeHeader` is always safe.
/// Note that in case of a leaf node, this might still be the shared root!
///
Only turn this into a `LeafNode` reference if you know it is not the shared root!
///
Shared references must be dereferencable *for the entire size of their pointee*,
///
so '&LeafNode` or `&InternalNode` pointing to the shared root is undefined behavior
.
/// Turning this into a `NodeHeader`
reference
is always safe.
pub
struct
NodeRef
<
BorrowType
,
K
,
V
,
Type
>
{
height
:
usize
,
node
:
NonNull
<
LeafNode
<
K
,
V
>>
,
//
This
is null unless the borrow type is `Mut`
//
`root`
is null unless the borrow type is `Mut`
root
:
*
const
Root
<
K
,
V
>
,
_
marker
:
PhantomData
<
(
BorrowType
,
Type
)
>
,
}
...
...
@@ -370,8 +370,13 @@ fn reborrow(&self) -> NodeRef<marker::Immut<'_>, K, V, Type> {
NodeRef
{
height
:
self
.height
,
node
:
self
.node
,
root
:
self
.root
,
_
marker
:
PhantomData
}
}
/// Assert that this is indeed a proper leaf node, and not the shared root.
/// Exposes the leaf "portion" of any leaf or internal node that is not the shared root.
/// If the node is a leaf, this function simply opens up its data.
/// If the node is an internal node, so not a leaf, it does have all the data a leaf has
/// (header, keys and values), and this function exposes that.
/// See `NodeRef` on why the node may not be a shared root.
unsafe
fn
as_leaf
(
&
self
)
->
&
LeafNode
<
K
,
V
>
{
debug_assert!
(
!
self
.is_shared_root
());
self
.node
.as_ref
()
}
...
...
@@ -379,14 +384,19 @@ fn as_header(&self) -> &NodeHeader<K, V> {
unsafe
{
&*
(
self
.node
.as_ptr
()
as
*
const
NodeHeader
<
K
,
V
>
)
}
}
/// Returns whether the node is the shared, empty root.
pub
fn
is_shared_root
(
&
self
)
->
bool
{
self
.as_header
()
.is_shared_root
()
}
/// Borrows a view into the keys stored in the node.
/// Works on all possible nodes, including the shared root.
pub
fn
keys
(
&
self
)
->
&
[
K
]
{
self
.reborrow
()
.into_key_slice
()
}
/// Borrows a view into the values stored in the node.
/// The caller must ensure that the node is not the shared root.
fn
vals
(
&
self
)
->
&
[
V
]
{
self
.reborrow
()
.into_val_slice
()
}
...
...
@@ -491,16 +501,24 @@ unsafe fn reborrow_mut(&mut self) -> NodeRef<marker::Mut<'_>, K, V, Type> {
NodeRef
{
height
:
self
.height
,
node
:
self
.node
,
root
:
self
.root
,
_
marker
:
PhantomData
}
}
/// Exposes the leaf "portion" of any leaf or internal node for writing.
/// If the node is a leaf, this function simply opens up its data.
/// If the node is an internal node, so not a leaf, it does have all the data a leaf has
/// (header, keys and values), and this function exposes that.
///
/// Returns a raw ptr to avoid asserting exclusive access to the entire node.
/// This also implies you can invoke this member on the shared root, but the resulting pointer
/// might not be properly aligned and definitely would not allow accessing keys and values.
fn
as_leaf_mut
(
&
mut
self
)
->
*
mut
LeafNode
<
K
,
V
>
{
// We are mutable, so we cannot be the shared root, so accessing this as a leaf is okay.
self
.node
.as_ptr
()
}
/// The caller must ensure that the node is not the shared root.
fn
keys_mut
(
&
mut
self
)
->
&
mut
[
K
]
{
unsafe
{
self
.reborrow_mut
()
.into_key_slice_mut
()
}
}
/// The caller must ensure that the node is not the shared root.
fn
vals_mut
(
&
mut
self
)
->
&
mut
[
V
]
{
unsafe
{
self
.reborrow_mut
()
.into_val_slice_mut
()
}
}
...
...
@@ -551,9 +569,10 @@ fn into_key_slice(self) -> &'a [K] {
}
}
/// The caller must ensure that the node is not the shared root.
fn
into_val_slice
(
self
)
->
&
'a
[
V
]
{
debug_assert!
(
!
self
.is_shared_root
());
// We cannot be the shared root, so `as_leaf` is okay
// We cannot be the shared root, so `as_leaf` is okay
.
unsafe
{
slice
::
from_raw_parts
(
MaybeUninit
::
first_ptr
(
&
self
.as_leaf
()
.vals
),
self
.len
())
}
}
...
...
@@ -587,6 +606,7 @@ fn into_key_slice_mut(mut self) -> &'a mut [K] {
}
}
/// The caller must ensure that the node is not the shared root.
fn
into_val_slice_mut
(
mut
self
)
->
&
'a
mut
[
V
]
{
debug_assert!
(
!
self
.is_shared_root
());
unsafe
{
...
...
@@ -597,6 +617,7 @@ fn into_val_slice_mut(mut self) -> &'a mut [V] {
}
}
/// The caller must ensure that the node is not the shared root.
fn
into_slices_mut
(
mut
self
)
->
(
&
'a
mut
[
K
],
&
'a
mut
[
V
])
{
debug_assert!
(
!
self
.is_shared_root
());
// We cannot use the getters here, because calling the second one
...
...
@@ -655,6 +676,7 @@ pub fn push(&mut self, key: K, val: V, edge: Root<K, V>) {
// Necessary for correctness, but this is an internal module
debug_assert!
(
edge
.height
==
self
.height
-
1
);
debug_assert!
(
self
.len
()
<
CAPACITY
);
debug_assert!
(
!
self
.is_shared_root
());
let
idx
=
self
.len
();
...
...
@@ -686,6 +708,7 @@ pub fn push_front(&mut self, key: K, val: V, edge: Root<K, V>) {
// Necessary for correctness, but this is an internal module
debug_assert!
(
edge
.height
==
self
.height
-
1
);
debug_assert!
(
self
.len
()
<
CAPACITY
);
debug_assert!
(
!
self
.is_shared_root
());
unsafe
{
slice_insert
(
self
.keys_mut
(),
0
,
key
);
...
...
@@ -773,6 +796,7 @@ pub fn pop_front(&mut self) -> (K, V, Option<Root<K, V>>) {
}
}
/// The caller must ensure that the node is not the shared root.
fn
into_kv_pointers_mut
(
mut
self
)
->
(
*
mut
K
,
*
mut
V
)
{
(
self
.keys_mut
()
.as_mut_ptr
(),
self
.vals_mut
()
.as_mut_ptr
())
}
...
...
@@ -1116,8 +1140,8 @@ pub fn split(mut self) -> (NodeRef<marker::Mut<'a>, K, V, marker::Leaf>, K, V, R
}
}
/// Removes the key/value pair pointed to by this handle
, returning the edge between th
e
///
now adjacent key/value pairs
to the left and right of this handle.
/// Removes the key/value pair pointed to by this handle
and returns it, along with the edg
e
///
between the now adjacent key/value pairs (if any)
to the left and right of this handle.
pub
fn
remove
(
mut
self
,
)
->
(
Handle
<
NodeRef
<
marker
::
Mut
<
'a
>
,
K
,
V
,
marker
::
Leaf
>
,
marker
::
Edge
>
,
K
,
V
)
{
...
...
@@ -1260,7 +1284,7 @@ pub fn merge(
}
}
/// This removes a key/value pair from the left child and
replaces it with the key/value pair
/// This removes a key/value pair from the left child and
places it in the key/value storage
/// pointed to by this handle while pushing the old key/value pair of this handle into the right
/// child.
pub
fn
steal_left
(
&
mut
self
)
{
...
...
@@ -1277,7 +1301,7 @@ pub fn steal_left(&mut self) {
}
}
/// This removes a key/value pair from the right child and
replaces it with the key/value pair
/// This removes a key/value pair from the right child and
places it in the key/value storage
/// pointed to by this handle while pushing the old key/value pair of this handle into the left
/// child.
pub
fn
steal_right
(
&
mut
self
)
{
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录