Max_Leb

Untitled

Dec 24th, 2022
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.51 KB | None | 0 0
  1. #pragma once
  2. #include <iostream>
  3. #include <map>
  4. #include <set>
  5. #include <unordered_map>
  6. #include <unordered_set>
  7.  
  8. template <typename A>
  9. struct is_set {
  10.   static constexpr bool value = false;
  11. };
  12.  
  13. template <typename A>
  14. struct is_set<std::set<A>> {
  15.   static constexpr bool value = true;
  16. };
  17.  
  18. template <typename A>
  19. struct is_multiset {
  20.   static constexpr bool value = false;
  21. };
  22.  
  23. template <typename A>
  24. struct is_multiset<std::multiset<A>> {
  25.   static constexpr bool value = true;
  26. };
  27.  
  28. template <typename A>
  29. struct is_unordered_set {
  30.   static constexpr bool value = false;
  31. };
  32.  
  33. template <typename A>
  34. struct is_unordered_set<std::unordered_set<A>> {
  35.   static constexpr bool value = true;
  36. };
  37.  
  38. template <typename A>
  39. struct is_unordered_multiset {
  40.   static constexpr bool value = false;
  41. };
  42.  
  43. template <typename A>
  44. struct is_unordered_multiset<std::unordered_multiset<A>> {
  45.   static constexpr bool value = true;
  46. };
  47.  
  48. template <typename A>
  49. struct is_map {
  50.   static constexpr bool value = false;
  51. };
  52.  
  53. template <typename A, typename B>
  54. struct is_map<std::map<A, B>> {
  55.   static constexpr bool value = true;
  56. };
  57.  
  58. template <typename A>
  59. struct is_multimap {
  60.   static constexpr bool value = false;
  61. };
  62.  
  63. template <typename A, typename B>
  64. struct is_multimap<std::multimap<A, B>> {
  65.   static constexpr bool value = true;
  66. };
  67.  
  68. template <typename A>
  69. struct is_unordered_map {
  70.   static constexpr bool value = false;
  71. };
  72.  
  73. template <typename A, typename B>
  74. struct is_unordered_map<std::unordered_map<A, B>> {
  75.   static constexpr bool value = true;
  76. };
  77.  
  78. template <typename A>
  79. struct is_unordered_multimap {
  80.   static constexpr bool value = false;
  81. };
  82.  
  83. template <typename A, typename B>
  84. struct is_unordered_multimap<std::unordered_multimap<A, B>> {
  85.   static constexpr bool value = true;
  86. };
  87.  
  88. template <typename A, typename B>
  89. struct are_associative_containers {
  90.   static constexpr bool value =
  91.       (is_set<A>::value || is_unordered_set<A>::value || is_map<A>::value ||
  92.        is_unordered_map<A>::value || is_multiset<A>::value ||
  93.        is_unordered_multiset<A>::value || is_multimap<A>::value ||
  94.        is_unordered_multimap<A>::value) &&
  95.       (is_set<B>::value || is_unordered_set<B>::value || is_map<B>::value ||
  96.        is_unordered_map<B>::value || is_multiset<B>::value ||
  97.        is_unordered_multiset<B>::value || is_multimap<B>::value ||
  98.        is_unordered_multimap<B>::value);
  99. };
  100.  
  101. template <typename A>
  102. struct is_some_map {
  103.   static constexpr bool value =
  104.       is_map<A>::value || is_unordered_map<A>::value || is_multimap<A>::value ||
  105.       is_unordered_multimap<A>::value;
  106. };
  107.  
  108. template <typename A>
  109. struct is_some_set {
  110.   static constexpr bool value =
  111.       is_set<A>::value || is_unordered_set<A>::value || is_multiset<A>::value ||
  112.       is_unordered_multiset<A>::value;
  113. };
  114.  
  115. template <typename A>
  116. struct is_multi {
  117.   static constexpr bool value =
  118.       is_multiset<A>::value || is_unordered_multiset<A>::value ||
  119.       is_multimap<A>::value || is_unordered_multimap<A>::value;
  120. };
  121.  
  122. template <class, typename = void>
  123. struct map_attribute : std::false_type {};
  124.  
  125. template <class Map>
  126. struct map_attribute<Map, std::void_t<typename Map::mapped_type>>
  127.     : std::true_type {};
  128.  
  129. template <template <typename...> class Set1, typename A,
  130.           template <typename...> class Set2, typename B,
  131.           std::enable_if_t<!map_attribute<Set1<A>>::value, int> = 0>
  132. bool compare(Set1<A> &&, const Set2<B> &&) {
  133.   return std::is_same<std::remove_cv_t<A>, std::remove_cv_t<B>>::value;
  134. }
  135.  
  136. template <template <typename...> class Map1, typename A, typename B,
  137.           template <typename...> class Map2, typename C, typename D,
  138.           std::enable_if_t<map_attribute<Map1<A, B>>::value, int> = 0>
  139. bool compare(Map1<A, B> &&, const Map2<C, D> &&) {
  140.   return std::is_same<std::remove_cv_t<A>, std::remove_cv_t<C>>::value &&
  141.          std::is_same<std::remove_cv_t<B>, std::remove_cv_t<D>>::value;
  142. }
  143.  
  144. template <class C1, class C2>
  145. bool MergeAssociative(C1 *c1, const C2 &c2) {
  146.   if constexpr ((are_associative_containers<C1, C2>::value) &&
  147.                 ((is_some_map<C1>::value && is_some_map<C2>::value) ||
  148.                  (is_some_set<C1>::value && is_some_set<C2>::value))) {
  149.     if ((is_multi<C1>::value) ||
  150.         ((!is_multi<C1>::value) && (!is_multi<C2>::value))) {
  151.       if (compare(std::remove_reference_t<C1>(*c1),
  152.                   std::remove_reference_t<C2>(c2))) {
  153.         for (auto el : c2) {
  154.           c1->insert(el);
  155.         }
  156.         return false;
  157.       }
  158.     }
  159.   }
  160.   return true;
  161. }
  162.  
Advertisement
Add Comment
Please, Sign In to add comment