Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the use of limitedmap in bitcoin

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article introduces the relevant knowledge of "what is the use of limitedmap in bitcoin". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Limitedmap in bitcoin basically implements the function of priority queue on std::map, which is mainly used to record the time record corresponding to inv from peer node. I didn't know that stl containers can be used so flexibly that multiple containers can be combined to achieve more powerful functions. The main trick of limitedmap is to use a multimap (multiple different key in limitedmap may map to the same value) to record the value in map and its location in map (iterator). There are many such uses in bitcoin, and the element of one container is the iterator of another container.

Template class limitedmap {public: typedef K key_type; typedef V mapped_type; typedef std::pair value_type; typedef typename std::map::const_iterator const_iterator; typedef typename std::map::size_type size_type;protected: std::map map; typedef typename std::map::iterator iterator; std::multimap rmap; typedef typename std::multimap::iterator rmap_iterator; size_type nMaxSize Public: / / the constructor establishes the invariant of class, and the element size does not exceed nMaxSizeIn explicit limitedmap (size_type nMaxSizeIn) {assert (nMaxSizeIn > 0); nMaxSize = nMaxSizeIn;} / / iterator, and read-only methods are proxied to the underlying 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) } / / the insertion here adopts inserting new elements first, then detecting the number exceeding the limit, and then deleting the smallest element after exceeding the limit (destroying the invariant and then restoring it) / / Why not check whether the number exceeds the limit first and return after exceeding the limit, instead of later deleting / / successfully adding one element to the underlying map at a time Update multimap to record the position of the new value in map void insert (const value_type& x) {std::pair 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)) }} / / Delete the corresponding element by key, first find the value / / corresponding to k in map, then take value as the key in multimap, locate the interval where key is value in multimap / / search in the interval which entry is the record void erase (const key_type& k) of k in record map {iterator itTarget = map.find (k) If (itTarget = = map.end () return; std::pair 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) } / / insert-like logic 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 Clippers 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 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 member, adjust the number of max limit 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;}}; that's all for "what's the use of limitedmap in bitcoin". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report