OLD | NEW |
---|---|
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 CHROME_COMMON_INSTANT_RESTRICTED_ID_CACHE_H_ | 5 #ifndef CHROME_COMMON_INSTANT_RESTRICTED_ID_CACHE_H_ |
6 #define CHROME_COMMON_INSTANT_RESTRICTED_ID_CACHE_H_ | 6 #define CHROME_COMMON_INSTANT_RESTRICTED_ID_CACHE_H_ |
7 | 7 |
8 #include <set> | 8 #include <set> |
9 #include <utility> | 9 #include <utility> |
10 #include <vector> | 10 #include <vector> |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
61 // Returns true if the |restricted_id| is present in the cache and if so, | 61 // Returns true if the |restricted_id| is present in the cache and if so, |
62 // returns a copy of the item. | 62 // returns a copy of the item. |
63 bool GetItemWithRestrictedID(InstantRestrictedID restricted_id, | 63 bool GetItemWithRestrictedID(InstantRestrictedID restricted_id, |
64 T* item) const; | 64 T* item) const; |
65 | 65 |
66 private: | 66 private: |
67 FRIEND_TEST_ALL_PREFIXES(InstantRestrictedIDCacheTest, AutoIDGeneration); | 67 FRIEND_TEST_ALL_PREFIXES(InstantRestrictedIDCacheTest, AutoIDGeneration); |
68 FRIEND_TEST_ALL_PREFIXES(InstantRestrictedIDCacheTest, CrazyIDGeneration); | 68 FRIEND_TEST_ALL_PREFIXES(InstantRestrictedIDCacheTest, CrazyIDGeneration); |
69 FRIEND_TEST_ALL_PREFIXES(InstantRestrictedIDCacheTest, ManualIDGeneration); | 69 FRIEND_TEST_ALL_PREFIXES(InstantRestrictedIDCacheTest, ManualIDGeneration); |
70 FRIEND_TEST_ALL_PREFIXES(InstantRestrictedIDCacheTest, MixIDGeneration); | 70 FRIEND_TEST_ALL_PREFIXES(InstantRestrictedIDCacheTest, MixIDGeneration); |
71 FRIEND_TEST_ALL_PREFIXES(InstantRestrictedIDCacheTest, AddEmptySet); | |
71 | 72 |
72 typedef base::MRUCache<InstantRestrictedID, T> CacheImpl; | 73 typedef base::MRUCache<InstantRestrictedID, T> CacheImpl; |
73 | 74 |
74 mutable CacheImpl cache_; | 75 mutable CacheImpl cache_; |
75 typename CacheImpl::reverse_iterator last_add_start_; | 76 typename CacheImpl::reverse_iterator last_add_start_; |
76 InstantRestrictedID last_restricted_id_; | 77 InstantRestrictedID last_restricted_id_; |
77 | 78 |
78 DISALLOW_COPY_AND_ASSIGN(InstantRestrictedIDCache); | 79 DISALLOW_COPY_AND_ASSIGN(InstantRestrictedIDCache); |
79 }; | 80 }; |
80 | 81 |
81 template <typename T> | 82 template <typename T> |
82 InstantRestrictedIDCache<T>::InstantRestrictedIDCache(size_t max_cache_size) | 83 InstantRestrictedIDCache<T>::InstantRestrictedIDCache(size_t max_cache_size) |
83 : cache_(max_cache_size), | 84 : cache_(max_cache_size), |
84 last_add_start_(cache_.rend()), | 85 last_add_start_(cache_.rend()), |
85 last_restricted_id_(0) { | 86 last_restricted_id_(0) { |
86 DCHECK(max_cache_size); | 87 DCHECK(max_cache_size); |
87 } | 88 } |
88 | 89 |
89 template <typename T> | 90 template <typename T> |
90 InstantRestrictedIDCache<T>::~InstantRestrictedIDCache() { | 91 InstantRestrictedIDCache<T>::~InstantRestrictedIDCache() { |
91 } | 92 } |
92 | 93 |
93 template <typename T> | 94 template <typename T> |
94 void InstantRestrictedIDCache<T>::AddItems(const ItemVector& items) { | 95 void InstantRestrictedIDCache<T>::AddItems(const ItemVector& items) { |
95 if (items.size() == 0 || items.size() > cache_.max_size()) | 96 if (items.size() > cache_.max_size()) |
96 return; | 97 return; |
brettw
2013/03/25 22:26:52
I'm wondering about this. If you add too many item
sreeram
2013/03/25 22:29:33
Adding too many items would be an error situation,
brettw
2013/03/25 22:34:04
Yeah, I like the dcheck.
sreeram
2013/03/25 22:39:26
Done.
| |
97 | 98 |
99 if (items.empty()) { | |
100 last_add_start_ = cache_.rend(); | |
101 return; | |
102 } | |
103 | |
98 for (size_t i = 0; i < items.size(); ++i) { | 104 for (size_t i = 0; i < items.size(); ++i) { |
99 InstantRestrictedID id = ++last_restricted_id_; | 105 InstantRestrictedID id = ++last_restricted_id_; |
100 cache_.Put(id, items[i]); | 106 cache_.Put(id, items[i]); |
101 if (i == 0) | 107 if (i == 0) |
102 last_add_start_ = --cache_.rend(); | 108 last_add_start_ = --cache_.rend(); |
103 } | 109 } |
104 } | 110 } |
105 | 111 |
106 template <typename T> | 112 template <typename T> |
107 void InstantRestrictedIDCache<T>::AddItemsWithRestrictedID( | 113 void InstantRestrictedIDCache<T>::AddItemsWithRestrictedID( |
108 const ItemIDVector& items) { | 114 const ItemIDVector& items) { |
109 if (items.size() == 0 || items.size() > cache_.max_size()) | 115 if (items.size() > cache_.max_size()) |
110 return; | 116 return; |
111 | 117 |
118 if (items.empty()) { | |
119 last_add_start_ = cache_.rend(); | |
120 return; | |
121 } | |
122 | |
112 std::set<InstantRestrictedID> ids_added; | 123 std::set<InstantRestrictedID> ids_added; |
113 for (size_t i = 0; i < items.size(); ++i) { | 124 for (size_t i = 0; i < items.size(); ++i) { |
114 const ItemIDPair& item_id = items[i]; | 125 const ItemIDPair& item_id = items[i]; |
115 | 126 |
116 DCHECK(ids_added.find(item_id.first) == ids_added.end()); | 127 DCHECK(ids_added.find(item_id.first) == ids_added.end()); |
117 ids_added.insert(item_id.first); | 128 ids_added.insert(item_id.first); |
118 | 129 |
119 cache_.Put(item_id.first, item_id.second); | 130 cache_.Put(item_id.first, item_id.second); |
120 if (i == 0) | 131 if (i == 0) |
121 last_add_start_ = --cache_.rend(); | 132 last_add_start_ = --cache_.rend(); |
(...skipping 18 matching lines...) Expand all Loading... | |
140 DCHECK(item); | 151 DCHECK(item); |
141 | 152 |
142 typename CacheImpl::const_iterator cache_it = cache_.Peek(restricted_id); | 153 typename CacheImpl::const_iterator cache_it = cache_.Peek(restricted_id); |
143 if (cache_it == cache_.end()) | 154 if (cache_it == cache_.end()) |
144 return false; | 155 return false; |
145 *item = cache_it->second; | 156 *item = cache_it->second; |
146 return true; | 157 return true; |
147 } | 158 } |
148 | 159 |
149 #endif // CHROME_COMMON_INSTANT_RESTRICTED_ID_CACHE_H_ | 160 #endif // CHROME_COMMON_INSTANT_RESTRICTED_ID_CACHE_H_ |
OLD | NEW |