| 114 | |
| 115 | template<class KeyClass, class ValueClass> |
| 116 | bool Cache<KeyClass, ValueClass>::deleteLast(const KeyClass& key) |
| 117 | { |
| 118 | if (_rank.size() == 0) |
| 119 | { |
| 120 | return false; /* nothing to do */ |
| 121 | }; |
| 122 | /* We need to perform the following (empty) loop in order to |
| 123 | obtain a forward-iterator pointing to the last entry of _rank. |
| 124 | Note: We cannot use rbegin() because we need the iterator for |
| 125 | erasing the last entry which is only implemented for forward |
| 126 | iterators by std::list. */ |
| 127 | std::list<int>::iterator itRank; |
| 128 | for (itRank = _rank.begin(); itRank != _rank.end(); itRank++) { } |
| 129 | itRank--; /* Now, this forward iterator points to the last list entry. */ |
| 130 | int deleteIndex = *itRank; /* index of (_key, _value)-pair with worst, |
| 131 | i.e., highest _rank */ |
| 132 | bool result = false; |
| 133 | |
| 134 | /* now delete entries in _key and _value with index deleteIndex */ |
| 135 | int k = 0; |
| 136 | typename std::list<KeyClass>::iterator itKey; |
| 137 | typename std::list<ValueClass>::iterator itValue = _value.begin(); |
| 138 | typename std::list<int>::iterator itWeights = _weights.begin(); |
| 139 | for (itKey = _key.begin(); itKey != _key.end(); itKey++) |
| 140 | { |
| 141 | if (k == deleteIndex) |
| 142 | { |
| 143 | result = (key.compare(*itKey) == 0); |
| 144 | break; |
| 145 | } |
| 146 | itValue++; |
| 147 | itWeights++; |
| 148 | k++; |
| 149 | } |
| 150 | _key.erase(itKey); |
| 151 | int deleteWeight = *itWeights; |
| 152 | _value.erase(itValue); |
| 153 | _weights.erase(itWeights); |
| 154 | |
| 155 | /* adjust total weight of this cache */ |
| 156 | _weight -= deleteWeight; |
| 157 | |
| 158 | /* now delete last entry of _rank and decrement all those indices |
| 159 | // in _rank by 1 which are larger than deleteIndex */ |
| 160 | _rank.erase(itRank); |
| 161 | for (itRank = _rank.begin(); itRank != _rank.end(); itRank++) |
| 162 | { |
| 163 | if (*itRank > deleteIndex) *itRank -= 1; |
| 164 | } |
| 165 | |
| 166 | return result; |
| 167 | } |
| 168 | |
| 169 | template<class KeyClass, class ValueClass> |
| 170 | bool Cache<KeyClass, ValueClass>::put (const KeyClass& key, |