-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSlidingWindowMap.py
30 lines (23 loc) · 949 Bytes
/
SlidingWindowMap.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
from queue import Queue
# keeps track of the newest self.length (key, value) pairs
# backed by hashmap so elements in the queue are guaranteed to be unique
# wrote this to keep track of the newest X number of reddit posts returned by a search
# same functionality can be achieved with just a hashmap, but that grows larger and larger over time
# so keep track of only self.length pairs
class SlidingWindowMap(object):
def __init__(self, length):
self.map = {}
self.q = Queue()
self.length = length
def put(self, key, value):
if key not in self.map:
# remove the oldest element if the queue reaches max size
if self.q.qsize() == self.length:
del self.map[self.q.get()]
# put the new post in
self.q.put(key)
self.map[key] = value
return True
return False
def get(self, key):
return self.map[key]