Moved nth_lex out of struct impl
This commit is contained in:
parent
d3a93a875f
commit
fdf46c9dd3
@ -16,26 +16,11 @@ fn binomial(n: usize, k: usize) -> usize {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone)]
|
pub fn nth_lex<T: Clone + Ord>(
|
||||||
pub struct Combinator<T: Clone + Ord> {
|
mut elements: Vec<T>,
|
||||||
pub current: Vec<T>,
|
k: usize,
|
||||||
pub k: usize,
|
nth: usize,
|
||||||
idx: usize,
|
) -> Result<Vec<T>, Box<dyn Error>> {
|
||||||
}
|
|
||||||
|
|
||||||
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>> {
|
|
||||||
elements.sort();
|
elements.sort();
|
||||||
let num_elements = elements.len();
|
let num_elements = elements.len();
|
||||||
let num_combinations = binomial(num_elements, k);
|
let num_combinations = binomial(num_elements, k);
|
||||||
@ -65,6 +50,25 @@ impl<T: Clone + Ord> Combinator<T> {
|
|||||||
}
|
}
|
||||||
Ok(comb)
|
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,
|
||||||
|
})
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T: Clone + Ord> Iterator for Combinator<T> {
|
impl<T: Clone + Ord> Iterator for Combinator<T> {
|
||||||
|
@ -8,20 +8,6 @@ fn factorial(num: usize) -> usize {
|
|||||||
fact
|
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
|
/// Explanation
|
||||||
///
|
///
|
||||||
/// there are 10! possible permutations
|
/// there are 10! possible permutations
|
||||||
@ -34,7 +20,7 @@ impl<T: Copy + Ord> Permutator<T> {
|
|||||||
/// every iteration we remove the digit by the idx from the original permutation
|
/// 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
|
/// 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>> {
|
pub fn nth_lex<T: Clone + Ord>(mut digits: Vec<T>, nth: usize) -> Result<Vec<T>, Box<dyn Error>> {
|
||||||
digits.sort();
|
digits.sort();
|
||||||
if nth == 1 {
|
if nth == 1 {
|
||||||
return Ok(digits);
|
return Ok(digits);
|
||||||
@ -48,14 +34,28 @@ impl<T: Copy + Ord> Permutator<T> {
|
|||||||
for idx in 1..=digits.len() {
|
for idx in 1..=digits.len() {
|
||||||
let permutations = remainder / factorial(num_unique_digits - idx);
|
let permutations = remainder / factorial(num_unique_digits - idx);
|
||||||
remainder %= factorial(num_unique_digits - idx);
|
remainder %= factorial(num_unique_digits - idx);
|
||||||
perm.push(digits[permutations]);
|
perm.push(digits[permutations].clone());
|
||||||
digits.remove(permutations);
|
digits.remove(permutations);
|
||||||
}
|
}
|
||||||
Ok(perm)
|
Ok(perm)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Clone)]
|
||||||
|
pub struct Permutator<T: Clone + Ord> {
|
||||||
|
pub current: Vec<T>,
|
||||||
|
idx: usize,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T: Copy + Ord> Iterator for Permutator<T> {
|
impl<T: Clone + Ord> Permutator<T> {
|
||||||
|
pub fn new(elements: Vec<T>) -> Self {
|
||||||
|
Self {
|
||||||
|
current: elements,
|
||||||
|
idx: 0,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T: Clone + Ord> Iterator for Permutator<T> {
|
||||||
type Item = Vec<T>;
|
type Item = Vec<T>;
|
||||||
/// Returns the next permutation and changes the current permutation to it
|
/// Returns the next permutation and changes the current permutation to it
|
||||||
/// This operation wraps around
|
/// This operation wraps around
|
||||||
@ -73,7 +73,7 @@ impl<T: Copy + Ord> Iterator for Permutator<T> {
|
|||||||
for idx in 1..=digits.len() {
|
for idx in 1..=digits.len() {
|
||||||
let permutations = remainder / factorial(num_unique_digits - idx);
|
let permutations = remainder / factorial(num_unique_digits - idx);
|
||||||
remainder %= factorial(num_unique_digits - idx);
|
remainder %= factorial(num_unique_digits - idx);
|
||||||
perm.push(digits[permutations]);
|
perm.push(digits[permutations].clone());
|
||||||
digits.remove(permutations);
|
digits.remove(permutations);
|
||||||
}
|
}
|
||||||
self.idx += 1;
|
self.idx += 1;
|
||||||
|
Loading…
Reference in New Issue
Block a user