Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
int
Rust
提交
1ce7902a
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,发现更多精彩内容 >>
提交
1ce7902a
编写于
8月 24, 2012
作者:
M
Michael Sullivan
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Add a Buildable interface for constructing general sequences. Work on #2921.
上级
c87e9a58
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
125 addition
and
9 deletion
+125
-9
src/libcore/at_vec.rs
src/libcore/at_vec.rs
+1
-4
src/libcore/iter.rs
src/libcore/iter.rs
+122
-0
src/libcore/vec.rs
src/libcore/vec.rs
+2
-5
未找到文件。
src/libcore/at_vec.rs
浏览文件 @
1ce7902a
...
...
@@ -24,9 +24,6 @@ fn vec_reserve_shared_actual(++t: *sys::TypeDesc,
fn
move_val_init
<
T
>
(
&
dst
:
T
,
-
src
:
T
);
}
/// A function used to initialize the elements of a vector
type
InitOp
<
T
>
=
fn
(
uint
)
->
T
;
/// Returns the number of elements the vector can hold without reallocating
#[inline(always)]
pure
fn
capacity
<
T
>
(
&&
v
:
@
[
const
T
])
->
uint
{
...
...
@@ -115,7 +112,7 @@ fn vec_reserve_shared_actual(++t: *sys::TypeDesc,
* Creates an immutable vector of size `n_elts` and initializes the elements
* to the value returned by the function `op`.
*/
pure
fn
from_fn
<
T
>
(
n_elts
:
uint
,
op
:
InitOp
<
T
>
)
->
@
[
T
]
{
pure
fn
from_fn
<
T
>
(
n_elts
:
uint
,
op
:
iter
::
InitOp
<
T
>
)
->
@
[
T
]
{
do
build_sized
(
n_elts
)
|
push
|
{
let
mut
i
:
uint
=
0u
;
while
i
<
n_elts
{
push
(
op
(
i
));
i
+=
1u
;
}
...
...
src/libcore/iter.rs
浏览文件 @
1ce7902a
/// A function used to initialize the elements of a sequence
type
InitOp
<
T
>
=
fn
(
uint
)
->
T
;
trait
BaseIter
<
A
>
{
pure
fn
each
(
blk
:
fn
(
A
)
->
bool
);
pure
fn
size_hint
()
->
option
<
uint
>
;
...
...
@@ -29,6 +32,26 @@ trait CopyableIter<A:copy> {
pure
fn
find
(
p
:
fn
(
A
)
->
bool
)
->
option
<
A
>
;
}
// A trait for sequences that can be by imperatively pushing elements
// onto them.
trait
Buildable
<
A
>
{
/**
* Builds a buildable sequence by calling a provided function with
* an argument function that pushes an element onto the back of
* the sequence.
* This version takes an initial size for the sequence.
*
* # Arguments
*
* * size - A hint for an initial size of the sequence
* * builder - A function that will construct the sequence. It recieves
* as an argument a function that will push an element
* onto the sequence being constructed.
*/
static
pure
fn
build_sized
(
size
:
uint
,
builder
:
fn
(
push
:
pure
fn
(
+
A
)))
->
self
;
}
pure
fn
eachi
<
A
,
IA
:
BaseIter
<
A
>>
(
self
:
IA
,
blk
:
fn
(
uint
,
A
)
->
bool
)
{
let
mut
i
=
0u
;
for
self
.each
|
a
|
{
...
...
@@ -172,6 +195,105 @@ trait CopyableIter<A:copy> {
return
none
;
}
// Some functions for just building
/**
* Builds a sequence by calling a provided function with an argument
* function that pushes an element to the back of a sequence.
*
* # Arguments
*
* * builder - A function that will construct the sequence. It recieves
* as an argument a function that will push an element
* onto the sequence being constructed.
*/
#[inline(always)]
pure
fn
build
<
A
,
B
:
Buildable
<
A
>>
(
builder
:
fn
(
push
:
pure
fn
(
+
A
)))
->
B
{
build_sized
(
4
,
builder
)
}
/**
* Builds a sequence by calling a provided function with an argument
* function that pushes an element to the back of a sequence.
* This version takes an initial size for the sequence.
*
* # Arguments
*
* * size - An option, maybe containing initial size of the sequence
* to reserve
* * builder - A function that will construct the sequence. It recieves
* as an argument a function that will push an element
* onto the sequence being constructed.
*/
#[inline(always)]
pure
fn
build_sized_opt
<
A
,
B
:
Buildable
<
A
>>
(
size
:
option
<
uint
>
,
builder
:
fn
(
push
:
pure
fn
(
+
A
)))
->
B
{
build_sized
(
size
.get_default
(
4
),
builder
)
}
// Functions that combine iteration and building
/// Apply a function to each element of an iterable and return the results
fn
map
<
T
,
IT
:
BaseIter
<
T
>
,
U
,
BU
:
Buildable
<
U
>>
(
v
:
IT
,
f
:
fn
(
T
)
->
U
)
->
BU
{
do
build_sized_opt
(
v
.size_hint
())
|
push
|
{
for
v
.each
()
|
elem
|
{
push
(
f
(
elem
));
}
}
}
/**
* Creates and initializes a generic sequence from a function
*
* Creates a generic sequence of size `n_elts` and initializes the elements
* to the value returned by the function `op`.
*/
pure
fn
from_fn
<
T
,
BT
:
Buildable
<
T
>>
(
n_elts
:
uint
,
op
:
InitOp
<
T
>
)
->
BT
{
do
build_sized
(
n_elts
)
|
push
|
{
let
mut
i
:
uint
=
0u
;
while
i
<
n_elts
{
push
(
op
(
i
));
i
+=
1u
;
}
}
}
/**
* Creates and initializes a generic sequence with some element
*
* Creates an immutable vector of size `n_elts` and initializes the elements
* to the value `t`.
*/
pure
fn
from_elem
<
T
:
copy
,
BT
:
Buildable
<
T
>>
(
n_elts
:
uint
,
t
:
T
)
->
BT
{
do
build_sized
(
n_elts
)
|
push
|
{
let
mut
i
:
uint
=
0u
;
while
i
<
n_elts
{
push
(
t
);
i
+=
1u
;
}
}
}
/// Appending two generic sequences
#[inline(always)]
pure
fn
append
<
T
:
copy
,
IT
:
BaseIter
<
T
>
,
BT
:
Buildable
<
T
>>
(
lhs
:
IT
,
rhs
:
IT
)
->
BT
{
let
size_opt
=
lhs
.size_hint
()
.chain
(
|
sz1
|
rhs
.size_hint
()
.map
(|
sz2
|
sz1
+
sz2
));
do
build_sized_opt
(
size_opt
)
|
push
|
{
for
lhs
.each
|
x
|
{
push
(
x
);
}
for
rhs
.each
|
x
|
{
push
(
x
);
}
}
}
/// Copies a generic sequence, possibly converting it to a different
/// type of sequence.
#[inline(always)]
pure
fn
copy_seq
<
T
:
copy
,
IT
:
BaseIter
<
T
>
,
BT
:
Buildable
<
T
>>
(
v
:
IT
)
->
BT
{
do
build_sized_opt
(
v
.size_hint
())
|
push
|
{
for
v
.each
|
x
|
{
push
(
x
);
}
}
}
/*
#[test]
fn test_enumerate() {
...
...
src/libcore/vec.rs
浏览文件 @
1ce7902a
...
...
@@ -110,9 +110,6 @@ fn vec_from_buf_shared(++t: *sys::TypeDesc,
fn
move_val_init
<
T
>
(
&
dst
:
T
,
-
src
:
T
);
}
/// A function used to initialize the elements of a vector
type
InitOp
/&<
T
>
=
fn
(
uint
)
->
T
;
/// Returns true if a vector contains no elements
pure
fn
is_empty
<
T
>
(
v
:
&
[
const
T
])
->
bool
{
as_const_buf
(
v
,
|
_
p
,
len
|
len
==
0u
)
...
...
@@ -188,7 +185,7 @@ fn reserve_at_least<T>(&v: ~[const T], n: uint) {
* Creates an immutable vector of size `n_elts` and initializes the elements
* to the value returned by the function `op`.
*/
pure
fn
from_fn
<
T
>
(
n_elts
:
uint
,
op
:
InitOp
<
T
>
)
->
~
[
T
]
{
pure
fn
from_fn
<
T
>
(
n_elts
:
uint
,
op
:
iter
::
InitOp
<
T
>
)
->
~
[
T
]
{
let
mut
v
=
~
[];
unchecked
{
reserve
(
v
,
n_elts
);}
let
mut
i
:
uint
=
0u
;
...
...
@@ -679,7 +676,7 @@ fn grow<T: copy>(&v: ~[const T], n: uint, initval: T) {
* * init_op - A function to call to retreive each appended element's
* value
*/
fn
grow_fn
<
T
>
(
&
v
:
~
[
const
T
],
n
:
uint
,
op
:
InitOp
<
T
>
)
{
fn
grow_fn
<
T
>
(
&
v
:
~
[
const
T
],
n
:
uint
,
op
:
iter
::
InitOp
<
T
>
)
{
reserve_at_least
(
v
,
len
(
v
)
+
n
);
let
mut
i
:
uint
=
0u
;
while
i
<
n
{
push
(
v
,
op
(
i
));
i
+=
1u
;
}
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录