summaryrefslogtreecommitdiff
path: root/include/utils
diff options
context:
space:
mode:
authorSergio Giro <sgiro@google.com>2015-10-02 17:31:34 +0100
committerSergio Giro <sgiro@google.com>2015-10-12 16:13:44 +0100
commit0cb59c0dce34bd1518aa775e45e1dd7810195b78 (patch)
treeaf243b47b9ec91f39829664ea6421ed7b41ddb8b /include/utils
parent2fb90dc8b2b590e674c5e433e8bf3d3f08a887c8 (diff)
system/core: fix iterator for LruCache
Was failing to return the first element Change-Id: Ic803f5d463a56519212014d0d190407cf4b859cf
Diffstat (limited to 'include/utils')
-rw-r--r--include/utils/LruCache.h19
1 files changed, 16 insertions, 3 deletions
diff --git a/include/utils/LruCache.h b/include/utils/LruCache.h
index 7818b4e2e..20a379ecc 100644
--- a/include/utils/LruCache.h
+++ b/include/utils/LruCache.h
@@ -99,17 +99,29 @@ private:
TValue mNullValue;
public:
+ // To be used like:
+ // while (it.next()) {
+ // it.value(); it.key();
+ // }
class Iterator {
public:
- Iterator(const LruCache<TKey, TValue>& cache): mCache(cache), mIterator(mCache.mSet->begin()) {
+ Iterator(const LruCache<TKey, TValue>& cache):
+ mCache(cache), mIterator(mCache.mSet->begin()), mBeginReturned(false) {
}
bool next() {
if (mIterator == mCache.mSet->end()) {
return false;
}
- std::advance(mIterator, 1);
- return mIterator != mCache.mSet->end();
+ if (!mBeginReturned) {
+ // mIterator has been initialized to the beginning and
+ // hasn't been returned. Do not advance:
+ mBeginReturned = true;
+ } else {
+ std::advance(mIterator, 1);
+ }
+ bool ret = (mIterator != mCache.mSet->end());
+ return ret;
}
const TValue& value() const {
@@ -122,6 +134,7 @@ public:
private:
const LruCache<TKey, TValue>& mCache;
typename LruCacheSet::iterator mIterator;
+ bool mBeginReturned;
};
};