Containers.hpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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_CONTAINERS_HPP
  14. #define ZT_CONTAINERS_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) { this->emplace(key, value); }
  61. };
  62. template< typename K, typename V >
  63. class MultiMap : public std::unordered_multimap< K, V, intl_MapHasher, std::equal_to< K >, Utils::Mallocator < std::pair< const K, V > > >
  64. {};
  65. #else
  66. template<typename K,typename V>
  67. class Map : public std::map< K,V,std::less<K>,Utils::Mallocator< std::pair<const K,V> > >
  68. {
  69. public:
  70. ZT_INLINE V *get(const K &key) noexcept
  71. {
  72. typename Map::iterator i(this->find(key));
  73. if (i == this->end())
  74. return nullptr;
  75. return &(i->second);
  76. }
  77. ZT_INLINE const V *get(const K &key) const noexcept
  78. {
  79. typename Map::const_iterator i(this->find(key));
  80. if (i == this->end())
  81. return nullptr;
  82. return &(i->second);
  83. }
  84. ZT_INLINE void set(const K &key,const V &value)
  85. { (*this)[key] = value; }
  86. };
  87. template<typename K,typename V>
  88. class MultiMap : public std::multimap< K,V,std::less<K>,Utils::Mallocator< std::pair<const K,V> > >
  89. {
  90. };
  91. #endif
  92. template< typename K, typename V >
  93. class SortedMap : public std::map< K, V, std::less< K >, Utils::Mallocator < std::pair< const K, V > > >
  94. {
  95. public:
  96. ZT_INLINE V *get(const K &key) noexcept
  97. {
  98. typename SortedMap::iterator i(this->find(key));
  99. if (i == this->end())
  100. return nullptr;
  101. return &(i->second);
  102. }
  103. ZT_INLINE const V *get(const K &key) const noexcept
  104. {
  105. typename SortedMap::const_iterator i(this->find(key));
  106. if (i == this->end())
  107. return nullptr;
  108. return &(i->second);
  109. }
  110. ZT_INLINE void set(const K &key, const V &value) { (*this)[key] = value; }
  111. };
  112. template< typename V >
  113. class Vector : public std::vector< V, Utils::Mallocator < V > >
  114. {
  115. public:
  116. ZT_INLINE Vector() {}
  117. template< typename I >
  118. ZT_INLINE Vector(I begin,I end) : std::vector< V, Utils::Mallocator < V > >(begin,end) {}
  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. ZT_INLINE String(const String &s) : std::basic_string< char, std::char_traits< char >, Utils::Mallocator < char > >(s.c_str()) {}
  133. ZT_INLINE String(const std::string &s) : std::basic_string< char, std::char_traits< char >, Utils::Mallocator < char > >(s.c_str()) {}
  134. ZT_INLINE String(const char *const s) : std::basic_string< char, std::char_traits< char >, Utils::Mallocator < char > >(s) {}
  135. ZT_INLINE String &operator=(const char *const s) { assign(s); return *this; }
  136. ZT_INLINE String &operator=(const std::string &s) { assign(s.c_str()); return *this; }
  137. };
  138. } // ZeroTier
  139. #endif