2
0

Containers.hpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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: 2025-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. template< typename V >
  30. class Vector : public std::vector< V >
  31. {
  32. public:
  33. ZT_INLINE Vector() :
  34. std::vector< V >()
  35. {}
  36. template< typename I >
  37. ZT_INLINE Vector(I begin,I end) :
  38. std::vector< V >(begin, end)
  39. {}
  40. };
  41. template< typename V >
  42. class List : public std::list< V >
  43. {};
  44. #ifdef __CPP11__
  45. struct intl_MapHasher
  46. {
  47. template< typename O >
  48. std::size_t operator()(const O &obj) const noexcept
  49. { return (std::size_t)obj.hashCode(); }
  50. std::size_t operator()(const uint64_t i) const noexcept
  51. { return (std::size_t)Utils::hash64(i ^ Utils::s_mapNonce); }
  52. std::size_t operator()(const int64_t i) const noexcept
  53. { return (std::size_t)Utils::hash64((uint64_t)i ^ Utils::s_mapNonce); }
  54. std::size_t operator()(const uint32_t i) const noexcept
  55. { return (std::size_t)Utils::hash32(i ^ (uint32_t)Utils::s_mapNonce); }
  56. std::size_t operator()(const int32_t i) const noexcept
  57. { return (std::size_t)Utils::hash32((uint32_t)i ^ (uint32_t)Utils::s_mapNonce); }
  58. };
  59. template< typename K, typename V >
  60. class Map : public std::unordered_map< K, V, intl_MapHasher >
  61. {};
  62. template< typename K, typename V >
  63. class MultiMap : public std::unordered_multimap< K, V, intl_MapHasher, std::equal_to< K > >
  64. {};
  65. #else
  66. template<typename K, typename V>
  67. class Map : public std::map< K, V >
  68. {};
  69. template<typename K, typename V>
  70. class MultiMap : public std::multimap< K, V >
  71. {};
  72. #endif
  73. template< typename K, typename V >
  74. class SortedMap : public std::map< K, V >
  75. {};
  76. #ifdef __CPP11__
  77. template< typename V >
  78. class ForwardList : public std::forward_list< V >
  79. {};
  80. #else
  81. template< typename V >
  82. class ForwardList : public std::list< V >
  83. {};
  84. #endif
  85. template< typename V >
  86. class Set : public std::set< V, std::less< V > >
  87. {};
  88. typedef std::string String;
  89. } // ZeroTier
  90. #endif