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