Skip to content

Commit 450c45b

Browse files
committed
---
yaml --- r: 7889 b: refs/heads/snap-stage3 c: f98210d h: refs/heads/master i: 7887: 1868dc7 v: v3
1 parent b072b53 commit 450c45b

File tree

2 files changed

+126
-4
lines changed

2 files changed

+126
-4
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
refs/heads/master: 2898dcc5d97da9427ac367542382b6239d9c0bbf
33
refs/heads/snap-stage1: e33de59e47c5076a89eadeb38f4934f58a3618a6
4-
refs/heads/snap-stage3: 025e6ff1586999caef7460a1884cbd9685ad2277
4+
refs/heads/snap-stage3: f98210db074595c726f8a2913149cb536a893de3
55
refs/heads/try: 2898dcc5d97da9427ac367542382b6239d9c0bbf
66
refs/tags/release-0.1: 1f5c5126e96c79d22cb7862f75304136e204f105

branches/snap-stage3/src/libcore/vec.rs

Lines changed: 125 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -706,15 +706,55 @@ fn count<T>(x: T, v: [const T]) -> uint {
706706
/*
707707
Function: find
708708
709-
Search for an element that matches a given predicate
709+
Search for the first element that matches a given predicate
710710
711711
Apply function `f` to each element of `v`, starting from the first.
712712
When function `f` returns true then an option containing the element
713713
is returned. If `f` matches no elements then none is returned.
714714
*/
715715
fn find<T: copy>(v: [T], f: fn(T) -> bool) -> option::t<T> {
716-
for elt: T in v { if f(elt) { ret some(elt); } }
717-
ret none;
716+
find_from(v, 0u, len(v), f)
717+
}
718+
719+
/*
720+
Function: find_from
721+
722+
Search for the first element that matches a given predicate within a range
723+
724+
Apply function `f` to each element of `v` within the range [`start`, `end`).
725+
When function `f` returns true then an option containing the element
726+
is returned. If `f` matches no elements then none is returned.
727+
*/
728+
fn find_from<T: copy>(v: [T], start: uint, end: uint, f: fn(T) -> bool) ->
729+
option::t<T> {
730+
option::map(position_from(v, start, end, f)) { |i| v[i] }
731+
}
732+
733+
/*
734+
Function: rfind
735+
736+
Search for the last element that matches a given predicate
737+
738+
Apply function `f` to each element of `v` in reverse order. When function `f`
739+
returns true then an option containing the element is returned. If `f`
740+
matches no elements then none is returned.
741+
*/
742+
fn rfind<T: copy>(v: [T], f: fn(T) -> bool) -> option::t<T> {
743+
rfind_from(v, 0u, len(v), f)
744+
}
745+
746+
/*
747+
Function: rfind_from
748+
749+
Search for the last element that matches a given predicate within a range
750+
751+
Apply function `f` to each element of `v` in reverse order within the range
752+
[`start`, `end`). When function `f` returns true then an option containing
753+
the element is returned. If `f` matches no elements then none is returned.
754+
*/
755+
fn rfind_from<T: copy>(v: [T], start: uint, end: uint, f: fn(T) -> bool) ->
756+
option::t<T> {
757+
option::map(rposition_from(v, start, end, f)) { |i| v[i] }
718758
}
719759

720760
/*
@@ -1673,6 +1713,46 @@ mod tests {
16731713
assert position_from(v, 4u, 4u, f) == none;
16741714
}
16751715

1716+
#[test]
1717+
fn test_find() {
1718+
assert find([], f) == none;
1719+
1720+
fn f(xy: (int, char)) -> bool { let (_x, y) = xy; y == 'b' }
1721+
fn g(xy: (int, char)) -> bool { let (_x, y) = xy; y == 'd' }
1722+
let v = [(0, 'a'), (1, 'b'), (2, 'c'), (3, 'b')];
1723+
1724+
assert find(v, f) == some((1, 'b'));
1725+
assert find(v, g) == none;
1726+
}
1727+
1728+
#[test]
1729+
fn test_find_from() {
1730+
assert find_from([], 0u, 0u, f) == none;
1731+
1732+
fn f(xy: (int, char)) -> bool { let (_x, y) = xy; y == 'b' }
1733+
let v = [(0, 'a'), (1, 'b'), (2, 'c'), (3, 'b')];
1734+
1735+
assert find_from(v, 0u, 0u, f) == none;
1736+
assert find_from(v, 0u, 1u, f) == none;
1737+
assert find_from(v, 0u, 2u, f) == some((1, 'b'));
1738+
assert find_from(v, 0u, 3u, f) == some((1, 'b'));
1739+
assert find_from(v, 0u, 4u, f) == some((1, 'b'));
1740+
1741+
assert find_from(v, 1u, 1u, f) == none;
1742+
assert find_from(v, 1u, 2u, f) == some((1, 'b'));
1743+
assert find_from(v, 1u, 3u, f) == some((1, 'b'));
1744+
assert find_from(v, 1u, 4u, f) == some((1, 'b'));
1745+
1746+
assert find_from(v, 2u, 2u, f) == none;
1747+
assert find_from(v, 2u, 3u, f) == none;
1748+
assert find_from(v, 2u, 4u, f) == some((3, 'b'));
1749+
1750+
assert find_from(v, 3u, 3u, f) == none;
1751+
assert find_from(v, 3u, 4u, f) == some((3, 'b'));
1752+
1753+
assert find_from(v, 4u, 4u, f) == none;
1754+
}
1755+
16761756
#[test]
16771757
fn test_rposition() {
16781758
assert find([], f) == none;
@@ -1712,6 +1792,48 @@ mod tests {
17121792

17131793
assert rposition_from(v, 4u, 4u, f) == none;
17141794
}
1795+
1796+
#[test]
1797+
fn test_rfind() {
1798+
assert rfind([], f) == none;
1799+
1800+
fn f(xy: (int, char)) -> bool { let (_x, y) = xy; y == 'b' }
1801+
fn g(xy: (int, char)) -> bool { let (_x, y) = xy; y == 'd' }
1802+
let v = [(0, 'a'), (1, 'b'), (2, 'c'), (3, 'b')];
1803+
1804+
assert rfind(v, f) == some((3, 'b'));
1805+
assert rfind(v, g) == none;
1806+
}
1807+
1808+
#[test]
1809+
fn test_rfind_from() {
1810+
assert rfind_from([], 0u, 0u, f) == none;
1811+
1812+
fn f(xy: (int, char)) -> bool { let (_x, y) = xy; y == 'b' }
1813+
let v = [(0, 'a'), (1, 'b'), (2, 'c'), (3, 'b')];
1814+
1815+
assert rfind_from(v, 0u, 0u, f) == none;
1816+
assert rfind_from(v, 0u, 1u, f) == none;
1817+
assert rfind_from(v, 0u, 2u, f) == some((1, 'b'));
1818+
assert rfind_from(v, 0u, 3u, f) == some((1, 'b'));
1819+
assert rfind_from(v, 0u, 4u, f) == some((3, 'b'));
1820+
1821+
assert rfind_from(v, 1u, 1u, f) == none;
1822+
assert rfind_from(v, 1u, 2u, f) == some((1, 'b'));
1823+
assert rfind_from(v, 1u, 3u, f) == some((1, 'b'));
1824+
assert rfind_from(v, 1u, 4u, f) == some((3, 'b'));
1825+
1826+
assert rfind_from(v, 2u, 2u, f) == none;
1827+
assert rfind_from(v, 2u, 3u, f) == none;
1828+
assert rfind_from(v, 2u, 4u, f) == some((3, 'b'));
1829+
1830+
assert rfind_from(v, 3u, 3u, f) == none;
1831+
assert rfind_from(v, 3u, 4u, f) == some((3, 'b'));
1832+
1833+
assert rfind_from(v, 4u, 4u, f) == none;
1834+
}
1835+
1836+
#[test]
17151837
fn reverse_and_reversed() {
17161838
let v: [mutable int] = [mutable 10, 20];
17171839
assert (v[0] == 10);

0 commit comments

Comments
 (0)