Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
int
Rust
提交
6b08683e
R
Rust
项目概览
int
/
Rust
12 个月 前同步成功
通知
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,发现更多精彩内容 >>
提交
6b08683e
编写于
1月 29, 2013
作者:
D
Daniel Micay
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
add intersection and union to the Set trait
上级
99eb4ddd
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
105 addition
and
17 deletion
+105
-17
src/libcore/container.rs
src/libcore/container.rs
+6
-0
src/libcore/hashmap.rs
src/libcore/hashmap.rs
+83
-1
src/libstd/treemap.rs
src/libstd/treemap.rs
+16
-16
未找到文件。
src/libcore/container.rs
浏览文件 @
6b08683e
...
...
@@ -81,4 +81,10 @@ pub trait Set<T>: Mutable {
/// Visit the values representing the symmetric difference
pure
fn
symmetric_difference
(
&
self
,
other
:
&
self
,
f
:
fn
(
&
T
)
->
bool
);
/// Visit the values representing the intersection
pure
fn
intersection
(
&
self
,
other
:
&
self
,
f
:
fn
(
&
T
)
->
bool
);
/// Visit the values representing the union
pure
fn
union
(
&
self
,
other
:
&
self
,
f
:
fn
(
&
T
)
->
bool
);
}
src/libcore/hashmap.rs
浏览文件 @
6b08683e
...
...
@@ -474,7 +474,7 @@ fn remove(&mut self, value: &T) -> bool { self.map.remove(value) }
pure
fn
difference
(
&
self
,
other
:
&
LinearSet
<
T
>
,
f
:
fn
(
&
T
)
->
bool
)
{
for
self
.each
|
v
|
{
if
!
other
.contains
(
v
)
{
if
!
f
(
v
)
{
return
;
}
if
!
f
(
v
)
{
return
}
}
}
}
...
...
@@ -485,6 +485,28 @@ fn remove(&mut self, value: &T) -> bool { self.map.remove(value) }
self
.difference
(
other
,
f
);
other
.difference
(
self
,
f
);
}
/// Visit the values representing the intersection
pure
fn
intersection
(
&
self
,
other
:
&
LinearSet
<
T
>
,
f
:
fn
(
&
T
)
->
bool
)
{
for
self
.each
|
v
|
{
if
other
.contains
(
v
)
{
if
!
f
(
v
)
{
return
}
}
}
}
/// Visit the values representing the union
pure
fn
union
(
&
self
,
other
:
&
LinearSet
<
T
>
,
f
:
fn
(
&
T
)
->
bool
)
{
for
self
.each
|
v
|
{
if
!
f
(
v
)
{
return
}
}
for
other
.each
|
v
|
{
if
!
self
.contains
(
v
)
{
if
!
f
(
v
)
{
return
}
}
}
}
}
pub
impl
<
T
:
Hash
IterBytes
Eq
>
LinearSet
<
T
>
{
...
...
@@ -698,6 +720,36 @@ fn test_subset_and_superset() {
assert
b
.is_superset
(
&
a
);
}
#[test]
fn
test_intersection
()
{
let
mut
a
=
linear
::
LinearSet
::
new
();
let
mut
b
=
linear
::
LinearSet
::
new
();
assert
a
.insert
(
11
);
assert
a
.insert
(
1
);
assert
a
.insert
(
3
);
assert
a
.insert
(
77
);
assert
a
.insert
(
103
);
assert
a
.insert
(
5
);
assert
a
.insert
(
-
5
);
assert
b
.insert
(
2
);
assert
b
.insert
(
11
);
assert
b
.insert
(
77
);
assert
b
.insert
(
-
9
);
assert
b
.insert
(
-
42
);
assert
b
.insert
(
5
);
assert
b
.insert
(
3
);
let
mut
i
=
0
;
let
expected
=
[
3
,
5
,
11
,
77
];
for
a
.intersection
(
&
b
)
|
x
|
{
assert
vec
::
contains
(
expected
,
x
);
i
+=
1
}
assert
i
==
expected
.len
();
}
#[test]
fn
test_difference
()
{
let
mut
a
=
linear
::
LinearSet
::
new
();
...
...
@@ -746,4 +798,34 @@ fn test_symmetric_difference() {
}
assert
i
==
expected
.len
();
}
#[test]
fn
test_union
()
{
let
mut
a
=
linear
::
LinearSet
::
new
();
let
mut
b
=
linear
::
LinearSet
::
new
();
assert
a
.insert
(
1
);
assert
a
.insert
(
3
);
assert
a
.insert
(
5
);
assert
a
.insert
(
9
);
assert
a
.insert
(
11
);
assert
a
.insert
(
16
);
assert
a
.insert
(
19
);
assert
a
.insert
(
24
);
assert
b
.insert
(
-
2
);
assert
b
.insert
(
1
);
assert
b
.insert
(
5
);
assert
b
.insert
(
9
);
assert
b
.insert
(
13
);
assert
b
.insert
(
19
);
let
mut
i
=
0
;
let
expected
=
[
-
2
,
1
,
3
,
5
,
9
,
11
,
13
,
16
,
19
,
24
];
for
a
.union
(
&
b
)
|
x
|
{
assert
vec
::
contains
(
expected
,
x
);
i
+=
1
}
assert
i
==
expected
.len
();
}
}
src/libstd/treemap.rs
浏览文件 @
6b08683e
...
...
@@ -432,22 +432,6 @@ fn remove(&mut self, value: &T) -> bool { self.map.remove(value) }
}
}
}
}
impl
<
T
:
Ord
>
TreeSet
<
T
>
{
/// Create an empty TreeSet
static
pure
fn
new
()
->
TreeSet
<
T
>
{
TreeSet
{
map
:
TreeMap
::
new
()}
}
/// Visit all values in reverse order
pure
fn
each_reverse
(
&
self
,
f
:
fn
(
&
T
)
->
bool
)
{
self
.map
.each_key_reverse
(
f
)
}
/// Get a lazy iterator over the values in the set.
/// Requires that it be frozen (immutable).
pure
fn
iter
(
&
self
)
->
TreeSetIterator
/&
self
<
T
>
{
TreeSetIterator
{
iter
:
self
.map
.iter
()}
}
/// Visit the values (in-order) representing the intersection
pure
fn
intersection
(
&
self
,
other
:
&
TreeSet
<
T
>
,
f
:
fn
(
&
T
)
->
bool
)
{
...
...
@@ -516,6 +500,22 @@ impl <T: Ord> TreeSet<T> {
}
}
impl
<
T
:
Ord
>
TreeSet
<
T
>
{
/// Create an empty TreeSet
static
pure
fn
new
()
->
TreeSet
<
T
>
{
TreeSet
{
map
:
TreeMap
::
new
()}
}
/// Visit all values in reverse order
pure
fn
each_reverse
(
&
self
,
f
:
fn
(
&
T
)
->
bool
)
{
self
.map
.each_key_reverse
(
f
)
}
/// Get a lazy iterator over the values in the set.
/// Requires that it be frozen (immutable).
pure
fn
iter
(
&
self
)
->
TreeSetIterator
/&
self
<
T
>
{
TreeSetIterator
{
iter
:
self
.map
.iter
()}
}
}
/// Lazy forward iterator over a set
pub
struct
TreeSetIterator
<
T
>
{
priv
iter
:
TreeMapIterator
<
T
,
()
>
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录