Containers.hpp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. template< typename K, typename V >
  48. class MultiMap : public std::unordered_multimap< K, V, intl_MapHasher, std::equal_to< K > >
  49. {};
  50. #else
  51. template<typename K, typename V>
  52. class Map : public std::map< K, V >
  53. {};
  54. template<typename K, typename V>
  55. class MultiMap : public std::multimap< K, V >
  56. {};
  57. #endif
  58. template< typename K, typename V >
  59. class SortedMap : public std::map< K, V >
  60. {};
  61. template< typename V >
  62. class Vector : public std::vector< V >
  63. {
  64. public:
  65. ZT_INLINE Vector()
  66. {}
  67. template< typename I >
  68. ZT_INLINE Vector(I begin,I end) :
  69. std::vector< V >(begin, end)
  70. {}
  71. };
  72. template< typename V >
  73. class List : public std::list< V >
  74. {};
  75. #ifdef __CPP11__
  76. template< typename V >
  77. class ForwardList : public std::forward_list< V >
  78. {};
  79. #else
  80. template< typename V >
  81. class ForwardList : public std::list< V >
  82. {};
  83. #endif
  84. template< typename V >
  85. class Set : public std::set< V, std::less< V > >
  86. {};
  87. typedef std::string String;
  88. } // ZeroTier
  89. #endif