summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLuboš Luňák <l.lunak@collabora.com>2021-06-09 17:10:42 +0200
committerMichael Meeks <michael.meeks@collabora.com>2021-06-10 13:14:42 +0200
commit76287fdb4b7176124f5ff7d1b6ffc1624fec8ba7 (patch)
tree6e3557e479b58a54870d7d254f9fca2b024a2b02
parentcb7a97c6ecf0bc1fbbf8a8b3494ed581cb9fb310 (diff)
allow altering the max size of o3tl::lru_cache
Change-Id: Id119b70275e1c88a8c57f89d915241427be9dbf5 Reviewed-on: https://gerrit.libreoffice.org/c/core/+/116927 Tested-by: Jenkins Reviewed-by: Luboš Luňák <l.lunak@collabora.com> (cherry picked from commit 62b58e88d897f51a7c4e12b41d14121ab8d3396f) Reviewed-on: https://gerrit.libreoffice.org/c/core/+/116897 Tested-by: Jenkins CollaboraOffice <jenkinscollaboraoffice@gmail.com> Reviewed-by: Michael Meeks <michael.meeks@collabora.com> (cherry picked from commit cb10c29a3255620bf69480aa7b653962fb04786e) Reviewed-on: https://gerrit.libreoffice.org/c/core/+/116900
-rw-r--r--include/o3tl/lru_map.hxx9
-rw-r--r--o3tl/qa/test-lru_map.cxx14
2 files changed, 22 insertions, 1 deletions
diff --git a/include/o3tl/lru_map.hxx b/include/o3tl/lru_map.hxx
index 33a1487fa239..19d037dbdc38 100644
--- a/include/o3tl/lru_map.hxx
+++ b/include/o3tl/lru_map.hxx
@@ -49,7 +49,7 @@ private:
list_t mLruList;
map_t mLruMap;
- const size_t mMaxSize;
+ size_t mMaxSize;
void checkLRU()
{
@@ -79,6 +79,13 @@ public:
aLruListTemp.swap(mLruList);
}
+ void setMaxSize(size_t nMaxSize)
+ {
+ mMaxSize = nMaxSize ? nMaxSize : std::min(mLruMap.max_size(), mLruList.max_size());
+ while (mLruMap.size() > mMaxSize)
+ checkLRU();
+ }
+
void insert(key_value_pair_t& rPair)
{
map_iterator_t i = mLruMap.find(rPair.first);
diff --git a/o3tl/qa/test-lru_map.cxx b/o3tl/qa/test-lru_map.cxx
index 5ceec149571c..fe616ee3d026 100644
--- a/o3tl/qa/test-lru_map.cxx
+++ b/o3tl/qa/test-lru_map.cxx
@@ -29,6 +29,7 @@ public:
void testCustomHash();
void testRemoveIf();
void testNoAutoCleanup();
+ void testChangeMaxSize();
CPPUNIT_TEST_SUITE(lru_map_test);
CPPUNIT_TEST(testBaseUsage);
@@ -38,6 +39,7 @@ public:
CPPUNIT_TEST(testCustomHash);
CPPUNIT_TEST(testRemoveIf);
CPPUNIT_TEST(testNoAutoCleanup);
+ CPPUNIT_TEST(testChangeMaxSize);
CPPUNIT_TEST_SUITE_END();
};
@@ -309,6 +311,18 @@ void lru_map_test::testNoAutoCleanup()
}
}
+void lru_map_test::testChangeMaxSize()
+{
+ o3tl::lru_map<int, int> lru(3);
+ CPPUNIT_ASSERT_EQUAL(size_t(0), lru.size());
+ lru.insert({ 0, 0 });
+ lru.insert({ 1, 1 });
+ lru.insert({ 2, 2 });
+ CPPUNIT_ASSERT_EQUAL(size_t(3), lru.size());
+ lru.setMaxSize(1);
+ CPPUNIT_ASSERT_EQUAL(size_t(1), lru.size());
+}
+
CPPUNIT_TEST_SUITE_REGISTRATION(lru_map_test);
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */