Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
int
Rust
提交
faa8f8ff
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,发现更多精彩内容 >>
提交
faa8f8ff
编写于
6月 29, 2013
作者:
H
Huon Wilson
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Convert vec::{bsearch, bsearch_elem} to methods.
上级
562dea18
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
95 addition
and
86 deletion
+95
-86
src/etc/unicode.py
src/etc/unicode.py
+2
-2
src/libstd/prelude.rs
src/libstd/prelude.rs
+1
-1
src/libstd/unicode.rs
src/libstd/unicode.rs
+4
-4
src/libstd/vec.rs
src/libstd/vec.rs
+88
-79
未找到文件。
src/etc/unicode.py
浏览文件 @
faa8f8ff
...
...
@@ -124,9 +124,9 @@ def emit_bsearch_range_table(f):
f
.
write
(
"""
fn bsearch_range_table(c: char, r: &'static [(char,char)]) -> bool {
use cmp::{Equal, Less, Greater};
use vec::
bsearch
;
use vec::
ImmutableVector
;
use option::None;
(do
bsearch(r)
|&(lo,hi)| {
(do
r.bsearch
|&(lo,hi)| {
if lo <= c && c <= hi { Equal }
else if hi < c { Less }
else { Greater }
...
...
src/libstd/prelude.rs
浏览文件 @
faa8f8ff
...
...
@@ -73,7 +73,7 @@
pub
use
tuple
::{
ImmutableTuple6
,
ImmutableTuple7
,
ImmutableTuple8
,
ImmutableTuple9
};
pub
use
tuple
::{
ImmutableTuple10
,
ImmutableTuple11
,
ImmutableTuple12
};
pub
use
vec
::{
VectorVector
,
CopyableVector
,
ImmutableVector
};
pub
use
vec
::{
ImmutableEqVector
,
ImmutableCopyableVector
};
pub
use
vec
::{
ImmutableEqVector
,
Immutable
TotalOrdVector
,
Immutable
CopyableVector
};
pub
use
vec
::{
OwnedVector
,
OwnedCopyableVector
,
MutableVector
};
pub
use
io
::{
Reader
,
ReaderUtil
,
Writer
,
WriterUtil
};
...
...
src/libstd/unicode.rs
浏览文件 @
faa8f8ff
...
...
@@ -16,9 +16,9 @@ pub mod general_category {
fn
bsearch_range_table
(
c
:
char
,
r
:
&
'static
[(
char
,
char
)])
->
bool
{
use
cmp
::{
Equal
,
Less
,
Greater
};
use
vec
::
bsearch
;
use
vec
::
ImmutableVector
;
use
option
::
None
;
(
do
bsearch
(
r
)
|
&
(
lo
,
hi
)|
{
(
do
r
.bsearch
|
&
(
lo
,
hi
)|
{
if
lo
<=
c
&&
c
<=
hi
{
Equal
}
else
if
hi
<
c
{
Less
}
else
{
Greater
}
...
...
@@ -1451,9 +1451,9 @@ pub mod derived_property {
fn
bsearch_range_table
(
c
:
char
,
r
:
&
'static
[(
char
,
char
)])
->
bool
{
use
cmp
::{
Equal
,
Less
,
Greater
};
use
vec
::
bsearch
;
use
vec
::
ImmutableVector
;
use
option
::
None
;
(
do
bsearch
(
r
)
|
&
(
lo
,
hi
)|
{
(
do
r
.bsearch
|
&
(
lo
,
hi
)|
{
if
lo
<=
c
&&
c
<=
hi
{
Equal
}
else
if
hi
<
c
{
Less
}
else
{
Greater
}
...
...
src/libstd/vec.rs
浏览文件 @
faa8f8ff
...
...
@@ -646,44 +646,6 @@ pub fn connect_vec(&self, sep: &T) -> ~[T] {
}
}
/**
* Binary search a sorted vector with a comparator function.
*
* The comparator should implement an order consistent with the sort
* order of the underlying vector, returning an order code that indicates
* whether its argument is `Less`, `Equal` or `Greater` the desired target.
*
* Returns the index where the comparator returned `Equal`, or `None` if
* not found.
*/
pub
fn
bsearch
<
T
>
(
v
:
&
[
T
],
f
:
&
fn
(
&
T
)
->
Ordering
)
->
Option
<
uint
>
{
let
mut
base
:
uint
=
0
;
let
mut
lim
:
uint
=
v
.len
();
while
lim
!=
0
{
let
ix
=
base
+
(
lim
>>
1
);
match
f
(
&
v
[
ix
])
{
Equal
=>
return
Some
(
ix
),
Less
=>
{
base
=
ix
+
1
;
lim
-=
1
;
}
Greater
=>
()
}
lim
>>=
1
;
}
return
None
;
}
/**
* Binary search a sorted vector for a given element.
*
* Returns the index of the element or None if not found.
*/
pub
fn
bsearch_elem
<
T
:
TotalOrd
>
(
v
:
&
[
T
],
x
:
&
T
)
->
Option
<
uint
>
{
bsearch
(
v
,
|
p
|
p
.cmp
(
x
))
}
// FIXME: if issue #586 gets implemented, could have a postcondition
// saying the two result lists have the same length -- or, could
// return a nominal record with a constraint saying that, instead of
...
...
@@ -1119,6 +1081,8 @@ pub trait ImmutableVector<'self, T> {
fn
flat_map
<
U
>
(
&
self
,
f
:
&
fn
(
t
:
&
T
)
->
~
[
U
])
->
~
[
U
];
fn
filter_mapped
<
U
:
Copy
>
(
&
self
,
f
:
&
fn
(
t
:
&
T
)
->
Option
<
U
>
)
->
~
[
U
];
unsafe
fn
unsafe_ref
(
&
self
,
index
:
uint
)
->
*
T
;
fn
bsearch
(
&
self
,
f
:
&
fn
(
&
T
)
->
Ordering
)
->
Option
<
uint
>
;
}
/// Extension methods for vectors
...
...
@@ -1264,6 +1228,35 @@ unsafe fn unsafe_ref(&self, index: uint) -> *T {
let
(
ptr
,
_
):
(
*
T
,
uint
)
=
transmute
(
*
self
);
ptr
.offset
(
index
)
}
/**
* Binary search a sorted vector with a comparator function.
*
* The comparator should implement an order consistent with the sort
* order of the underlying vector, returning an order code that indicates
* whether its argument is `Less`, `Equal` or `Greater` the desired target.
*
* Returns the index where the comparator returned `Equal`, or `None` if
* not found.
*/
fn
bsearch
(
&
self
,
f
:
&
fn
(
&
T
)
->
Ordering
)
->
Option
<
uint
>
{
let
mut
base
:
uint
=
0
;
let
mut
lim
:
uint
=
self
.len
();
while
lim
!=
0
{
let
ix
=
base
+
(
lim
>>
1
);
match
f
(
&
self
[
ix
])
{
Equal
=>
return
Some
(
ix
),
Less
=>
{
base
=
ix
+
1
;
lim
-=
1
;
}
Greater
=>
()
}
lim
>>=
1
;
}
return
None
;
}
}
#[allow(missing_doc)]
...
...
@@ -1293,6 +1286,22 @@ fn contains(&self, x: &T) -> bool {
}
}
#[allow(missing_doc)]
pub
trait
ImmutableTotalOrdVector
<
T
:
TotalOrd
>
{
fn
bsearch_elem
(
&
self
,
x
:
&
T
)
->
Option
<
uint
>
;
}
impl
<
'self
,
T
:
TotalOrd
>
ImmutableTotalOrdVector
<
T
>
for
&
'self
[
T
]
{
/**
* Binary search a sorted vector for a given element.
*
* Returns the index of the element or None if not found.
*/
fn
bsearch_elem
(
&
self
,
x
:
&
T
)
->
Option
<
uint
>
{
self
.bsearch
(|
p
|
p
.cmp
(
x
))
}
}
#[allow(missing_doc)]
pub
trait
ImmutableCopyableVector
<
T
>
{
fn
filtered
(
&
self
,
f
:
&
fn
(
&
T
)
->
bool
)
->
~
[
T
];
...
...
@@ -2841,47 +2850,47 @@ fn test_rposition() {
#[test]
fn
test_bsearch_elem
()
{
assert_eq!
(
bsearch_elem
([
1
,
2
,
3
,
4
,
5
],
&
5
),
Some
(
4
));
assert_eq!
(
bsearch_elem
([
1
,
2
,
3
,
4
,
5
],
&
4
),
Some
(
3
));
assert_eq!
(
bsearch_elem
([
1
,
2
,
3
,
4
,
5
],
&
3
),
Some
(
2
));
assert_eq!
(
bsearch_elem
([
1
,
2
,
3
,
4
,
5
],
&
2
),
Some
(
1
));
assert_eq!
(
bsearch_elem
([
1
,
2
,
3
,
4
,
5
],
&
1
),
Some
(
0
));
assert_eq!
(
bsearch_elem
([
2
,
4
,
6
,
8
,
10
],
&
1
),
None
);
assert_eq!
(
bsearch_elem
([
2
,
4
,
6
,
8
,
10
],
&
5
),
None
);
assert_eq!
(
bsearch_elem
([
2
,
4
,
6
,
8
,
10
],
&
4
),
Some
(
1
));
assert_eq!
(
bsearch_elem
([
2
,
4
,
6
,
8
,
10
],
&
10
),
Some
(
4
));
assert_eq!
(
bsearch_elem
([
2
,
4
,
6
,
8
],
&
1
),
None
);
assert_eq!
(
bsearch_elem
([
2
,
4
,
6
,
8
],
&
5
),
None
);
assert_eq!
(
bsearch_elem
([
2
,
4
,
6
,
8
],
&
4
),
Some
(
1
));
assert_eq!
(
bsearch_elem
([
2
,
4
,
6
,
8
],
&
8
),
Some
(
3
));
assert_eq!
(
bsearch_elem
([
2
,
4
,
6
],
&
1
),
None
);
assert_eq!
(
bsearch_elem
([
2
,
4
,
6
],
&
5
),
None
);
assert_eq!
(
bsearch_elem
([
2
,
4
,
6
],
&
4
),
Some
(
1
));
assert_eq!
(
bsearch_elem
([
2
,
4
,
6
],
&
6
),
Some
(
2
));
assert_eq!
(
bsearch_elem
([
2
,
4
],
&
1
),
None
);
assert_eq!
(
bsearch_elem
([
2
,
4
],
&
5
),
None
);
assert_eq!
(
bsearch_elem
([
2
,
4
],
&
2
),
Some
(
0
));
assert_eq!
(
bsearch_elem
([
2
,
4
],
&
4
),
Some
(
1
));
assert_eq!
(
bsearch_elem
([
2
],
&
1
),
None
);
assert_eq!
(
bsearch_elem
([
2
],
&
5
),
None
);
assert_eq!
(
bsearch_elem
([
2
],
&
2
),
Some
(
0
));
assert_eq!
(
bsearch_elem
([],
&
1
),
None
);
assert_eq!
(
bsearch_elem
([],
&
5
),
None
);
assert
!
(
bsearch_elem
([
1
,
1
,
1
,
1
,
1
],
&
1
)
!=
None
);
assert
!
(
bsearch_elem
([
1
,
1
,
1
,
1
,
2
],
&
1
)
!=
None
);
assert
!
(
bsearch_elem
([
1
,
1
,
1
,
2
,
2
],
&
1
)
!=
None
);
assert
!
(
bsearch_elem
([
1
,
1
,
2
,
2
,
2
],
&
1
)
!=
None
);
assert_eq!
(
bsearch_elem
([
1
,
2
,
2
,
2
,
2
],
&
1
),
Some
(
0
));
assert_eq!
(
bsearch_elem
([
1
,
2
,
3
,
4
,
5
],
&
6
),
None
);
assert_eq!
(
bsearch_elem
([
1
,
2
,
3
,
4
,
5
],
&
0
),
None
);
assert_eq!
(
[
1
,
2
,
3
,
4
,
5
]
.bsearch_elem
(
&
5
),
Some
(
4
));
assert_eq!
(
[
1
,
2
,
3
,
4
,
5
]
.bsearch_elem
(
&
4
),
Some
(
3
));
assert_eq!
(
[
1
,
2
,
3
,
4
,
5
]
.bsearch_elem
(
&
3
),
Some
(
2
));
assert_eq!
(
[
1
,
2
,
3
,
4
,
5
]
.bsearch_elem
(
&
2
),
Some
(
1
));
assert_eq!
(
[
1
,
2
,
3
,
4
,
5
]
.bsearch_elem
(
&
1
),
Some
(
0
));
assert_eq!
(
[
2
,
4
,
6
,
8
,
10
]
.bsearch_elem
(
&
1
),
None
);
assert_eq!
(
[
2
,
4
,
6
,
8
,
10
]
.bsearch_elem
(
&
5
),
None
);
assert_eq!
(
[
2
,
4
,
6
,
8
,
10
]
.bsearch_elem
(
&
4
),
Some
(
1
));
assert_eq!
(
[
2
,
4
,
6
,
8
,
10
]
.bsearch_elem
(
&
10
),
Some
(
4
));
assert_eq!
(
[
2
,
4
,
6
,
8
]
.bsearch_elem
(
&
1
),
None
);
assert_eq!
(
[
2
,
4
,
6
,
8
]
.bsearch_elem
(
&
5
),
None
);
assert_eq!
(
[
2
,
4
,
6
,
8
]
.bsearch_elem
(
&
4
),
Some
(
1
));
assert_eq!
(
[
2
,
4
,
6
,
8
]
.bsearch_elem
(
&
8
),
Some
(
3
));
assert_eq!
(
[
2
,
4
,
6
]
.bsearch_elem
(
&
1
),
None
);
assert_eq!
(
[
2
,
4
,
6
]
.bsearch_elem
(
&
5
),
None
);
assert_eq!
(
[
2
,
4
,
6
]
.bsearch_elem
(
&
4
),
Some
(
1
));
assert_eq!
(
[
2
,
4
,
6
]
.bsearch_elem
(
&
6
),
Some
(
2
));
assert_eq!
(
[
2
,
4
]
.bsearch_elem
(
&
1
),
None
);
assert_eq!
(
[
2
,
4
]
.bsearch_elem
(
&
5
),
None
);
assert_eq!
(
[
2
,
4
]
.bsearch_elem
(
&
2
),
Some
(
0
));
assert_eq!
(
[
2
,
4
]
.bsearch_elem
(
&
4
),
Some
(
1
));
assert_eq!
(
[
2
]
.bsearch_elem
(
&
1
),
None
);
assert_eq!
(
[
2
]
.bsearch_elem
(
&
5
),
None
);
assert_eq!
(
[
2
]
.bsearch_elem
(
&
2
),
Some
(
0
));
assert_eq!
(
[]
.bsearch_elem
(
&
1
),
None
);
assert_eq!
(
[]
.bsearch_elem
(
&
5
),
None
);
assert
!
(
[
1
,
1
,
1
,
1
,
1
]
.bsearch_elem
(
&
1
)
!=
None
);
assert
!
(
[
1
,
1
,
1
,
1
,
2
]
.bsearch_elem
(
&
1
)
!=
None
);
assert
!
(
[
1
,
1
,
1
,
2
,
2
]
.bsearch_elem
(
&
1
)
!=
None
);
assert
!
(
[
1
,
1
,
2
,
2
,
2
]
.bsearch_elem
(
&
1
)
!=
None
);
assert_eq!
(
[
1
,
2
,
2
,
2
,
2
]
.bsearch_elem
(
&
1
),
Some
(
0
));
assert_eq!
(
[
1
,
2
,
3
,
4
,
5
]
.bsearch_elem
(
&
6
),
None
);
assert_eq!
(
[
1
,
2
,
3
,
4
,
5
]
.bsearch_elem
(
&
0
),
None
);
}
#[test]
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录