Map.hpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /*
  2. * Copyright (c)2013-2020 ZeroTier, Inc.
  3. *
  4. * Use of this software is governed by the Business Source License included
  5. * in the LICENSE.TXT file in the project's root directory.
  6. *
  7. * Change Date: 2024-01-01
  8. *
  9. * On the date above, in accordance with the Business Source License, use
  10. * of this software will be governed by version 2.0 of the Apache License.
  11. */
  12. /****/
  13. #ifndef ZT_MAP_HPP
  14. #define ZT_MAP_HPP
  15. /*
  16. * This wraps std::unordered_map (or std::map if that is not available) and gives
  17. * it a few extra methods. It also uses the built-in hashCode methods in key objects
  18. * in ZeroTier instead of requiring hashers all over the place.
  19. */
  20. #include "Constants.hpp"
  21. #include "Utils.hpp"
  22. #ifdef __CPP11__
  23. #include <unordered_map>
  24. #else
  25. #include <map>
  26. #endif
  27. namespace ZeroTier {
  28. #ifdef __CPP11__
  29. struct _MapHasher
  30. {
  31. template<typename O>
  32. std::size_t operator()(const O &obj) const noexcept { return (std::size_t)obj.hashCode() ^ (std::size_t)Utils::s_mapNonce; }
  33. std::size_t operator()(const uint64_t i) const noexcept { return (std::size_t)Utils::hash64(i ^ Utils::s_mapNonce); }
  34. std::size_t operator()(const int64_t i) const noexcept { return (std::size_t)Utils::hash64((uint64_t)i ^ Utils::s_mapNonce); }
  35. std::size_t operator()(const uint32_t i) const noexcept { return (std::size_t)Utils::hash32(i ^ (uint32_t)Utils::s_mapNonce); }
  36. std::size_t operator()(const int32_t i) const noexcept { return (std::size_t)Utils::hash32((uint32_t)i ^ (uint32_t)Utils::s_mapNonce); }
  37. };
  38. template<typename K,typename V>
  39. class Map : public std::unordered_map<K,V,_MapHasher>
  40. {
  41. public:
  42. ZT_INLINE V *get(const K &key) noexcept
  43. {
  44. typename Map::iterator i(this->find(key));
  45. if (i == this->end())
  46. return nullptr;
  47. return &(i->second);
  48. }
  49. ZT_INLINE const V *get(const K &key) const noexcept
  50. {
  51. typename Map::const_iterator i(this->find(key));
  52. if (i == this->end())
  53. return nullptr;
  54. return &(i->second);
  55. }
  56. ZT_INLINE void set(const K &key,const V &value)
  57. {
  58. this->emplace(key,value);
  59. }
  60. };
  61. #else
  62. template<typename K,typename V>
  63. class Map : public std::map<K,V>
  64. {
  65. public:
  66. ZT_INLINE V *get(const K &key) noexcept
  67. {
  68. typename Map::iterator i(this->find(key));
  69. if (i == this->end())
  70. return nullptr;
  71. return &(i->second);
  72. }
  73. ZT_INLINE const V *get(const K &key) const noexcept
  74. {
  75. typename Map::const_iterator i(this->find(key));
  76. if (i == this->end())
  77. return nullptr;
  78. return &(i->second);
  79. }
  80. ZT_INLINE void set(const K &key,const V &value)
  81. {
  82. (*this)[key] = value;
  83. }
  84. };
  85. #endif
  86. } // ZeroTier
  87. #endif