Containers.hpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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 { return (std::size_t)obj.hashCode(); }
  32. std::size_t operator()(const uint64_t i) const noexcept { return (std::size_t)Utils::hash64(i + Utils::s_mapNonce); }
  33. std::size_t operator()(const int64_t i) const noexcept { return (std::size_t)Utils::hash64((uint64_t)i + Utils::s_mapNonce); }
  34. std::size_t operator()(const uint32_t i) const noexcept { return (std::size_t)Utils::hash32(i + (uint32_t)Utils::s_mapNonce); }
  35. std::size_t operator()(const int32_t i) const noexcept { return (std::size_t)Utils::hash32((uint32_t)i + (uint32_t)Utils::s_mapNonce); }
  36. };
  37. template<typename K,typename V>
  38. class Map : public std::unordered_map< K,V,intl_MapHasher,std::equal_to<K>,Utils::Mallocator< std::pair<const K,V> > >
  39. {
  40. public:
  41. ZT_INLINE V *get(const K &key) noexcept
  42. {
  43. typename Map::iterator i(this->find(key));
  44. if (i == this->end())
  45. return nullptr;
  46. return &(i->second);
  47. }
  48. ZT_INLINE const V *get(const K &key) const noexcept
  49. {
  50. typename Map::const_iterator i(this->find(key));
  51. if (i == this->end())
  52. return nullptr;
  53. return &(i->second);
  54. }
  55. ZT_INLINE void set(const K &key,const V &value)
  56. {
  57. this->emplace(key,value);
  58. }
  59. };
  60. template<typename K,typename V>
  61. class MultiMap : public std::unordered_multimap< K,V,intl_MapHasher,std::equal_to<K>,Utils::Mallocator< std::pair<const K,V> > >
  62. {
  63. };
  64. #else
  65. template<typename K,typename V>
  66. class Map : public std::map< K,V,std::less<K>,Utils::Mallocator< std::pair<const K,V> > >
  67. {
  68. public:
  69. ZT_INLINE V *get(const K &key) noexcept
  70. {
  71. typename Map::iterator i(this->find(key));
  72. if (i == this->end())
  73. return nullptr;
  74. return &(i->second);
  75. }
  76. ZT_INLINE const V *get(const K &key) const noexcept
  77. {
  78. typename Map::const_iterator i(this->find(key));
  79. if (i == this->end())
  80. return nullptr;
  81. return &(i->second);
  82. }
  83. ZT_INLINE void set(const K &key,const V &value)
  84. {
  85. (*this)[key] = value;
  86. }
  87. };
  88. template<typename K,typename V>
  89. class MultiMap : public std::multimap< K,V,std::less<K>,Utils::Mallocator< std::pair<const K,V> > >
  90. {
  91. };
  92. #endif
  93. template<typename K,typename V>
  94. class SortedMap : public std::map< K,V,std::less<K>,Utils::Mallocator< std::pair<const K,V> > >
  95. {
  96. public:
  97. ZT_INLINE V *get(const K &key) noexcept
  98. {
  99. typename SortedMap::iterator i(this->find(key));
  100. if (i == this->end())
  101. return nullptr;
  102. return &(i->second);
  103. }
  104. ZT_INLINE const V *get(const K &key) const noexcept
  105. {
  106. typename SortedMap::const_iterator i(this->find(key));
  107. if (i == this->end())
  108. return nullptr;
  109. return &(i->second);
  110. }
  111. ZT_INLINE void set(const K &key,const V &value)
  112. {
  113. (*this)[key] = value;
  114. }
  115. };
  116. template<typename V>
  117. class Vector : public std::vector< V,Utils::Mallocator<V> >
  118. {
  119. };
  120. template<typename V>
  121. class List : public std::list< V,Utils::Mallocator<V> >
  122. {
  123. };
  124. template<typename V>
  125. class Set : public std::set< V,std::less<V>,Utils::Mallocator<V> >
  126. {
  127. };
  128. class String : public std::basic_string< char,std::char_traits<char>,Utils::Mallocator<char> >
  129. {
  130. public:
  131. ZT_INLINE String() {}
  132. explicit ZT_INLINE String(const char *const s) { assign(s); }
  133. ZT_INLINE String &operator=(const char *const s) { assign(s); return *this; }
  134. };
  135. } // ZeroTier
  136. #endif