Created
March 7, 2013 07:27
-
-
Save zzzfree/5106203 to your computer and use it in GitHub Desktop.
MapQueue
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MapQueue { | |
def max=0 | |
def last=0 | |
def c = [:] | |
def q = [] | |
def MapQueue(m){ | |
max=m | |
} | |
def put(k,v){ | |
if(c.size()<max){ | |
q << k | |
} | |
else{ | |
c.remove(q[last]) | |
q[last] = k | |
last++ | |
if(last>max-1){ | |
last=0 | |
} | |
} | |
c.put(k, v) | |
println c | |
} | |
def get(k){ | |
return c.get(k) | |
} | |
static main(args) { | |
def m = new MapQueue(3) | |
6.times{ | |
m.put(it, it) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment