mirror of
https://github.com/MaskRay/ccls.git
synced 2024-11-22 15:45:08 +00:00
Simplify LruCache a little bit. (#199)
This commit is contained in:
parent
d7d8b820ac
commit
32bcac38af
@ -40,6 +40,7 @@ struct LruCache {
|
|||||||
uint32_t score = 0;
|
uint32_t score = 0;
|
||||||
TKey key;
|
TKey key;
|
||||||
std::shared_ptr<TValue> value;
|
std::shared_ptr<TValue> value;
|
||||||
|
bool operator<(const Entry &other) const { return score < other.score; }
|
||||||
};
|
};
|
||||||
|
|
||||||
void IncrementScore();
|
void IncrementScore();
|
||||||
@ -69,8 +70,8 @@ std::shared_ptr<TValue> LruCache<TKey, TValue>::TryGet(const TKey& key) {
|
|||||||
// Assign new score.
|
// Assign new score.
|
||||||
for (Entry& entry : entries_) {
|
for (Entry& entry : entries_) {
|
||||||
if (entry.key == key) {
|
if (entry.key == key) {
|
||||||
IncrementScore();
|
|
||||||
entry.score = next_score_;
|
entry.score = next_score_;
|
||||||
|
IncrementScore();
|
||||||
return entry.value;
|
return entry.value;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -94,25 +95,12 @@ std::shared_ptr<TValue> LruCache<TKey, TValue>::TryTake(const TKey& key) {
|
|||||||
template <typename TKey, typename TValue>
|
template <typename TKey, typename TValue>
|
||||||
void LruCache<TKey, TValue>::Insert(const TKey& key,
|
void LruCache<TKey, TValue>::Insert(const TKey& key,
|
||||||
const std::shared_ptr<TValue>& value) {
|
const std::shared_ptr<TValue>& value) {
|
||||||
if (entries_.size() >= max_entries_) {
|
if (entries_.size() >= max_entries_)
|
||||||
// Find entry with the lowest score.
|
entries_.erase(std::min_element(entries_.begin(), entries_.end()));
|
||||||
size_t lowest_idx = 0;
|
|
||||||
uint32_t lowest_score = std::numeric_limits<uint32_t>::max();
|
|
||||||
for (size_t i = 0; i < entries_.size(); ++i) {
|
|
||||||
if (entries_[i].score < lowest_score) {
|
|
||||||
lowest_idx = i;
|
|
||||||
lowest_score = entries_[i].score;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Remove it.
|
|
||||||
entries_.erase(entries_.begin() + lowest_idx);
|
|
||||||
}
|
|
||||||
|
|
||||||
IncrementScore();
|
|
||||||
|
|
||||||
Entry entry;
|
Entry entry;
|
||||||
entry.score = next_score_;
|
entry.score = next_score_;
|
||||||
|
IncrementScore();
|
||||||
entry.key = key;
|
entry.key = key;
|
||||||
entry.value = value;
|
entry.value = value;
|
||||||
entries_.push_back(entry);
|
entries_.push_back(entry);
|
||||||
@ -129,12 +117,9 @@ void LruCache<TKey, TValue>::IterateValues(TFunc func) {
|
|||||||
|
|
||||||
template <typename TKey, typename TValue>
|
template <typename TKey, typename TValue>
|
||||||
void LruCache<TKey, TValue>::IncrementScore() {
|
void LruCache<TKey, TValue>::IncrementScore() {
|
||||||
next_score_ += 1;
|
|
||||||
|
|
||||||
// Overflow.
|
// Overflow.
|
||||||
if (next_score_ == 0) {
|
if (++next_score_ == 0) {
|
||||||
std::sort(entries_.begin(), entries_.end(),
|
std::sort(entries_.begin(), entries_.end());
|
||||||
[](const Entry& a, const Entry& b) { return a.score < b.score; });
|
|
||||||
for (Entry& entry : entries_)
|
for (Entry& entry : entries_)
|
||||||
entry.score = next_score_++;
|
entry.score = next_score_++;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user