Loading...
Searching...
No Matches
Cache< KeyClass, ValueClass > Class Template Reference

Class Cache is a template-implementation of a cache with arbitrary classes for representing keys and values, respectively. More...

`#include <Cache.h>`

## Public Member Functions

| Cache ()
| A constructor for class Cache.
| ~Cache ()
| A destructor for class Cache.
| Cache (const Cache &c)
| Copy implementation for class Cache.
| Cache (const int maxEntries, const int maxWeight)
| A user-suited constructor for class Cache.
int | getWeight () const
| A method for retrieving the momentary weight of the cache.
int | getNumberOfEntries () const
| A method for retrieving the momentary number of (key --> value) pairs in the cache.
int | getMaxNumberOfEntries () const
| A method for retrieving the maximum number of (key --> value) pairs in the cache.
int | getMaxWeight () const
| A method for retrieving the maximum weight of the cache.
bool | hasKey (const KeyClass &key) const
| Checks whether the cache contains a pair (k --> v) such that k equals the given key.
ValueClass | getValue (const KeyClass &key) const
| Returns the value for a given key.
bool | put (const KeyClass &key, const ValueClass &value)
| Inserts the pair (key --> value) in the cache.
void | clear ()
| Clears the cache so that it has no entry.
std::string | toString () const
| A method for providing a printable version of the represented cache, including all contained (key --> value) pairs.
void | print () const
| A method for printing a string representation of the given cache to std::cout.

## Private Member Functions

int | getIndexInKey (const KeyClass &key) const
| A method for providing the index of a given key in the vector _key.
int | getIndexInRank (const ValueClass &value) const
| A method for providing the index of a given value in the vector _rank.
bool | shrink (const KeyClass &key)
| A method for shrinking the given cache so that it meet the bounds on the maximum number of entries and total weight again.
bool | deleteLast (const KeyClass &key)
| A method for deleting the least-ranked cache entry.

## Private Attributes

std::list< int > | _rank
| A bijection on the set {0, ..., _key.size() - 1}.
std::list< KeyClass > | _key
| _key is sorted in ascending order, i.e., j < i ==> _key(j) < _key(i), where the right-hand side comparator "<" needs to be implemented in KeyClass.
std::list< ValueClass > | _value
| _value captures the actual objects of interest; `_value`[i] corresponds to `_key`[i] and may be retrieved by calling Cache::getValue (const KeyClass&) const with the argument `_key`[i]).
std::list< int > | _weights
| container for the weights of all cached values
std::list< KeyClass >::const_iterator | _itKey
| a pointer to some element of _key; We make this mutable so that methods which leave the cache unmodified but which alter _itKey can still be declared as const, as the user would expect for these methods.
std::list< ValueClass >::const_iterator | _itValue
| a pointer to some element of _value; We make this mutable so that methods which leave the cache unmodified but which alter _itValue can still be declared as const, as the user would expect for these methods.
int | _weight
| for storing the momentary weight of the given cache; This is the sum of `_value`[i].getWeight() over all i, i.e., over all cached values.
int | _maxEntries
| the bound for the number of cached `key --> value` pairs; The cache will automatically ensure that this bound will never be exceeded; see Cache::shrink (const KeyClass&) and Cache::deleteLast ().
int | _maxWeight
| the bound on total cache weight; The cache will automatically ensure that this bound will never be exceeded; see see Cache::shrink (const KeyClass&) and Cache::deleteLast ().

## Detailed Description

template<class KeyClass, class ValueClass>
class Cache< KeyClass, ValueClass >

Class Cache is a template-implementation of a cache with arbitrary classes for representing keys and values, respectively.

