Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
int
Rust
提交
4bedc314
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,发现更多精彩内容 >>
提交
4bedc314
编写于
5月 21, 2018
作者:
M
Michael Woerister
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Cleanup SortedMap by wrapping element lookup in a method.
上级
e850d78b
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
16 addition
and
21 deletion
+16
-21
src/librustc_data_structures/sorted_map.rs
src/librustc_data_structures/sorted_map.rs
+16
-21
未找到文件。
src/librustc_data_structures/sorted_map.rs
浏览文件 @
4bedc314
...
...
@@ -42,9 +42,7 @@ pub fn from_presorted_elements(elements: Vec<(K, V)>) -> SortedMap<K, V>
#[inline]
pub
fn
insert
(
&
mut
self
,
key
:
K
,
mut
value
:
V
)
->
Option
<
V
>
{
let
index
=
self
.data
.binary_search_by
(|
&
(
ref
x
,
_
)|
x
.cmp
(
&
key
));
match
index
{
match
self
.lookup_index_for
(
&
key
)
{
Ok
(
index
)
=>
{
let
mut
slot
=
unsafe
{
self
.data
.get_unchecked_mut
(
index
)
...
...
@@ -61,9 +59,7 @@ pub fn insert(&mut self, key: K, mut value: V) -> Option<V> {
#[inline]
pub
fn
remove
(
&
mut
self
,
key
:
&
K
)
->
Option
<
V
>
{
let
index
=
self
.data
.binary_search_by
(|
&
(
ref
x
,
_
)|
x
.cmp
(
key
));
match
index
{
match
self
.lookup_index_for
(
key
)
{
Ok
(
index
)
=>
{
Some
(
self
.data
.remove
(
index
)
.1
)
}
...
...
@@ -75,9 +71,7 @@ pub fn remove(&mut self, key: &K) -> Option<V> {
#[inline]
pub
fn
get
(
&
self
,
key
:
&
K
)
->
Option
<&
V
>
{
let
index
=
self
.data
.binary_search_by
(|
&
(
ref
x
,
_
)|
x
.cmp
(
key
));
match
index
{
match
self
.lookup_index_for
(
key
)
{
Ok
(
index
)
=>
{
unsafe
{
Some
(
&
self
.data
.get_unchecked
(
index
)
.1
)
...
...
@@ -91,9 +85,7 @@ pub fn get(&self, key: &K) -> Option<&V> {
#[inline]
pub
fn
get_mut
(
&
mut
self
,
key
:
&
K
)
->
Option
<&
mut
V
>
{
let
index
=
self
.data
.binary_search_by
(|
&
(
ref
x
,
_
)|
x
.cmp
(
key
));
match
index
{
match
self
.lookup_index_for
(
key
)
{
Ok
(
index
)
=>
{
unsafe
{
Some
(
&
mut
self
.data
.get_unchecked_mut
(
index
)
.1
)
...
...
@@ -168,12 +160,9 @@ pub fn insert_presorted(&mut self, mut elements: Vec<(K, V)>) {
debug_assert!
(
elements
.windows
(
2
)
.all
(|
w
|
w
[
0
]
.0
<
w
[
1
]
.0
));
let
index
=
{
let
first_element
=
&
elements
[
0
]
.0
;
self
.data
.binary_search_by
(|
&
(
ref
x
,
_
)|
x
.cmp
(
first_element
))
};
let
start_index
=
self
.lookup_index_for
(
&
elements
[
0
]
.0
);
let
drain
=
match
index
{
let
drain
=
match
start_
index
{
Ok
(
index
)
=>
{
let
mut
drain
=
elements
.drain
(
..
);
self
.data
[
index
]
=
drain
.next
()
.unwrap
();
...
...
@@ -200,18 +189,24 @@ pub fn insert_presorted(&mut self, mut elements: Vec<(K, V)>) {
}
}
/// Looks up the key in `self.data` via `slice::binary_search()`.
#[inline(always)]
fn
lookup_index_for
(
&
self
,
key
:
&
K
)
->
Result
<
usize
,
usize
>
{
self
.data
.binary_search_by
(|
&
(
ref
x
,
_
)|
x
.cmp
(
key
))
}
#[inline]
fn
range_slice_indices
<
R
>
(
&
self
,
range
:
R
)
->
(
usize
,
usize
)
where
R
:
RangeBounds
<
K
>
{
let
start
=
match
range
.start
()
{
Bound
::
Included
(
ref
k
)
=>
{
match
self
.
data
.binary_search_by
(|
&
(
ref
x
,
_
)|
x
.cmp
(
k
)
)
{
match
self
.
lookup_index_for
(
k
)
{
Ok
(
index
)
|
Err
(
index
)
=>
index
}
}
Bound
::
Excluded
(
ref
k
)
=>
{
match
self
.
data
.binary_search_by
(|
&
(
ref
x
,
_
)|
x
.cmp
(
k
)
)
{
match
self
.
lookup_index_for
(
k
)
{
Ok
(
index
)
=>
index
+
1
,
Err
(
index
)
=>
index
,
}
...
...
@@ -221,13 +216,13 @@ fn range_slice_indices<R>(&self, range: R) -> (usize, usize)
let
end
=
match
range
.end
()
{
Bound
::
Included
(
ref
k
)
=>
{
match
self
.
data
.binary_search_by
(|
&
(
ref
x
,
_
)|
x
.cmp
(
k
)
)
{
match
self
.
lookup_index_for
(
k
)
{
Ok
(
index
)
=>
index
+
1
,
Err
(
index
)
=>
index
,
}
}
Bound
::
Excluded
(
ref
k
)
=>
{
match
self
.
data
.binary_search_by
(|
&
(
ref
x
,
_
)|
x
.cmp
(
k
)
)
{
match
self
.
lookup_index_for
(
k
)
{
Ok
(
index
)
|
Err
(
index
)
=>
index
,
}
}
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录