本篇內(nèi)容介紹了“bitcoin中的limitedmap有什么用”的有關(guān)知識(shí),在實(shí)際案例的操作過(guò)程中,不少人都會(huì)遇到這樣的困境,接下來(lái)就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!

創(chuàng)新互聯(lián)公司為您提適合企業(yè)的網(wǎng)站設(shè)計(jì)?讓您的網(wǎng)站在搜索引擎具有高度排名,讓您的網(wǎng)站具備超強(qiáng)的網(wǎng)絡(luò)競(jìng)爭(zhēng)力!結(jié)合企業(yè)自身,進(jìn)行網(wǎng)站設(shè)計(jì)及把握,最后結(jié)合企業(yè)文化和具體宗旨等,才能創(chuàng)作出一份性化解決方案。從網(wǎng)站策劃到成都網(wǎng)站設(shè)計(jì)、網(wǎng)站制作、外貿(mào)營(yíng)銷(xiāo)網(wǎng)站建設(shè), 我們的網(wǎng)頁(yè)設(shè)計(jì)師為您提供的解決方案。
bitcoin 里面的limitedmap 基本上是在std::map 上實(shí)現(xiàn)了優(yōu)先隊(duì)列的功能, 主要用在記錄向?qū)Φ裙?jié)點(diǎn)索取inv 對(duì)應(yīng)的時(shí)間記錄。 以前不了解, stl 容器可以如此靈活使用, 把多個(gè)容器組合一起,完成更強(qiáng)大的功能。limitedmap 的主要技巧是,使用一個(gè)multimap(limitedmap 中多個(gè)不同的key可能映射到同一個(gè)value), 記錄map里面value及其在map的位置(迭代器), bitcoin 里面有不少這種用法, 一個(gè)容器的元素是另一個(gè)容器的迭代器。
template <typename K, typename V>
class limitedmap
{
public:
typedef K key_type;
typedef V mapped_type;
typedef std::pair<const key_type, mapped_type> value_type;
typedef typename std::map<K, V>::const_iterator const_iterator;
typedef typename std::map<K, V>::size_type size_type;
protected:
std::map<K, V> map;
typedef typename std::map<K, V>::iterator iterator;
std::multimap<V, iterator> rmap;
typedef typename std::multimap<V, iterator>::iterator rmap_iterator;
size_type nMaxSize;
public:
// 構(gòu)造器建立class 的不變量,元素size 不超過(guò)nMaxSizeIn
explicit limitedmap(size_type nMaxSizeIn)
{
assert(nMaxSizeIn > 0);
nMaxSize = nMaxSizeIn;
}
//迭代器,只讀方法都代理給底層的std::map
const_iterator begin() const { return map.begin(); }
const_iterator end() const { return map.end(); }
size_type size() const { return map.size(); }
bool empty() const { return map.empty(); }
const_iterator find(const key_type& k) const { return map.find(k); }
size_type count(const key_type& k) const { return map.count(k); }
//這里插入采用了先插入新元素, 然后再檢測(cè)個(gè)數(shù)超限,超限后再刪除最小的元素(破壞了不變量,然后再恢復(fù)回來(lái))
//為什么不插入前,先檢測(cè)個(gè)數(shù)是否超限, 已經(jīng)超限就返回,不用再后來(lái)刪除了
//每次往底層map成功地添加一個(gè)元素,就更新multimap, 記錄新的值在map中的位置
void insert(const value_type& x)
{
std::pair<iterator, bool> ret = map.insert(x);
if (ret.second) {
if (map.size() > nMaxSize) {
map.erase(rmap.begin()->second);
rmap.erase(rmap.begin());
}
rmap.insert(make_pair(x.second, ret.first));
}
}
//按key刪除對(duì)應(yīng)的元素,首先再map中查找k對(duì)應(yīng)的value
//然后拿value,作為multimap 中的key, 在multimap中定位key是value的區(qū)間
//在區(qū)間內(nèi)搜索哪個(gè)條目是記錄map 中k 的記錄
void erase(const key_type& k)
{
iterator itTarget = map.find(k);
if (itTarget == map.end())
return;
std::pair<rmap_iterator, rmap_iterator> itPair = rmap.equal_range(itTarget->second);
for (rmap_iterator it = itPair.first; it != itPair.second; ++it)
if (it->second == itTarget) {
rmap.erase(it);
map.erase(itTarget);
return;
}
// Shouldn't ever get here
assert(0);
}
//類(lèi)似insert 的邏輯
void update(const_iterator itIn, const mapped_type& v)
{
// Using map::erase() with empty range instead of map::find() to get a non-const iterator,
// since it is a constant time operation in C++11. For more details, see
// https://stackoverflow.com/questions/765148/how-to-remove-constness-of-const-iterator
iterator itTarget = map.erase(itIn, itIn);
if (itTarget == map.end())
return;
std::pair<rmap_iterator, rmap_iterator> itPair = rmap.equal_range(itTarget->second);
for (rmap_iterator it = itPair.first; it != itPair.second; ++it)
if (it->second == itTarget) {
rmap.erase(it);
itTarget->second = v;
rmap.insert(make_pair(v, itTarget));
return;
}
// Shouldn't ever get here
assert(0);
}
size_type max_size() const { return nMaxSize; }
//overload max_size 成員, 調(diào)整max limit 數(shù)目
size_type max_size(size_type s)
{
assert(s > 0);
while (map.size() > s) {
map.erase(rmap.begin()->second);
rmap.erase(rmap.begin());
}
nMaxSize = s;
return nMaxSize;
}
};“bitcoin中的limitedmap有什么用”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識(shí)可以關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實(shí)用文章!
文章名稱(chēng):bitcoin中的limitedmap有什么用
分享鏈接:http://www.chinadenli.net/article48/peeoep.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站建設(shè)、網(wǎng)站排名、全網(wǎng)營(yíng)銷(xiāo)推廣、品牌網(wǎng)站設(shè)計(jì)、網(wǎng)站制作、微信小程序
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話(huà):028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)