Advertisement
Guest User

Untitled

a guest
Apr 22nd, 2019
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.41 KB | None | 0 0
  1. #[derive(Debug, Clone, PartialEq)]
  2. struct RleRun<T> {
  3. data : Vec<T>,
  4. length : usize,
  5. }
  6.  
  7. impl<T> RleRun<T> {
  8. pub fn new() -> Self {
  9. Self {
  10. data : Vec::new(),
  11. length : 0,
  12. }
  13. }
  14. pub fn from_data(run_data : Vec<T>, run_length : usize) -> Self {
  15. Self {
  16. data : run_data,
  17. length : run_length,
  18. }
  19. }
  20. }
  21.  
  22. fn find_maximum_run<T: Clone + PartialEq>(input : Vec<T>) -> Option<RleRun<T>> {
  23. if input.is_empty() {
  24. return None
  25. }
  26. let mut the_run : Vec<T> = vec![input[0].clone()];
  27. let mut run_data_length : usize = 1;
  28. let mut run_length : usize = 0;
  29.  
  30. let mut found = false;
  31. while !found {
  32. let mut j = 0;
  33. while j < input.len() {
  34. dbg!(&j);
  35. if (j + 1) % run_data_length == 0 {
  36. eprintln!("Incrementing");
  37. run_length += 1;
  38. dbg!(&run_length);
  39. }
  40. if the_run[j % run_data_length] != input[j] {
  41. eprintln!("No match");
  42. the_run.push(input[j].clone());
  43. run_data_length += 1;
  44. run_length = 0;
  45. j = 0;
  46. }
  47. else {
  48. j += 1;
  49. }
  50. }
  51. found = true;
  52. }
  53. Some(RleRun::from_data(the_run, run_length))
  54. }
  55.  
  56. fn main() {
  57. dbg!(find_maximum_run(vec![1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]));
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement