Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(338)

Side by Side Diff: components/copresence/timed_map.h

Issue 419073002: Add the copresence DirectiveHandler. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 6 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(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 <utility>
11 #include <vector>
12
13 #include "base/macros.h"
14 #include "base/time/default_tick_clock.h"
15 #include "base/time/tick_clock.h"
16 #include "base/time/time.h"
17 #include "base/timer/timer.h"
18
19 namespace copresence {
20
21 // TimedMap is a map with the added functionality of clearing any
22 // key/value pair after its specified lifetime is over.
23 template <typename KeyType, typename ValueType>
24 class TimedMap {
25 public:
26 TimedMap(const base::TimeDelta& lifetime, size_t max_elements)
27 : kEmptyValue(ValueType()),
28 clock_(new base::DefaultTickClock()),
29 lifetime_(lifetime),
30 max_elements_(max_elements) {
31 timer_.Start(FROM_HERE, lifetime_, this, &TimedMap::ClearExpiredTokens);
32 }
33
34 ~TimedMap() {}
35
36 void Add(const KeyType& key, const ValueType& value) {
37 map_[key] = value;
38 expiry_queue_.push(KeyTimeTuple(key, clock_->NowTicks() + lifetime_));
39 while (map_.size() > max_elements_)
40 ClearOldestToken();
41 }
42
43 bool HasKey(const KeyType& key) {
44 ClearExpiredTokens();
45 return map_.find(key) != map_.end();
46 }
47
48 const ValueType& GetValue(const KeyType& key) {
49 ClearExpiredTokens();
50 typename std::map<KeyType, ValueType>::const_iterator elt = map_.find(key);
51 return elt == map_.end() ? kEmptyValue : elt->second;
52 }
53
54 void set_clock_for_testing(base::TickClock* clock) { clock_ = clock; }
55
56 private:
57 void ClearExpiredTokens() {
58 while (!expiry_queue_.empty() &&
59 expiry_queue_.top().second <= clock_->NowTicks())
60 ClearOldestToken();
61 }
62
63 void ClearOldestToken() {
64 map_.erase(expiry_queue_.top().first);
65 expiry_queue_.pop();
66 }
67
68 typedef std::pair<KeyType, base::TimeTicks> KeyTimeTuple;
69
70 class EarliestFirstComparator {
71 public:
72 // This will sort our queue with the 'earliest' time being the top.
73 bool operator()(const KeyTimeTuple& left, const KeyTimeTuple& right) const {
74 return left.second > right.second;
75 }
76 };
77
78 typedef std::priority_queue<KeyTimeTuple, std::vector<KeyTimeTuple>,
79 EarliestFirstComparator> ExpiryQueue;
80
81 const ValueType kEmptyValue;
82
83 base::TickClock* clock_;
84 base::RepeatingTimer<TimedMap> timer_;
85 const base::TimeDelta lifetime_;
86 const size_t max_elements_;
87 std::map<KeyType, ValueType> map_;
88 // Priority queue with our element keys ordered by the earliest expiring keys
89 // first.
90 ExpiryQueue expiry_queue_;
91
92 DISALLOW_COPY_AND_ASSIGN(TimedMap);
93 };
94
95 } // namespace copresence
96
97 #endif // COMPONENTS_COPRESENCE_TIMED_MAP_
OLDNEW
« no previous file with comments | « components/copresence/test/audio_test_support.cc ('k') | components/copresence/timed_map_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698