Advertisement
Lyend

Untitled

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