Containers.hpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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. #include <string>
  26. namespace ZeroTier {
  27. #ifdef __CPP11__
  28. struct intl_MapHasher
  29. {
  30. template< typename O >
  31. std::size_t operator()(const O &obj) const noexcept
  32. { return (std::size_t)obj.hashCode(); }
  33. std::size_t operator()(const uint64_t i) const noexcept
  34. { return (std::size_t)Utils::hash64(i + Utils::s_mapNonce); }
  35. std::size_t operator()(const int64_t i) const noexcept
  36. { return (std::size_t)Utils::hash64((uint64_t)i + Utils::s_mapNonce); }
  37. std::size_t operator()(const uint32_t i) const noexcept
  38. { return (std::size_t)Utils::hash32(i + (uint32_t)Utils::s_mapNonce); }
  39. std::size_t operator()(const int32_t i) const noexcept
  40. { return (std::size_t)Utils::hash32((uint32_t)i + (uint32_t)Utils::s_mapNonce); }
  41. };
  42. template< typename K, typename V >
  43. class Map : public std::unordered_map< K, V, intl_MapHasher, std::equal_to< K >, Utils::Mallocator < std::pair< const K, V > >
  44. > {
  45. public:
  46. ZT_INLINE V *get(const K &key) noexcept
  47. {
  48. typename Map::iterator i(this->find(key));
  49. if (i == this->end())
  50. return nullptr;
  51. return &(i->second);
  52. }
  53. ZT_INLINE const V *get(const K &key) const noexcept
  54. {
  55. typename Map::const_iterator i(this->find(key));
  56. if (i == this->end())
  57. return nullptr;
  58. return &(i->second);
  59. }
  60. ZT_INLINE void set(const K &key, const V &value)
  61. { this->emplace(key, value); }
  62. };
  63. template< typename K, typename V >
  64. class MultiMap : public std::unordered_multimap< K, V, intl_MapHasher, std::equal_to< K >, Utils::Mallocator < std::pair< const K, V > >
  65. >
  66. {
  67. };
  68. #else
  69. template<typename K,typename V>
  70. class Map : public std::map< K,V,std::less<K>,Utils::Mallocator< std::pair<const K,V> > >
  71. {
  72. public:
  73. ZT_INLINE V *get(const K &key) noexcept
  74. {
  75. typename Map::iterator i(this->find(key));
  76. if (i == this->end())
  77. return nullptr;
  78. return &(i->second);
  79. }
  80. ZT_INLINE const V *get(const K &key) const noexcept
  81. {
  82. typename Map::const_iterator i(this->find(key));
  83. if (i == this->end())
  84. return nullptr;
  85. return &(i->second);
  86. }
  87. ZT_INLINE void set(const K &key,const V &value)
  88. { (*this)[key] = value; }
  89. };
  90. template<typename K,typename V>
  91. class MultiMap : public std::multimap< K,V,std::less<K>,Utils::Mallocator< std::pair<const K,V> > >
  92. {
  93. };
  94. #endif
  95. template< typename K, typename V >
  96. class SortedMap : public std::map< K, V, std::less< K >, Utils::Mallocator < std::pair< const K, V > >
  97. >
  98. {
  99. public:
  100. ZT_INLINE V *get(const K &key) noexcept
  101. {
  102. typename SortedMap::iterator i(this->find(key));
  103. if (i == this->end())
  104. return nullptr;
  105. return &(i->second);
  106. }
  107. ZT_INLINE const V *get(const K &key) const noexcept
  108. {
  109. typename SortedMap::const_iterator i(this->find(key));
  110. if (i == this->end())
  111. return nullptr;
  112. return &(i->second);
  113. }
  114. ZT_INLINE void set(const K &key, const V &value)
  115. { (*this)[key] = value; }
  116. };
  117. template< typename V >
  118. class Vector : public std::vector< V, Utils::Mallocator < V >
  119. >
  120. {
  121. public:
  122. ZT_INLINE Vector()
  123. {}
  124. template< typename I >
  125. ZT_INLINE Vector(I
  126. begin,
  127. I end
  128. ) : std::vector< V, Utils::Mallocator < V > >(begin,end) {
  129. }
  130. };
  131. template< typename V >
  132. class List : public std::list< V, Utils::Mallocator < V >
  133. >
  134. {
  135. };
  136. template< typename V >
  137. class Set : public std::set< V, std::less< V >, Utils::Mallocator < V >
  138. >
  139. {
  140. };
  141. class String : public std::basic_string< char, std::char_traits< char >, Utils::Mallocator < char >
  142. >
  143. {
  144. public:
  145. ZT_INLINE String()
  146. {}
  147. ZT_INLINE String(const String &s) : std::basic_string< char, std::char_traits< char >, Utils::Mallocator < char >
  148. >(s.
  149. c_str()
  150. ) {
  151. }
  152. ZT_INLINE String(const std::string &s) : std::basic_string< char, std::char_traits< char >, Utils::Mallocator < char >
  153. >(s.
  154. c_str()
  155. ) {
  156. }
  157. ZT_INLINE String(const char *const s) : std::basic_string< char, std::char_traits< char >, Utils::Mallocator < char >
  158. >(s) {
  159. }
  160. ZT_INLINE String &operator=(const char *const s)
  161. {
  162. assign(s);
  163. return *this;
  164. }
  165. ZT_INLINE String &operator=(const std::string &s)
  166. {
  167. assign(s.c_str());
  168. return *this;
  169. }
  170. };
  171. } // ZeroTier
  172. #endif