Make a bunch more of the iteration functions/methods marked pure. Closes #3253.
This commit is contained in:
parent
0f0a9775ba
commit
226fd87199
10 changed files with 146 additions and 120 deletions
|
@ -4,7 +4,7 @@ import ptr::addr_of;
|
||||||
|
|
||||||
export init_op;
|
export init_op;
|
||||||
export capacity;
|
export capacity;
|
||||||
export build_sized, build;
|
export build_sized, build, build_sized_opt;
|
||||||
export map;
|
export map;
|
||||||
export from_fn, from_elem;
|
export from_fn, from_elem;
|
||||||
export unsafe;
|
export unsafe;
|
||||||
|
@ -78,6 +78,24 @@ pure fn build<A>(builder: fn(push: pure fn(+A))) -> @[A] {
|
||||||
build_sized(4, builder)
|
build_sized(4, builder)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Builds a vector by calling a provided function with an argument
|
||||||
|
* function that pushes an element to the back of a vector.
|
||||||
|
* This version takes an initial size for the vector.
|
||||||
|
*
|
||||||
|
* # Arguments
|
||||||
|
*
|
||||||
|
* * size - An option, maybe containing initial size of the vector to reserve
|
||||||
|
* * builder - A function that will construct the vector. It recieves
|
||||||
|
* as an argument a function that will push an element
|
||||||
|
* onto the vector being constructed.
|
||||||
|
*/
|
||||||
|
#[inline(always)]
|
||||||
|
pure fn build_sized_opt<A>(size: option<uint>,
|
||||||
|
builder: fn(push: pure fn(+A))) -> @[A] {
|
||||||
|
build_sized(size.get_default(4), builder)
|
||||||
|
}
|
||||||
|
|
||||||
// Appending
|
// Appending
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
pure fn append<T: copy>(lhs: @[T], rhs: &[const T]) -> @[T] {
|
pure fn append<T: copy>(lhs: @[T], rhs: &[const T]) -> @[T] {
|
||||||
|
|
|
@ -8,7 +8,7 @@ import Path = path::Path;
|
||||||
import tuple::{TupleOps, ExtendedTupleOps};
|
import tuple::{TupleOps, ExtendedTupleOps};
|
||||||
import str::{StrSlice, UniqueStr};
|
import str::{StrSlice, UniqueStr};
|
||||||
import vec::{ConstVector, CopyableVector, ImmutableVector};
|
import vec::{ConstVector, CopyableVector, ImmutableVector};
|
||||||
import vec::{ImmutableCopyableVector, IterTraitExtensions};
|
import vec::{ImmutableCopyableVector};
|
||||||
import iter::{BaseIter, ExtendedIter, CopyableIter, Times, TimesIx};
|
import iter::{BaseIter, ExtendedIter, CopyableIter, Times, TimesIx};
|
||||||
import num::Num;
|
import num::Num;
|
||||||
import ptr::Ptr;
|
import ptr::Ptr;
|
||||||
|
|
|
@ -95,7 +95,7 @@ impl T: iter::Times {
|
||||||
will execute the given function exactly x times. If we assume that \
|
will execute the given function exactly x times. If we assume that \
|
||||||
`x` is an int, this is functionally equivalent to \
|
`x` is an int, this is functionally equivalent to \
|
||||||
`for int::range(0, x) |_i| { /* anything */ }`."]
|
`for int::range(0, x) |_i| { /* anything */ }`."]
|
||||||
fn times(it: fn() -> bool) {
|
pure fn times(it: fn() -> bool) {
|
||||||
if self < 0 {
|
if self < 0 {
|
||||||
fail fmt!{"The .times method expects a nonnegative number, \
|
fail fmt!{"The .times method expects a nonnegative number, \
|
||||||
but found %?", self};
|
but found %?", self};
|
||||||
|
@ -111,7 +111,7 @@ impl T: iter::Times {
|
||||||
impl T: iter::TimesIx {
|
impl T: iter::TimesIx {
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
/// Like `times`, but provides an index
|
/// Like `times`, but provides an index
|
||||||
fn timesi(it: fn(uint) -> bool) {
|
pure fn timesi(it: fn(uint) -> bool) {
|
||||||
let slf = self as uint;
|
let slf = self as uint;
|
||||||
if slf < 0u {
|
if slf < 0u {
|
||||||
fail fmt!{"The .timesi method expects a nonnegative number, \
|
fail fmt!{"The .timesi method expects a nonnegative number, \
|
||||||
|
|
|
@ -6,43 +6,39 @@ import inst::{IMPL_T, EACH, SIZE_HINT};
|
||||||
export extensions;
|
export extensions;
|
||||||
|
|
||||||
impl<A> IMPL_T<A>: iter::BaseIter<A> {
|
impl<A> IMPL_T<A>: iter::BaseIter<A> {
|
||||||
fn each(blk: fn(A) -> bool) { EACH(self, blk) }
|
pure fn each(blk: fn(A) -> bool) { EACH(self, blk) }
|
||||||
fn size_hint() -> option<uint> { SIZE_HINT(self) }
|
pure fn size_hint() -> option<uint> { SIZE_HINT(self) }
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<A> IMPL_T<A>: iter::ExtendedIter<A> {
|
impl<A> IMPL_T<A>: iter::ExtendedIter<A> {
|
||||||
fn eachi(blk: fn(uint, A) -> bool) { iter::eachi(self, blk) }
|
pure fn eachi(blk: fn(uint, A) -> bool) { iter::eachi(self, blk) }
|
||||||
fn all(blk: fn(A) -> bool) -> bool { iter::all(self, blk) }
|
pure fn all(blk: fn(A) -> bool) -> bool { iter::all(self, blk) }
|
||||||
fn any(blk: fn(A) -> bool) -> bool { iter::any(self, blk) }
|
pure fn any(blk: fn(A) -> bool) -> bool { iter::any(self, blk) }
|
||||||
fn foldl<B>(+b0: B, blk: fn(B, A) -> B) -> B {
|
pure fn foldl<B>(+b0: B, blk: fn(B, A) -> B) -> B {
|
||||||
iter::foldl(self, b0, blk)
|
iter::foldl(self, b0, blk)
|
||||||
}
|
}
|
||||||
fn contains(x: A) -> bool { iter::contains(self, x) }
|
pure fn contains(x: A) -> bool { iter::contains(self, x) }
|
||||||
fn count(x: A) -> uint { iter::count(self, x) }
|
pure fn count(x: A) -> uint { iter::count(self, x) }
|
||||||
fn position(f: fn(A) -> bool) -> option<uint> {
|
pure fn position(f: fn(A) -> bool) -> option<uint> {
|
||||||
iter::position(self, f)
|
iter::position(self, f)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<A: copy> IMPL_T<A>: iter::CopyableIter<A> {
|
impl<A: copy> IMPL_T<A>: iter::CopyableIter<A> {
|
||||||
fn filter_to_vec(pred: fn(A) -> bool) -> ~[A] {
|
pure fn filter_to_vec(pred: fn(A) -> bool) -> ~[A] {
|
||||||
iter::filter_to_vec(self, pred)
|
iter::filter_to_vec(self, pred)
|
||||||
}
|
}
|
||||||
fn map_to_vec<B>(op: fn(A) -> B) -> ~[B] { iter::map_to_vec(self, op) }
|
pure fn map_to_vec<B>(op: fn(A) -> B) -> ~[B] {
|
||||||
fn to_vec() -> ~[A] { iter::to_vec(self) }
|
iter::map_to_vec(self, op)
|
||||||
|
}
|
||||||
|
pure fn to_vec() -> ~[A] { iter::to_vec(self) }
|
||||||
|
|
||||||
// FIXME--bug in resolve prevents this from working (#2611)
|
// FIXME--bug in resolve prevents this from working (#2611)
|
||||||
// fn flat_map_to_vec<B:copy,IB:base_iter<B>>(op: fn(A) -> IB) -> ~[B] {
|
// fn flat_map_to_vec<B:copy,IB:base_iter<B>>(op: fn(A) -> IB) -> ~[B] {
|
||||||
// iter::flat_map_to_vec(self, op)
|
// iter::flat_map_to_vec(self, op)
|
||||||
// }
|
// }
|
||||||
|
|
||||||
fn min() -> A { iter::min(self) }
|
pure fn min() -> A { iter::min(self) }
|
||||||
fn max() -> A { iter::max(self) }
|
pure fn max() -> A { iter::max(self) }
|
||||||
|
pure fn find(p: fn(A) -> bool) -> option<A> { iter::find(self, p) }
|
||||||
fn find(p: fn(A) -> bool) -> option<A> {
|
|
||||||
for self.each |i| {
|
|
||||||
if p(i) { return some(i) }
|
|
||||||
}
|
|
||||||
return none;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -29,6 +29,6 @@ pure fn EACH<A>(self: IMPL_T<A>, f: fn(A) -> bool) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn SIZE_HINT<A>(self: IMPL_T<A>) -> option<uint> {
|
pure fn SIZE_HINT<A>(self: IMPL_T<A>) -> option<uint> {
|
||||||
some(self.len())
|
some(self.len())
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,10 +6,10 @@ type IMPL_T<A> = dvec::DVec<A>;
|
||||||
*
|
*
|
||||||
* Attempts to access this dvec during iteration will fail.
|
* Attempts to access this dvec during iteration will fail.
|
||||||
*/
|
*/
|
||||||
fn EACH<A>(self: IMPL_T<A>, f: fn(A) -> bool) {
|
pure fn EACH<A>(self: IMPL_T<A>, f: fn(A) -> bool) {
|
||||||
self.swap(|v| { vec::each(v, f); v })
|
unsafe { self.swap(|v| { vec::each(v, f); v }) }
|
||||||
}
|
}
|
||||||
|
|
||||||
fn SIZE_HINT<A>(self: IMPL_T<A>) -> option<uint> {
|
pure fn SIZE_HINT<A>(self: IMPL_T<A>) -> option<uint> {
|
||||||
some(self.len())
|
some(self.len())
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,7 +7,7 @@ pure fn EACH<A>(self: IMPL_T<A>, f: fn(A) -> bool) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn SIZE_HINT<A>(self: IMPL_T<A>) -> option<uint> {
|
pure fn SIZE_HINT<A>(self: IMPL_T<A>) -> option<uint> {
|
||||||
match self {
|
match self {
|
||||||
none => some(0u),
|
none => some(0u),
|
||||||
some(_) => some(1u)
|
some(_) => some(1u)
|
||||||
|
|
|
@ -1,35 +1,35 @@
|
||||||
trait BaseIter<A> {
|
trait BaseIter<A> {
|
||||||
fn each(blk: fn(A) -> bool);
|
pure fn each(blk: fn(A) -> bool);
|
||||||
fn size_hint() -> option<uint>;
|
pure fn size_hint() -> option<uint>;
|
||||||
}
|
}
|
||||||
|
|
||||||
trait ExtendedIter<A> {
|
trait ExtendedIter<A> {
|
||||||
fn eachi(blk: fn(uint, A) -> bool);
|
pure fn eachi(blk: fn(uint, A) -> bool);
|
||||||
fn all(blk: fn(A) -> bool) -> bool;
|
pure fn all(blk: fn(A) -> bool) -> bool;
|
||||||
fn any(blk: fn(A) -> bool) -> bool;
|
pure fn any(blk: fn(A) -> bool) -> bool;
|
||||||
fn foldl<B>(+b0: B, blk: fn(B, A) -> B) -> B;
|
pure fn foldl<B>(+b0: B, blk: fn(B, A) -> B) -> B;
|
||||||
fn contains(x: A) -> bool;
|
pure fn contains(x: A) -> bool;
|
||||||
fn count(x: A) -> uint;
|
pure fn count(x: A) -> uint;
|
||||||
fn position(f: fn(A) -> bool) -> option<uint>;
|
pure fn position(f: fn(A) -> bool) -> option<uint>;
|
||||||
}
|
}
|
||||||
|
|
||||||
trait Times {
|
trait Times {
|
||||||
fn times(it: fn() -> bool);
|
pure fn times(it: fn() -> bool);
|
||||||
}
|
}
|
||||||
trait TimesIx{
|
trait TimesIx{
|
||||||
fn timesi(it: fn(uint) -> bool);
|
pure fn timesi(it: fn(uint) -> bool);
|
||||||
}
|
}
|
||||||
|
|
||||||
trait CopyableIter<A:copy> {
|
trait CopyableIter<A:copy> {
|
||||||
fn filter_to_vec(pred: fn(A) -> bool) -> ~[A];
|
pure fn filter_to_vec(pred: fn(A) -> bool) -> ~[A];
|
||||||
fn map_to_vec<B>(op: fn(A) -> B) -> ~[B];
|
pure fn map_to_vec<B>(op: fn(A) -> B) -> ~[B];
|
||||||
fn to_vec() -> ~[A];
|
pure fn to_vec() -> ~[A];
|
||||||
fn min() -> A;
|
pure fn min() -> A;
|
||||||
fn max() -> A;
|
pure fn max() -> A;
|
||||||
fn find(p: fn(A) -> bool) -> option<A>;
|
pure fn find(p: fn(A) -> bool) -> option<A>;
|
||||||
}
|
}
|
||||||
|
|
||||||
fn eachi<A,IA:BaseIter<A>>(self: IA, blk: fn(uint, A) -> bool) {
|
pure fn eachi<A,IA:BaseIter<A>>(self: IA, blk: fn(uint, A) -> bool) {
|
||||||
let mut i = 0u;
|
let mut i = 0u;
|
||||||
for self.each |a| {
|
for self.each |a| {
|
||||||
if !blk(i, a) { break; }
|
if !blk(i, a) { break; }
|
||||||
|
@ -37,52 +37,51 @@ fn eachi<A,IA:BaseIter<A>>(self: IA, blk: fn(uint, A) -> bool) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn all<A,IA:BaseIter<A>>(self: IA, blk: fn(A) -> bool) -> bool {
|
pure fn all<A,IA:BaseIter<A>>(self: IA, blk: fn(A) -> bool) -> bool {
|
||||||
for self.each |a| {
|
for self.each |a| {
|
||||||
if !blk(a) { return false; }
|
if !blk(a) { return false; }
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
fn any<A,IA:BaseIter<A>>(self: IA, blk: fn(A) -> bool) -> bool {
|
pure fn any<A,IA:BaseIter<A>>(self: IA, blk: fn(A) -> bool) -> bool {
|
||||||
for self.each |a| {
|
for self.each |a| {
|
||||||
if blk(a) { return true; }
|
if blk(a) { return true; }
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
fn filter_to_vec<A:copy,IA:BaseIter<A>>(self: IA,
|
pure fn filter_to_vec<A:copy,IA:BaseIter<A>>(self: IA,
|
||||||
prd: fn(A) -> bool) -> ~[A] {
|
prd: fn(A) -> bool) -> ~[A] {
|
||||||
let mut result = ~[];
|
do vec::build_sized_opt(self.size_hint()) |push| {
|
||||||
self.size_hint().iter(|hint| vec::reserve(result, hint));
|
for self.each |a| {
|
||||||
for self.each |a| {
|
if prd(a) { push(a); }
|
||||||
if prd(a) { vec::push(result, a); }
|
|
||||||
}
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
fn map_to_vec<A:copy,B,IA:BaseIter<A>>(self: IA, op: fn(A) -> B) -> ~[B] {
|
|
||||||
let mut result = ~[];
|
|
||||||
self.size_hint().iter(|hint| vec::reserve(result, hint));
|
|
||||||
for self.each |a| {
|
|
||||||
vec::push(result, op(a));
|
|
||||||
}
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
fn flat_map_to_vec<A:copy,B:copy,IA:BaseIter<A>,IB:BaseIter<B>>(
|
|
||||||
self: IA, op: fn(A) -> IB) -> ~[B] {
|
|
||||||
|
|
||||||
let mut result = ~[];
|
|
||||||
for self.each |a| {
|
|
||||||
for op(a).each |b| {
|
|
||||||
vec::push(result, b);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return result;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn foldl<A,B,IA:BaseIter<A>>(self: IA, +b0: B, blk: fn(B, A) -> B) -> B {
|
pure fn map_to_vec<A:copy,B,IA:BaseIter<A>>(self: IA, op: fn(A) -> B)
|
||||||
|
-> ~[B] {
|
||||||
|
do vec::build_sized_opt(self.size_hint()) |push| {
|
||||||
|
for self.each |a| {
|
||||||
|
push(op(a));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pure fn flat_map_to_vec<A:copy,B:copy,IA:BaseIter<A>,IB:BaseIter<B>>(
|
||||||
|
self: IA, op: fn(A) -> IB) -> ~[B] {
|
||||||
|
|
||||||
|
do vec::build |push| {
|
||||||
|
for self.each |a| {
|
||||||
|
for op(a).each |b| {
|
||||||
|
push(b);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pure fn foldl<A,B,IA:BaseIter<A>>(self: IA, +b0: B, blk: fn(B, A) -> B) -> B {
|
||||||
let mut b <- b0;
|
let mut b <- b0;
|
||||||
for self.each |a| {
|
for self.each |a| {
|
||||||
b = blk(b, a);
|
b = blk(b, a);
|
||||||
|
@ -90,18 +89,18 @@ fn foldl<A,B,IA:BaseIter<A>>(self: IA, +b0: B, blk: fn(B, A) -> B) -> B {
|
||||||
return b;
|
return b;
|
||||||
}
|
}
|
||||||
|
|
||||||
fn to_vec<A:copy,IA:BaseIter<A>>(self: IA) -> ~[A] {
|
pure fn to_vec<A:copy,IA:BaseIter<A>>(self: IA) -> ~[A] {
|
||||||
foldl::<A,~[A],IA>(self, ~[], |r, a| vec::append(r, ~[a]))
|
foldl::<A,~[A],IA>(self, ~[], |r, a| vec::append(r, ~[a]))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn contains<A,IA:BaseIter<A>>(self: IA, x: A) -> bool {
|
pure fn contains<A,IA:BaseIter<A>>(self: IA, x: A) -> bool {
|
||||||
for self.each |a| {
|
for self.each |a| {
|
||||||
if a == x { return true; }
|
if a == x { return true; }
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
fn count<A,IA:BaseIter<A>>(self: IA, x: A) -> uint {
|
pure fn count<A,IA:BaseIter<A>>(self: IA, x: A) -> uint {
|
||||||
do foldl(self, 0u) |count, value| {
|
do foldl(self, 0u) |count, value| {
|
||||||
if value == x {
|
if value == x {
|
||||||
count + 1u
|
count + 1u
|
||||||
|
@ -111,7 +110,7 @@ fn count<A,IA:BaseIter<A>>(self: IA, x: A) -> uint {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn position<A,IA:BaseIter<A>>(self: IA, f: fn(A) -> bool)
|
pure fn position<A,IA:BaseIter<A>>(self: IA, f: fn(A) -> bool)
|
||||||
-> option<uint> {
|
-> option<uint> {
|
||||||
let mut i = 0;
|
let mut i = 0;
|
||||||
for self.each |a| {
|
for self.each |a| {
|
||||||
|
@ -125,7 +124,7 @@ fn position<A,IA:BaseIter<A>>(self: IA, f: fn(A) -> bool)
|
||||||
// iter interface, such as would provide "reach" in addition to "each". as is,
|
// iter interface, such as would provide "reach" in addition to "each". as is,
|
||||||
// it would have to be implemented with foldr, which is too inefficient.
|
// it would have to be implemented with foldr, which is too inefficient.
|
||||||
|
|
||||||
fn repeat(times: uint, blk: fn() -> bool) {
|
pure fn repeat(times: uint, blk: fn() -> bool) {
|
||||||
let mut i = 0u;
|
let mut i = 0u;
|
||||||
while i < times {
|
while i < times {
|
||||||
if !blk() { break }
|
if !blk() { break }
|
||||||
|
@ -133,7 +132,7 @@ fn repeat(times: uint, blk: fn() -> bool) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn min<A:copy,IA:BaseIter<A>>(self: IA) -> A {
|
pure fn min<A:copy,IA:BaseIter<A>>(self: IA) -> A {
|
||||||
match do foldl::<A,option<A>,IA>(self, none) |a, b| {
|
match do foldl::<A,option<A>,IA>(self, none) |a, b| {
|
||||||
match a {
|
match a {
|
||||||
some(a_) if a_ < b => {
|
some(a_) if a_ < b => {
|
||||||
|
@ -149,7 +148,7 @@ fn min<A:copy,IA:BaseIter<A>>(self: IA) -> A {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn max<A:copy,IA:BaseIter<A>>(self: IA) -> A {
|
pure fn max<A:copy,IA:BaseIter<A>>(self: IA) -> A {
|
||||||
match do foldl::<A,option<A>,IA>(self, none) |a, b| {
|
match do foldl::<A,option<A>,IA>(self, none) |a, b| {
|
||||||
match a {
|
match a {
|
||||||
some(a_) if a_ > b => {
|
some(a_) if a_ > b => {
|
||||||
|
@ -165,6 +164,14 @@ fn max<A:copy,IA:BaseIter<A>>(self: IA) -> A {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pure fn find<A: copy,IA:BaseIter<A>>(self: IA,
|
||||||
|
p: fn(A) -> bool) -> option<A> {
|
||||||
|
for self.each |i| {
|
||||||
|
if p(i) { return some(i) }
|
||||||
|
}
|
||||||
|
return none;
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
#[test]
|
#[test]
|
||||||
fn test_enumerate() {
|
fn test_enumerate() {
|
||||||
|
|
|
@ -87,7 +87,7 @@ impl T: iter::Times {
|
||||||
will execute the given function exactly x times. If we assume that \
|
will execute the given function exactly x times. If we assume that \
|
||||||
`x` is an int, this is functionally equivalent to \
|
`x` is an int, this is functionally equivalent to \
|
||||||
`for int::range(0, x) |_i| { /* anything */ }`."]
|
`for int::range(0, x) |_i| { /* anything */ }`."]
|
||||||
fn times(it: fn() -> bool) {
|
pure fn times(it: fn() -> bool) {
|
||||||
let mut i = self;
|
let mut i = self;
|
||||||
while i > 0 {
|
while i > 0 {
|
||||||
if !it() { break }
|
if !it() { break }
|
||||||
|
@ -99,7 +99,7 @@ impl T: iter::Times {
|
||||||
impl T: iter::TimesIx {
|
impl T: iter::TimesIx {
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
/// Like `times`, but with an index, `eachi`-style.
|
/// Like `times`, but with an index, `eachi`-style.
|
||||||
fn timesi(it: fn(uint) -> bool) {
|
pure fn timesi(it: fn(uint) -> bool) {
|
||||||
let slf = self as uint;
|
let slf = self as uint;
|
||||||
let mut i = 0u;
|
let mut i = 0u;
|
||||||
while i < slf {
|
while i < slf {
|
||||||
|
|
|
@ -18,7 +18,7 @@ export len;
|
||||||
export from_fn;
|
export from_fn;
|
||||||
export from_elem;
|
export from_elem;
|
||||||
export from_slice;
|
export from_slice;
|
||||||
export build, build_sized;
|
export build, build_sized, build_sized_opt;
|
||||||
export to_mut;
|
export to_mut;
|
||||||
export from_mut;
|
export from_mut;
|
||||||
export head;
|
export head;
|
||||||
|
@ -259,6 +259,24 @@ pure fn build<A>(builder: fn(push: pure fn(+A))) -> ~[A] {
|
||||||
build_sized(4, builder)
|
build_sized(4, builder)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Builds a vector by calling a provided function with an argument
|
||||||
|
* function that pushes an element to the back of a vector.
|
||||||
|
* This version takes an initial size for the vector.
|
||||||
|
*
|
||||||
|
* # Arguments
|
||||||
|
*
|
||||||
|
* * size - An option, maybe containing initial size of the vector to reserve
|
||||||
|
* * builder - A function that will construct the vector. It recieves
|
||||||
|
* as an argument a function that will push an element
|
||||||
|
* onto the vector being constructed.
|
||||||
|
*/
|
||||||
|
#[inline(always)]
|
||||||
|
pure fn build_sized_opt<A>(size: option<uint>,
|
||||||
|
builder: fn(push: pure fn(+A))) -> ~[A] {
|
||||||
|
build_sized(size.get_default(4), builder)
|
||||||
|
}
|
||||||
|
|
||||||
/// Produces a mut vector from an immutable vector.
|
/// Produces a mut vector from an immutable vector.
|
||||||
pure fn to_mut<T>(+v: ~[T]) -> ~[mut T] {
|
pure fn to_mut<T>(+v: ~[T]) -> ~[mut T] {
|
||||||
unsafe { ::unsafe::transmute(v) }
|
unsafe { ::unsafe::transmute(v) }
|
||||||
|
@ -1505,7 +1523,6 @@ impl<T> &[T]: ImmutableVector<T> {
|
||||||
|
|
||||||
trait ImmutableCopyableVector<T> {
|
trait ImmutableCopyableVector<T> {
|
||||||
pure fn filter(f: fn(T) -> bool) -> ~[T];
|
pure fn filter(f: fn(T) -> bool) -> ~[T];
|
||||||
pure fn find(f: fn(T) -> bool) -> option<T>;
|
|
||||||
pure fn rfind(f: fn(T) -> bool) -> option<T>;
|
pure fn rfind(f: fn(T) -> bool) -> option<T>;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1520,15 +1537,6 @@ impl<T: copy> &[T]: ImmutableCopyableVector<T> {
|
||||||
*/
|
*/
|
||||||
#[inline]
|
#[inline]
|
||||||
pure fn filter(f: fn(T) -> bool) -> ~[T] { filter(self, f) }
|
pure fn filter(f: fn(T) -> bool) -> ~[T] { filter(self, f) }
|
||||||
/**
|
|
||||||
* Search for the first element that matches a given predicate
|
|
||||||
*
|
|
||||||
* Apply function `f` to each element of `v`, starting from the first.
|
|
||||||
* When function `f` returns true then an option containing the element
|
|
||||||
* is returned. If `f` matches no elements then none is returned.
|
|
||||||
*/
|
|
||||||
#[inline]
|
|
||||||
pure fn find(f: fn(T) -> bool) -> option<T> { find(self, f) }
|
|
||||||
/**
|
/**
|
||||||
* Search for the last element that matches a given predicate
|
* Search for the last element that matches a given predicate
|
||||||
*
|
*
|
||||||
|
@ -1756,44 +1764,41 @@ mod u8 {
|
||||||
// required in the slice.
|
// required in the slice.
|
||||||
|
|
||||||
impl<A> &[A]: iter::BaseIter<A> {
|
impl<A> &[A]: iter::BaseIter<A> {
|
||||||
fn each(blk: fn(A) -> bool) { each(self, blk) }
|
pure fn each(blk: fn(A) -> bool) { each(self, blk) }
|
||||||
fn size_hint() -> option<uint> { some(len(self)) }
|
pure fn size_hint() -> option<uint> { some(len(self)) }
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<A> &[A]: iter::ExtendedIter<A> {
|
impl<A> &[A]: iter::ExtendedIter<A> {
|
||||||
fn eachi(blk: fn(uint, A) -> bool) { iter::eachi(self, blk) }
|
pure fn eachi(blk: fn(uint, A) -> bool) { iter::eachi(self, blk) }
|
||||||
fn all(blk: fn(A) -> bool) -> bool { iter::all(self, blk) }
|
pure fn all(blk: fn(A) -> bool) -> bool { iter::all(self, blk) }
|
||||||
fn any(blk: fn(A) -> bool) -> bool { iter::any(self, blk) }
|
pure fn any(blk: fn(A) -> bool) -> bool { iter::any(self, blk) }
|
||||||
fn foldl<B>(+b0: B, blk: fn(B, A) -> B) -> B {
|
pure fn foldl<B>(+b0: B, blk: fn(B, A) -> B) -> B {
|
||||||
iter::foldl(self, b0, blk)
|
iter::foldl(self, b0, blk)
|
||||||
}
|
}
|
||||||
fn contains(x: A) -> bool { iter::contains(self, x) }
|
pure fn contains(x: A) -> bool { iter::contains(self, x) }
|
||||||
fn count(x: A) -> uint { iter::count(self, x) }
|
pure fn count(x: A) -> uint { iter::count(self, x) }
|
||||||
fn position(f: fn(A) -> bool) -> option<uint> { iter::position(self, f) }
|
pure fn position(f: fn(A) -> bool) -> option<uint> {
|
||||||
|
iter::position(self, f)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
trait IterTraitExtensions<A> {
|
impl<A: copy> &[A]: iter::CopyableIter<A> {
|
||||||
fn filter_to_vec(pred: fn(A) -> bool) -> ~[A];
|
pure fn filter_to_vec(pred: fn(A) -> bool) -> ~[A] {
|
||||||
fn map_to_vec<B>(op: fn(A) -> B) -> ~[B];
|
|
||||||
fn to_vec() -> ~[A];
|
|
||||||
fn min() -> A;
|
|
||||||
fn max() -> A;
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<A: copy> &[A]: IterTraitExtensions<A> {
|
|
||||||
fn filter_to_vec(pred: fn(A) -> bool) -> ~[A] {
|
|
||||||
iter::filter_to_vec(self, pred)
|
iter::filter_to_vec(self, pred)
|
||||||
}
|
}
|
||||||
fn map_to_vec<B>(op: fn(A) -> B) -> ~[B] { iter::map_to_vec(self, op) }
|
pure fn map_to_vec<B>(op: fn(A) -> B) -> ~[B] {
|
||||||
fn to_vec() -> ~[A] { iter::to_vec(self) }
|
iter::map_to_vec(self, op)
|
||||||
|
}
|
||||||
|
pure fn to_vec() -> ~[A] { iter::to_vec(self) }
|
||||||
|
|
||||||
// FIXME--bug in resolve prevents this from working (#2611)
|
// FIXME--bug in resolve prevents this from working (#2611)
|
||||||
// fn flat_map_to_vec<B:copy,IB:base_iter<B>>(op: fn(A) -> IB) -> ~[B] {
|
// fn flat_map_to_vec<B:copy,IB:base_iter<B>>(op: fn(A) -> IB) -> ~[B] {
|
||||||
// iter::flat_map_to_vec(self, op)
|
// iter::flat_map_to_vec(self, op)
|
||||||
// }
|
// }
|
||||||
|
|
||||||
fn min() -> A { iter::min(self) }
|
pure fn min() -> A { iter::min(self) }
|
||||||
fn max() -> A { iter::max(self) }
|
pure fn max() -> A { iter::max(self) }
|
||||||
|
pure fn find(p: fn(A) -> bool) -> option<A> { iter::find(self, p) }
|
||||||
}
|
}
|
||||||
// ___________________________________________________________________________
|
// ___________________________________________________________________________
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue