Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion src/cachemap.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,9 @@ class CacheMap
void SetMaxSize(size_type nMaxSizeIn)
{
nMaxSize = nMaxSizeIn;
while (listItems.size() > nMaxSize) {
PruneLast();
}
}

size_type GetMaxSize() const {
Expand All @@ -101,10 +104,13 @@ class CacheMap

bool Insert(const K& key, const V& value)
{
if (nMaxSize == 0) {
return false;
}
if(mapIndex.find(key) != mapIndex.end()) {
return false;
}
if(listItems.size() == nMaxSize) {
while (listItems.size() >= nMaxSize) {
PruneLast();
}
listItems.push_front(item_t(key, value));
Expand Down
24 changes: 14 additions & 10 deletions src/cachemultimap.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ class CacheMultiMap
mapIndex()
{}

explicit CacheMultiMap(const CacheMap<K,V>& other)
CacheMultiMap(const CacheMultiMap& other)
: nMaxSize(other.nMaxSize),
listItems(other.listItems),
mapIndex()
Expand All @@ -74,6 +74,9 @@ class CacheMultiMap
void SetMaxSize(size_type nMaxSizeIn)
{
nMaxSize = nMaxSizeIn;
while (listItems.size() > nMaxSize) {
PruneLast();
}
}

size_type GetMaxSize() const {
Expand All @@ -86,22 +89,20 @@ class CacheMultiMap

bool Insert(const K& key, const V& value)
{
map_it mit = mapIndex.find(key);
if(mit == mapIndex.end()) {
mit = mapIndex.emplace(key, it_map_t()).first;
if (nMaxSize == 0) {
return false;
}
it_map_t& mapIt = mit->second;

if(mapIt.count(value) > 0) {
if (map_cit mit = mapIndex.find(key); mit != mapIndex.end() && mit->second.count(value) > 0) {
// Don't insert duplicates
return false;
}

if(listItems.size() == nMaxSize) {
// Pruning may erase this key's entry in mapIndex, so look it up only afterwards
while (listItems.size() >= nMaxSize) {
PruneLast();
}
listItems.push_front(item_t(key, value));
mapIt.emplace(value, listItems.begin());
mapIndex[key].emplace(value, listItems.begin());
return true;
}

Expand Down Expand Up @@ -184,8 +185,11 @@ class CacheMultiMap
return listItems;
}

CacheMap<K,V>& operator=(const CacheMap<K,V>& other)
CacheMultiMap& operator=(const CacheMultiMap& other)
{
if (this == &other) {
return *this;
}
nMaxSize = other.nMaxSize;
listItems = other.listItems;
RebuildIndex();
Expand Down
29 changes: 29 additions & 0 deletions src/test/cachemap_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -135,4 +135,33 @@ BOOST_AUTO_TEST_CASE(cachemap_test)
BOOST_CHECK(Compare(cmapTest1, mapTest4));
}

BOOST_AUTO_TEST_CASE(capacity_limits)
{
CacheMap<int,int> cache(0);
BOOST_CHECK(!cache.Insert(1, 1));
BOOST_CHECK_EQUAL(cache.GetSize(), 0);
BOOST_CHECK(!cache.HasKey(1));

cache.Clear();
cache.SetMaxSize(10);
for (int i = 0; i < 10; ++i) {
BOOST_REQUIRE(cache.Insert(i, i));
}
cache.SetMaxSize(5);
BOOST_CHECK_EQUAL(cache.GetSize(), 5);
for (int i = 0; i < 10; ++i) {
int value = -1;
if (i < 5) {
BOOST_CHECK(!cache.Get(i, value));
} else {
BOOST_CHECK(cache.Get(i, value));
BOOST_CHECK_EQUAL(value, i);
}
}
cache.SetMaxSize(0);
BOOST_CHECK_EQUAL(cache.GetSize(), 0);
BOOST_CHECK(!cache.HasKey(9));
BOOST_CHECK(!cache.Insert(1, 1));
}

BOOST_AUTO_TEST_SUITE_END()
53 changes: 53 additions & 0 deletions src/test/cachemultimap_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,59 @@ BOOST_AUTO_TEST_CASE(cachemultimap_test)
CacheMultiMap<int,int> mapTest4;
mapTest4 = cmmapTest1;
BOOST_CHECK(Compare(cmmapTest1, mapTest4));
Comment thread
PastaPastaPasta marked this conversation as resolved.
Resolved

// copies must keep working after the source's storage is gone
cmmapTest1.Clear();
for (auto* copy : {&cmmapTest3, &mapTest4}) {
std::vector<int> vals;
BOOST_CHECK(copy->GetAll(5, vals));
BOOST_CHECK(vals == std::vector<int>({1, 2, 4}));
copy->Erase(5);
BOOST_CHECK(!copy->HasKey(5));
}
}

BOOST_AUTO_TEST_CASE(same_key_eviction_at_capacity)
{
CacheMultiMap<int,int> cache(1);
BOOST_REQUIRE(cache.Insert(42, 1));
BOOST_CHECK(!cache.Insert(42, 1));
BOOST_REQUIRE(cache.Insert(42, 2));
BOOST_CHECK_EQUAL(cache.GetSize(), 1);
int value = 0;
BOOST_CHECK(cache.Get(42, value));
BOOST_CHECK_EQUAL(value, 2);
}

BOOST_AUTO_TEST_CASE(capacity_limits)
{
CacheMultiMap<int,int> cache(0);
BOOST_CHECK(!cache.Insert(1, 1));
BOOST_CHECK_EQUAL(cache.GetSize(), 0);
BOOST_CHECK(!cache.HasKey(1));

cache.Clear();
cache.SetMaxSize(10);
for (int i = 0; i < 10; ++i) {
BOOST_REQUIRE(cache.Insert(i / 2, i));
}
cache.SetMaxSize(5);
BOOST_CHECK_EQUAL(cache.GetSize(), 5);
for (int i = 0; i < 5; ++i) {
std::vector<int> values;
const bool found = cache.GetAll(i, values);
if (i < 2) {
BOOST_CHECK(!found);
} else {
BOOST_CHECK(found);
const std::vector<int> expected = i == 2 ? std::vector<int>{5} : std::vector<int>{i * 2, i * 2 + 1};
BOOST_CHECK_EQUAL_COLLECTIONS(values.begin(), values.end(), expected.begin(), expected.end());
}
}
cache.SetMaxSize(0);
BOOST_CHECK_EQUAL(cache.GetSize(), 0);
BOOST_CHECK(!cache.HasKey(4));
BOOST_CHECK(!cache.Insert(1, 1));
}

BOOST_AUTO_TEST_SUITE_END()
Loading