cz.cuni.jagrlib
Class LRUCache<T>

java.lang.Object
  extended by java.util.AbstractCollection<T>
      extended by cz.cuni.jagrlib.IntMap<T>
          extended by cz.cuni.jagrlib.LRUCache<T>
All Implemented Interfaces:
java.io.Serializable, java.lang.Cloneable, java.lang.Iterable<T>, java.util.Collection<T>

public class LRUCache<T>
extends IntMap<T>

LRU-caching integer map. Stores only limited number of [int,value] pairs.

Since:
0.26
See Also:
LRUCache.java, Serialized Form

Nested Class Summary
protected static class LRUCache.LRUItem
           
 
Nested classes/interfaces inherited from class cz.cuni.jagrlib.IntMap
IntMap.CommonIterator, IntMap.TargetIterator, IntMap.TupleIterator
 
Field Summary
protected  int cacheCapacity
          Cache capacity in items.
protected  LRUCache.LRUItem head
          Head of the LRU-list.
protected  int HEAD_KEY
          Special value used for LRU-list head.
protected  LRUCache.LRUItem recycled
          Linked list of recycled items (older is used).
 
Fields inherited from class cz.cuni.jagrlib.IntMap
maxLoad, NULL_KEY, REMOVED_KEY
 
Constructor Summary
LRUCache()
          Dafault parameters (small initial capacity, default load factor = 0.6).
LRUCache(int capacity)
          Explicit map capacity, default load factor (0.6).
LRUCache(int capacity, float loadFactor)
          Explicit map capacity and load-factor.
LRUCache(java.util.Map<? extends java.lang.Integer,? extends T> m)
          The same mappings as the specified Map.
 
Method Summary
protected  void access(int key)
          Marks the given key as most recently used.
 void clear()
          Removes all mappings from this map.
 java.lang.Object clone()
          Returns a shallow copy of this IntMap instance: the values themselves are not cloned.
 void compact()
          Rehashes the data structure to be more compact (and fast - if remove(int) operations were performed before).
 T get(int key)
          Returns the value to which this map maps the specified key.
 T put(int key, T value)
          Associates the specified value with the specified key in this map If the map previously contained a mapping for this key, the old value is replaced by the specified value (a returned back).
 T remove(int key)
          Removes the mapping for this key from this map if it is present Returns the value to which the map previously associated the key, or null if the map contained no mapping for this key.
 void setCacheCapacity(int cacheCap)
          Sets a new cache capacity.
 
Methods inherited from class cz.cuni.jagrlib.IntMap
containsKey, containsValue, find, isEmpty, iterator, keySet, putAll, readObject, rehash, size, tupleIterator, values, writeObject
 
Methods inherited from class java.util.AbstractCollection
add, addAll, contains, containsAll, remove, removeAll, retainAll, toArray, toArray, toString
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface java.util.Collection
equals, hashCode
 

Field Detail

HEAD_KEY

protected final int HEAD_KEY
Special value used for LRU-list head.

See Also:
Constant Field Values

head

protected LRUCache.LRUItem head
Head of the LRU-list.


cacheCapacity

protected int cacheCapacity
Cache capacity in items.


recycled

protected LRUCache.LRUItem recycled
Linked list of recycled items (older is used).

Constructor Detail

LRUCache

public LRUCache()
Dafault parameters (small initial capacity, default load factor = 0.6).


LRUCache

public LRUCache(int capacity)
Explicit map capacity, default load factor (0.6).


LRUCache

public LRUCache(int capacity,
                float loadFactor)
Explicit map capacity and load-factor.


LRUCache

public LRUCache(java.util.Map<? extends java.lang.Integer,? extends T> m)
The same mappings as the specified Map. The IntMap is created with default load factor (0.6) and an initial capacity sufficient to hold the mappings in the specified Map.

Parameters:
m - the map whose mappings are to be placed in this map.
Method Detail

access

protected final void access(int key)
Marks the given key as most recently used.


setCacheCapacity

public void setCacheCapacity(int cacheCap)
Sets a new cache capacity.


get

public T get(int key)
Returns the value to which this map maps the specified key. Cannot distinguish between empty mapping (i.e. no put( key, x ) was called) and mapping with null value (put( key, null ) was called). The containsKey predicate can be used to distinguish these two cases.

Overrides:
get in class IntMap<T>
Parameters:
key - Key whose associated value is to be returned.
Returns:
The value to which this map maps the specified key, or null if the map contains no mapping for this key.
See Also:
IntMap.containsKey(int)

put

public T put(int key,
             T value)
Associates the specified value with the specified key in this map If the map previously contained a mapping for this key, the old value is replaced by the specified value (a returned back).

Overrides:
put in class IntMap<T>
Parameters:
key - Key with which the specified value is to be associated.
value - Value to be associated with the specified key.
Returns:
Previous value associated with specified key, or null if there was no mapping for key. A null return can also indicate that the map previously associated null with the specified key.

remove

public T remove(int key)
Removes the mapping for this key from this map if it is present

Returns the value to which the map previously associated the key, or null if the map contained no mapping for this key. (A null return can also indicate that the map previously associated null with the specified key). The map will not contain a mapping for the specified key once the call returns.

Overrides:
remove in class IntMap<T>
Parameters:
key - Key whose mapping is to be removed from the map.
Returns:
Previous value associated with specified key, or null if there was no mapping for key.

clear

public final void clear()
Removes all mappings from this map.

Specified by:
clear in interface java.util.Collection<T>
Overrides:
clear in class IntMap<T>

compact

public void compact()
Rehashes the data structure to be more compact (and fast - if remove(int) operations were performed before).

Overrides:
compact in class IntMap<T>

clone

public java.lang.Object clone()
Returns a shallow copy of this IntMap instance: the values themselves are not cloned.

Overrides:
clone in class IntMap<T>
Returns:
A shallow copy of this map.