Class: LRUCache

Inherits:
Object
  • Object
show all
Defined in:
lib/simms_structures/lru_cache.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(max, prc) ⇒ LRUCache

Returns a new instance of LRUCache.



8
9
10
11
12
13
# File 'lib/simms_structures/lru_cache.rb', line 8

def initialize(max, prc)
  @map = HashMap.new
  @store = LinkedList.new
  @max = max
  @prc = prc
end

Instance Attribute Details

#countObject (readonly)

Returns the value of attribute count.



6
7
8
# File 'lib/simms_structures/lru_cache.rb', line 6

def count
  @count
end

Instance Method Details

#get(key) ⇒ Object



19
20
21
22
23
24
25
26
27
28
# 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_sObject



30
31
32
# File 'lib/simms_structures/lru_cache.rb', line 30

def to_s
  "Map: " + @map.to_s + "\n" + "Store: " + @store.to_s
end