From 17b11428c135203342aff38cabc8047e673f38ac Mon Sep 17 00:00:00 2001 From: Pieter Wuille Date: Tue, 28 Apr 2015 10:27:16 -0700 Subject: Cache transaction validation successes --- src/mruset.h | 60 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) (limited to 'src/mruset.h') diff --git a/src/mruset.h b/src/mruset.h index 398aa173bf..9dff5694ba 100644 --- a/src/mruset.h +++ b/src/mruset.h @@ -9,6 +9,10 @@ #include #include +#include +#include +#include + /** STL-like set container that only keeps the most recent N elements. */ template class mruset @@ -62,4 +66,60 @@ public: size_type max_size() const { return nMaxSize; } }; +/** STL-like map container that only keeps the most recent N elements. */ +template +class mrumap +{ +private: + struct key_extractor { + typedef K result_type; + const result_type& operator()(const std::pair& e) const { return e.first; } + result_type& operator()(std::pair* e) const { return e->first; } + }; + + typedef boost::multi_index_container< + std::pair, + boost::multi_index::indexed_by< + boost::multi_index::sequenced<>, + boost::multi_index::ordered_unique + > + > map_type; + +public: + typedef K key_type; + typedef std::pair value_type; + typedef typename map_type::iterator iterator; + typedef typename map_type::const_iterator const_iterator; + typedef typename map_type::size_type size_type; + +protected: + map_type m_; + size_type max_size_; + +public: + mrumap(size_type max_size_in = 1) { clear(max_size_in); } + iterator begin() { return m_.begin(); } + iterator end() { return m_.end(); } + const_iterator begin() const { return m_.begin(); } + const_iterator end() const { return m_.end(); } + size_type size() const { return m_.size(); } + bool empty() const { return m_.empty(); } + iterator find(const key_type& key) { return m_.template project<0>(boost::get<1>(m_).find(key)); } + const_iterator find(const key_type& key) const { return m_.template project<0>(boost::get<1>(m_).find(key)); } + size_type count(const key_type& key) const { return boost::get<1>(m_).count(key); } + void clear(size_type max_size_in) { m_.clear(); max_size_ = max_size_in; } + std::pair insert(const K& key, const V& value) + { + std::pair elem(key, value); + std::pair p = m_.push_front(elem); + if (p.second && m_.size() > max_size_) { + m_.pop_back(); + } + return p; + } + void erase(iterator it) { m_.erase(it); } + void erase(const key_type& k) { boost::get<1>(m_).erase(k); } + size_type max_size() const { return max_size_; } +}; + #endif // BITCOIN_MRUSET_H -- cgit v1.2.3