2021-04-20 21:33:02 +02:00
|
|
|
// Copyright (c) 2014-2020 The Dash Core developers
|
2016-11-13 18:52:34 +01:00
|
|
|
// Distributed under the MIT/X11 software license, see the accompanying
|
|
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
|
|
|
2018-04-02 00:30:17 +02:00
|
|
|
#ifndef BITCOIN_CACHEMULTIMAP_H
|
|
|
|
#define BITCOIN_CACHEMULTIMAP_H
|
2016-11-13 18:52:34 +01:00
|
|
|
|
|
|
|
#include <cstddef>
|
|
|
|
#include <map>
|
|
|
|
#include <list>
|
|
|
|
#include <set>
|
|
|
|
|
2020-03-19 23:46:56 +01:00
|
|
|
#include <serialize.h>
|
2016-11-13 18:52:34 +01:00
|
|
|
|
2020-03-19 23:46:56 +01:00
|
|
|
#include <cachemap.h>
|
2016-11-13 18:52:34 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Map like container that keeps the N most recently added items
|
|
|
|
*/
|
|
|
|
template<typename K, typename V, typename Size = uint32_t>
|
|
|
|
class CacheMultiMap
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
typedef Size size_type;
|
|
|
|
|
|
|
|
typedef CacheItem<K,V> item_t;
|
|
|
|
|
|
|
|
typedef std::list<item_t> list_t;
|
|
|
|
|
|
|
|
typedef typename list_t::iterator list_it;
|
|
|
|
|
|
|
|
typedef typename list_t::const_iterator list_cit;
|
|
|
|
|
|
|
|
typedef std::map<V,list_it> it_map_t;
|
|
|
|
|
|
|
|
typedef typename it_map_t::iterator it_map_it;
|
|
|
|
|
|
|
|
typedef typename it_map_t::const_iterator it_map_cit;
|
|
|
|
|
|
|
|
typedef std::map<K, it_map_t> map_t;
|
|
|
|
|
|
|
|
typedef typename map_t::iterator map_it;
|
|
|
|
|
|
|
|
typedef typename map_t::const_iterator map_cit;
|
|
|
|
|
|
|
|
private:
|
|
|
|
size_type nMaxSize;
|
|
|
|
|
|
|
|
list_t listItems;
|
|
|
|
|
|
|
|
map_t mapIndex;
|
|
|
|
|
|
|
|
public:
|
|
|
|
CacheMultiMap(size_type nMaxSizeIn = 0)
|
|
|
|
: nMaxSize(nMaxSizeIn),
|
|
|
|
listItems(),
|
|
|
|
mapIndex()
|
|
|
|
{}
|
|
|
|
|
|
|
|
CacheMultiMap(const CacheMap<K,V>& other)
|
|
|
|
: nMaxSize(other.nMaxSize),
|
|
|
|
listItems(other.listItems),
|
|
|
|
mapIndex()
|
|
|
|
{
|
|
|
|
RebuildIndex();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Clear()
|
|
|
|
{
|
|
|
|
mapIndex.clear();
|
|
|
|
listItems.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
void SetMaxSize(size_type nMaxSizeIn)
|
|
|
|
{
|
|
|
|
nMaxSize = nMaxSizeIn;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_type GetMaxSize() const {
|
|
|
|
return nMaxSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_type GetSize() const {
|
2018-02-06 12:08:43 +01:00
|
|
|
return listItems.size();
|
2016-11-13 18:52:34 +01:00
|
|
|
}
|
|
|
|
|
2016-11-14 19:17:42 +01:00
|
|
|
bool Insert(const K& key, const V& value)
|
2016-11-13 18:52:34 +01:00
|
|
|
{
|
|
|
|
map_it mit = mapIndex.find(key);
|
|
|
|
if(mit == mapIndex.end()) {
|
2018-02-06 12:08:43 +01:00
|
|
|
mit = mapIndex.emplace(key, it_map_t()).first;
|
2016-11-13 18:52:34 +01:00
|
|
|
}
|
|
|
|
it_map_t& mapIt = mit->second;
|
|
|
|
|
|
|
|
if(mapIt.count(value) > 0) {
|
|
|
|
// Don't insert duplicates
|
2016-11-14 19:17:42 +01:00
|
|
|
return false;
|
2016-11-13 18:52:34 +01:00
|
|
|
}
|
|
|
|
|
2018-02-06 12:08:43 +01:00
|
|
|
if(listItems.size() == nMaxSize) {
|
|
|
|
PruneLast();
|
|
|
|
}
|
2016-11-13 18:52:34 +01:00
|
|
|
listItems.push_front(item_t(key, value));
|
2018-02-06 12:08:43 +01:00
|
|
|
mapIt.emplace(value, listItems.begin());
|
2016-11-14 19:17:42 +01:00
|
|
|
return true;
|
2016-11-13 18:52:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool HasKey(const K& key) const
|
|
|
|
{
|
2018-02-06 12:08:43 +01:00
|
|
|
return (mapIndex.find(key) != mapIndex.end());
|
2016-11-13 18:52:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Get(const K& key, V& value) const
|
|
|
|
{
|
|
|
|
map_cit it = mapIndex.find(key);
|
|
|
|
if(it == mapIndex.end()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
const it_map_t& mapIt = it->second;
|
|
|
|
const item_t& item = *(mapIt.begin()->second);
|
|
|
|
value = item.value;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool GetAll(const K& key, std::vector<V>& vecValues)
|
|
|
|
{
|
|
|
|
map_cit mit = mapIndex.find(key);
|
|
|
|
if(mit == mapIndex.end()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
const it_map_t& mapIt = mit->second;
|
|
|
|
|
|
|
|
for(it_map_cit it = mapIt.begin(); it != mapIt.end(); ++it) {
|
|
|
|
const item_t& item = *(it->second);
|
|
|
|
vecValues.push_back(item.value);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-03-06 08:46:59 +01:00
|
|
|
void GetKeys(std::vector<K>& vecKeys)
|
|
|
|
{
|
|
|
|
for(map_cit it = mapIndex.begin(); it != mapIndex.end(); ++it) {
|
|
|
|
vecKeys.push_back(it->first);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-13 18:52:34 +01:00
|
|
|
void Erase(const K& key)
|
|
|
|
{
|
|
|
|
map_it mit = mapIndex.find(key);
|
|
|
|
if(mit == mapIndex.end()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
it_map_t& mapIt = mit->second;
|
|
|
|
|
|
|
|
for(it_map_it it = mapIt.begin(); it != mapIt.end(); ++it) {
|
|
|
|
listItems.erase(it->second);
|
|
|
|
}
|
|
|
|
|
|
|
|
mapIndex.erase(mit);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Erase(const K& key, const V& value)
|
|
|
|
{
|
|
|
|
map_it mit = mapIndex.find(key);
|
|
|
|
if(mit == mapIndex.end()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
it_map_t& mapIt = mit->second;
|
|
|
|
|
|
|
|
it_map_it it = mapIt.find(value);
|
|
|
|
if(it == mapIt.end()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
listItems.erase(it->second);
|
|
|
|
mapIt.erase(it);
|
|
|
|
|
2018-02-06 12:08:43 +01:00
|
|
|
if(mapIt.empty()) {
|
2016-11-13 18:52:34 +01:00
|
|
|
mapIndex.erase(mit);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const list_t& GetItemList() const {
|
|
|
|
return listItems;
|
|
|
|
}
|
|
|
|
|
|
|
|
CacheMap<K,V>& operator=(const CacheMap<K,V>& other)
|
|
|
|
{
|
|
|
|
nMaxSize = other.nMaxSize;
|
|
|
|
listItems = other.listItems;
|
|
|
|
RebuildIndex();
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2021-05-27 17:17:29 +02:00
|
|
|
SERIALIZE_METHODS(CacheMultiMap, obj)
|
2016-11-13 18:52:34 +01:00
|
|
|
{
|
2021-05-27 17:17:29 +02:00
|
|
|
READWRITE(obj.nMaxSize, obj.listItems);
|
|
|
|
SER_READ(obj, obj.RebuildIndex());
|
2016-11-13 18:52:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
void PruneLast()
|
|
|
|
{
|
2018-02-06 12:08:43 +01:00
|
|
|
if(listItems.empty()) {
|
2016-11-13 18:52:34 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
list_it lit = listItems.end();
|
|
|
|
--lit;
|
|
|
|
item_t& item = *lit;
|
|
|
|
|
|
|
|
map_it mit = mapIndex.find(item.key);
|
|
|
|
|
|
|
|
if(mit != mapIndex.end()) {
|
|
|
|
it_map_t& mapIt = mit->second;
|
|
|
|
|
|
|
|
mapIt.erase(item.value);
|
|
|
|
|
2018-02-06 12:08:43 +01:00
|
|
|
if(mapIt.empty()) {
|
2016-11-13 18:52:34 +01:00
|
|
|
mapIndex.erase(item.key);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
listItems.pop_back();
|
|
|
|
}
|
|
|
|
|
|
|
|
void RebuildIndex()
|
|
|
|
{
|
|
|
|
mapIndex.clear();
|
|
|
|
for(list_it lit = listItems.begin(); lit != listItems.end(); ++lit) {
|
|
|
|
item_t& item = *lit;
|
|
|
|
map_it mit = mapIndex.find(item.key);
|
|
|
|
if(mit == mapIndex.end()) {
|
2018-02-06 12:08:43 +01:00
|
|
|
mit = mapIndex.emplace(item.key, it_map_t()).first;
|
2016-11-13 18:52:34 +01:00
|
|
|
}
|
|
|
|
it_map_t& mapIt = mit->second;
|
2018-02-06 12:08:43 +01:00
|
|
|
mapIt.emplace(item.value, lit);
|
2016-11-13 18:52:34 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-04-02 00:30:17 +02:00
|
|
|
#endif // BITCOIN_CACHEMULTIMAP_H
|