September 2017 Update
This commit is contained in:
parent
bba7bfd280
commit
ba8067c3b7
14570 changed files with 153136 additions and 63871 deletions
34
Task/Permutations/Rust/permutations-1.rust
Normal file
34
Task/Permutations/Rust/permutations-1.rust
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
struct QuickPerm<T> {
|
||||
idxs: Vec<usize>,
|
||||
elems: Vec<T>,
|
||||
idx: usize,
|
||||
}
|
||||
|
||||
impl<T: Clone> QuickPerm<T> {
|
||||
fn new(elems: Vec<T>) -> Self {
|
||||
QuickPerm { idxs: (0..elems.len()+1).collect(), elems: elems, idx: 1 }
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Clone> Iterator for QuickPerm<T> {
|
||||
type Item = Vec<T>;
|
||||
fn next(&mut self) -> Option<Self::Item> {
|
||||
if self.idx == self.elems.len() { return None; }
|
||||
|
||||
self.idxs[self.idx] -= 1;
|
||||
let other = if self.idx % 2 == 1 { self.idxs[self.idx] } else { 0 };
|
||||
self.elems.swap(self.idx, other);
|
||||
self.idx = 1;
|
||||
while self.idxs[self.idx] == 0 {
|
||||
self.idxs[self.idx] = self.idx;
|
||||
self.idx += 1;
|
||||
}
|
||||
Some(self.elems.clone())
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
for perm in QuickPerm::new(vec![1,2,3]) {
|
||||
println!("{:?}", perm);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue