Raven Core  3.0.0
P2P Digital Currency
indirectmap.h
Go to the documentation of this file.
1 // Copyright (c) 2016 The Bitcoin Core developers
2 // Copyright (c) 2017-2019 The Raven Core developers
3 // Distributed under the MIT software license, see the accompanying
4 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
5 
6 #ifndef RAVEN_INDIRECTMAP_H
7 #define RAVEN_INDIRECTMAP_H
8 
9 template <class T>
10 struct DereferencingComparator { bool operator()(const T a, const T b) const { return *a < *b; } };
11 
12 /* Map whose keys are pointers, but are compared by their dereferenced values.
13  *
14  * Differs from a plain std::map<const K*, T, DereferencingComparator<K*> > in
15  * that methods that take a key for comparison take a K rather than taking a K*
16  * (taking a K* would be confusing, since it's the value rather than the address
17  * of the object for comparison that matters due to the dereferencing comparator).
18  *
19  * Objects pointed to by keys must not be modified in any way that changes the
20  * result of DereferencingComparator.
21  */
22 template <class K, class T>
23 class indirectmap {
24 private:
25  typedef std::map<const K*, T, DereferencingComparator<const K*> > base;
26  base m;
27 public:
28  typedef typename base::iterator iterator;
29  typedef typename base::const_iterator const_iterator;
30  typedef typename base::size_type size_type;
31  typedef typename base::value_type value_type;
32 
33  // passthrough (pointer interface)
34  std::pair<iterator, bool> insert(const value_type& value) { return m.insert(value); }
35 
36  // pass address (value interface)
37  iterator find(const K& key) { return m.find(&key); }
38  const_iterator find(const K& key) const { return m.find(&key); }
39  iterator lower_bound(const K& key) { return m.lower_bound(&key); }
40  const_iterator lower_bound(const K& key) const { return m.lower_bound(&key); }
41  size_type erase(const K& key) { return m.erase(&key); }
42  size_type count(const K& key) const { return m.count(&key); }
43 
44  // passthrough
45  bool empty() const { return m.empty(); }
46  size_type size() const { return m.size(); }
47  size_type max_size() const { return m.max_size(); }
48  void clear() { m.clear(); }
49  iterator begin() { return m.begin(); }
50  iterator end() { return m.end(); }
51  const_iterator begin() const { return m.begin(); }
52  const_iterator end() const { return m.end(); }
53  const_iterator cbegin() const { return m.cbegin(); }
54  const_iterator cend() const { return m.cend(); }
55 };
56 
57 #endif // RAVEN_INDIRECTMAP_H
size_type count(const K &key) const
Definition: indirectmap.h:42
const_iterator end() const
Definition: indirectmap.h:52
const_iterator lower_bound(const K &key) const
Definition: indirectmap.h:40
const_iterator cend() const
Definition: indirectmap.h:54
const_iterator cbegin() const
Definition: indirectmap.h:53
bool empty() const
Definition: indirectmap.h:45
base::iterator iterator
Definition: indirectmap.h:28
base::value_type value_type
Definition: indirectmap.h:31
void clear()
Definition: indirectmap.h:48
iterator end()
Definition: indirectmap.h:50
base::const_iterator const_iterator
Definition: indirectmap.h:29
std::pair< iterator, bool > insert(const value_type &value)
Definition: indirectmap.h:34
const_iterator find(const K &key) const
Definition: indirectmap.h:38
iterator lower_bound(const K &key)
Definition: indirectmap.h:39
const_iterator begin() const
Definition: indirectmap.h:51
iterator begin()
Definition: indirectmap.h:49
std::map< const K *, T, DereferencingComparator< const K * > > base
Definition: indirectmap.h:25
base::size_type size_type
Definition: indirectmap.h:30
size_type max_size() const
Definition: indirectmap.h:47
size_type size() const
Definition: indirectmap.h:46
bool operator()(const T a, const T b) const
Definition: indirectmap.h:10
iterator find(const K &key)
Definition: indirectmap.h:37
size_type erase(const K &key)
Definition: indirectmap.h:41