Parsing Engine

danbikel.parser
Class ProbabilityCache

java.lang.Object
  extended byjava.util.AbstractMap
      extended bydanbikel.util.AbstractMapToPrimitive
          extended bydanbikel.util.HashMapPrimitive
              extended bydanbikel.util.HashMapDouble
                  extended bydanbikel.parser.ProbabilityCache
All Implemented Interfaces:
Cloneable, FlexibleMap, Map, MapToPrimitive, Serializable

public class ProbabilityCache
extends HashMapDouble

A cache for storing arbitrary objects with their probabilities. This class uses a hash map, and offers several replacement strategies.

See Also:
Serialized Form

Nested Class Summary
 
Nested classes inherited from class danbikel.util.HashMapDouble
HashMapDouble.Entry
 
Field Summary
static int BUCKET_LRU
          Integer to indicate to delete the least-recently used entry in the same bucket as an entry being added after the size limit of this cache has been reached or exceeded.
static int CLEAR_ALL
          Integer to indicate to delete all of the elements every time the size limit of this cache has been reached or exceeded.
static int HALF_LIFE
          Integer to indicate to delete a random half of the elements every time the size limit of this cache has been reached or exceeded.
static int RANDOM
          Integer to indicate to delete a random element every time the size limit of this cache has been exceeded.
 
Fields inherited from class danbikel.util.HashMapPrimitive
count, DEFAULT_LOAD_FACTOR, DEFAULT_SIZE, modCount, table
 
Constructor Summary
ProbabilityCache(int maxCapacity)
          Constructs a ProbabilityCache with the specified maximum capacity and the default replacement strategy.
ProbabilityCache(int maxCapacity, int initialCapacity)
          Constructs a ProbabilityCache with the specified maximum capacity, the specified initial capacity and the default replacement strategy.
ProbabilityCache(int maxCapacity, int initialCapacity, float loadFactor)
          Constructs a ProbabilityCache with the specified maximum capacity, the specified initial capacity, the specified load factor and the default replacement strategy.
 
Method Summary
 boolean containsKey(Object key)
          Returns true if this map contains a mapping for the specified key.
 Object get(Object key)
          Throws an UnsupportedOperationException, as the only way to get values from this specialized cache is through the getProb(Object) method.
 MapToPrimitive.Entry getProb(Object key)
          Returns the Double containing the probability of the specified key, or null if the specified key is not in this cache.
 double put(Object key, double probability)
          Adds the specified key with the specified probability to this cache.
 Object put(Object key, Object value)
          Throws an UnsupportedOperationException, as the only way to add keys to this specialized cache is through the put(Object,double) method.
 double putAndRemove(Object key, double probability)
          A synonym for putAndRemove(key, key.hashCode(), probability).
 double putAndRemove(Object key, int hash, double probability)
          Puts the specified key into the cache with the specified probability, removing the least-recently used key from this key's bucket if the bucket is not currently empty.
 void removeRandom()
          Removes a random mapping from this map (optional operation).
 void setMaxCapacity(int maxCapacity)
          Sets the maximum capacity for this cache.
 ProbabilityCache setStrategy(int strategy)
          Sets the strategy for replacement when the size limit of this cache has been reached.
 
Methods inherited from class danbikel.util.HashMapDouble
getNewEntry
 
Methods inherited from class danbikel.util.HashMapPrimitive
add, add, add, add, add, add, addEntryMRU, clear, clone, containsValue, entrySet, get, getCapacity, getEntry, getEntry, getEntryMRU, getEntryMRU, getLoadFactor, getStats, isEmpty, keySet, put, put, put, put, put, put, put, putAll, putAll, remove, removeLRU, removeLRU, removeRandom, size, values
 
Methods inherited from class danbikel.util.AbstractMapToPrimitive
add, add, add, add, add, add, put, put, put, put, put, put, toString
 
Methods inherited from class java.util.AbstractMap
equals, hashCode
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface java.util.Map
equals, hashCode
 

Field Detail

RANDOM

public static final int RANDOM
Integer to indicate to delete a random element every time the size limit of this cache has been exceeded.

See Also:
setStrategy(int), put(Object,double), Constant Field Values

BUCKET_LRU

public static final int BUCKET_LRU
Integer to indicate to delete the least-recently used entry in the same bucket as an entry being added after the size limit of this cache has been reached or exceeded. Put another way, after the size limit of the cache has been reached (the size limit is specified at construction), every time an element is cached, if its bucket contains at least one item, then the least-recently used item of that bucket is first deleted and then the current item is added. Thus, this replacement strategy will treat the maximum capacity of this cache as a soft limit, for in instances where the bucket is already empty, this strategy will simply add an item, deleting nothing. A consequence is that the average bucket size can increase over time, but never by more than 1 + the average bucket size at the time the maximum capacity was reached (because only empty buckets can increase in size from 0 to 1).

This strategy is the default.

See Also:
setStrategy(int), put(Object,double), Constant Field Values

HALF_LIFE

public static final int HALF_LIFE
Integer to indicate to delete a random half of the elements every time the size limit of this cache has been reached or exceeded.

See Also:
setStrategy(int), put(Object,double), Constant Field Values

CLEAR_ALL

public static final int CLEAR_ALL
Integer to indicate to delete all of the elements every time the size limit of this cache has been reached or exceeded.

See Also:
setStrategy(int), put(Object,double), Constant Field Values
Constructor Detail

ProbabilityCache

public ProbabilityCache(int maxCapacity)
Constructs a ProbabilityCache with the specified maximum capacity and the default replacement strategy.

Parameters:
maxCapacity - the maximum number of elements held by this cache
See Also:
BUCKET_LRU, setStrategy(int)

ProbabilityCache

public ProbabilityCache(int maxCapacity,
                        int initialCapacity)
Constructs a ProbabilityCache with the specified maximum capacity, the specified initial capacity and the default replacement strategy.

Parameters:
maxCapacity - the maximum number of elements held by this cache
initialCapacity - the initial capacity of the underlying hash map
See Also:
BUCKET_LRU, setStrategy(int)

ProbabilityCache

public ProbabilityCache(int maxCapacity,
                        int initialCapacity,
                        float loadFactor)
Constructs a ProbabilityCache with the specified maximum capacity, the specified initial capacity, the specified load factor and the default replacement strategy.

Parameters:
maxCapacity - the maximum number of elements held by this cache
initialCapacity - the initial capacity of the underlying hash map
loadFactor - the load factor of the underlying hash map
See Also:
BUCKET_LRU, setStrategy(int)
Method Detail

setStrategy

public ProbabilityCache setStrategy(int strategy)
Sets the strategy for replacement when the size limit of this cache has been reached.

Returns:
this probability cache object
See Also:
RANDOM, BUCKET_LRU, HALF_LIFE, CLEAR_ALL

setMaxCapacity

public void setMaxCapacity(int maxCapacity)
Sets the maximum capacity for this cache.

Throws:
IllegalArgumentException - if the specified maximum capacity is zero or negative

put

public Object put(Object key,
                  Object value)
Throws an UnsupportedOperationException, as the only way to add keys to this specialized cache is through the put(Object,double) method.

Specified by:
put in interface Map
Overrides:
put in class HashMapPrimitive
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 HashMap previously associated null with the specified key.
See Also:
put(Object,double)

put

public double put(Object key,
                  double probability)
Adds the specified key with the specified probability to this cache. As a side effect, if the maximum capacity of this cache has been reached or exceeded at the time this method is invoked, then one or more cached elements may be removed, depending on the cache strategy being used.

Specified by:
put in interface MapToPrimitive
Overrides:
put in class AbstractMapToPrimitive
Parameters:
key - the key to add to this cache
probability - the probability of the specified key to be cached
Returns:
the old value associated with key, or null if there was no mapping for this key
See Also:
setStrategy(int)

putAndRemove

public final double putAndRemove(Object key,
                                 double probability)
A synonym for putAndRemove(key, key.hashCode(), probability).

Parameters:
key - the key to be inserted into this cache
probability - the probability of the key
Returns:
the old probability of the specified key, or Double.NaN if the key did not exist in this map

putAndRemove

public final double putAndRemove(Object key,
                                 int hash,
                                 double probability)
Puts the specified key into the cache with the specified probability, removing the least-recently used key from this key's bucket if the bucket is not currently empty.

Parameters:
key - the key to be inserted into this cache
hash - the hash value of the specified key
probability - the probability of the specified key
Returns:
the old probability of the specified key, or Double.NaN if the key did not exist in this map

get

public Object get(Object key)
Throws an UnsupportedOperationException, as the only way to get values from this specialized cache is through the getProb(Object) method.

Specified by:
get in interface Map
Overrides:
get in class HashMapPrimitive
Parameters:
key - key whose associated value is to be returned.
Returns:
the value to which this map maps the specified key.
See Also:
getProb(Object)

getProb

public MapToPrimitive.Entry getProb(Object key)
Returns the Double containing the probability of the specified key, or null if the specified key is not in this cache.

Parameters:
key - the key to look up in this cache
Returns:
the probability of the specified key or null if it is not in this cache

containsKey

public boolean containsKey(Object key)
Description copied from class: HashMapPrimitive
Returns true if this map contains a mapping for the specified key.

Specified by:
containsKey in interface Map
Overrides:
containsKey in class HashMapPrimitive
Parameters:
key - key whose presence in this Map is to be tested.
Returns:
true if this map contains a mapping for the specified key.

removeRandom

public void removeRandom()
Description copied from class: AbstractMapToPrimitive
Removes a random mapping from this map (optional operation).

Specified by:
removeRandom in interface MapToPrimitive
Overrides:
removeRandom in class AbstractMapToPrimitive

Parsing Engine

Author: Dan Bikel.