From daa1e777fa9791ca60cd882a560254acc3481575 Mon Sep 17 00:00:00 2001 From: Ferran Basora Date: Mon, 25 Feb 2019 19:14:24 +0000 Subject: [PATCH] Pseudo n-ary --- src/alphabets.rs | 36 +++++++++++++++++++++++++++--------- tmux-thumbs.sh | 2 +- 2 files changed, 28 insertions(+), 10 deletions(-) diff --git a/src/alphabets.rs b/src/alphabets.rs index 7f59da4..8f041d3 100644 --- a/src/alphabets.rs +++ b/src/alphabets.rs @@ -38,18 +38,22 @@ impl<'a> Alphabet<'a> { pub fn hints(&self, matches: usize) -> Vec { let letters: Vec = self.letters.chars().map(|s| s.to_string()).collect(); - if matches <= letters.len() { - letters.iter().take(matches).map(|x| x.clone()).collect::>() - } else { - // TODO - let mut f = letters.iter().take(letters.len() - 1).map(|x| x.clone()).collect::>(); - let l = letters.iter().last().unwrap(); - let mut g = letters.iter().take(matches - (letters.len() - 1)).map(|s| l.clone() + s).collect(); + let mut expansion = letters.clone(); + let mut expanded: Vec = Vec::new(); - f.append(&mut g); + loop { + if expansion.len() + expanded.len() >= matches { break; } + if expansion.len() == 0 { break; } - f + let prefix = expansion.pop().expect("Ouch!"); + let sub_expansion: Vec = letters.iter().take(matches - expansion.len() - expanded.len()).map(|s| prefix.clone() + s).collect(); + + expanded.splice(0..0, sub_expansion); } + + expansion = expansion.iter().take(matches - expanded.len()).map(|s| s.clone()).collect(); + expansion.append(&mut expanded); + expansion } } @@ -76,4 +80,18 @@ mod tests { let hints = alphabet.hints(6); assert_eq!(hints, ["a", "b", "c", "da", "db", "dc"]); } + + #[test] + fn composed_matches_multiple () { + let alphabet = Alphabet::new("abcd"); + let hints = alphabet.hints(8); + assert_eq!(hints, ["a", "b", "ca", "cb", "da", "db", "dc", "dd"]); + } + + #[test] + fn composed_matches_max () { + let alphabet = Alphabet::new("ab"); + let hints = alphabet.hints(8); + assert_eq!(hints, ["aa", "ab", "ba", "bb"]); + } } diff --git a/tmux-thumbs.sh b/tmux-thumbs.sh index bba09a3..913dea7 100755 --- a/tmux-thumbs.sh +++ b/tmux-thumbs.sh @@ -1,3 +1,3 @@ #!/usr/bin/env bash -/home/ubuntu/dev/tmux-thumbs/target/release/tmux-thumbs -a qwerty -u +/home/ubuntu/dev/tmux-thumbs/target/release/tmux-thumbs -a qwerty -r -u