Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #pragma once
- #include <iostream>
- #include <map>
- #include <set>
- #include <unordered_map>
- #include <unordered_set>
- template <typename A>
- struct is_set {
- static constexpr bool value = false;
- };
- template <typename A>
- struct is_set<std::set<A>> {
- static constexpr bool value = true;
- };
- template <typename A>
- struct is_multiset {
- static constexpr bool value = false;
- };
- template <typename A>
- struct is_multiset<std::multiset<A>> {
- static constexpr bool value = true;
- };
- template <typename A>
- struct is_unordered_set {
- static constexpr bool value = false;
- };
- template <typename A>
- struct is_unordered_set<std::unordered_set<A>> {
- static constexpr bool value = true;
- };
- template <typename A>
- struct is_unordered_multiset {
- static constexpr bool value = false;
- };
- template <typename A>
- struct is_unordered_multiset<std::unordered_multiset<A>> {
- static constexpr bool value = true;
- };
- template <typename A>
- struct is_map {
- static constexpr bool value = false;
- };
- template <typename A, typename B>
- struct is_map<std::map<A, B>> {
- static constexpr bool value = true;
- };
- template <typename A>
- struct is_multimap {
- static constexpr bool value = false;
- };
- template <typename A, typename B>
- struct is_multimap<std::multimap<A, B>> {
- static constexpr bool value = true;
- };
- template <typename A>
- struct is_unordered_map {
- static constexpr bool value = false;
- };
- template <typename A, typename B>
- struct is_unordered_map<std::unordered_map<A, B>> {
- static constexpr bool value = true;
- };
- template <typename A>
- struct is_unordered_multimap {
- static constexpr bool value = false;
- };
- template <typename A, typename B>
- struct is_unordered_multimap<std::unordered_multimap<A, B>> {
- static constexpr bool value = true;
- };
- template <typename A, typename B>
- struct are_associative_containers {
- static constexpr bool value =
- (is_set<A>::value || is_unordered_set<A>::value || is_map<A>::value ||
- is_unordered_map<A>::value || is_multiset<A>::value ||
- is_unordered_multiset<A>::value || is_multimap<A>::value ||
- is_unordered_multimap<A>::value) &&
- (is_set<B>::value || is_unordered_set<B>::value || is_map<B>::value ||
- is_unordered_map<B>::value || is_multiset<B>::value ||
- is_unordered_multiset<B>::value || is_multimap<B>::value ||
- is_unordered_multimap<B>::value);
- };
- template <typename A>
- struct is_some_map {
- static constexpr bool value =
- is_map<A>::value || is_unordered_map<A>::value || is_multimap<A>::value ||
- is_unordered_multimap<A>::value;
- };
- template <typename A>
- struct is_some_set {
- static constexpr bool value =
- is_set<A>::value || is_unordered_set<A>::value || is_multiset<A>::value ||
- is_unordered_multiset<A>::value;
- };
- template <typename A>
- struct is_multi {
- static constexpr bool value =
- is_multiset<A>::value || is_unordered_multiset<A>::value ||
- is_multimap<A>::value || is_unordered_multimap<A>::value;
- };
- template <class, typename = void>
- struct map_attribute : std::false_type {};
- template <class Map>
- struct map_attribute<Map, std::void_t<typename Map::mapped_type>>
- : std::true_type {};
- template <template <typename...> class Set1, typename A,
- template <typename...> class Set2, typename B,
- std::enable_if_t<!map_attribute<Set1<A>>::value, int> = 0>
- bool compare(Set1<A> &&, const Set2<B> &&) {
- return std::is_same<std::remove_cv_t<A>, std::remove_cv_t<B>>::value;
- }
- template <template <typename...> class Map1, typename A, typename B,
- template <typename...> class Map2, typename C, typename D,
- std::enable_if_t<map_attribute<Map1<A, B>>::value, int> = 0>
- bool compare(Map1<A, B> &&, const Map2<C, D> &&) {
- return std::is_same<std::remove_cv_t<A>, std::remove_cv_t<C>>::value &&
- std::is_same<std::remove_cv_t<B>, std::remove_cv_t<D>>::value;
- }
- template <class C1, class C2>
- bool MergeAssociative(C1 *c1, const C2 &c2) {
- if constexpr ((are_associative_containers<C1, C2>::value) &&
- ((is_some_map<C1>::value && is_some_map<C2>::value) ||
- (is_some_set<C1>::value && is_some_set<C2>::value))) {
- if ((is_multi<C1>::value) ||
- ((!is_multi<C1>::value) && (!is_multi<C2>::value))) {
- if (compare(std::remove_reference_t<C1>(*c1),
- std::remove_reference_t<C2>(c2))) {
- for (auto el : c2) {
- c1->insert(el);
- }
- return false;
- }
- }
- }
- return true;
- }
Advertisement
Add Comment
Please, Sign In to add comment