Containers.hpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. #endif
  27. namespace ZeroTier {
  28. #ifdef __CPP11__
  29. struct intl_MapHasher
  30. {
  31. template< typename O >
  32. std::size_t operator()(const O &obj) const noexcept
  33. { return (std::size_t)obj.hashCode(); }
  34. std::size_t operator()(const uint64_t i) const noexcept
  35. { return (std::size_t)Utils::hash64(i + Utils::s_mapNonce); }
  36. std::size_t operator()(const int64_t i) const noexcept
  37. { return (std::size_t)Utils::hash64((uint64_t)i + Utils::s_mapNonce); }
  38. std::size_t operator()(const uint32_t i) const noexcept
  39. { return (std::size_t)Utils::hash32(i + (uint32_t)Utils::s_mapNonce); }
  40. std::size_t operator()(const int32_t i) const noexcept
  41. { return (std::size_t)Utils::hash32((uint32_t)i + (uint32_t)Utils::s_mapNonce); }
  42. };
  43. template< typename K, typename V >
  44. class Map : public std::unordered_map< K, V, intl_MapHasher >
  45. {
  46. public:
  47. ZT_INLINE V *get(const K &key) noexcept
  48. {
  49. typename Map::iterator i(this->find(key));
  50. if (i == this->end())
  51. return nullptr;
  52. return &(i->second);
  53. }
  54. ZT_INLINE const V *get(const K &key) const noexcept
  55. {
  56. typename Map::const_iterator i(this->find(key));
  57. if (i == this->end())
  58. return nullptr;
  59. return &(i->second);
  60. }
  61. ZT_INLINE void set(const K &key, const V &value) { 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 > >
  65. {};
  66. #else
  67. template<typename K,typename V>
  68. class Map : public std::map< K,V,std::less<K> >
  69. {
  70. public:
  71. ZT_INLINE V *get(const K &key) noexcept
  72. {
  73. typename Map::iterator i(this->find(key));
  74. if (i == this->end())
  75. return nullptr;
  76. return &(i->second);
  77. }
  78. ZT_INLINE const V *get(const K &key) const noexcept
  79. {
  80. typename Map::const_iterator i(this->find(key));
  81. if (i == this->end())
  82. return nullptr;
  83. return &(i->second);
  84. }
  85. ZT_INLINE void set(const K &key,const V &value)
  86. { (*this)[key] = value; }
  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 >
  95. {};
  96. template< typename V >
  97. class Vector : public std::vector< V >
  98. {
  99. public:
  100. ZT_INLINE Vector()
  101. {}
  102. template< typename I >
  103. ZT_INLINE Vector(I begin,I end) :
  104. std::vector< V >(begin, end)
  105. {}
  106. };
  107. template< typename V >
  108. class List : public std::list< V >
  109. {};
  110. template< typename V >
  111. class Set : public std::set< V, std::less< V > >
  112. {};
  113. typedef std::string String;
  114. } // ZeroTier
  115. #endif