Advertisement
Lyend

Untitled

Jan 11th, 2019
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rust 2.91 KB | None | 0 0
  1. use std::collections::HashSet;
  2.  
  3. enum Patterns {
  4.     Straight,
  5.     EvenSkip,
  6.     OddSkip,
  7.     Modulus
  8. }
  9.  
  10. struct PermutationIterator {
  11.     index: usize,
  12.     root: String,
  13.     rules: Vec<(char, Vec<char>)>,
  14.     buffer: HashSet<String>,
  15.     pattern: Patterns
  16. }
  17.  
  18. mod char_patterns {
  19.     use super::PermutationIterator;
  20.     pub fn straight(_instance: PermutationIterator) -> bool { true }
  21.     pub fn even_skip(instance: PermutationIterator) -> bool { instance.index % 2 == 0 }
  22.     pub fn odd_skip(instance: PermutationIterator) -> bool { instance.index % 1 == 0 }
  23.     pub fn modulus(instance: PermutationIterator) -> bool { instance.index % instance.root.len() == 0 }
  24. }
  25.  
  26. fn index_replace(i: usize, data: &String, replacement: &String) -> String {
  27.   data.chars()
  28.   .take(i-replacement.len())
  29.   .chain(replacement.chars())
  30.   .chain(
  31.     data.chars()
  32.     .skip(i)
  33.     .take(data.len()-i)
  34.   ).collect()
  35. }
  36.  
  37. impl PermutationIterator {
  38.     fn new(word: String,  rules: Vec<(char, Vec<char>)>) -> PermutationIterator {
  39.        PermutationIterator {
  40.           index: 0,
  41.           root: word,
  42.           rules: rules,
  43.           buffer: HashSet::new(),
  44.           pattern: Patterns::Straight
  45.         }
  46.     }
  47.     fn with_pattern(mut self, pattern: Patterns) -> Self {
  48.         self.pattern = pattern;
  49.         self
  50.     }
  51.  
  52. }
  53.  
  54. impl Iterator for PermutationIterator {
  55.     type Item = HashSet<String>;
  56.     fn next(&mut self) -> Option<Self::Item> {
  57.         if self.index <= self.root.len() {
  58.             if self.index == 0 {
  59.                 self.buffer.insert(self.root.clone());
  60.             }
  61.             self.index += 1;
  62.  
  63.             for word in self.buffer.clone() {
  64.                 for (rc, replace) in &self.rules {
  65.                     for (i, c) in word.chars().enumerate() {
  66.                         if rc == &c {
  67.                        
  68.                         let check = match &self.pattern {
  69.                             Patterns::Straight => char_patterns::straight,
  70.                             Patterns::EvenSkip => char_patterns::even_skip,
  71.                             Patterns::OddSkip => char_patterns::odd_skip,
  72.                             Patterns::Modulus => char_patterns::modulus
  73.                         };
  74.                        
  75.                         if check(self) {
  76.                             for ch in replace {
  77.                                 self.buffer.insert(index_replace(i+1, &word, &ch.to_string()));
  78.                             }
  79.                         }
  80.                        
  81.                         }
  82.                     }
  83.                 }
  84.             }
  85.             Some(self.buffer.clone())
  86.         }
  87.         else {
  88.             None
  89.         }
  90.     }
  91. }
  92. fn main() {
  93.     let permutator = PermutationIterator::new("password".to_string(), vec![
  94.             ('p', vec!['P', 'p', 'q', 's'])
  95.         ]);
  96.    
  97.     for x in permutator {
  98.         println!("{:?}", x)
  99.     };
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement