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 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
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 FRIEND_TEST_ALL_PREFIXES(InstantRestrictedIDCacheTest, AddEmptySet); |
| 72 FRIEND_TEST_ALL_PREFIXES(InstantRestrictedIDCacheTest, |
| 73 AddItemsWithRestrictedID); |
72 | 74 |
73 typedef base::MRUCache<InstantRestrictedID, T> CacheImpl; | 75 typedef base::MRUCache<InstantRestrictedID, T> CacheImpl; |
74 | 76 |
75 mutable CacheImpl cache_; | 77 mutable CacheImpl cache_; |
76 typename CacheImpl::reverse_iterator last_add_start_; | 78 typename CacheImpl::reverse_iterator last_add_start_; |
77 InstantRestrictedID last_restricted_id_; | 79 InstantRestrictedID last_restricted_id_; |
78 | 80 |
79 DISALLOW_COPY_AND_ASSIGN(InstantRestrictedIDCache); | 81 DISALLOW_COPY_AND_ASSIGN(InstantRestrictedIDCache); |
80 }; | 82 }; |
81 | 83 |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
119 } | 121 } |
120 | 122 |
121 std::set<InstantRestrictedID> ids_added; | 123 std::set<InstantRestrictedID> ids_added; |
122 for (size_t i = 0; i < items.size(); ++i) { | 124 for (size_t i = 0; i < items.size(); ++i) { |
123 const ItemIDPair& item_id = items[i]; | 125 const ItemIDPair& item_id = items[i]; |
124 | 126 |
125 DCHECK(ids_added.find(item_id.first) == ids_added.end()); | 127 DCHECK(ids_added.find(item_id.first) == ids_added.end()); |
126 ids_added.insert(item_id.first); | 128 ids_added.insert(item_id.first); |
127 | 129 |
128 cache_.Put(item_id.first, item_id.second); | 130 cache_.Put(item_id.first, item_id.second); |
129 if (i == 0) | |
130 last_add_start_ = --cache_.rend(); | |
131 last_restricted_id_ = std::max(item_id.first, last_restricted_id_); | 131 last_restricted_id_ = std::max(item_id.first, last_restricted_id_); |
132 } | 132 } |
| 133 |
| 134 // cache_.Put() can invalidate the iterator |last_add_start_| is pointing to. |
| 135 // Therefore, update |last_add_start_| after adding all the items to the |
| 136 // |cache_|. |
| 137 last_add_start_ = cache_.rend(); |
| 138 for (size_t i = 0; i < items.size(); ++i) |
| 139 --last_add_start_; |
133 } | 140 } |
134 | 141 |
135 template <typename T> | 142 template <typename T> |
136 void InstantRestrictedIDCache<T>::GetCurrentItems(ItemIDVector* items) const { | 143 void InstantRestrictedIDCache<T>::GetCurrentItems(ItemIDVector* items) const { |
137 items->clear(); | 144 items->clear(); |
138 | 145 |
139 for (typename CacheImpl::reverse_iterator it = last_add_start_; | 146 for (typename CacheImpl::reverse_iterator it = last_add_start_; |
140 it != cache_.rend(); ++it) { | 147 it != cache_.rend(); ++it) { |
141 items->push_back(std::make_pair(it->first, it->second)); | 148 items->push_back(std::make_pair(it->first, it->second)); |
142 } | 149 } |
143 } | 150 } |
144 | 151 |
145 template <typename T> | 152 template <typename T> |
146 bool InstantRestrictedIDCache<T>::GetItemWithRestrictedID( | 153 bool InstantRestrictedIDCache<T>::GetItemWithRestrictedID( |
147 InstantRestrictedID restricted_id, | 154 InstantRestrictedID restricted_id, |
148 T* item) const { | 155 T* item) const { |
149 DCHECK(item); | 156 DCHECK(item); |
150 | 157 |
151 typename CacheImpl::const_iterator cache_it = cache_.Peek(restricted_id); | 158 typename CacheImpl::const_iterator cache_it = cache_.Peek(restricted_id); |
152 if (cache_it == cache_.end()) | 159 if (cache_it == cache_.end()) |
153 return false; | 160 return false; |
154 *item = cache_it->second; | 161 *item = cache_it->second; |
155 return true; | 162 return true; |
156 } | 163 } |
157 | 164 |
158 #endif // CHROME_COMMON_INSTANT_RESTRICTED_ID_CACHE_H_ | 165 #endif // CHROME_COMMON_INSTANT_RESTRICTED_ID_CACHE_H_ |
OLD | NEW |