Class LRUMap
In: lib/collections/lru_map.rb
Parent: Object

Least Recently Used Map

Least Recently Used discards the least recently used items first.

Description

A Map implementation with a fixed maximum size which removes the least recently used entry if an entry is added when full.

Usage

 require 'rubygems'
 require 'collections'
 # or
 require 'collections/lr_map'

 map = LRUMap.new :max_size => 3
 map[:a] = "a value"
 map[:b] = "b value"
 map.full? # => false
 map.max_size # => 3
 map.size # => 2
 map[:c] = "c value"
 map[:d] = "d value"
 map.size # => 3
 map.key?(:a) # => false
 map.inspect # => {:b=>"b value", :c=>"c value", :d=>"d value"}
 x = map[:b]
 map[:f] = "f value"
 map.inspect # => {:b=>"b value", :d=>"d value", :f=>"f value"}

Source

code.juretta.com/project/collections/

Author

 Stefan Saasen <s@juretta.com>

Licence

 MIT

Methods

[]   []=   contains_key?   full?   get   inspect   is_full?   key?   keys   new   put   size   values  

Constants

DEFAULT_MAX_SIZE = 100

Attributes

max_size  [R] 

Public Class methods

map = LRUMap.new # default 100 map = LRUMap.new( :max_size => 10000) # Max Size is set to 1000

Public Instance methods

[](key)

Alias for get

[]=(key, val)

Alias for put

contains_key?(key)

Alias for key?

returns true if the Map is full, false otherwise

Returns the value for the key

Return the contents of this Map as a string.

is_full?()

Alias for full?

Returns true if the Map contains the key

Returns a new array populated with the keys from this hash. See also +LRUMap#values+.

adds a Key-Value pair to the map. If the Map is full the least recently used item is discarded.

Returns the number of elements (key-value pairs) in Map

Returns a new array populated with the values from hsh. See also +LRUMap#keys+.

[Validate]