Bitcoin ABC  0.29.2
P2P Digital Currency
indirectmap.h
Go to the documentation of this file.
1 // Copyright (c) 2016 The Bitcoin Core developers
2 // Distributed under the MIT software license, see the accompanying
3 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
4 
5 #ifndef BITCOIN_INDIRECTMAP_H
6 #define BITCOIN_INDIRECTMAP_H
7 
8 #include <map>
9 
10 template <class T> struct DereferencingComparator {
11  bool operator()(const T a, const T b) const { return *a < *b; }
12 };
13 
26 template <class K, class T> class indirectmap {
27 private:
28  typedef std::map<const K *, T, DereferencingComparator<const K *>> base;
30 
31 public:
32  typedef typename base::iterator iterator;
33  typedef typename base::const_iterator const_iterator;
34  typedef typename base::size_type size_type;
35  typedef typename base::value_type value_type;
36 
37  // passthrough (pointer interface)
38  std::pair<iterator, bool> insert(const value_type &value) {
39  return m.insert(value);
40  }
41 
42  // pass address (value interface)
43  iterator find(const K &key) { return m.find(&key); }
44  const_iterator find(const K &key) const { return m.find(&key); }
45  iterator lower_bound(const K &key) { return m.lower_bound(&key); }
46  const_iterator lower_bound(const K &key) const {
47  return m.lower_bound(&key);
48  }
49  size_type erase(const K &key) { return m.erase(&key); }
50  size_type count(const K &key) const { return m.count(&key); }
51 
52  // passthrough
53  bool empty() const { return m.empty(); }
54  size_type size() const { return m.size(); }
55  size_type max_size() const { return m.max_size(); }
56  void clear() { m.clear(); }
57  iterator begin() { return m.begin(); }
58  iterator end() { return m.end(); }
59  const_iterator begin() const { return m.begin(); }
60  const_iterator end() const { return m.end(); }
61  const_iterator cbegin() const { return m.cbegin(); }
62  const_iterator cend() const { return m.cend(); }
63 };
64 
65 #endif // BITCOIN_INDIRECTMAP_H
Map whose keys are pointers, but are compared by their dereferenced values.
Definition: indirectmap.h:26
iterator lower_bound(const K &key)
Definition: indirectmap.h:45
const_iterator cbegin() const
Definition: indirectmap.h:61
std::pair< iterator, bool > insert(const value_type &value)
Definition: indirectmap.h:38
base::const_iterator const_iterator
Definition: indirectmap.h:33
bool empty() const
Definition: indirectmap.h:53
iterator begin()
Definition: indirectmap.h:57
size_type size() const
Definition: indirectmap.h:54
base::iterator iterator
Definition: indirectmap.h:32
iterator find(const K &key)
Definition: indirectmap.h:43
const_iterator cend() const
Definition: indirectmap.h:62
const_iterator lower_bound(const K &key) const
Definition: indirectmap.h:46
size_type max_size() const
Definition: indirectmap.h:55
size_type erase(const K &key)
Definition: indirectmap.h:49
base::value_type value_type
Definition: indirectmap.h:35
std::map< const K *, T, DereferencingComparator< const K * > > base
Definition: indirectmap.h:28
const_iterator end() const
Definition: indirectmap.h:60
iterator end()
Definition: indirectmap.h:58
base::size_type size_type
Definition: indirectmap.h:34
size_type count(const K &key) const
Definition: indirectmap.h:50
void clear()
Definition: indirectmap.h:56
const_iterator find(const K &key) const
Definition: indirectmap.h:44
const_iterator begin() const
Definition: indirectmap.h:59
bool operator()(const T a, const T b) const
Definition: indirectmap.h:11