Skip to content

Instantly share code, notes, and snippets.

@jimwhite
Created September 3, 2012 20:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jimwhite/3613264 to your computer and use it in GitHub Desktop.
Save jimwhite/3613264 to your computer and use it in GitHub Desktop.
Improved solution for Cedric's sliding window key problem.
// http://beust.com/weblog/2012/09/02/coding-challenge-a-sliding-window-map/
import java.util.concurrent.PriorityBlockingQueue
public class SlidingWindowMap
{
def queue
def windowWidthMillis
public SlidingWindowMap(Set<String> keys, int maxCount, long periodMs)
{
queue = new PriorityBlockingQueue(keys.size() * maxCount, { a, b -> a[0].peek() <=> b[0].peek() } as Comparator)
def random = new Random()
// Seed the timestamps with random values that are earlier than the beginning of the window could be.
// Since System.currentTimeMillis() is a positive long we can use negative millions safely.
keys.each { key -> maxCount.times { queue.add([-random.nextInt(2**24), key]) } }
windowWidthMillis = periodMs
}
/**
* @return a key that has been used less than `maxCount` times during the
* past `periodMs` milliseconds or null if no such key exists.
*/
public String getNextKey()
{
// Get the key that was used first.
def keyrec = queue.poll()
if (keyrec) {
// Can we use it again?
if (keyrec[0] < System.currentTimeMillis() - windowWidthMillis) {
def key = keyrec[1]
queue.add ([System.currentTimeMillis(), key])
return key
}
// No. Just put it back.
queue.add(keyrec)
}
return null
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment