Advertisement
Guest User

Untitled

a guest
Oct 27th, 2016
373
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.78 KB | None | 0 0
  1. package movingMinMax
  2. import java.util.TreeMap;
  3.  
  4. abstract class Filter[A <% Ordered[A]](val windowSize: Int) {
  5. def insert(value: A): (A, A)
  6. }
  7.  
  8. /**
  9. * The slow moving min-max filter class given as a reference.
  10. * Each apply call takes time \Omega(windowSize).
  11. * Do not modify the code in this class.
  12. */
  13. class SlowFilter[A <% Ordered[A]](windowSize: Int) extends Filter[A](windowSize) {
  14. val queue = new scala.collection.mutable.Queue[A]()
  15.  
  16. /**
  17. * Insert the value to the moving window, remove the earliest one, and
  18. * return the minimum and maximum values in the current window.
  19. */
  20. def insert(value: A): (A, A) = {
  21. if (queue.size == windowSize) queue.dequeue
  22. queue.enqueue(value)
  23. // The following is faster than just saying (queue.min, queue.max)
  24. // because only one pass over the queue is made
  25. var min = queue.head
  26. var max = queue.head
  27. for (v <- queue) {
  28. if (v < min) min = v
  29. if (v > max) max = v
  30. }
  31. (min, max)
  32. }
  33. }
  34.  
  35. /**
  36. * The faster moving min-max filter class.
  37. * Each apply should run in time O(log windowSize).
  38. */
  39. class FastFilter[A <% Ordered[A]](windowSize: Int) extends Filter[A](windowSize) {
  40. val queue = new scala.collection.mutable.Queue[A]()
  41. /* Insert additional data structures here */
  42. val tree = new java.util.TreeMap[A, Int]()
  43. /**
  44. * Insert the value to the moving window, remove the earliest one, and
  45. * return the minimum and maximum values in the current window.
  46. */
  47. def insert(value: A): (A, A) = {
  48.  
  49. if (queue.size == windowSize) { // jos frame on täynnä, pitää perästä poistaa
  50. if (tree.containsKey(value)) { // jos sisältää jo ennestään lisättävän arvon, tulee arvoa lisätä
  51.  
  52. tree.put(value, tree.get(value) + 1) //arvon lisäys
  53. if (tree.get(queue.head) > 1) { tree.put(queue.head, tree.get(value) - 1); queue.dequeue } // jos ensimmäinen jonossa (poistettava) on kopio toisesta jonossa, tulee puun vastaavan mapin arvoa vähentää
  54. else {
  55. tree.remove(queue.head); queue.dequeue;
  56. }
  57. } else // Jos viimeinen jonossa on uniikki, poistetaan vain puusta jonon kärkeä vastaava map ja poistetaan jonon kärki.
  58. { queue.enqueue(value) } // Jos jonossa ei ole jos instanssia lisättävästä valuea, lisätään jonoon
  59.  
  60. } // jos frame ei ole täynnä, pitää tsekata löytyykö avain jo mutta poistoa ei tehdä
  61. else {
  62. if (tree.containsKey(value)) { // jos puu sisältää jo ennestään lisättävän valuen, tulee int-arvoa kasvattaa
  63. tree.put(value, tree.get(value) + 1)
  64. } else { tree.put(value, 1) }
  65.  
  66. queue.enqueue(value)
  67.  
  68. }
  69.  
  70. (tree.firstKey, tree.lastKey)
  71.  
  72. }
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement