java.lang.ObjectmoveToFront.MoveToFrontCache<T>
T - - the type of the element stored in the move to front cachepublic class MoveToFrontCache<T>
MoveToFront cache project, due Monday, Oct 28th, 6pm
Implementation of a move-to-front cache, used in move-to-front encoding, as described in http://en.wikipedia.org/wiki/Move-to-front_transform
The abstraction we use was first described in A Locally Adaptive Data Compression Scheme, although we are using -1 to represent a value that doesn't occur, and they use one more than the last valid index. They also start their index values at 1 rather than 0.
In summary, maintains a set of values, ordered according to when they were last accessed. An index of 0 indicates the most recently accessed value. Supports two operations: findOrAdd and get.
The findOrAdd method either finds a value in the cache (returning the position of the value in the cache), or if not found adds the value to the cache and returns -1. Either way, after the operation, the value is moved to the front of the cache.
The get method returns a value at a particular index, and updates the cache by moving that value to the front of the list.
| Constructor Summary | |
|---|---|
MoveToFrontCache()
Construct an empty move to front cache |
|
| Method Summary | ||
|---|---|---|
static
|
decodeUsingMTF(java.util.List<java.lang.Integer> encoding,
java.util.List<T> uniqueValues)
Decode a list of |
|
static
|
encodeUsingMTF(java.util.List<T> lst,
java.util.List<java.lang.Integer> encoding,
java.util.List<T> uniqueValues)
Encode a list of |
|
int |
findOrAdd(T t)
Determine the position of t in the cache and move t to the front of the list. |
|
T |
get(int i)
Return the value at position i in the cache, and alter the cache by moving that element to the front of the cache. |
|
| Methods inherited from class java.lang.Object |
|---|
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Constructor Detail |
|---|
public MoveToFrontCache()
| Method Detail |
|---|
public static <T> java.util.List<T> decodeUsingMTF(java.util.List<java.lang.Integer> encoding,
java.util.List<T> uniqueValues)
encoding - a list of the integer values encoding the positions of the
values in the cacheuniqueValues - a list of the unique values that have been encoded and need to
be put into the resulting list
public static <T> void encodeUsingMTF(java.util.List<T> lst,
java.util.List<java.lang.Integer> encoding,
java.util.List<T> uniqueValues)
T - the type of the cache elementslst - the list to be encoded; elements cannot be null, list must not
be modifiedencoding - initially empty, stores the integer values of the encodinguniqueValues - initially empty, stores the unique values seen in the list,
the order seenpublic int findOrAdd(T t)
t - the value to find in or add to the cache
java.lang.NullPointerException - if t is null; Cache cannot contain null values;public T get(int i)
i - the position of the element to be returned
java.lang.IndexOutOfBoundsException - if index < 0 or >= number of elements in cache