Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
int
Rust
提交
f0f4a00e
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,发现更多精彩内容 >>
提交
f0f4a00e
编写于
3月 24, 2013
作者:
D
Daniel Micay
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
smallintmap: add find_mut method
上级
89e2578a
变更
1
显示空白变更内容
内联
并排
Showing
1 changed file
with
28 addition
and
2 deletion
+28
-2
src/libstd/smallintmap.rs
src/libstd/smallintmap.rs
+28
-2
未找到文件。
src/libstd/smallintmap.rs
浏览文件 @
f0f4a00e
...
...
@@ -86,7 +86,7 @@ fn each_value(&self, blk: &fn(value: &V) -> bool) {
self
.each
(|
&
(
_
,
v
)|
blk
(
v
))
}
///
Visit all key-value pairs in order
///
Iterate over the map and mutate the contained values
fn
mutate_values
(
&
mut
self
,
it
:
&
fn
(
&
uint
,
&
'self
mut
V
)
->
bool
)
{
for
uint
::
range
(
0
,
self
.v
.len
())
|
i
|
{
match
self
.v
[
i
]
{
...
...
@@ -96,7 +96,7 @@ fn mutate_values(&mut self, it: &fn(&uint, &'self mut V) -> bool) {
}
}
///
Iterate over the map and mutate the contained values
///
Return a reference to the value corresponding to the key
fn
find
(
&
self
,
key
:
&
uint
)
->
Option
<&
'self
V
>
{
if
*
key
<
self
.v
.len
()
{
match
self
.v
[
*
key
]
{
...
...
@@ -140,6 +140,18 @@ fn new() -> SmallIntMap<V> { SmallIntMap{v: ~[]} }
fn
get
(
&
self
,
key
:
&
uint
)
->
&
'self
V
{
self
.find
(
key
)
.expect
(
"key not present"
)
}
/// Return a mutable reference to the value corresponding to the key
fn
find_mut
(
&
mut
self
,
key
:
&
uint
)
->
Option
<&
'self
mut
V
>
{
if
*
key
<
self
.v
.len
()
{
match
self
.v
[
*
key
]
{
Some
(
ref
mut
value
)
=>
Some
(
value
),
None
=>
None
}
}
else
{
None
}
}
}
pub
impl
<
V
:
Copy
>
SmallIntMap
<
V
>
{
...
...
@@ -160,6 +172,20 @@ fn update(&mut self, key: uint, newval: V, ff: &fn(V, V) -> V) -> bool {
#[cfg(test)]
mod
tests
{
use
super
::
SmallIntMap
;
use
core
::
prelude
::
*
;
#[test]
fn
test_find_mut
()
{
let
mut
m
=
SmallIntMap
::
new
();
fail_unless!
(
m
.insert
(
1
,
12
));
fail_unless!
(
m
.insert
(
2
,
8
));
fail_unless!
(
m
.insert
(
5
,
14
));
let
new
=
100
;
match
m
.find_mut
(
&
5
)
{
None
=>
fail
!
(),
Some
(
x
)
=>
*
x
=
new
}
assert_eq!
(
m
.find
(
&
5
),
Some
(
&
new
));
}
#[test]
fn
test_len
()
{
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录