Each entry of the cache is of the form key --> value, where key is an instance of some `KeyClass`, and value an instance of some `ValueClass`.
This implementation comes with the possibility to define bounds on both the number of cached pairs key --> value and on the total weight of the cache.
Here, the weight of a cache is defined as the sum of weights of all cached values, where the weight of each value needs to be implemented in the actual class `ValueClass`. An example for the weight of a cached value may simply be its size in bytes, or may capture some other useful notion of how heavy the value is. E.g., when caching polynomials, the weight may be defined to be equal to the number of its monomials.
The key --> value pairs of a cache are being stored in two standard lists `_key` and `_value` of equal length L.
In order to enable a fast value lookup, the vector `_key` maintains an ordering such that
i < j ==> _key[i] < _key[j],
where the right-hand side comparator < needs to be implemented by class `KeyClass`. Note that this ordering allows for value lookup in time O(log(L)), when given a specific key.
In addition to `_key` and `_value`, there is a third book-keeping structure in Cache: The vector `_rank` of integers captures ranking information among all cached values. More concretely,
_rank : {0, 1, 2, ..., L - 1} --> {0, 1, 2, ..., L - 1}
is a bijection with the semantic
_rank[s] < _rank[t] :<==> _value[_rank[s]] < _value[_rank[t]],
where the right-hand side comparator < needs to be implemented by class `ValueClass`. The intention here is that any relation _rank[s] < _rank[t] is to imply that the key-value pair _key[_rank[t]] --> _value[_rank[t]] will be kept at least as long in cache as _key[_rank[s]] --> _value[_rank[s]]. (I.e., loosely speaking, the higher the rank, the longer a pair is going to be cached.)
Whenever the cache becomes either too large (in terms of number of entries), or too heavy (in terms of accumulated weight), it will automatically shrink until both bounds will have been re-established. To this end, the key --> value pair with least value rank will be erased from the cache. This process is repeated until the cache is small enough again, in terms of both the number of entries and the weight. (Note that it may be necessary to erase numerous pairs after inserting just one key --> value when this value's weight is unproportionally high.)
In order to make the above procedures work, the two template classes `KeyClass` and `ValueClass` need to implement the following methods:
`bool KeyClass::operator< (const KeyClass& key),`
`bool KeyClass::operator== (const KeyClass& key),`
`bool ValueClass::operator< (const ValueClass& key),`
`bool ValueClass::operator== (const ValueClass& key),`
`int ValueClass::getWeight ().`

Author
  Frank Seelisch, http://www.mathematik.uni-kl.de/~seelisch

Definition at line 68 of file Cache.h.

## Constructor & Destructor Documentation

## Cache() [1/3]

template<class KeyClass, class ValueClass>

Cache< KeyClass, ValueClass >::Cache | ( |  | ) |

A constructor for class Cache.

The method makes sure that all member vectors be empty.

Definition at line 421 of file CacheImplementation.h.

{ }

## ~Cache()

template<class KeyClass, class ValueClass>

Cache< KeyClass, ValueClass >::~Cache | ( |  | ) |

A destructor for class Cache.

The method clears all member vectors. (This includes that destructors are invoked, accordingly.)

Definition at line 45 of file CacheImplementation.h.

{
_rank.clear();
_key.clear();
_value.clear();
_weights.clear();
}
Cache::_weights
std::list< int > _weights
container for the weights of all cached values
Definition Cache.h:98
Cache::_value
std::list< ValueClass > _value
_value captures the actual objects of interest; _value[i] corresponds to _key[i] and may be retrieve...
Definition Cache.h:93
Cache::_rank
std::list< int > _rank
A bijection on the set {0, ..., _key.size() - 1}.
Definition Cache.h:77
Cache::_key
std::list< KeyClass > _key
_key is sorted in ascending order, i.e., j < i ==> _key(j) < _key(i), where the right-hand side compa...
Definition Cache.h:85

## Cache() [2/3]

template<class KeyClass, class ValueClass>

Cache< KeyClass, ValueClass >::Cache | ( | const Cache< KeyClass, ValueClass > & | c | ) |

Copy implementation for class Cache.

Apart from copying all flat members, all vectors are being deep-copied.

Definition at line 424 of file CacheImplementation.h.

{
_rank = c._rank;
_value = c._value;
_weights = c._weights;
_key = c._key;
_weight = c._weight;
_maxEntries = c._maxEntries;
_maxWeight = c._maxWeight;
}
Cache
Class Cache is a template-implementation of a cache with arbitrary classes for representing keys and ...
Definition Cache.h:69
Cache::_maxWeight
int _maxWeight
the bound on total cache weight; The cache will automatically ensure that this bound will never be e...
Definition Cache.h:138
Cache::_maxEntries
int _maxEntries
the bound for the number of cached key --> value pairs; The cache will automatically ensure that thi...
Definition Cache.h:130
Cache::_weight
int _weight
for storing the momentary weight of the given cache; This is the sum of _value[i]....
Definition Cache.h:122

## Cache() [3/3]

template<class KeyClass, class ValueClass>

Cache< KeyClass, ValueClass >::Cache | ( | const int | maxEntries,
|  | const int | maxWeight )

A user-suited constructor for class Cache.

The method makes sure that all member vectors be empty. Moreover, the user can provide bounds for the maximum number of entries in the cache, and for the total weight of the cache.

Parameters

  maxEntries | the (positive) maximum number of pairs (key --> value) in the cache
  maxWeight | the (positive) maximum weight of the cache

Definition at line 10 of file CacheImplementation.h.

{
_maxEntries = maxEntries;
_maxWeight = maxWeight;
_rank.clear();
_key.clear();
_value.clear();
_weights.clear();
_itKey = _key.end(); /* referring to past-the-end element in the list */
_itValue = _value.end(); /* referring to past-the-end element in the list */
_weight = 0;
}
Cache::_itKey
std::list< KeyClass >::const_iterator _itKey
a pointer to some element of _key; We make this mutable so that methods which leave the cache unmodif...
Definition Cache.h:106
Cache::_itValue
std::list< ValueClass >::const_iterator _itValue
a pointer to some element of _value; We make this mutable so that methods which leave the cache unmod...
Definition Cache.h:114

## Member Function Documentation

## clear()

template<class KeyClass, class ValueClass>

void Cache< KeyClass, ValueClass >::clear | ( |  | ) |

Clears the cache so that it has no entry.

This method will also enforce destruction of all former entries of the cache.

Definition at line 36 of file CacheImplementation.h.

{
_rank.clear();
_key.clear();
_value.clear();
_weights.clear();
}

## deleteLast()

template<class KeyClass, class ValueClass>

bool Cache< KeyClass, ValueClass >::deleteLast | ( | const KeyClass & | key | ) |

private

A method for deleting the least-ranked cache entry.

The method returns true iff the deleted pair (k --> v) satisfies k == key.

Parameters

  key | an instance of KeyClass

Returns
  true iff a pair (key --> *) was deleted

Definition at line 116 of file CacheImplementation.h.

{
if (_rank.size() == 0)
{
return false; /* nothing to do */
};
/* We need to perform the following (empty) loop in order to
obtain a forward-iterator pointing to the last entry of _rank.
Note: We cannot use rbegin() because we need the iterator for
erasing the last entry which is only implemented for forward
iterators by std::list. */
std::list<int>::iterator itRank;
for (itRank = _rank.begin(); itRank != _rank.end(); itRank++) { }
itRank--; /* Now, this forward iterator points to the last list entry. */
int deleteIndex = *itRank; /* index of (_key, _value)-pair with worst,
i.e., highest _rank */
bool result = false;
/* now delete entries in _key and _value with index deleteIndex */
int k = 0;
typename std::list<KeyClass>::iterator itKey;
typename std::list<ValueClass>::iterator itValue = _value.begin();
typename std::list<int>::iterator itWeights = _weights.begin();
for (itKey = _key.begin(); itKey != _key.end(); itKey++)
{
if (k == deleteIndex)
{
result = (key.compare(*itKey) == 0);
break;
}
itValue++;
itWeights++;
k++;
}
_key.erase(itKey);
int deleteWeight = *itWeights;
_value.erase(itValue);
_weights.erase(itWeights);
/* adjust total weight of this cache */
_weight -= deleteWeight;
/* now delete last entry of _rank and decrement all those indices
// in _rank by 1 which are larger than deleteIndex */
_rank.erase(itRank);
for (itRank = _rank.begin(); itRank != _rank.end(); itRank++)
{
if (*itRank > deleteIndex) *itRank -= 1;
}
return result;
}

## getIndexInKey()

template<class KeyClass, class ValueClass>

int Cache< KeyClass, ValueClass >::getIndexInKey | ( | const KeyClass & | key | ) | const

private

A method for providing the index of a given key in the vector _key.

Either _key contains the given key, then its index will be returned. Otherwise the position in _key, at which the given key should be placed (with respect to the ordering in _key) is returned.

Parameters

  key | an instance of KeyClass

Returns
  the actual or would-be index of key in _key

See also
  Cache::hasKey (const KeyClass&) const

## getIndexInRank()

template<class KeyClass, class ValueClass>

int Cache< KeyClass, ValueClass >::getIndexInRank | ( | const ValueClass & | value | ) | const

private

A method for providing the index of a given value in the vector _rank.

Based on the rank of the given value, the position in _rank at which the given value should be inserted, is returned. (The method also works, when the given value is already contained in the cache.)

Parameters

  value | an instance of ValueClass

Returns
  the actual or would-be index of value in _rank

## getMaxNumberOfEntries()

template<class KeyClass, class ValueClass>

int Cache< KeyClass, ValueClass >::getMaxNumberOfEntries | ( |  | ) | const

A method for retrieving the maximum number of (key --> value) pairs in the cache.

Returns
  the maximum number of cached values of the cache

See also
  Cache::getNumberOfEntries () const
  Cache::Cache (const int, const int)

Definition at line 104 of file CacheImplementation.h.

{
return _maxEntries;
}

## getMaxWeight()

template<class KeyClass, class ValueClass>

int Cache< KeyClass, ValueClass >::getMaxWeight | ( |  | ) | const

A method for retrieving the maximum weight of the cache.

Returns
  the maximum weight of the cache

See also
  Cache::getWeight () const
  Cache::Cache (const int, const int)

Definition at line 110 of file CacheImplementation.h.

{
return _maxWeight;
}

## getNumberOfEntries()

template<class KeyClass, class ValueClass>

int Cache< KeyClass, ValueClass >::getNumberOfEntries | ( |  | ) | const

A method for retrieving the momentary number of (key --> value) pairs in the cache.

The return value will always be less than or equal to the result of Cache::getMaxNumberOfEntries () const.

Returns
  the momentary number of cached values of the cache

See also
  Cache::getMaxNumberOfEntries () const

Definition at line 30 of file CacheImplementation.h.

{
return _rank.size();
}

## getValue()

template<class KeyClass, class ValueClass>

ValueClass Cache< KeyClass, ValueClass >::getValue | ( | const KeyClass & | key | ) | const

Returns the value for a given key.

The method assumes that there is actually an entry of the form (key --> *) in the cache. This can be checked before using Cache::hasKey (const KeyClass&) const. (Note that calling both methods in direct succession does not result in extra computational efforts.)

Assertions
  If the given key is not contained in the cache, program execution will be stopped.

Parameters

  key | the key, for which the corresponding value is to be returned

Returns
  the value corresponding to the given key

See also
  Cache::hasKey (const KeyClass&) const

Definition at line 78 of file CacheImplementation.h.

{
if (_itKey == _key.end())
/* _itKey refers to past-the-end element in the list;
thus, getValue has been called although hasKey
produced no match */
assume(false);
return *_itValue;
}
assume
#define assume(x)
Definition mod2.h:389

## getWeight()

template<class KeyClass, class ValueClass>

int Cache< KeyClass, ValueClass >::getWeight | ( |  | ) | const

A method for retrieving the momentary weight of the cache.

The return value will always be less than or equal to the result of Cache::getMaxWeight () const.
Semantically, the total weight of a cache is the sum of weights of all cached values.

Returns
  the momentary weight of the cache

See also
  Cache::getMaxWeight () const
  MinorValue::getWeight () const

Definition at line 24 of file CacheImplementation.h.

{
return _weight;
}

## hasKey()

template<class KeyClass, class ValueClass>

bool Cache< KeyClass, ValueClass >::hasKey | ( | const KeyClass & | key | ) | const

Checks whether the cache contains a pair (k --> v) such that k equals the given key.

If so, the method returns true; false otherwise. In order to make Cache::getValue (const KeyClass&) const work properly, the user is strongly advised to always check key containment by means of Cache::hasKey (const KeyClass&) const. (The implementation at hand ensures that invoking hasKey and getValue does not result in extra computational efforts.)

Parameters

  key | the key for which containment is to be checked

Returns
  true iff the cache contains the given key

See also
  Cache::getValue (const KeyClass&) const

Definition at line 54 of file CacheImplementation.h.

{
_itKey = _key.end(); // referring to past-the-end element in the list
typename std::list<KeyClass>::const_iterator itKey;
_itValue = _value.begin();
/* As _key is a sorted list, the following could actually be implemented
in logarithmic time, by bisection. However, for lists this does not work.
But often, we can still terminate the linear loop before having visited
all elements. */
for (itKey = _key.begin(); itKey != _key.end(); itKey++)
{
int c = key.compare(*itKey);
if (c == 0)
{
_itKey = itKey;
return true;
}
if (c == -1) return false;
_itValue++;
}
return false;
}

## print()

template<class KeyClass, class ValueClass>

void Cache< KeyClass, ValueClass >::print | ( |  | ) | const

A method for printing a string representation of the given cache to std::cout.

This includes string representations of all contained (key --> value) pairs.

Definition at line 415 of file CacheImplementation.h.

{
PrintS(this->toString().c_str());
}
Cache::toString
std::string toString() const
A method for providing a printable version of the represented cache, including all contained (key -->...
Definition CacheImplementation.h:355
PrintS
void PrintS(const char *s)
Definition reporter.cc:288

## put()

template<class KeyClass, class ValueClass>

bool Cache< KeyClass, ValueClass >::put | ( | const KeyClass & | key,
|  | const ValueClass & | value )

Inserts the pair (key --> value) in the cache.

If there is already some entry (key --> value'), then value will be replaced by value'. As putting some new pair in the cache may result in a violation of the maximal number of entries or the weight of the cache, or both, Cache::put (const KeyClass&, const ValueClass&) will always finalize by calling the private method Cache::shrink(const KeyClass&), in order to re-establish both bounds. Note that this may even result in deleting the newly inserted pair (key --> value).
Because of that undesirable but possible effect, the method returns whether the pair is actually contained in the cache after invocation of Cache::put (const KeyClass&, const ValueClass&).

Parameters

  key | an instance of KeyClass
  value | an instance of ValueClass

Returns
  whether the pair (key --> value) is contained in the modified cache

See also
  Cache::getValue (const KeyClass&) const

Definition at line 170 of file CacheImplementation.h.

{
bool keyWasContained = false;
int oldIndexInKey = -1;
int newIndexInKey = _key.size(); /* default to enter new (key, value)-pair
is at the end of the two lists;
only used in the case
keyWasContained == false */
int k = 0;
typename std::list<KeyClass>::iterator itKey;
// itOldValue will later only be used in the case keyWasContained == true: */
typename std::list<ValueClass>::iterator itOldValue = _value.begin();
/* itOldWeights will later only be used in the case
keyWasContained == true */
typename std::list<int>::iterator itOldWeights = _weights.begin();
for (itKey = _key.begin(); itKey != _key.end(); itKey++)
{
int c = key.compare(*itKey);
if (c == -1)
{
newIndexInKey = k;
break;
}
if (c == 0)
{
keyWasContained = true;
oldIndexInKey = k;
break;
}
itOldValue++;
itOldWeights++;
k++;
}
int utility = value.getUtility();
int newWeight = value.getWeight();
k = 0;
typename std::list<ValueClass>::iterator itValue = _value.begin();
for (itValue = _value.begin(); itValue != _value.end(); itValue++)
{
if (itValue->getUtility() > utility) k++;
}
int newIndexInRank = k;
if (keyWasContained)
{
/* There was already a pair of the form (key --> *). */
/*adjusting the weight of the cache */
ValueClass oldValue = *itOldValue;
_weight += newWeight - *itOldWeights;
/* overwriting old value by argument value */
itOldValue = _value.erase(itOldValue);
itOldWeights = _weights.erase(itOldWeights);
ValueClass myValueCopy = value;
_value.insert(itOldValue, myValueCopy);
_weights.insert(itOldWeights, newWeight);
int oldIndexInRank = -1;
/* oldIndexInRank is to be the position in _rank such that
_rank[oldIndexInRank] == oldIndexInKey, i.e.
_key[_rank[oldIndexInRank]] == key: */
std::list<int>::iterator itRank;
k = 0;
for (itRank = _rank.begin(); itRank != _rank.end(); itRank++)
{
if (*itRank == oldIndexInKey)
{
oldIndexInRank = k;
}
k++;
}
/* Although the key stays the same, the ranking of the (key --> value)
pair may be completely different from before. Thus, we need to repair
the entries of _rank: */
if (oldIndexInRank < newIndexInRank)
{ /* first insert, then erase */
k = 0;
/* insert 'oldIndexInKey' at new position 'newIndexInRank': */
for (itRank = _rank.begin(); itRank != _rank.end(); itRank++)
{
if (k == newIndexInRank) break;
k++;
}
_rank.insert(itRank, oldIndexInKey); /* note that this may also insert
at position itRank == _rank.end(),
i.e. when above loop did not
terminate because of a 'break'
statement */
k = 0;
/* erase 'oldIndexInKey' at old position 'oldIndexInRank': */
for (itRank = _rank.begin(); itRank != _rank.end(); itRank++)
{
if (k == oldIndexInRank)
{
_rank.erase(itRank);
break;
}
k++;
}
}
else
{ /* oldIndexInRank >= newIndexInRank */
if (oldIndexInRank > newIndexInRank)
{ /* first erase, then insert */
k = 0;
/* erase 'oldIndexInKey' at old position 'oldIndexInRank': */
for (itRank = _rank.begin(); itRank != _rank.end(); itRank++)
{
if (k == oldIndexInRank)
{
_rank.erase(itRank);
break;
}
k++;
}
k = 0;
/* insert 'oldIndexInKey' at new position 'newIndexInRank': */
for (itRank = _rank.begin(); itRank != _rank.end(); itRank++)
{
if (k == newIndexInRank)
{
_rank.insert(itRank, oldIndexInKey);
break;
}
k++;
}
}
}
}
else
{
/* There is no pair of the form (key --> *). We are about to insert
a completely new (key, value)-pair.
After this "else" branch, we shall have _key[newIndexInKey] = key;
_value[newIndexInKey] = value. Note that, after the above computation,
newIndexInKey contains the correct target position.
Let's make room for the assignment
_rank[newIndexInRank] := newIndexInKey: */
std::list<int>::iterator itRank;
for (itRank = _rank.begin(); itRank != _rank.end(); itRank++)
{
if (newIndexInKey <= *itRank)
{
*itRank += 1;
}
}
k = 0;
for (itRank = _rank.begin(); itRank != _rank.end(); itRank++)
{
if (k == newIndexInRank) break;
k++;
}
_rank.insert(itRank, newIndexInKey);
/* let's insert new key and new value at index newIndexInKey: */
itValue = _value.begin();
typename std::list<int>::iterator itWeights = _weights.begin();
k = 0;
for (itKey = _key.begin(); itKey != _key.end(); itKey++)
{
if (k == newIndexInKey) break;
itValue++;
itWeights++;
k++;
}
KeyClass myKeyCopy = key;
ValueClass myValueCopy = value;
_key.insert(itKey, myKeyCopy);
_value.insert(itValue, myValueCopy);
_weights.insert(itWeights, newWeight);
/* adjusting the total weight of the cache: */
_weight += newWeight;
};
/* We may now have to shrink the cache: */
bool result = shrink(key); /* true iff shrinking deletes the
new (key, value)-pair */
assume(_rank.size() == _key.size());
assume(_rank.size() == _value.size());
return !result; /* true iff the new (key --> value) pair is
actually in the cache now */
}
Cache::getWeight
int getWeight() const
A method for retrieving the momentary weight of the cache.
Definition CacheImplementation.h:24
Cache::shrink
bool shrink(const KeyClass &key)
A method for shrinking the given cache so that it meet the bounds on the maximum number of entries an...
Definition CacheImplementation.h:90

## shrink()

template<class KeyClass, class ValueClass>

bool Cache< KeyClass, ValueClass >::shrink | ( | const KeyClass & | key | ) |

private

A method for shrinking the given cache so that it meet the bounds on the maximum number of entries and total weight again.

The method returns true iff the process of shrinking deleted a pair (k --> v) from the cache such that k equals the given key.

Parameters

  key | an instance of KeyClass

Returns
  true iff shrinking deleted a pair (key --> *)

Definition at line 90 of file CacheImplementation.h.

{
/* We need to return true iff the pair with given key needed to
be erased during the shrinking procedure. So far, we assume no: */
bool result = false;
/* Shrink until both bounds will be met again: */
while (int(_key.size()) > _maxEntries || _weight > _maxWeight)
{
if (deleteLast(key)) result = true;
}
return result;
}
Cache::deleteLast
bool deleteLast(const KeyClass &key)
A method for deleting the least-ranked cache entry.
Definition CacheImplementation.h:116

## toString()

template<class KeyClass, class ValueClass>

std::string Cache< KeyClass, ValueClass >::toString | ( |  | ) | const

A method for providing a printable version of the represented cache, including all contained (key --> value) pairs.

Returns
  a printable version of the given instance as instance of class string

Definition at line 355 of file CacheImplementation.h.

{
char h[12]; /*max.int length +1 for \0 */
std::string s = "Cache:";
s += "\n entries: ";
snprintf(h,12, "%d", getNumberOfEntries()); s += h;
s += " of at most ";
snprintf(h,12, "%d", getMaxNumberOfEntries()); s += h;
s += "\n weight: ";
snprintf(h,12, "%d", getWeight()); s += h;
s += " of at most ";
snprintf(h,12, "%d", getMaxWeight()); s += h;
if (_key.size() == 0)
{
s += "\n no pairs, i.e. cache is empty";
}
else
{
int k = 1;
s += "\n (key --> value) pairs in ascending order of keys:";
typename std::list<KeyClass>::const_iterator itKey;
typename std::list<ValueClass>::const_iterator itValue = _value.begin();
for (itKey = _key.begin(); itKey != _key.end(); itKey++)
{
s += "\n ";
snprintf(h,12, "%d", k); s += h;
s += ". ";
s += itKey->toString();
s += " --> ";
s += itValue->toString();
itValue++;
k++;
}
s += "\n (key --> value) pairs in descending order of ranks:";
std::list<int>::const_iterator itRank;
int r = 1;
for (itRank = _rank.begin(); itRank != _rank.end(); itRank++)
{
int index = *itRank;
itValue = _value.begin();
k = 0;
for (itKey = _key.begin(); itKey != _key.end(); itKey++)
{
if (k == index) break;
k++;
itValue++;
}
s += "\n ";
snprintf(h,12, "%d", r); s += h;
s += ". ";
s += itKey->toString();
s += " --> ";
s += itValue->toString();
r++;
}
}
return s;
}
Cache::getNumberOfEntries
int getNumberOfEntries() const
A method for retrieving the momentary number of (key --> value) pairs in the cache.
Definition CacheImplementation.h:30
Cache::getMaxWeight
int getMaxWeight() const
A method for retrieving the maximum weight of the cache.
Definition CacheImplementation.h:110
Cache::getMaxNumberOfEntries
int getMaxNumberOfEntries() const
A method for retrieving the maximum number of (key --> value) pairs in the cache.
Definition CacheImplementation.h:104

## Field Documentation

## _itKey

template<class KeyClass, class ValueClass>

std::list<KeyClass>::const_iterator Cache< KeyClass, ValueClass >::_itKey

mutable private

a pointer to some element of _key; We make this mutable so that methods which leave the cache unmodified but which alter _itKey can still be declared as const, as the user would expect for these methods.

Definition at line 106 of file Cache.h.

## _itValue

template<class KeyClass, class ValueClass>

std::list<ValueClass>::const_iterator Cache< KeyClass, ValueClass >::_itValue

mutable private

a pointer to some element of _value; We make this mutable so that methods which leave the cache unmodified but which alter _itValue can still be declared as const, as the user would expect for these methods.

Definition at line 114 of file Cache.h.

## _key

template<class KeyClass, class ValueClass>

std::list<KeyClass> Cache< KeyClass, ValueClass >::_key

private

_key is sorted in ascending order, i.e., j < i ==> _key(j) < _key(i), where the right-hand side comparator "<" needs to be implemented in KeyClass.

Definition at line 85 of file Cache.h.

## _maxEntries

template<class KeyClass, class ValueClass>

int Cache< KeyClass, ValueClass >::_maxEntries

private

the bound for the number of cached `key --> value` pairs;
The cache will automatically ensure that this bound will never be exceeded; see Cache::shrink (const KeyClass&) and Cache::deleteLast ().

Definition at line 130 of file Cache.h.

## _maxWeight

template<class KeyClass, class ValueClass>

int Cache< KeyClass, ValueClass >::_maxWeight

private

the bound on total cache weight;
The cache will automatically ensure that this bound will never be exceeded; see see Cache::shrink (const KeyClass&) and Cache::deleteLast ().

Definition at line 138 of file Cache.h.

## _rank

template<class KeyClass, class ValueClass>

std::list<int> Cache< KeyClass, ValueClass >::_rank

private

A bijection on the set {0, ..., _key.size() - 1}.

Semantically, `_rank(j) > _rank(i)` means that the pair `key(_rank(j)) --> value(_rank(j))` will be cached at least as long as the pair `key(_rank(i)) --> value(_rank(i))`.

Definition at line 77 of file Cache.h.

## _value

template<class KeyClass, class ValueClass>

std::list<ValueClass> Cache< KeyClass, ValueClass >::_value

private

_value captures the actual objects of interest;
`_value`[i] corresponds to `_key`[i] and may be retrieved by calling Cache::getValue (const KeyClass&) const with the argument `_key`[i]).

Definition at line 93 of file Cache.h.

## _weight

template<class KeyClass, class ValueClass>

int Cache< KeyClass, ValueClass >::_weight

private

for storing the momentary weight of the given cache;
This is the sum of `_value`[i].getWeight() over all i, i.e., over all cached values.

Equivalently, this is equal to all weights stored in _weights.

Definition at line 122 of file Cache.h.

## _weights

template<class KeyClass, class ValueClass>

std::list<int> Cache< KeyClass, ValueClass >::_weights

private

container for the weights of all cached values

Definition at line 98 of file Cache.h.

The documentation for this class was generated from the following files:

- kernel/linear_algebra/Cache.h
- kernel/linear_algebra/CacheImplementation.h