| 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 DCHECK_LE(items.size(), cache_.max_size()); |
| 97 |
| 98 if (items.empty()) { |
| 99 last_add_start_ = cache_.rend(); |
| 96 return; | 100 return; |
| 101 } |
| 97 | 102 |
| 98 for (size_t i = 0; i < items.size(); ++i) { | 103 for (size_t i = 0; i < items.size(); ++i) { |
| 99 InstantRestrictedID id = ++last_restricted_id_; | 104 InstantRestrictedID id = ++last_restricted_id_; |
| 100 cache_.Put(id, items[i]); | 105 cache_.Put(id, items[i]); |
| 101 if (i == 0) | 106 if (i == 0) |
| 102 last_add_start_ = --cache_.rend(); | 107 last_add_start_ = --cache_.rend(); |
| 103 } | 108 } |
| 104 } | 109 } |
| 105 | 110 |
| 106 template <typename T> | 111 template <typename T> |
| 107 void InstantRestrictedIDCache<T>::AddItemsWithRestrictedID( | 112 void InstantRestrictedIDCache<T>::AddItemsWithRestrictedID( |
| 108 const ItemIDVector& items) { | 113 const ItemIDVector& items) { |
| 109 if (items.size() == 0 || items.size() > cache_.max_size()) | 114 DCHECK_LE(items.size(), cache_.max_size()); |
| 115 |
| 116 if (items.empty()) { |
| 117 last_add_start_ = cache_.rend(); |
| 110 return; | 118 return; |
| 119 } |
| 111 | 120 |
| 112 std::set<InstantRestrictedID> ids_added; | 121 std::set<InstantRestrictedID> ids_added; |
| 113 for (size_t i = 0; i < items.size(); ++i) { | 122 for (size_t i = 0; i < items.size(); ++i) { |
| 114 const ItemIDPair& item_id = items[i]; | 123 const ItemIDPair& item_id = items[i]; |
| 115 | 124 |
| 116 DCHECK(ids_added.find(item_id.first) == ids_added.end()); | 125 DCHECK(ids_added.find(item_id.first) == ids_added.end()); |
| 117 ids_added.insert(item_id.first); | 126 ids_added.insert(item_id.first); |
| 118 | 127 |
| 119 cache_.Put(item_id.first, item_id.second); | 128 cache_.Put(item_id.first, item_id.second); |
| 120 if (i == 0) | 129 if (i == 0) |
| (...skipping 19 matching lines...) Expand all Loading... |
| 140 DCHECK(item); | 149 DCHECK(item); |
| 141 | 150 |
| 142 typename CacheImpl::const_iterator cache_it = cache_.Peek(restricted_id); | 151 typename CacheImpl::const_iterator cache_it = cache_.Peek(restricted_id); |
| 143 if (cache_it == cache_.end()) | 152 if (cache_it == cache_.end()) |
| 144 return false; | 153 return false; |
| 145 *item = cache_it->second; | 154 *item = cache_it->second; |
| 146 return true; | 155 return true; |
| 147 } | 156 } |
| 148 | 157 |
| 149 #endif // CHROME_COMMON_INSTANT_RESTRICTED_ID_CACHE_H_ | 158 #endif // CHROME_COMMON_INSTANT_RESTRICTED_ID_CACHE_H_ |
| OLD | NEW |