/usr/include/singular/singular/kernel/linear_algebra/CacheImplementation.h is in libsingular4-dev-common 1:4.1.0-p3+ds-2build1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 | #ifndef CACHE_IMPLEMENTATION_H
#define CACHE_IMPLEMENTATION_H
#include <reporter/reporter.h>
#include <cstdio> // for sprintf
#include <iostream>
template<class KeyClass, class ValueClass>
Cache<KeyClass, ValueClass>::Cache (const int maxEntries, const int maxWeight)
{
_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;
}
template<class KeyClass, class ValueClass>
int Cache<KeyClass, ValueClass>::getWeight() const
{
return _weight;
}
template<class KeyClass, class ValueClass>
int Cache<KeyClass, ValueClass>::getNumberOfEntries() const
{
return _rank.size();
}
template<class KeyClass, class ValueClass>
void Cache<KeyClass, ValueClass>::clear()
{
_rank.clear();
_key.clear();
_value.clear();
_weights.clear();
}
template<class KeyClass, class ValueClass>
Cache<KeyClass, ValueClass>::~Cache()
{
_rank.clear();
_key.clear();
_value.clear();
_weights.clear();
}
template<class KeyClass, class ValueClass>
bool Cache<KeyClass, ValueClass>::hasKey (const KeyClass& key) const
{
_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;
}
template<class KeyClass, class ValueClass>
ValueClass Cache<KeyClass, ValueClass>::getValue (const KeyClass& /*key*/) const
{
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;
}
template<class KeyClass, class ValueClass>
bool Cache<KeyClass, ValueClass>::shrink(const KeyClass& key)
{
/* 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;
}
template<class KeyClass, class ValueClass>
int Cache<KeyClass, ValueClass>::getMaxNumberOfEntries() const
{
return _maxEntries;
}
template<class KeyClass, class ValueClass>
int Cache<KeyClass, ValueClass>::getMaxWeight() const
{
return _maxWeight;
}
template<class KeyClass, class ValueClass>
bool Cache<KeyClass, ValueClass>::deleteLast(const KeyClass& key)
{
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;
}
template<class KeyClass, class ValueClass>
bool Cache<KeyClass, ValueClass>::put (const KeyClass& key,
const ValueClass& value)
{
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 */
}
template<class KeyClass, class ValueClass>
std::string Cache<KeyClass, ValueClass>::toString() const
{
char h[10];
std::string s = "Cache:";
s += "\n entries: ";
sprintf(h, "%d", getNumberOfEntries()); s += h;
s += " of at most ";
sprintf(h, "%d", getMaxNumberOfEntries()); s += h;
s += "\n weight: ";
sprintf(h, "%d", getWeight()); s += h;
s += " of at most ";
sprintf(h, "%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 ";
sprintf(h, "%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 ";
sprintf(h, "%d", r); s += h;
s += ". ";
s += itKey->toString();
s += " --> ";
s += itValue->toString();
r++;
}
}
return s;
}
template<class KeyClass, class ValueClass>
void Cache<KeyClass, ValueClass>::print() const
{
PrintS(this->toString().c_str());
}
template<class KeyClass, class ValueClass>
Cache<KeyClass, ValueClass>::Cache() { }
template<class KeyClass, class ValueClass>
Cache<KeyClass, ValueClass>::Cache(const Cache& c)
{
_rank = c._rank;
_value = c._value;
_weights = c._weights;
_key = c._key;
_weight = c._weight;
_maxEntries = c._maxEntries;
_maxWeight = c._maxWeight;
}
#endif
/* CACHE_IMPLEMENTATION_H */
|