| 168 | |
| 169 | template<class KeyClass, class ValueClass> |
| 170 | bool Cache<KeyClass, ValueClass>::put (const KeyClass& key, |
| 171 | const ValueClass& value) |
| 172 | { |
| 173 | bool keyWasContained = false; |
| 174 | int oldIndexInKey = -1; |
| 175 | int newIndexInKey = _key.size(); /* default to enter new (key, value)-pair |
| 176 | is at the end of the two lists; |
| 177 | only used in the case |
| 178 | keyWasContained == false */ |
| 179 | int k = 0; |
| 180 | typename std::list<KeyClass>::iterator itKey; |
| 181 | // itOldValue will later only be used in the case keyWasContained == true: */ |
| 182 | typename std::list<ValueClass>::iterator itOldValue = _value.begin(); |
| 183 | /* itOldWeights will later only be used in the case |
| 184 | keyWasContained == true */ |
| 185 | typename std::list<int>::iterator itOldWeights = _weights.begin(); |
| 186 | for (itKey = _key.begin(); itKey != _key.end(); itKey++) |
| 187 | { |
| 188 | int c = key.compare(*itKey); |
| 189 | if (c == -1) |
| 190 | { |
| 191 | newIndexInKey = k; |
| 192 | break; |
| 193 | } |
| 194 | if (c == 0) |
| 195 | { |
| 196 | keyWasContained = true; |
| 197 | oldIndexInKey = k; |
| 198 | break; |
| 199 | } |
| 200 | itOldValue++; |
| 201 | itOldWeights++; |
| 202 | k++; |
| 203 | } |
| 204 | int utility = value.getUtility(); |
| 205 | int newWeight = value.getWeight(); |
| 206 | k = 0; |
| 207 | typename std::list<ValueClass>::iterator itValue = _value.begin(); |
| 208 | for (itValue = _value.begin(); itValue != _value.end(); itValue++) |
| 209 | { |
| 210 | if (itValue->getUtility() > utility) k++; |
| 211 | } |
| 212 | int newIndexInRank = k; |
| 213 | |
| 214 | if (keyWasContained) |
| 215 | { |
| 216 | /* There was already a pair of the form (key --> *). */ |
| 217 | |
| 218 | /*adjusting the weight of the cache */ |
| 219 | ValueClass oldValue = *itOldValue; |
| 220 | _weight += newWeight - *itOldWeights; |
| 221 | |
| 222 | /* overwriting old value by argument value */ |
| 223 | itOldValue = _value.erase(itOldValue); |
| 224 | itOldWeights = _weights.erase(itOldWeights); |
| 225 | ValueClass myValueCopy = value; |
| 226 | _value.insert(itOldValue, myValueCopy); |
| 227 | _weights.insert(itOldWeights, newWeight); |
no test coverage detected