Containers.hpp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. {}
  35. template< typename I >
  36. ZT_INLINE Vector(I begin,I end) :
  37. std::vector< V >(begin, end)
  38. {}
  39. };
  40. template< typename V >
  41. class List : public std::list< V >
  42. {};
  43. #ifdef __CPP11__
  44. struct intl_MapHasher
  45. {
  46. template< typename O >
  47. std::size_t operator()(const O &obj) const noexcept
  48. { return (std::size_t)obj.hashCode(); }
  49. std::size_t operator()(const uint64_t i) const noexcept
  50. { return (std::size_t)Utils::hash64(i ^ Utils::s_mapNonce); }
  51. std::size_t operator()(const int64_t i) const noexcept
  52. { return (std::size_t)Utils::hash64((uint64_t)i ^ Utils::s_mapNonce); }
  53. std::size_t operator()(const uint32_t i) const noexcept
  54. { return (std::size_t)Utils::hash32(i ^ (uint32_t)Utils::s_mapNonce); }
  55. std::size_t operator()(const int32_t i) const noexcept
  56. { return (std::size_t)Utils::hash32((uint32_t)i ^ (uint32_t)Utils::s_mapNonce); }
  57. };
  58. template< typename K, typename V >
  59. class Map : public std::unordered_map< K, V, intl_MapHasher >
  60. {};
  61. template< typename K, typename V >
  62. class MultiMap : public std::unordered_multimap< K, V, intl_MapHasher, std::equal_to< K > >
  63. {};
  64. #else
  65. template<typename K, typename V>
  66. class Map : public std::map< K, V >
  67. {};
  68. template<typename K, typename V>
  69. class MultiMap : public std::multimap< K, V >
  70. {};
  71. #endif
  72. template< typename K, typename V >
  73. class SortedMap : public std::map< K, 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