Skip to content

Commit 2264c79

Browse files
tmmcguireemberian
authored andcommitted
Add reverse_part, replace each_permutation, add tests
1 parent 00eef96 commit 2264c79

File tree

1 file changed

+193
-18
lines changed

1 file changed

+193
-18
lines changed

src/libcore/vec.rs

Lines changed: 193 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1445,6 +1445,46 @@ pub fn reverse<T>(v: &mut [T]) {
14451445
}
14461446
}
14471447

1448+
/**
1449+
* Reverse part of a vector in place.
1450+
*
1451+
* Reverse the elements in the vector between `start` and `end - 1`.
1452+
*
1453+
* # Arguments
1454+
*
1455+
* * `v` - The mutable vector to be modified
1456+
*
1457+
* * `start` - Index of the first element of the slice
1458+
*
1459+
* * `end` - Index one past the final element to be reversed.
1460+
*
1461+
* # Example
1462+
*
1463+
* Assume a mutable vector `v` contains `[1,2,3,4,5]`. After the call:
1464+
*
1465+
* ~~~
1466+
*
1467+
* reverse_part(v, 1, 4);
1468+
*
1469+
* ~~~
1470+
*
1471+
* `v` now contains `[1,4,3,2,5]`.
1472+
*
1473+
* # Safety note
1474+
*
1475+
* Behavior is undefined if `start` or `end` do not represent valid
1476+
* positions in `v`.
1477+
*/
1478+
pub fn reverse_part<T>(v: &mut [T], start: uint, end : uint) {
1479+
let mut i = start;
1480+
let mut j = end - 1;
1481+
while i < j {
1482+
v[i] <-> v[j];
1483+
i += 1;
1484+
j -= 1;
1485+
}
1486+
}
1487+
14481488
/// Returns a vector with the order of elements reversed
14491489
pub fn reversed<T:Copy>(v: &const [T]) -> ~[T] {
14501490
let mut rs: ~[T] = ~[];
@@ -1739,31 +1779,75 @@ pub fn each2_mut<U, T>(v1: &mut [U], v2: &mut [T], f: &fn(u: &mut U, t: &mut T)
17391779
*
17401780
* The total number of permutations produced is `len(v)!`. If `v` contains
17411781
* repeated elements, then some permutations are repeated.
1782+
*
1783+
* See [Algorithms to generate
1784+
* permutations](http://en.wikipedia.org/wiki/Permutation).
1785+
*
1786+
* # Arguments
1787+
*
1788+
* * `values` - A vector of values from which the permutations are
1789+
* chosen
1790+
*
1791+
* * `fun` - The function to iterate over the combinations
17421792
*/
17431793
#[cfg(not(stage0))]
1744-
pub fn each_permutation<T:Copy>(v: &[T], put: &fn(ts: &[T]) -> bool) -> bool {
1745-
let ln = len(v);
1746-
if ln <= 1 {
1747-
put(v);
1748-
} else {
1749-
// This does not seem like the most efficient implementation. You
1750-
// could make far fewer copies if you put your mind to it.
1751-
let mut i = 0u;
1752-
while i < ln {
1753-
let elt = v[i];
1754-
let mut rest = slice(v, 0u, i).to_vec();
1755-
rest.push_all(const_slice(v, i+1u, ln));
1756-
for each_permutation(rest) |permutation| {
1757-
if !put(append(~[elt], permutation)) {
1758-
return false;
1759-
}
1760-
}
1761-
i += 1u;
1794+
pub fn each_permutation<T:Copy>(values: &[T], fun: &fn(perm : &[T]) -> bool) {
1795+
let length = values.len();
1796+
let mut permutation = vec::from_fn(length, |i| values[i]);
1797+
if length <= 1 {
1798+
fun(permutation);
1799+
return;
1800+
}
1801+
let mut indices = vec::from_fn(length, |i| i);
1802+
loop {
1803+
if !fun(permutation) { return; }
1804+
// find largest k such that indices[k] < indices[k+1]
1805+
// if no such k exists, all permutations have been generated
1806+
let mut k = length - 2;
1807+
while k > 0 && indices[k] >= indices[k+1] {
1808+
k -= 1;
1809+
}
1810+
if k == 0 && indices[0] > indices[1] { return; }
1811+
// find largest l such that indices[k] < indices[l]
1812+
// k+1 is guaranteed to be such
1813+
let mut l = length - 1;
1814+
while indices[k] >= indices[l] {
1815+
l -= 1;
1816+
}
1817+
// swap indices[k] and indices[l]; sort indices[k+1..]
1818+
// (they're just reversed)
1819+
indices[k] <-> indices[l];
1820+
unsafe {
1821+
reverse_part(indices, k+1, length);
1822+
}
1823+
// fixup permutation based on indices
1824+
for uint::range(k, length) |i| {
1825+
permutation[i] = values[indices[i]];
17621826
}
17631827
}
17641828
return true;
17651829
}
17661830

1831+
/**
1832+
* Iterate over all permutations of vector `values`.
1833+
*
1834+
* This is an alternative to each_permutation that uses references to
1835+
* avoid copying the elements of the values vector.
1836+
*
1837+
* To avoid copying, the iterator will be passed a reference to a vector
1838+
* containing references to the elements in the original `values` vector.
1839+
*
1840+
* # Arguments
1841+
*
1842+
* * `values` - A vector of values from which the permutations are chosen
1843+
*
1844+
* * `fun` - The function to iterate over the permutations
1845+
*/
1846+
#[cfg(not(stage0))]
1847+
pub fn each_permutation_ref<T>(values : &'v[T], fun : &fn(perm : &[&'v T]) -> bool) {
1848+
each_permutation(vec::from_fn(values.len(), |i| &values[i]), fun);
1849+
}
1850+
17671851
/**
17681852
* Iterate over all contiguous windows of length `n` of the vector `v`.
17691853
*
@@ -4730,6 +4814,97 @@ mod tests {
47304814
}
47314815
}
47324816

4817+
fn dup<T:Copy>(values : &[&T]) -> ~[T] {
4818+
from_fn(values.len(), |i| *values[i])
4819+
}
4820+
4821+
#[test]
4822+
fn test_reverse_part() {
4823+
let mut values = [1,2,3,4,5];
4824+
reverse_part(values,1,4);
4825+
assert values == [1,4,3,2,5];
4826+
}
4827+
4828+
#[test]
4829+
fn test_permutations0() {
4830+
let values = [];
4831+
let mut v : ~[~[int]] = ~[];
4832+
for each_permutation(values) |p| {
4833+
v.push(vec::from_slice(p));
4834+
}
4835+
assert v == ~[~[]];
4836+
}
4837+
4838+
#[test]
4839+
fn test_permutations0_ref() {
4840+
let values = [];
4841+
let mut v : ~[~[int]] = ~[];
4842+
for each_permutation_ref(values) |p| {
4843+
v.push(dup(p));
4844+
}
4845+
assert v == ~[~[]];
4846+
}
4847+
4848+
#[test]
4849+
fn test_permutations1() {
4850+
let values = [1];
4851+
let mut v : ~[~[int]] = ~[];
4852+
for each_permutation(values) |p| {
4853+
v.push(vec::from_slice(p));
4854+
}
4855+
assert v == ~[~[1]];
4856+
}
4857+
4858+
#[test]
4859+
fn test_permutations1_ref() {
4860+
let values = [1];
4861+
let mut v : ~[~[int]] = ~[];
4862+
for each_permutation_ref(values) |p| {
4863+
v.push(dup(p));
4864+
}
4865+
assert v == ~[~[1]];
4866+
}
4867+
4868+
#[test]
4869+
fn test_permutations2() {
4870+
let values = [1,2];
4871+
let mut v : ~[~[int]] = ~[];
4872+
for each_permutation(values) |p| {
4873+
v.push(vec::from_slice(p));
4874+
}
4875+
assert v == ~[~[1,2],~[2,1]];
4876+
}
4877+
4878+
#[test]
4879+
fn test_permutations2_ref() {
4880+
let values = [1,2];
4881+
let mut v : ~[~[int]] = ~[];
4882+
for each_permutation_ref(values) |p| {
4883+
v.push(dup(p));
4884+
}
4885+
assert v == ~[~[1,2],~[2,1]];
4886+
}
4887+
4888+
#[test]
4889+
fn test_permutations3() {
4890+
let values = [1,2,3];
4891+
let mut v : ~[~[int]] = ~[];
4892+
for each_permutation(values) |p| {
4893+
v.push(vec::from_slice(p));
4894+
}
4895+
assert v == ~[~[1,2,3],~[1,3,2],~[2,1,3],~[2,3,1],~[3,1,2],~[3,2,1]];
4896+
}
4897+
4898+
#[test]
4899+
fn test_permutations3_ref() {
4900+
let values = [1,2,3];
4901+
let mut v : ~[~[int]] = ~[];
4902+
for each_permutation_ref(values) |p| {
4903+
v.push(dup(p));
4904+
}
4905+
assert v == ~[~[1,2,3],~[1,3,2],~[2,1,3],~[2,3,1],~[3,1,2],~[3,2,1]];
4906+
}
4907+
47334908
#[test]
47344909
fn test_each_val() {
47354910
use old_iter::CopyableNonstrictIter;

0 commit comments

Comments
 (0)