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_H_ | 5 #ifndef COMPONENTS_COPRESENCE_TIMED_MAP_H_ |
6 #define COMPONENTS_COPRESENCE_TIMED_MAP_H_ | 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> |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
45 ClearExpiredTokens(); | 45 ClearExpiredTokens(); |
46 return map_.find(key) != map_.end(); | 46 return map_.find(key) != map_.end(); |
47 } | 47 } |
48 | 48 |
49 const ValueType& GetValue(const KeyType& key) { | 49 const ValueType& GetValue(const KeyType& key) { |
50 ClearExpiredTokens(); | 50 ClearExpiredTokens(); |
51 auto elt = map_.find(key); | 51 auto elt = map_.find(key); |
52 return elt == map_.end() ? kEmptyValue : elt->second; | 52 return elt == map_.end() ? kEmptyValue : elt->second; |
53 } | 53 } |
54 | 54 |
55 ValueType* GetMutableValue(const KeyType& key) { | |
56 ClearExpiredTokens(); | |
57 return &map_[key]; | |
rkc
2015/01/07 23:06:10
If the element doesn't exist, this will actually a
Charlie
2015/01/08 16:47:34
That's more consistent with the rest of the API -
| |
58 } | |
59 | |
60 size_t Erase(const KeyType& key) { | |
rkc
2015/01/07 23:06:10
Add a TODO to add a unit test to test this.
Charlie
2015/01/08 16:47:34
Done.
| |
61 return map_.erase(key); | |
62 } | |
63 | |
55 void set_clock_for_testing(scoped_ptr<base::TickClock> clock) { | 64 void set_clock_for_testing(scoped_ptr<base::TickClock> clock) { |
56 clock_ = clock.Pass(); | 65 clock_ = clock.Pass(); |
57 } | 66 } |
58 | 67 |
59 private: | 68 private: |
60 void ClearExpiredTokens() { | 69 void ClearExpiredTokens() { |
61 while (!expiry_queue_.empty() && | 70 while (!expiry_queue_.empty() && |
62 expiry_queue_.top().second <= clock_->NowTicks()) | 71 expiry_queue_.top().second <= clock_->NowTicks()) |
63 ClearOldestToken(); | 72 ClearOldestToken(); |
64 } | 73 } |
(...skipping 26 matching lines...) Expand all Loading... | |
91 // Priority queue with our element keys ordered by the earliest expiring keys | 100 // Priority queue with our element keys ordered by the earliest expiring keys |
92 // first. | 101 // first. |
93 ExpiryQueue expiry_queue_; | 102 ExpiryQueue expiry_queue_; |
94 | 103 |
95 DISALLOW_COPY_AND_ASSIGN(TimedMap); | 104 DISALLOW_COPY_AND_ASSIGN(TimedMap); |
96 }; | 105 }; |
97 | 106 |
98 } // namespace copresence | 107 } // namespace copresence |
99 | 108 |
100 #endif // COMPONENTS_COPRESENCE_TIMED_MAP_H_ | 109 #endif // COMPONENTS_COPRESENCE_TIMED_MAP_H_ |
OLD | NEW |