Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #ifndef CHROME_COMMON_INSTANT_RESTRICTED_ID_CACHE_H_ | |
| 6 #define CHROME_COMMON_INSTANT_RESTRICTED_ID_CACHE_H_ | |
| 7 | |
| 8 #include <algorithm> | |
| 9 #include <deque> | |
| 10 #include <vector> | |
| 11 | |
| 12 #include "base/gtest_prod_util.h" | |
| 13 #include "base/logging.h" | |
| 14 #include "chrome/common/instant_types.h" | |
| 15 #include "content/public/common/page_transition_types.h" | |
| 16 #include "googleurl/src/gurl.h" | |
| 17 | |
| 18 // T needs to be copyable. | |
|
dhollowa
2013/03/06 02:12:58
nit: Please add documentation.
Shishir
2013/03/11 21:07:24
Done.
| |
| 19 template <typename T> | |
| 20 class InstantRestrictedIdCache { | |
| 21 public: | |
| 22 explicit InstantRestrictedIdCache(size_t max_cache_size); | |
| 23 ~InstantRestrictedIdCache(); | |
| 24 | |
| 25 // Adds items to the cache, assigning restricted ids in the process. | |
|
dhollowa
2013/03/06 02:12:58
nit: s/ids/IDs
Shishir
2013/03/11 21:07:24
Done.
| |
| 26 void AddItems(const std::vector<T>& items); | |
| 27 | |
| 28 // Returns the last set of items added to the cache. | |
|
dhollowa
2013/03/06 02:12:58
Please mention that this set will change upon the
Shishir
2013/03/11 21:07:24
Done.
| |
| 29 void GetCurrentItems(std::vector<std::pair<size_t, T> >* items) const; | |
|
dhollowa
2013/03/06 02:12:58
size_t seems wrong, it implies a size. int or int
Shishir
2013/03/11 21:07:24
Done.
| |
| 30 | |
| 31 // Returns true if the |restricted_id| is present in the cache and if so, | |
| 32 // returns a copy of the item. | |
| 33 bool GetItemWithRestrictedId(size_t restricted_id, T* item) const; | |
|
dhollowa
2013/03/06 02:12:58
Is this only from the "current" items?
Shishir
2013/03/11 21:07:24
No. It can be any id. Top level comments should ma
| |
| 34 | |
| 35 // Clears the cache. | |
| 36 void Reset(); | |
| 37 | |
| 38 FRIEND_TEST_ALL_PREFIXES(InstantRestrictedIdCacheTest, AddAndFetchTest); | |
| 39 FRIEND_TEST_ALL_PREFIXES(InstantRestrictedIdCacheTest, EdgeCasesTest); | |
| 40 | |
| 41 // Adds a single item to the cache. | |
| 42 void AddItem(const T& item); | |
|
dhollowa
2013/03/06 02:12:58
This should be grouped with |AddItems|.
Shishir
2013/03/11 21:07:24
Function does not exist.
| |
| 43 | |
| 44 std::deque<T> cache_; | |
|
dhollowa
2013/03/06 02:12:58
These should be private.
Shishir
2013/03/11 21:07:24
Done.
| |
| 45 size_t results_base_; | |
| 46 size_t last_addition_size_; // The number of items added last. | |
| 47 const size_t max_cache_size_; | |
| 48 | |
| 49 private: | |
| 50 DISALLOW_COPY_AND_ASSIGN(InstantRestrictedIdCache); | |
| 51 }; | |
| 52 | |
| 53 template <typename T> | |
| 54 InstantRestrictedIdCache<T>::InstantRestrictedIdCache(size_t max_cache_size) | |
| 55 : results_base_(0), | |
| 56 last_addition_size_(0), | |
| 57 max_cache_size_(max_cache_size) { | |
| 58 CHECK(max_cache_size_); | |
|
dhollowa
2013/03/06 02:12:58
DCHECK since this is programmer error.
Shishir
2013/03/11 21:07:24
Done.
| |
| 59 } | |
| 60 | |
| 61 template <typename T> | |
| 62 InstantRestrictedIdCache<T>::~InstantRestrictedIdCache() { | |
| 63 } | |
| 64 | |
| 65 template <typename T> | |
| 66 void InstantRestrictedIdCache<T>::AddItems(const std::vector<T>& items) { | |
| 67 for (typename std::vector<T>::const_iterator it = items.begin(); | |
| 68 it != items.end(); ++it) { | |
| 69 AddItem(*it); | |
| 70 } | |
| 71 last_addition_size_ = items.size(); | |
| 72 } | |
| 73 | |
| 74 template <typename T> | |
| 75 void InstantRestrictedIdCache<T>::GetCurrentItems( | |
| 76 std::vector<std::pair<size_t, T> >* items) const { | |
| 77 DCHECK(items); | |
|
dhollowa
2013/03/06 02:12:58
no need for DCHECK since next line will crash.
Shishir
2013/03/11 21:07:24
Done.
| |
| 78 items->clear(); | |
| 79 | |
| 80 if (last_addition_size_ == 0) | |
| 81 return; | |
| 82 | |
| 83 size_t start = static_cast<size_t>( | |
| 84 std::max(0, static_cast<int>(cache_.size() - last_addition_size_))); | |
|
dhollowa
2013/03/06 02:12:58
Seems like last_addition_size_ should never be gre
Shishir
2013/03/11 21:07:24
It can be greater. While retrieving we take care o
| |
| 85 for (size_t i = start; i < cache_.size(); ++i) { | |
| 86 size_t restricted_id = results_base_ + i; | |
| 87 items->push_back(std::make_pair(restricted_id, cache_[i])); | |
| 88 } | |
| 89 } | |
| 90 | |
| 91 template <typename T> | |
| 92 bool InstantRestrictedIdCache<T>::GetItemWithRestrictedId(size_t restricted_id, | |
| 93 T* item) const { | |
|
dhollowa
2013/03/06 02:12:58
nit: indent
Shishir
2013/03/11 21:07:24
Done.
| |
| 94 DCHECK(item); | |
| 95 | |
| 96 if (restricted_id < results_base_ || | |
| 97 restricted_id >= (results_base_ + cache_.size())) { | |
| 98 return false; | |
| 99 } | |
| 100 | |
| 101 *item = cache_[restricted_id - results_base_]; | |
| 102 return true; | |
| 103 } | |
| 104 | |
| 105 template <typename T> | |
| 106 void InstantRestrictedIdCache<T>::Reset() { | |
| 107 cache_.clear(); | |
| 108 results_base_ = 0; | |
| 109 last_addition_size_ = 0; | |
| 110 } | |
| 111 | |
| 112 template <typename T> | |
| 113 void InstantRestrictedIdCache<T>::AddItem(const T& item) { | |
| 114 if (cache_.size() == max_cache_size_) { | |
| 115 cache_.pop_front(); | |
| 116 ++results_base_; | |
| 117 } | |
| 118 | |
| 119 cache_.push_back(item); | |
| 120 } | |
| 121 | |
| 122 #endif // CHROME_COMMON_INSTANT_RESTRICTED_ID_CACHE_H_ | |
| OLD | NEW |