組み合わせ \(n^m\)

For each \(\{ x_i : 0 \leq i \lt m, 0 \leq x_i \lt n \}\)

Rust

// n^m
struct PowerPermutation { n: usize, m: usize, ar: Vec<usize> }
impl PowerPermutation {
    fn new(n: usize, m: usize) -> PowerPermutation {
        let ar = vec![0; m];
        PowerPermutation { n: n, m: m, ar: ar }
    }
}
impl Iterator for PowerPermutation {
    type Item = Vec<usize>;
    fn next(&mut self) -> Option<Vec<usize>> {
        if self.ar[self.m-1] >= self.n { return None }
        let r = self.ar.clone();
        self.ar[0] += 1;
        for i in 0..self.m-1 {
            if self.ar[i] == self.n {
                self.ar[i] = 0;
                self.ar[i + 1] += 1;
            } else {
                break;
            }
        }
        return Some(r);
    }
}

Example

fn main() {
    let mut count = 0;
    for a in PowerPermutation::new(3, 2) {
        count += 1;
        println!("{:?}", a);
    }
    println!("{:?}", count);
}
[0, 0]
[1, 0]
[2, 0]
[0, 1]
[1, 1]
[2, 1]
[0, 2]
[1, 2]
[2, 2]
9