Moved nth_lex out of struct impl

This commit is contained in:
Fabian Schmidt 2024-11-18 11:20:35 +01:00
parent d3a93a875f
commit fdf46c9dd3
2 changed files with 71 additions and 67 deletions

View File

@ -16,26 +16,11 @@ fn binomial(n: usize, k: usize) -> usize {
}
}
#[derive(Clone)]
pub struct Combinator<T: Clone + Ord> {
pub current: Vec<T>,
pub k: usize,
idx: usize,
}
impl<T: Clone + Ord> Combinator<T> {
pub fn new(elements: Vec<T>, k: usize) -> Result<Combinator<T>, Box<dyn Error>> {
if k > elements.len() || k == 0 {
return Err(Box::from("Out of bounds"));
}
Ok(Self {
current: elements,
k,
idx: 0,
})
}
pub fn nth_lex(mut elements: Vec<T>, k: usize, nth: usize) -> Result<Vec<T>, Box<dyn Error>> {
pub fn nth_lex<T: Clone + Ord>(
mut elements: Vec<T>,
k: usize,
nth: usize,
) -> Result<Vec<T>, Box<dyn Error>> {
elements.sort();
let num_elements = elements.len();
let num_combinations = binomial(num_elements, k);
@ -64,6 +49,25 @@ impl<T: Clone + Ord> Combinator<T> {
i += 1;
}
Ok(comb)
}
#[derive(Clone)]
pub struct Combinator<T: Clone + Ord> {
pub current: Vec<T>,
pub k: usize,
idx: usize,
}
impl<T: Clone + Ord> Combinator<T> {
pub fn new(elements: Vec<T>, k: usize) -> Result<Combinator<T>, Box<dyn Error>> {
if k > elements.len() || k == 0 {
return Err(Box::from("Out of bounds"));
}
Ok(Self {
current: elements,
k,
idx: 0,
})
}
}

View File

@ -8,33 +8,19 @@ fn factorial(num: usize) -> usize {
fact
}
#[derive(Clone)]
pub struct Permutator<T: Copy + Ord> {
pub current: Vec<T>,
idx: usize,
}
impl<T: Copy + Ord> Permutator<T> {
pub fn new(elements: Vec<T>) -> Self {
Self {
current: elements,
idx: 0,
}
}
/// Explanation
///
/// there are 10! possible permutations
/// for each first number there are 9!, for each first 2 numbers 8!, etc.
/// we check how many times we have 9! permutations before we're over 1_000_000
/// aka. 1000000 / 9!
/// we take the remainder and check how many times we have 8! before we?re over it
/// (1000000 % 9!) 8!
/// etc.
/// every iteration we remove the digit by the idx from the original permutation
/// we only check for 999999 permutations because we already have the first one
///
pub fn nth_lex(mut digits: Vec<T>, nth: usize) -> Result<Vec<T>, Box<dyn Error>> {
/// Explanation
///
/// there are 10! possible permutations
/// for each first number there are 9!, for each first 2 numbers 8!, etc.
/// we check how many times we have 9! permutations before we're over 1_000_000
/// aka. 1000000 / 9!
/// we take the remainder and check how many times we have 8! before we?re over it
/// (1000000 % 9!) 8!
/// etc.
/// every iteration we remove the digit by the idx from the original permutation
/// we only check for 999999 permutations because we already have the first one
///
pub fn nth_lex<T: Clone + Ord>(mut digits: Vec<T>, nth: usize) -> Result<Vec<T>, Box<dyn Error>> {
digits.sort();
if nth == 1 {
return Ok(digits);
@ -48,14 +34,28 @@ impl<T: Copy + Ord> Permutator<T> {
for idx in 1..=digits.len() {
let permutations = remainder / factorial(num_unique_digits - idx);
remainder %= factorial(num_unique_digits - idx);
perm.push(digits[permutations]);
perm.push(digits[permutations].clone());
digits.remove(permutations);
}
Ok(perm)
}
#[derive(Clone)]
pub struct Permutator<T: Clone + Ord> {
pub current: Vec<T>,
idx: usize,
}
impl<T: Clone + Ord> Permutator<T> {
pub fn new(elements: Vec<T>) -> Self {
Self {
current: elements,
idx: 0,
}
}
}
impl<T: Copy + Ord> Iterator for Permutator<T> {
impl<T: Clone + Ord> Iterator for Permutator<T> {
type Item = Vec<T>;
/// Returns the next permutation and changes the current permutation to it
/// This operation wraps around
@ -73,7 +73,7 @@ impl<T: Copy + Ord> Iterator for Permutator<T> {
for idx in 1..=digits.len() {
let permutations = remainder / factorial(num_unique_digits - idx);
remainder %= factorial(num_unique_digits - idx);
perm.push(digits[permutations]);
perm.push(digits[permutations].clone());
digits.remove(permutations);
}
self.idx += 1;