class PIM::LRUCache

Constants

DEFAULT_MAX_SIZE

Attributes

max_size[R]

Public Class Methods

new(max_size = DEFAULT_MAX_SIZE) click to toggle source
# File pim.rb, line 1999
def initialize(max_size = DEFAULT_MAX_SIZE)
  @max_size = max_size
  @data = {}
end

Public Instance Methods

[](key) click to toggle source
# File pim.rb, line 2021
def [](key)
  found = true
  value = @data.delete(key){ found = false }
  if found
    @data[key] = value
  else
    nil
  end
end
[]=(key,val) click to toggle source
# File pim.rb, line 2031
def []=(key,val)
  @data.delete(key)
  @data[key] = val
  if @data.length > @max_size
    @data.delete(@data.first[0])
  end
  val
end
clear() click to toggle source
# File pim.rb, line 2066
def clear
  @data.clear
end
count() click to toggle source
# File pim.rb, line 2070
def count
  @data.count
end
delete(k) click to toggle source
# File pim.rb, line 2062
def delete(k)
  @data.delete(k)
end
each() { |pair| ... } click to toggle source
# File pim.rb, line 2044
def each
  @data.reverse.each do |pair|
    yield pair
  end
end
include?(key) click to toggle source
# File pim.rb, line 2040
def include?(key)
  @data.include?(key)
end
max_size=(size) click to toggle source
# File pim.rb, line 2010
def max_size=(size)
  raise ArgumentError.new(:max_size) if size < 1
  @max_size = size
  if @max_size < @data.size
    @data.keys[0..@max_size-@data.size].each do |k|
      @data.delete(k)
    end
  end
  return @max_size
end
min_size=(size) click to toggle source
# File pim.rb, line 2004
def min_size=(size)
  raise ArgumentError.new(:min_size) if size < 1
  return @max_size if size <= @max_size
  return max_size=(size)
end
to_a() click to toggle source
# File pim.rb, line 2050
def to_a
  @data.to_a.reverse
end
to_h() click to toggle source
# File pim.rb, line 2054
def to_h
  Hash[@data.to_a]
end
to_s() click to toggle source
# File pim.rb, line 2058
def to_s
  to_h.to_s
end