Containers.hpp 3.3 KB

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