Containers.hpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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 _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,_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. #else
  61. template<typename K,typename V>
  62. class Map : public std::map< K,V,std::less<K>,Utils::Mallocator< std::pair<const K,V> > >
  63. {
  64. public:
  65. ZT_INLINE V *get(const K &key) noexcept
  66. {
  67. typename Map::iterator i(this->find(key));
  68. if (i == this->end())
  69. return nullptr;
  70. return &(i->second);
  71. }
  72. ZT_INLINE const V *get(const K &key) const noexcept
  73. {
  74. typename Map::const_iterator i(this->find(key));
  75. if (i == this->end())
  76. return nullptr;
  77. return &(i->second);
  78. }
  79. ZT_INLINE void set(const K &key,const V &value)
  80. {
  81. (*this)[key] = value;
  82. }
  83. };
  84. #endif
  85. template<typename K,typename V>
  86. class SortedMap : public std::map< K,V,std::less<K>,Utils::Mallocator< std::pair<const K,V> > >
  87. {
  88. public:
  89. ZT_INLINE V *get(const K &key) noexcept
  90. {
  91. typename SortedMap::iterator i(this->find(key));
  92. if (i == this->end())
  93. return nullptr;
  94. return &(i->second);
  95. }
  96. ZT_INLINE const V *get(const K &key) const noexcept
  97. {
  98. typename SortedMap::const_iterator i(this->find(key));
  99. if (i == this->end())
  100. return nullptr;
  101. return &(i->second);
  102. }
  103. ZT_INLINE void set(const K &key,const V &value)
  104. {
  105. (*this)[key] = value;
  106. }
  107. };
  108. template<typename V>
  109. class Vector : public std::vector< V,Utils::Mallocator<V> >
  110. {
  111. };
  112. template<typename V>
  113. class List : public std::list< V,Utils::Mallocator<V> >
  114. {
  115. };
  116. template<typename V>
  117. class Set : public std::set< V,std::less<V>,Utils::Mallocator<V> >
  118. {
  119. };
  120. class String : public std::basic_string< char,std::char_traits<char>,Utils::Mallocator<char> >
  121. {
  122. public:
  123. explicit ZT_INLINE String(const char *const s) { assign(s); }
  124. };
  125. } // ZeroTier
  126. #endif