Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #ifndef COMPONENTS_COPRESENCE_TIMED_MAP_ | |
| 6 #define COMPONENTS_COPRESENCE_TIMED_MAP_ | |
| 7 | |
| 8 #include <map> | |
| 9 #include <queue> | |
| 10 #include <vector> | |
| 11 | |
| 12 #include "base/macros.h" | |
| 13 #include "base/time/default_tick_clock.h" | |
| 14 #include "base/time/tick_clock.h" | |
| 15 #include "base/time/time.h" | |
| 16 #include "base/timer/timer.h" | |
| 17 | |
| 18 namespace copresence { | |
| 19 | |
| 20 // TimedMap is a map with the added functionality of clearing any | |
| 21 // key/value pair after its specified lifetime is over. | |
| 22 template <typename KeyType, typename ValueType> | |
| 23 class TimedMap { | |
| 24 public: | |
| 25 TimedMap(const base::TimeDelta& lifetime, size_t max_elements) | |
| 26 : kEmptyValue(ValueType()), | |
| 27 clock_(new base::DefaultTickClock()), | |
| 28 lifetime_(lifetime), | |
| 29 max_elements_(max_elements) { | |
| 30 timer_.Start(FROM_HERE, lifetime_, this, &TimedMap::ClearExpiredTokens); | |
| 31 } | |
| 32 | |
| 33 ~TimedMap() {} | |
|
jochen (gone - plz use gerrit)
2014/07/31 20:38:48
kinda surprised clang doesn't complain about non-t
rkc
2014/07/31 20:42:43
I followed this pattern from base/containers/mru_c
| |
| 34 | |
| 35 void Add(const KeyType& key, const ValueType& value) { | |
| 36 map_[key] = value; | |
| 37 expiry_queue_.push(KeyTimeTuple(key, clock_->NowTicks() + lifetime_)); | |
| 38 while (map_.size() > max_elements_) | |
| 39 ClearOldestToken(); | |
| 40 } | |
| 41 | |
| 42 bool HasKey(const KeyType& key) { | |
| 43 ClearExpiredTokens(); | |
| 44 return map_.find(key) != map_.end(); | |
| 45 } | |
| 46 | |
| 47 const ValueType& GetValue(const KeyType& key) { | |
| 48 ClearExpiredTokens(); | |
| 49 typename std::map<KeyType, ValueType>::const_iterator elt = map_.find(key); | |
| 50 return elt == map_.end() ? kEmptyValue : elt->second; | |
| 51 } | |
| 52 | |
| 53 private: | |
| 54 void ClearExpiredTokens() { | |
| 55 while (!expiry_queue_.empty() && | |
| 56 expiry_queue_.top().second <= clock_->NowTicks()) | |
| 57 ClearOldestToken(); | |
| 58 } | |
| 59 | |
| 60 void ClearOldestToken() { | |
| 61 map_.erase(expiry_queue_.top().first); | |
| 62 expiry_queue_.pop(); | |
| 63 } | |
| 64 | |
| 65 typedef std::pair<KeyType, base::TimeTicks> KeyTimeTuple; | |
| 66 | |
| 67 class EarliestFirstComparator { | |
| 68 public: | |
| 69 // This will sort our queue with the 'earliest' time being the top. | |
| 70 bool operator()(const KeyTimeTuple& left, const KeyTimeTuple& right) const { | |
| 71 return left.second > right.second; | |
| 72 } | |
| 73 }; | |
| 74 | |
| 75 typedef std::priority_queue<KeyTimeTuple, std::vector<KeyTimeTuple>, | |
| 76 EarliestFirstComparator> ExpiryQueue; | |
| 77 | |
| 78 const ValueType kEmptyValue; | |
| 79 | |
| 80 base::TickClock* clock_; | |
| 81 base::RepeatingTimer<TimedMap> timer_; | |
| 82 const base::TimeDelta lifetime_; | |
| 83 const size_t max_elements_; | |
| 84 std::map<KeyType, ValueType> map_; | |
| 85 // Priority queue with our element keys ordered by the earliest expiring keys | |
| 86 // first. | |
| 87 ExpiryQueue expiry_queue_; | |
| 88 | |
| 89 DISALLOW_COPY_AND_ASSIGN(TimedMap); | |
| 90 }; | |
| 91 | |
| 92 } // namespace copresence | |
| 93 | |
| 94 #endif // COMPONENTS_COPRESENCE_TIMED_MAP_ | |
| OLD | NEW |