OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef COMPONENTS_COPRESENCE_TIMED_MAP_ | 5 #ifndef COMPONENTS_COPRESENCE_TIMED_MAP_H_ |
6 #define COMPONENTS_COPRESENCE_TIMED_MAP_ | 6 #define COMPONENTS_COPRESENCE_TIMED_MAP_H_ |
7 | 7 |
8 #include <map> | 8 #include <map> |
9 #include <queue> | 9 #include <queue> |
10 #include <utility> | 10 #include <utility> |
11 #include <vector> | 11 #include <vector> |
12 | 12 |
13 #include "base/macros.h" | 13 #include "base/macros.h" |
| 14 #include "base/memory/scoped_ptr.h" |
14 #include "base/time/default_tick_clock.h" | 15 #include "base/time/default_tick_clock.h" |
15 #include "base/time/tick_clock.h" | 16 #include "base/time/tick_clock.h" |
16 #include "base/time/time.h" | 17 #include "base/time/time.h" |
17 #include "base/timer/timer.h" | 18 #include "base/timer/timer.h" |
18 | 19 |
19 namespace copresence { | 20 namespace copresence { |
20 | 21 |
21 // TimedMap is a map with the added functionality of clearing any | 22 // TimedMap is a map with the added functionality of clearing any |
22 // key/value pair after its specified lifetime is over. | 23 // key/value pair after its specified lifetime is over. |
23 template <typename KeyType, typename ValueType> | 24 template <typename KeyType, typename ValueType> |
(...skipping 20 matching lines...) Expand all Loading... |
44 ClearExpiredTokens(); | 45 ClearExpiredTokens(); |
45 return map_.find(key) != map_.end(); | 46 return map_.find(key) != map_.end(); |
46 } | 47 } |
47 | 48 |
48 const ValueType& GetValue(const KeyType& key) { | 49 const ValueType& GetValue(const KeyType& key) { |
49 ClearExpiredTokens(); | 50 ClearExpiredTokens(); |
50 typename std::map<KeyType, ValueType>::const_iterator elt = map_.find(key); | 51 typename std::map<KeyType, ValueType>::const_iterator elt = map_.find(key); |
51 return elt == map_.end() ? kEmptyValue : elt->second; | 52 return elt == map_.end() ? kEmptyValue : elt->second; |
52 } | 53 } |
53 | 54 |
54 void set_clock_for_testing(base::TickClock* clock) { clock_ = clock; } | 55 void set_clock_for_testing(scoped_ptr<base::TickClock> clock) { |
| 56 clock_ = clock.Pass(); |
| 57 } |
55 | 58 |
56 private: | 59 private: |
57 void ClearExpiredTokens() { | 60 void ClearExpiredTokens() { |
58 while (!expiry_queue_.empty() && | 61 while (!expiry_queue_.empty() && |
59 expiry_queue_.top().second <= clock_->NowTicks()) | 62 expiry_queue_.top().second <= clock_->NowTicks()) |
60 ClearOldestToken(); | 63 ClearOldestToken(); |
61 } | 64 } |
62 | 65 |
63 void ClearOldestToken() { | 66 void ClearOldestToken() { |
64 map_.erase(expiry_queue_.top().first); | 67 map_.erase(expiry_queue_.top().first); |
65 expiry_queue_.pop(); | 68 expiry_queue_.pop(); |
66 } | 69 } |
67 | 70 |
68 typedef std::pair<KeyType, base::TimeTicks> KeyTimeTuple; | 71 typedef std::pair<KeyType, base::TimeTicks> KeyTimeTuple; |
69 | 72 |
70 class EarliestFirstComparator { | 73 class EarliestFirstComparator { |
71 public: | 74 public: |
72 // This will sort our queue with the 'earliest' time being the top. | 75 // This will sort our queue with the 'earliest' time being the top. |
73 bool operator()(const KeyTimeTuple& left, const KeyTimeTuple& right) const { | 76 bool operator()(const KeyTimeTuple& left, const KeyTimeTuple& right) const { |
74 return left.second > right.second; | 77 return left.second > right.second; |
75 } | 78 } |
76 }; | 79 }; |
77 | 80 |
78 typedef std::priority_queue<KeyTimeTuple, std::vector<KeyTimeTuple>, | 81 typedef std::priority_queue<KeyTimeTuple, std::vector<KeyTimeTuple>, |
79 EarliestFirstComparator> ExpiryQueue; | 82 EarliestFirstComparator> ExpiryQueue; |
80 | 83 |
81 const ValueType kEmptyValue; | 84 const ValueType kEmptyValue; |
82 | 85 |
83 base::TickClock* clock_; | 86 scoped_ptr<base::TickClock> clock_; |
84 base::RepeatingTimer<TimedMap> timer_; | 87 base::RepeatingTimer<TimedMap> timer_; |
85 const base::TimeDelta lifetime_; | 88 const base::TimeDelta lifetime_; |
86 const size_t max_elements_; | 89 const size_t max_elements_; |
87 std::map<KeyType, ValueType> map_; | 90 std::map<KeyType, ValueType> map_; |
88 // Priority queue with our element keys ordered by the earliest expiring keys | 91 // Priority queue with our element keys ordered by the earliest expiring keys |
89 // first. | 92 // first. |
90 ExpiryQueue expiry_queue_; | 93 ExpiryQueue expiry_queue_; |
91 | 94 |
92 DISALLOW_COPY_AND_ASSIGN(TimedMap); | 95 DISALLOW_COPY_AND_ASSIGN(TimedMap); |
93 }; | 96 }; |
94 | 97 |
95 } // namespace copresence | 98 } // namespace copresence |
96 | 99 |
97 #endif // COMPONENTS_COPRESENCE_TIMED_MAP_ | 100 #endif // COMPONENTS_COPRESENCE_TIMED_MAP_H_ |
OLD | NEW |