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 1791
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 1813
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 1823
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 1858
def clear
  @data.clear
end
count() click to toggle source
# File pim.rb, line 1862
def count
  @data.count
end
delete(k) click to toggle source
# File pim.rb, line 1854
def delete(k)
  @data.delete(k)
end
each() { |pair| ... } click to toggle source
# File pim.rb, line 1836
def each
  @data.reverse.each do |pair|
    yield pair
  end
end
include?(key) click to toggle source
# File pim.rb, line 1832
def include?(key)
  @data.include?(key)
end
max_size=(size) click to toggle source
# File pim.rb, line 1802
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 1796
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 1842
def to_a
  @data.to_a.reverse
end
to_h() click to toggle source
# File pim.rb, line 1846
def to_h
  Hash[@data.to_a]
end
to_s() click to toggle source
# File pim.rb, line 1850
def to_s
  to_h.to_s
end