class LRUCache
Attributes
count[R]
Public Class Methods
new(max, prc)
click to toggle source
# File lib/simms_structures/lru_cache.rb, line 8 def initialize(max, prc) @map = HashMap.new @store = LinkedList.new @max = max @prc = prc end
Public Instance Methods
get(key)
click to toggle source
# File lib/simms_structures/lru_cache.rb, line 19 def get(key) link = @map[key] if link update_link!(link) link.val else eject! if @map.count == @max calc!(key).val end end
to_s()
click to toggle source
# File lib/simms_structures/lru_cache.rb, line 30 def to_s "Map: " + @map.to_s + "\n" + "Store: " + @store.to_s end
Private Instance Methods
calc!(key)
click to toggle source
insert an (un-cached) key
# File lib/simms_structures/lru_cache.rb, line 37 def calc!(key) @store.insert(key, @prc.call(key)) @map[key] = @store.last end
eject!()
click to toggle source
# File lib/simms_structures/lru_cache.rb, line 48 def eject! link = @store.first @store.remove(link) end
update_link!(link)
click to toggle source
move a link to the end of the list
# File lib/simms_structures/lru_cache.rb, line 43 def update_link!(link) @store.insert(link.key, link.val) end