Advertisement
Guest User

Untitled

a guest
Aug 25th, 2019
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.83 KB | None | 0 0
  1. #![feature(core_intrinsics)]
  2.  
  3. use std::fmt;
  4. use std::io::{self, Write};
  5. use std::intrinsics::likely;
  6.  
  7. const WINDOW_SIZE: usize = 10;
  8. const UPPER_BOUND: usize = 10;
  9.  
  10. struct FloatingWindow {
  11. data: [usize; WINDOW_SIZE],
  12. start_index: usize,
  13. current_size: usize,
  14. curr_sum: usize,
  15. counter: [usize; UPPER_BOUND]
  16. }
  17.  
  18. impl FloatingWindow {
  19. pub fn new() -> Self {
  20. Self {
  21. start_index: 0,
  22. current_size: 0,
  23. data: [0; WINDOW_SIZE],
  24. curr_sum: 0,
  25. counter: [0; UPPER_BOUND],
  26. }
  27. }
  28.  
  29. pub fn at(&self, idx: usize) -> usize {
  30. self.data[(self.start_index + idx) % WINDOW_SIZE]
  31. }
  32.  
  33. pub fn push(&mut self, value: usize) {
  34. let old_value = self.at(self.current_size);
  35. self.curr_sum -= old_value;
  36. self.curr_sum += value;
  37. unsafe {
  38. if likely(self.counter[old_value] != 0) {
  39. self.counter[old_value] -= 1;
  40. }
  41. }
  42. self.counter[value] += 1;
  43. self.data[(self.start_index + self.current_size) % WINDOW_SIZE] = value;
  44. if self.current_size < WINDOW_SIZE {
  45. self.current_size += 1;
  46. } else {
  47. self.start_index += 1;
  48. }
  49. }
  50.  
  51. pub fn sum(&self) -> usize {
  52. return self.curr_sum;
  53. }
  54.  
  55. pub fn avg(&self) -> Option<usize> {
  56. if self.current_size != 0 {
  57. Some(self.curr_sum / (self.current_size))
  58. } else {
  59. None
  60. }
  61. }
  62.  
  63. pub fn most_common(&self) -> usize {
  64. let mut num = 0;
  65. for (idx, &value) in self.counter.iter().enumerate() {
  66. if value > self.counter[num] {
  67. num = idx;
  68. }
  69. }
  70. num
  71. }
  72. }
  73.  
  74. impl fmt::Debug for FloatingWindow {
  75. fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
  76. write!(
  77. f,
  78. "FloatingWindow {{ {:?}, sum: {:?}, avg: {:?}, most_common: {:?} }}",
  79. self.into_iter().collect::<Vec<usize>>(),
  80. self.sum(),
  81. self.avg(),
  82. self.most_common()
  83. )
  84. }
  85. }
  86.  
  87. struct FloatingWindowIterator<'a> {
  88. window: &'a FloatingWindow,
  89. curr_index: usize,
  90. }
  91.  
  92. impl<'a> IntoIterator for &'a FloatingWindow {
  93. type Item = usize;
  94. type IntoIter = FloatingWindowIterator<'a>;
  95.  
  96. fn into_iter(self) -> Self::IntoIter {
  97. FloatingWindowIterator::new(self)
  98. }
  99. }
  100.  
  101. impl<'a> FloatingWindowIterator<'a> {
  102. pub fn new(window: &'a FloatingWindow) -> Self {
  103. Self {
  104. window,
  105. curr_index: 0,
  106. }
  107. }
  108. }
  109.  
  110. impl<'a> Iterator for FloatingWindowIterator<'a> {
  111. type Item = usize;
  112.  
  113. fn next(&mut self) -> Option<Self::Item> {
  114. if self.curr_index < self.window.current_size {
  115. self.curr_index += 1;
  116. Some(self.window.at(self.curr_index - 1))
  117. } else {
  118. None
  119. }
  120. }
  121. }
  122.  
  123. fn main() {
  124. let stdin = io::stdin();
  125. let mut stdout = io::stdout();
  126. let mut user_input = String::new();
  127. let mut window = FloatingWindow::new();
  128. loop {
  129. print!("> ");
  130. stdout.flush().expect("I/O error");
  131. user_input.truncate(0);
  132. stdin.read_line(&mut user_input).expect("I/O error");
  133.  
  134. if user_input == "" || user_input == "q\n" {
  135. break;
  136. }
  137.  
  138. user_input = user_input.trim().to_string();
  139.  
  140. if user_input == "" {
  141. continue;
  142. }
  143.  
  144. if user_input == "p" {
  145. println!("{:?}", window);
  146. } else {
  147. match user_input.parse() {
  148. Ok(num) => {
  149. if num >= UPPER_BOUND {
  150. eprintln!("num must be less than {}", UPPER_BOUND);
  151. } else {
  152. window.push(num);
  153. }
  154. }
  155. Err(err) => eprintln!("Error parsing number: {}", err),
  156. }
  157. }
  158. }
  159. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement