Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
int
Rust
提交
b2b88b32
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,发现更多精彩内容 >>
提交
b2b88b32
编写于
7月 11, 2013
作者:
B
blake2-ppc
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
dlist: Name the type DList for doubly-linked list
上级
a8e7bdd1
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
72 addition
and
72 deletion
+72
-72
src/libextra/dlist.rs
src/libextra/dlist.rs
+67
-67
src/libextra/serialize.rs
src/libextra/serialize.rs
+5
-5
未找到文件。
src/libextra/dlist.rs
浏览文件 @
b2b88b32
...
...
@@ -10,13 +10,13 @@
//! A doubly-linked list with owned nodes.
//!
//! The List allows pushing and popping elements at either end.
//! The
D
List allows pushing and popping elements at either end.
// List is constructed like a singly-linked list over the field `next`.
//
D
List is constructed like a singly-linked list over the field `next`.
// including the last link being None; each Node owns its `next` field.
//
// Backlinks over List::prev are raw pointers that form a full chain in
// Backlinks over
D
List::prev are raw pointers that form a full chain in
// the reverse direction.
use
std
::
cast
;
...
...
@@ -28,7 +28,7 @@
use
container
::
Deque
;
/// A doubly-linked list
pub
struct
List
<
T
>
{
pub
struct
D
List
<
T
>
{
priv
length
:
uint
,
priv
list_head
:
Link
<
T
>
,
priv
list_tail
:
Rawlink
<
Node
<
T
>>
,
...
...
@@ -43,42 +43,42 @@ struct Node<T> {
priv
value
:
T
,
}
/// List iterator
///
D
List iterator
pub
struct
ForwardIterator
<
'self
,
T
>
{
priv
list
:
&
'self
List
<
T
>
,
priv
list
:
&
'self
D
List
<
T
>
,
priv
next
:
&
'self
Link
<
T
>
,
priv
nelem
:
uint
,
}
/// List reverse iterator
///
D
List reverse iterator
pub
struct
ReverseIterator
<
'self
,
T
>
{
priv
list
:
&
'self
List
<
T
>
,
priv
list
:
&
'self
D
List
<
T
>
,
priv
next
:
Rawlink
<
Node
<
T
>>
,
priv
nelem
:
uint
,
}
/// List mutable iterator
///
D
List mutable iterator
pub
struct
MutForwardIterator
<
'self
,
T
>
{
priv
list
:
&
'self
mut
List
<
T
>
,
priv
list
:
&
'self
mut
D
List
<
T
>
,
priv
curs
:
Rawlink
<
Node
<
T
>>
,
priv
nelem
:
uint
,
}
/// List mutable reverse iterator
///
D
List mutable reverse iterator
pub
struct
MutReverseIterator
<
'self
,
T
>
{
priv
list
:
&
'self
mut
List
<
T
>
,
priv
list
:
&
'self
mut
D
List
<
T
>
,
priv
next
:
Rawlink
<
Node
<
T
>>
,
priv
nelem
:
uint
,
}
/// List consuming iterator
///
D
List consuming iterator
pub
struct
ConsumeIterator
<
T
>
{
priv
list
:
List
<
T
>
priv
list
:
D
List
<
T
>
}
/// List reverse consuming iterator
///
D
List reverse consuming iterator
pub
struct
ConsumeRevIterator
<
T
>
{
priv
list
:
List
<
T
>
priv
list
:
D
List
<
T
>
}
/// Rawlink is a type like Option<T> but for holding a raw pointer
...
...
@@ -114,7 +114,7 @@ fn link_with_prev<T>(mut next: ~Node<T>, prev: Rawlink<Node<T>>) -> Link<T> {
Some
(
next
)
}
impl
<
T
>
Container
for
List
<
T
>
{
impl
<
T
>
Container
for
D
List
<
T
>
{
/// O(1)
fn
is_empty
(
&
self
)
->
bool
{
self
.list_head
.is_none
()
...
...
@@ -125,16 +125,16 @@ fn len(&self) -> uint {
}
}
impl
<
T
>
Mutable
for
List
<
T
>
{
/// Remove all elements from the List
impl
<
T
>
Mutable
for
D
List
<
T
>
{
/// Remove all elements from the
D
List
///
/// O(N)
fn
clear
(
&
mut
self
)
{
*
self
=
List
::
new
()
*
self
=
D
List
::
new
()
}
}
impl
<
T
>
Deque
<
T
>
for
List
<
T
>
{
impl
<
T
>
Deque
<
T
>
for
D
List
<
T
>
{
/// Provide a reference to the front element, or None if the list is empty
fn
front
<
'a
>
(
&
'a
self
)
->
Option
<&
'a
T
>
{
self
.list_head
.chain_ref
(|
x
|
Some
(
&
x
.value
))
...
...
@@ -245,23 +245,23 @@ fn pop_front(&mut self) -> Option<T> {
}
}
impl
<
T
>
List
<
T
>
{
/// Create an empty List
impl
<
T
>
D
List
<
T
>
{
/// Create an empty
D
List
#[inline]
pub
fn
new
()
->
List
<
T
>
{
List
{
list_head
:
None
,
list_tail
:
Rawlink
::
none
(),
length
:
0
}
pub
fn
new
()
->
D
List
<
T
>
{
D
List
{
list_head
:
None
,
list_tail
:
Rawlink
::
none
(),
length
:
0
}
}
/// Add all elements from `other` to the end of the list
///
/// O(1)
pub
fn
append
(
&
mut
self
,
other
:
List
<
T
>
)
{
pub
fn
append
(
&
mut
self
,
other
:
D
List
<
T
>
)
{
match
self
.list_tail
.resolve
()
{
None
=>
*
self
=
other
,
Some
(
tail
)
=>
{
match
other
{
List
{
list_head
:
None
,
list_tail
:
_
,
length
:
_
}
=>
return
,
List
{
list_head
:
Some
(
node
),
list_tail
:
o_tail
,
length
:
o_length
}
=>
{
D
List
{
list_head
:
None
,
list_tail
:
_
,
length
:
_
}
=>
return
,
D
List
{
list_head
:
Some
(
node
),
list_tail
:
o_tail
,
length
:
o_length
}
=>
{
tail
.next
=
link_with_prev
(
node
,
self
.list_tail
);
self
.list_tail
=
o_tail
;
self
.length
+=
o_length
;
...
...
@@ -274,7 +274,7 @@ pub fn append(&mut self, other: List<T>) {
/// Add all elements from `other` to the beginning of the list
///
/// O(1)
pub
fn
prepend
(
&
mut
self
,
mut
other
:
List
<
T
>
)
{
pub
fn
prepend
(
&
mut
self
,
mut
other
:
D
List
<
T
>
)
{
util
::
swap
(
self
,
&
mut
other
);
self
.append
(
other
);
}
...
...
@@ -300,7 +300,7 @@ pub fn insert_when(&mut self, elt: T, f: &fn(&T, &T) -> bool) {
/// Merge, using the function `f`; take `a` if `f(a, b)` is true, else `b`.
///
/// O(max(N, M))
pub
fn
merge
(
&
mut
self
,
mut
other
:
List
<
T
>
,
f
:
&
fn
(
&
T
,
&
T
)
->
bool
)
{
pub
fn
merge
(
&
mut
self
,
mut
other
:
D
List
<
T
>
,
f
:
&
fn
(
&
T
,
&
T
)
->
bool
)
{
{
let
mut
it
=
self
.mut_iter
();
loop
{
...
...
@@ -351,7 +351,7 @@ pub fn consume_rev_iter(self) -> ConsumeRevIterator<T> {
/// Insert sorted in ascending order
///
/// O(N)
impl
<
T
:
cmp
::
TotalOrd
>
List
<
T
>
{
impl
<
T
:
cmp
::
TotalOrd
>
D
List
<
T
>
{
fn
insert_ordered
(
&
mut
self
,
elt
:
T
)
{
self
.insert_when
(
elt
,
|
a
,
b
|
a
.cmp
(
b
)
!=
cmp
::
Less
);
}
...
...
@@ -445,7 +445,7 @@ fn size_hint(&self) -> (uint, Option<uint>) {
}
}
/// Allow mutating the List while iterating
/// Allow mutating the
D
List while iterating
pub
trait
ListInsertion
<
A
>
{
/// Insert `elt` just previous to the most recently yielded element
fn
insert_before
(
&
mut
self
,
elt
:
A
);
...
...
@@ -497,32 +497,32 @@ fn size_hint(&self) -> (uint, Option<uint>) {
}
}
impl
<
A
,
T
:
Iterator
<
A
>>
FromIterator
<
A
,
T
>
for
List
<
A
>
{
fn
from_iterator
(
iterator
:
&
mut
T
)
->
List
<
A
>
{
let
mut
ret
=
List
::
new
();
impl
<
A
,
T
:
Iterator
<
A
>>
FromIterator
<
A
,
T
>
for
D
List
<
A
>
{
fn
from_iterator
(
iterator
:
&
mut
T
)
->
D
List
<
A
>
{
let
mut
ret
=
D
List
::
new
();
for
iterator
.advance
|
elt
|
{
ret
.push_back
(
elt
);
}
ret
}
}
impl
<
A
:
Eq
>
Eq
for
List
<
A
>
{
fn
eq
(
&
self
,
other
:
&
List
<
A
>
)
->
bool
{
impl
<
A
:
Eq
>
Eq
for
D
List
<
A
>
{
fn
eq
(
&
self
,
other
:
&
D
List
<
A
>
)
->
bool
{
self
.len
()
==
other
.len
()
&&
self
.iter
()
.zip
(
other
.iter
())
.all
(|(
a
,
b
)|
a
.eq
(
b
))
}
fn
ne
(
&
self
,
other
:
&
List
<
A
>
)
->
bool
{
fn
ne
(
&
self
,
other
:
&
D
List
<
A
>
)
->
bool
{
!
self
.eq
(
other
)
}
}
impl
<
A
:
Clone
>
Clone
for
List
<
A
>
{
fn
clone
(
&
self
)
->
List
<
A
>
{
impl
<
A
:
Clone
>
Clone
for
D
List
<
A
>
{
fn
clone
(
&
self
)
->
D
List
<
A
>
{
self
.iter
()
.transform
(|
x
|
x
.clone
())
.collect
()
}
}
#[cfg(test)]
pub
fn
check_links
<
T
>
(
list
:
&
List
<
T
>
)
{
pub
fn
check_links
<
T
>
(
list
:
&
D
List
<
T
>
)
{
let
mut
len
=
0u
;
let
mut
last_ptr
:
Option
<&
Node
<
T
>>
=
None
;
let
mut
node_ptr
:
&
Node
<
T
>
;
...
...
@@ -563,7 +563,7 @@ mod tests {
#[test]
fn
test_basic
()
{
let
mut
m
=
List
::
new
::
<~
int
>
();
let
mut
m
=
D
List
::
new
::
<~
int
>
();
assert_eq!
(
m
.pop_front
(),
None
);
assert_eq!
(
m
.pop_back
(),
None
);
assert_eq!
(
m
.pop_front
(),
None
);
...
...
@@ -582,7 +582,7 @@ fn test_basic() {
m
.push_back
(
~
7
);
assert_eq!
(
m
.pop_front
(),
Some
(
~
1
));
let
mut
n
=
List
::
new
();
let
mut
n
=
D
List
::
new
();
n
.push_front
(
2
);
n
.push_front
(
3
);
{
...
...
@@ -602,20 +602,20 @@ fn test_basic() {
}
#[cfg(test)]
fn
generate_test
()
->
List
<
int
>
{
fn
generate_test
()
->
D
List
<
int
>
{
list_from
(
&
[
0
,
1
,
2
,
3
,
4
,
5
,
6
])
}
#[cfg(test)]
fn
list_from
<
T
:
Copy
>
(
v
:
&
[
T
])
->
List
<
T
>
{
fn
list_from
<
T
:
Copy
>
(
v
:
&
[
T
])
->
D
List
<
T
>
{
v
.iter
()
.transform
(|
x
|
copy
*
x
)
.collect
()
}
#[test]
fn
test_append
()
{
{
let
mut
m
=
List
::
new
();
let
mut
n
=
List
::
new
();
let
mut
m
=
D
List
::
new
();
let
mut
n
=
D
List
::
new
();
n
.push_back
(
2
);
m
.append
(
n
);
assert_eq!
(
m
.len
(),
1
);
...
...
@@ -623,8 +623,8 @@ fn test_append() {
check_links
(
&
m
);
}
{
let
mut
m
=
List
::
new
();
let
n
=
List
::
new
();
let
mut
m
=
D
List
::
new
();
let
n
=
D
List
::
new
();
m
.push_back
(
2
);
m
.append
(
n
);
assert_eq!
(
m
.len
(),
1
);
...
...
@@ -647,8 +647,8 @@ fn test_append() {
#[test]
fn
test_prepend
()
{
{
let
mut
m
=
List
::
new
();
let
mut
n
=
List
::
new
();
let
mut
m
=
D
List
::
new
();
let
mut
n
=
D
List
::
new
();
n
.push_back
(
2
);
m
.prepend
(
n
);
assert_eq!
(
m
.len
(),
1
);
...
...
@@ -674,7 +674,7 @@ fn test_iterator() {
for
m
.iter
()
.enumerate
()
.advance
|(
i
,
elt
)|
{
assert_eq!
(
i
as
int
,
*
elt
);
}
let
mut
n
=
List
::
new
();
let
mut
n
=
D
List
::
new
();
assert_eq!
(
n
.iter
()
.next
(),
None
);
n
.push_front
(
4
);
let
mut
it
=
n
.iter
();
...
...
@@ -690,7 +690,7 @@ fn test_rev_iter() {
for
m
.rev_iter
()
.enumerate
()
.advance
|(
i
,
elt
)|
{
assert_eq!
((
6
-
i
)
as
int
,
*
elt
);
}
let
mut
n
=
List
::
new
();
let
mut
n
=
D
List
::
new
();
assert_eq!
(
n
.rev_iter
()
.next
(),
None
);
n
.push_front
(
4
);
let
mut
it
=
n
.rev_iter
();
...
...
@@ -709,7 +709,7 @@ fn test_mut_iter() {
len
-=
1
;
}
assert_eq!
(
len
,
0
);
let
mut
n
=
List
::
new
();
let
mut
n
=
D
List
::
new
();
assert
!
(
n
.mut_iter
()
.next
()
.is_none
());
n
.push_front
(
4
);
let
mut
it
=
n
.mut_iter
();
...
...
@@ -760,12 +760,12 @@ fn test_merge() {
#[test]
fn
test_insert_ordered
()
{
let
mut
n
=
List
::
new
();
let
mut
n
=
D
List
::
new
();
n
.insert_ordered
(
1
);
assert_eq!
(
n
.len
(),
1
);
assert_eq!
(
n
.pop_front
(),
Some
(
1
));
let
mut
m
=
List
::
new
();
let
mut
m
=
D
List
::
new
();
m
.push_back
(
2
);
m
.push_back
(
4
);
m
.insert_ordered
(
3
);
...
...
@@ -779,7 +779,7 @@ fn test_mut_rev_iter() {
for
m
.mut_rev_iter
()
.enumerate
()
.advance
|(
i
,
elt
)|
{
assert_eq!
((
6
-
i
)
as
int
,
*
elt
);
}
let
mut
n
=
List
::
new
();
let
mut
n
=
D
List
::
new
();
assert
!
(
n
.mut_rev_iter
()
.next
()
.is_none
());
n
.push_front
(
4
);
let
mut
it
=
n
.mut_rev_iter
();
...
...
@@ -798,7 +798,7 @@ fn test_send() {
#[test]
fn
test_eq
()
{
let
mut
n
:
List
<
u8
>
=
list_from
([]);
let
mut
n
:
D
List
<
u8
>
=
list_from
([]);
let
mut
m
=
list_from
([]);
assert_eq!
(
&
n
,
&
m
);
n
.push_front
(
1
);
...
...
@@ -818,7 +818,7 @@ fn test_fuzz() {
#[cfg(test)]
fn
fuzz_test
(
sz
:
int
)
{
let
mut
m
=
List
::
new
::
<
int
>
();
let
mut
m
=
D
List
::
new
::
<
int
>
();
let
mut
v
=
~
[];
for
int
::
range
(
0
i
,
sz
)
|
i
|
{
check_links
(
&
m
);
...
...
@@ -857,7 +857,7 @@ fn fuzz_test(sz: int) {
fn
bench_collect_into
(
b
:
&
mut
test
::
BenchHarness
)
{
let
v
=
&
[
0
,
..
64
];
do
b
.iter
{
let
_
:
List
<
int
>
=
v
.iter
()
.transform
(|
&
x
|
x
)
.collect
();
let
_
:
D
List
<
int
>
=
v
.iter
()
.transform
(|
&
x
|
x
)
.collect
();
}
}
#[bench]
...
...
@@ -870,7 +870,7 @@ fn bench_collect_into_vec(b: &mut test::BenchHarness) {
#[bench]
fn
bench_push_front
(
b
:
&
mut
test
::
BenchHarness
)
{
let
mut
m
=
List
::
new
::
<
int
>
();
let
mut
m
=
D
List
::
new
::
<
int
>
();
do
b
.iter
{
m
.push_front
(
0
);
}
...
...
@@ -886,7 +886,7 @@ fn bench_push_front_vec_size10(b: &mut test::BenchHarness) {
#[bench]
fn
bench_push_back
(
b
:
&
mut
test
::
BenchHarness
)
{
let
mut
m
=
List
::
new
::
<
int
>
();
let
mut
m
=
D
List
::
new
::
<
int
>
();
do
b
.iter
{
m
.push_back
(
0
);
}
...
...
@@ -901,7 +901,7 @@ fn bench_push_back_vec(b: &mut test::BenchHarness) {
#[bench]
fn
bench_push_back_pop_back
(
b
:
&
mut
test
::
BenchHarness
)
{
let
mut
m
=
List
::
new
::
<
int
>
();
let
mut
m
=
D
List
::
new
::
<
int
>
();
do
b
.iter
{
m
.push_back
(
0
);
m
.pop_back
();
...
...
@@ -919,7 +919,7 @@ fn bench_push_back_pop_back_vec(b: &mut test::BenchHarness) {
#[bench]
fn
bench_iter
(
b
:
&
mut
test
::
BenchHarness
)
{
let
v
=
&
[
0
,
..
128
];
let
m
:
List
<
int
>
=
v
.iter
()
.transform
(|
&
x
|
x
)
.collect
();
let
m
:
D
List
<
int
>
=
v
.iter
()
.transform
(|
&
x
|
x
)
.collect
();
do
b
.iter
{
for
m
.iter
()
.advance
|
_
|
{}
}
...
...
@@ -927,7 +927,7 @@ fn bench_iter(b: &mut test::BenchHarness) {
#[bench]
fn
bench_iter_mut
(
b
:
&
mut
test
::
BenchHarness
)
{
let
v
=
&
[
0
,
..
128
];
let
mut
m
:
List
<
int
>
=
v
.iter
()
.transform
(|
&
x
|
x
)
.collect
();
let
mut
m
:
D
List
<
int
>
=
v
.iter
()
.transform
(|
&
x
|
x
)
.collect
();
do
b
.iter
{
for
m
.mut_iter
()
.advance
|
_
|
{}
}
...
...
@@ -935,7 +935,7 @@ fn bench_iter_mut(b: &mut test::BenchHarness) {
#[bench]
fn
bench_iter_rev
(
b
:
&
mut
test
::
BenchHarness
)
{
let
v
=
&
[
0
,
..
128
];
let
m
:
List
<
int
>
=
v
.iter
()
.transform
(|
&
x
|
x
)
.collect
();
let
m
:
D
List
<
int
>
=
v
.iter
()
.transform
(|
&
x
|
x
)
.collect
();
do
b
.iter
{
for
m
.rev_iter
()
.advance
|
_
|
{}
}
...
...
@@ -943,7 +943,7 @@ fn bench_iter_rev(b: &mut test::BenchHarness) {
#[bench]
fn
bench_iter_mut_rev
(
b
:
&
mut
test
::
BenchHarness
)
{
let
v
=
&
[
0
,
..
128
];
let
mut
m
:
List
<
int
>
=
v
.iter
()
.transform
(|
&
x
|
x
)
.collect
();
let
mut
m
:
D
List
<
int
>
=
v
.iter
()
.transform
(|
&
x
|
x
)
.collect
();
do
b
.iter
{
for
m
.mut_rev_iter
()
.advance
|
_
|
{}
}
...
...
src/libextra/serialize.rs
浏览文件 @
b2b88b32
...
...
@@ -25,7 +25,7 @@
use
std
::
vec
;
use
ringbuf
::
RingBuf
;
use
container
::
Deque
;
use
dlist
::
List
;
use
dlist
::
D
List
;
use
treemap
::{
TreeMap
,
TreeSet
};
pub
trait
Encoder
{
...
...
@@ -653,7 +653,7 @@ fn decode(d: &mut D) -> (T0, T1, T2, T3, T4) {
impl
<
S
:
Encoder
,
T
:
Encodable
<
S
>
+
Copy
>
Encodable
<
S
>
for
List
<
T
>
{
>
Encodable
<
S
>
for
D
List
<
T
>
{
fn
encode
(
&
self
,
s
:
&
mut
S
)
{
do
s
.emit_seq
(
self
.len
())
|
s
|
{
let
mut
i
=
0
;
...
...
@@ -665,9 +665,9 @@ fn encode(&self, s: &mut S) {
}
}
impl
<
D
:
Decoder
,
T
:
Decodable
<
D
>>
Decodable
<
D
>
for
List
<
T
>
{
fn
decode
(
d
:
&
mut
D
)
->
List
<
T
>
{
let
mut
list
=
List
::
new
();
impl
<
D
:
Decoder
,
T
:
Decodable
<
D
>>
Decodable
<
D
>
for
D
List
<
T
>
{
fn
decode
(
d
:
&
mut
D
)
->
D
List
<
T
>
{
let
mut
list
=
D
List
::
new
();
do
d
.read_seq
|
d
,
len
|
{
for
uint
::
range
(
0
,
len
)
|
i
|
{
list
.push_back
(
d
.read_seq_elt
(
i
,
|
d
|
Decodable
::
decode
(
d
)));
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录