You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

indirectmap.h 2.5KB

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