Containers.hpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. /* This defines a Map, SortedMap, Vector, etc. based on STL templates. */
  16. #include "Constants.hpp"
  17. #include "Utils.hpp"
  18. #ifdef __CPP11__
  19. #include <unordered_map>
  20. #endif
  21. #include <map>
  22. #include <vector>
  23. #include <list>
  24. #include <set>
  25. namespace ZeroTier {
  26. #ifdef __CPP11__
  27. struct _MapHasher
  28. {
  29. template<typename O>
  30. std::size_t operator()(const O &obj) const noexcept { return (std::size_t)obj.hashCode(); }
  31. std::size_t operator()(const uint64_t i) const noexcept { return (std::size_t)Utils::hash64(i ^ Utils::s_mapNonce); }
  32. std::size_t operator()(const int64_t i) const noexcept { return (std::size_t)Utils::hash64((uint64_t)i ^ Utils::s_mapNonce); }
  33. std::size_t operator()(const uint32_t i) const noexcept { return (std::size_t)Utils::hash32(i ^ (uint32_t)Utils::s_mapNonce); }
  34. std::size_t operator()(const int32_t i) const noexcept { return (std::size_t)Utils::hash32((uint32_t)i ^ (uint32_t)Utils::s_mapNonce); }
  35. };
  36. template<typename K,typename V>
  37. class Map : public std::unordered_map< K,V,_MapHasher,std::equal_to<K>,Utils::Mallocator< std::pair<const K,V> > >
  38. {
  39. public:
  40. ZT_INLINE V *get(const K &key) noexcept
  41. {
  42. typename Map::iterator i(this->find(key));
  43. if (i == this->end())
  44. return nullptr;
  45. return &(i->second);
  46. }
  47. ZT_INLINE const V *get(const K &key) const noexcept
  48. {
  49. typename Map::const_iterator i(this->find(key));
  50. if (i == this->end())
  51. return nullptr;
  52. return &(i->second);
  53. }
  54. ZT_INLINE void set(const K &key,const V &value)
  55. {
  56. this->emplace(key,value);
  57. }
  58. };
  59. #else
  60. template<typename K,typename V>
  61. class Map : public std::map< K,V,std::less<K>,Utils::Mallocator< std::pair<const K,V> > >
  62. {
  63. public:
  64. ZT_INLINE V *get(const K &key) noexcept
  65. {
  66. typename Map::iterator i(this->find(key));
  67. if (i == this->end())
  68. return nullptr;
  69. return &(i->second);
  70. }
  71. ZT_INLINE const V *get(const K &key) const noexcept
  72. {
  73. typename Map::const_iterator i(this->find(key));
  74. if (i == this->end())
  75. return nullptr;
  76. return &(i->second);
  77. }
  78. ZT_INLINE void set(const K &key,const V &value)
  79. {
  80. (*this)[key] = value;
  81. }
  82. };
  83. #endif
  84. template<typename K,typename V>
  85. class SortedMap : public std::map< K,V,std::less<K>,Utils::Mallocator< std::pair<const K,V> > >
  86. {
  87. public:
  88. ZT_INLINE V *get(const K &key) noexcept
  89. {
  90. typename SortedMap::iterator i(this->find(key));
  91. if (i == this->end())
  92. return nullptr;
  93. return &(i->second);
  94. }
  95. ZT_INLINE const V *get(const K &key) const noexcept
  96. {
  97. typename SortedMap::const_iterator i(this->find(key));
  98. if (i == this->end())
  99. return nullptr;
  100. return &(i->second);
  101. }
  102. ZT_INLINE void set(const K &key,const V &value)
  103. {
  104. (*this)[key] = value;
  105. }
  106. };
  107. template<typename V>
  108. class Vector : public std::vector< V,Utils::Mallocator<V> >
  109. {
  110. };
  111. template<typename V>
  112. class List : public std::list< V,Utils::Mallocator<V> >
  113. {
  114. };
  115. template<typename V>
  116. class Set : public std::set< V,std::less<V>,Utils::Mallocator<V> >
  117. {
  118. };
  119. } // ZeroTier
  120. #endif