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_H_ | |
6 #define COMPONENTS_COPRESENCE_TIMED_MAP_H_ | |
7 | |
8 #include <stddef.h> | |
9 | |
10 #include <map> | |
11 #include <memory> | |
12 #include <queue> | |
13 #include <utility> | |
14 #include <vector> | |
15 | |
16 #include "base/macros.h" | |
17 #include "base/time/default_tick_clock.h" | |
18 #include "base/time/tick_clock.h" | |
19 #include "base/time/time.h" | |
20 #include "base/timer/timer.h" | |
21 | |
22 namespace copresence { | |
23 | |
24 // TimedMap is a map with the added functionality of clearing any | |
25 // key/value pair after its specified lifetime is over. | |
26 // TODO(ckehoe): Why is this interface so different from std::map? | |
27 template <typename KeyType, typename ValueType> | |
28 class TimedMap { | |
29 public: | |
30 TimedMap(const base::TimeDelta& lifetime, size_t max_elements) | |
31 : kEmptyValue(ValueType()), | |
32 clock_(new base::DefaultTickClock()), | |
33 lifetime_(lifetime), | |
34 max_elements_(max_elements) { | |
35 timer_.Start(FROM_HERE, lifetime_, this, &TimedMap::ClearExpiredTokens); | |
36 } | |
37 | |
38 ~TimedMap() {} | |
39 | |
40 void Add(const KeyType& key, const ValueType& value) { | |
41 map_[key] = value; | |
42 expiry_queue_.push(KeyTimeTuple(key, clock_->NowTicks() + lifetime_)); | |
43 while (map_.size() > max_elements_) | |
44 ClearOldestToken(); | |
45 } | |
46 | |
47 bool HasKey(const KeyType& key) { | |
48 ClearExpiredTokens(); | |
49 return map_.find(key) != map_.end(); | |
50 } | |
51 | |
52 const ValueType& GetValue(const KeyType& key) { | |
53 ClearExpiredTokens(); | |
54 auto elt = map_.find(key); | |
55 return elt == map_.end() ? kEmptyValue : elt->second; | |
56 } | |
57 | |
58 ValueType* GetMutableValue(const KeyType& key) { | |
59 ClearExpiredTokens(); | |
60 auto elt = map_.find(key); | |
61 return elt == map_.end() ? nullptr : &(elt->second); | |
62 } | |
63 | |
64 // TODO(ckehoe): Add a unit test for this. | |
65 size_t Erase(const KeyType& key) { | |
66 return map_.erase(key); | |
67 } | |
68 | |
69 void set_clock_for_testing(std::unique_ptr<base::TickClock> clock) { | |
70 clock_ = std::move(clock); | |
71 } | |
72 | |
73 private: | |
74 void ClearExpiredTokens() { | |
75 while (!expiry_queue_.empty() && | |
76 expiry_queue_.top().second <= clock_->NowTicks()) | |
77 ClearOldestToken(); | |
78 } | |
79 | |
80 void ClearOldestToken() { | |
81 map_.erase(expiry_queue_.top().first); | |
82 expiry_queue_.pop(); | |
83 } | |
84 | |
85 using KeyTimeTuple = std::pair<KeyType, base::TimeTicks>; | |
86 | |
87 class EarliestFirstComparator { | |
88 public: | |
89 // This will sort our queue with the 'earliest' time being the top. | |
90 bool operator()(const KeyTimeTuple& left, const KeyTimeTuple& right) const { | |
91 return left.second > right.second; | |
92 } | |
93 }; | |
94 | |
95 using ExpiryQueue = std::priority_queue< | |
96 KeyTimeTuple, std::vector<KeyTimeTuple>, EarliestFirstComparator>; | |
97 | |
98 const ValueType kEmptyValue; | |
99 | |
100 std::unique_ptr<base::TickClock> clock_; | |
101 base::RepeatingTimer timer_; | |
102 const base::TimeDelta lifetime_; | |
103 const size_t max_elements_; | |
104 std::map<KeyType, ValueType> map_; | |
105 // Priority queue with our element keys ordered by the earliest expiring keys | |
106 // first. | |
107 ExpiryQueue expiry_queue_; | |
108 | |
109 DISALLOW_COPY_AND_ASSIGN(TimedMap); | |
110 }; | |
111 | |
112 } // namespace copresence | |
113 | |
114 #endif // COMPONENTS_COPRESENCE_TIMED_MAP_H_ | |
OLD | NEW |