Implement merge

This commit is contained in:
Daniel James 2017-02-27 03:59:02 +00:00
parent 21a24d6cd7
commit d89aadc56c
8 changed files with 522 additions and 0 deletions

View File

@ -7,6 +7,10 @@ function echo_unordered_docs(
$name = 'unordered_'.
($equivalent_keys ? 'multi' : '').
($map ? 'map' : 'set');
// For merge....
$node_partner = 'unordered_'.
($equivalent_keys ? '' : 'multi').
($map ? 'map' : 'set');
if ($map)
{
@ -1157,6 +1161,68 @@ EOL;
the equality predieate and hash function are swapped using their copy constructors.</para>
</notes>
</method>
<method name="merge">
<template>
<template-type-parameter name="H2">
</template-type-parameter>
<template-type-parameter name="P2">
</template-type-parameter>
</template>
<parameter name="source">
<?php if ($map): ?>
<paramtype><?php echo $name; ?>&lt;Key, Mapped, H2, P2, Alloc&gt;&amp;</paramtype>
<?php else: ?>
<paramtype><?php echo $name; ?>&lt;Value, H2, P2, Alloc&gt;&amp;</paramtype>
<?php endif; ?>
</parameter>
</method>
<method name="merge">
<template>
<template-type-parameter name="H2">
</template-type-parameter>
<template-type-parameter name="P2">
</template-type-parameter>
</template>
<parameter name="source">
<?php if ($map): ?>
<paramtype><?php echo $name; ?>&lt;Key, Mapped, H2, P2, Alloc&gt;&amp;&amp;</paramtype>
<?php else: ?>
<paramtype><?php echo $name; ?>&lt;Value, H2, P2, Alloc&gt;&amp;&amp;</paramtype>
<?php endif; ?>
</parameter>
</method>
<?php /*
<method name="merge">
<template>
<template-type-parameter name="H2">
</template-type-parameter>
<template-type-parameter name="P2">
</template-type-parameter>
</template>
<parameter name="source">
<?php if ($map): ?>
<paramtype><?php echo $node_partner; ?>&lt;Key, Mapped, H2, P2, Alloc&gt;&amp;</paramtype>
<?php else: ?>
<paramtype><?php echo $node_partner; ?>&lt;Value, H2, P2, Alloc&gt;&amp;</paramtype>
<?php endif; ?>
</parameter>
</method>
<method name="merge">
<template>
<template-type-parameter name="H2">
</template-type-parameter>
<template-type-parameter name="P2">
</template-type-parameter>
</template>
<parameter name="source">
<?php if ($map): ?>
<paramtype><?php echo $node_partner; ?>&lt;Key, Mapped, H2, P2, Alloc&gt;&amp;&amp;</paramtype>
<?php else: ?>
<paramtype><?php echo $node_partner; ?>&lt;Value, H2, P2, Alloc&gt;&amp;&amp;</paramtype>
<?php endif; ?>
</parameter>
</method>
*/ ?>
</method-group>
<method-group name="observers">
<method name="hash_function" cv="const">

View File

@ -1028,6 +1028,28 @@ file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
the equality predieate and hash function are swapped using their copy constructors.</para>
</notes>
</method>
<method name="merge">
<template>
<template-type-parameter name="H2">
</template-type-parameter>
<template-type-parameter name="P2">
</template-type-parameter>
</template>
<parameter name="source">
<paramtype>unordered_set&lt;Value, H2, P2, Alloc&gt;&amp;</paramtype>
</parameter>
</method>
<method name="merge">
<template>
<template-type-parameter name="H2">
</template-type-parameter>
<template-type-parameter name="P2">
</template-type-parameter>
</template>
<parameter name="source">
<paramtype>unordered_set&lt;Value, H2, P2, Alloc&gt;&amp;&amp;</paramtype>
</parameter>
</method>
</method-group>
<method-group name="observers">
<method name="hash_function" cv="const">
@ -2382,6 +2404,28 @@ file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
the equality predieate and hash function are swapped using their copy constructors.</para>
</notes>
</method>
<method name="merge">
<template>
<template-type-parameter name="H2">
</template-type-parameter>
<template-type-parameter name="P2">
</template-type-parameter>
</template>
<parameter name="source">
<paramtype>unordered_multiset&lt;Value, H2, P2, Alloc&gt;&amp;</paramtype>
</parameter>
</method>
<method name="merge">
<template>
<template-type-parameter name="H2">
</template-type-parameter>
<template-type-parameter name="P2">
</template-type-parameter>
</template>
<parameter name="source">
<paramtype>unordered_multiset&lt;Value, H2, P2, Alloc&gt;&amp;&amp;</paramtype>
</parameter>
</method>
</method-group>
<method-group name="observers">
<method name="hash_function" cv="const">
@ -3859,6 +3903,28 @@ file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
the equality predieate and hash function are swapped using their copy constructors.</para>
</notes>
</method>
<method name="merge">
<template>
<template-type-parameter name="H2">
</template-type-parameter>
<template-type-parameter name="P2">
</template-type-parameter>
</template>
<parameter name="source">
<paramtype>unordered_map&lt;Key, Mapped, H2, P2, Alloc&gt;&amp;</paramtype>
</parameter>
</method>
<method name="merge">
<template>
<template-type-parameter name="H2">
</template-type-parameter>
<template-type-parameter name="P2">
</template-type-parameter>
</template>
<parameter name="source">
<paramtype>unordered_map&lt;Key, Mapped, H2, P2, Alloc&gt;&amp;&amp;</paramtype>
</parameter>
</method>
</method-group>
<method-group name="observers">
<method name="hash_function" cv="const">
@ -5260,6 +5326,28 @@ file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
the equality predieate and hash function are swapped using their copy constructors.</para>
</notes>
</method>
<method name="merge">
<template>
<template-type-parameter name="H2">
</template-type-parameter>
<template-type-parameter name="P2">
</template-type-parameter>
</template>
<parameter name="source">
<paramtype>unordered_multimap&lt;Key, Mapped, H2, P2, Alloc&gt;&amp;</paramtype>
</parameter>
</method>
<method name="merge">
<template>
<template-type-parameter name="H2">
</template-type-parameter>
<template-type-parameter name="P2">
</template-type-parameter>
</template>
<parameter name="source">
<paramtype>unordered_multimap&lt;Key, Mapped, H2, P2, Alloc&gt;&amp;&amp;</paramtype>
</parameter>
</method>
</method-group>
<method-group name="observers">
<method name="hash_function" cv="const">

View File

@ -3840,6 +3840,30 @@ struct table_impl : boost::unordered::detail::table<Types>
return iterator(pos);
}
template <typename Types2> void merge_impl(table_impl<Types2>& other)
{
if (other.size_) {
link_pointer prev = other.get_previous_start();
while (prev->next_) {
node_pointer n = other.next_node(prev);
const_key_type& k = this->get_key(n->value());
std::size_t key_hash = this->hash(k);
node_pointer pos = this->find_node(key_hash, k);
if (pos) {
prev = n;
} else {
this->reserve_for_insert(this->size_ + 1);
prev->next_ = n->next_;
--other.size_;
other.fix_bucket(other.hash_to_bucket(n->hash_), prev);
this->add_node(n, key_hash);
}
}
}
}
////////////////////////////////////////////////////////////////////////
// Insert range methods
//

View File

@ -617,6 +617,17 @@ template <class K, class T, class H, class P, class A> class unordered_map
void clear();
void swap(unordered_map&);
template <typename H2, typename P2>
void merge(boost::unordered_map<K, T, H2, P2, A>& source);
#if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
template <typename H2, typename P2>
void merge(boost::unordered_map<K, T, H2, P2, A>&& source);
#endif
// template <typename H2, typename P2>
// void merge(boost::unordered_multimap<K,T,H2,P2,A>& source);
// template <typename H2, typename P2>
// void merge(boost::unordered_multimap<K,T,H2,P2,A>&& source);
// observers
hasher hash_function() const;
@ -1055,6 +1066,17 @@ template <class K, class T, class H, class P, class A> class unordered_multimap
void clear();
void swap(unordered_multimap&);
template <typename H2, typename P2>
void merge(boost::unordered_multimap<K, T, H2, P2, A>& source);
#if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
template <typename H2, typename P2>
void merge(boost::unordered_multimap<K, T, H2, P2, A>&& source);
#endif
// template <typename H2, typename P2>
// void merge(boost::unordered_map<K,T,H2,P2,A>& source);
// template <typename H2, typename P2>
// void merge(boost::unordered_map<K,T,H2,P2,A>&& source);
// observers
hasher hash_function() const;
@ -1356,6 +1378,24 @@ void unordered_map<K, T, H, P, A>::swap(unordered_map& other)
table_.swap(other.table_);
}
template <class K, class T, class H, class P, class A>
template <typename H2, typename P2>
void unordered_map<K, T, H, P, A>::merge(
boost::unordered_map<K, T, H2, P2, A>& source)
{
table_.merge_impl(source.table_);
}
#if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
template <class K, class T, class H, class P, class A>
template <typename H2, typename P2>
void unordered_map<K, T, H, P, A>::merge(
boost::unordered_map<K, T, H2, P2, A>&& source)
{
table_.merge_impl(source.table_);
}
#endif
// observers
template <class K, class T, class H, class P, class A>
@ -1756,6 +1796,28 @@ unordered_multimap<K, T, H, P, A>::key_eq() const
return table_.key_eq();
}
template <class K, class T, class H, class P, class A>
template <typename H2, typename P2>
void unordered_multimap<K, T, H, P, A>::merge(
boost::unordered_multimap<K, T, H2, P2, A>& source)
{
while (!source.empty()) {
insert(source.extract(source.begin()));
}
}
#if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
template <class K, class T, class H, class P, class A>
template <typename H2, typename P2>
void unordered_multimap<K, T, H, P, A>::merge(
boost::unordered_multimap<K, T, H2, P2, A>&& source)
{
while (!source.empty()) {
insert(source.extract(source.begin()));
}
}
#endif
// lookup
template <class K, class T, class H, class P, class A>

View File

@ -391,6 +391,17 @@ template <class T, class H, class P, class A> class unordered_set
void clear();
void swap(unordered_set&);
template <typename H2, typename P2>
void merge(boost::unordered_set<T, H2, P2, A>& source);
#if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
template <typename H2, typename P2>
void merge(boost::unordered_set<T, H2, P2, A>&& source);
#endif
// template <typename H2, typename P2>
// void merge(boost::unordered_multiset<T,H2,P2,A>& source);
// template <typename H2, typename P2>
// void merge(boost::unordered_multiset<T,H2,P2,A>&& source);
// observers
hasher hash_function() const;
@ -817,6 +828,17 @@ template <class T, class H, class P, class A> class unordered_multiset
void clear();
void swap(unordered_multiset&);
template <typename H2, typename P2>
void merge(boost::unordered_multiset<T, H2, P2, A>& source);
#if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
template <typename H2, typename P2>
void merge(boost::unordered_multiset<T, H2, P2, A>&& source);
#endif
// template <typename H2, typename P2>
// void merge(boost::unordered_set<T,H2,P2,A>& source);
// template <typename H2, typename P2>
// void merge(boost::unordered_set<T,H2,P2,A>&& source);
// observers
hasher hash_function() const;
@ -1123,6 +1145,24 @@ unordered_set<T, H, P, A>::key_eq() const
return table_.key_eq();
}
template <class T, class H, class P, class A>
template <typename H2, typename P2>
void unordered_set<T, H, P, A>::merge(
boost::unordered_set<T, H2, P2, A>& source)
{
table_.merge_impl(source.table_);
}
#if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
template <class T, class H, class P, class A>
template <typename H2, typename P2>
void unordered_set<T, H, P, A>::merge(
boost::unordered_set<T, H2, P2, A>&& source)
{
table_.merge_impl(source.table_);
}
#endif
// lookup
template <class T, class H, class P, class A>
@ -1460,6 +1500,28 @@ unordered_multiset<T, H, P, A>::key_eq() const
return table_.key_eq();
}
template <class T, class H, class P, class A>
template <typename H2, typename P2>
void unordered_multiset<T, H, P, A>::merge(
boost::unordered_multiset<T, H2, P2, A>& source)
{
while (!source.empty()) {
insert(source.extract(source.begin()));
}
}
#if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
template <class T, class H, class P, class A>
template <typename H2, typename P2>
void unordered_multiset<T, H, P, A>::merge(
boost::unordered_multiset<T, H2, P2, A>&& source)
{
while (!source.empty()) {
insert(source.extract(source.begin()));
}
}
#endif
// lookup
template <class T, class H, class P, class A>

View File

@ -49,6 +49,7 @@ test-suite unordered
[ run unordered/erase_equiv_tests.cpp ]
[ run unordered/extract_tests.cpp ]
[ run unordered/node_handle_tests.cpp ]
[ run unordered/merge_tests.cpp ]
[ compile-fail unordered/insert_node_type_fail.cpp : <define>UNORDERED_TEST_MAP : insert_node_type_fail_map ]
[ compile-fail unordered/insert_node_type_fail.cpp : <define>UNORDERED_TEST_MULTIMAP : insert_node_type_fail_multimap ]
[ compile-fail unordered/insert_node_type_fail.cpp : <define>UNORDERED_TEST_SET : insert_node_type_fail_set ]

View File

@ -551,6 +551,11 @@ void unordered_test(X& x, Key& k, Hash& hf, Pred& eq)
a.max_load_factor((float)2.0);
a.rehash(100);
a.merge(a2);
#if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
a.merge(rvalue_default<X>());
#endif
// Avoid unused variable warnings:
sink(a);

View File

@ -0,0 +1,214 @@
// Copyright 2016 Daniel James.
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
#include "../helpers/postfix.hpp"
#include "../helpers/prefix.hpp"
#include <boost/unordered_map.hpp>
#include <boost/unordered_set.hpp>
#include "../helpers/count.hpp"
#include "../helpers/helpers.hpp"
#include "../helpers/invariants.hpp"
#include "../helpers/random_values.hpp"
#include "../helpers/test.hpp"
#include "../helpers/tracker.hpp"
#include "../objects/test.hpp"
#include <boost/next_prior.hpp>
namespace merge_tests {
UNORDERED_AUTO_TEST(merge_set)
{
boost::unordered_set<int> x;
boost::unordered_set<int> y;
x.merge(y);
BOOST_TEST(x.empty());
BOOST_TEST(y.empty());
x.insert(10);
x.merge(y);
BOOST_TEST(x.size() == 1);
BOOST_TEST(x.count(10) == 1);
BOOST_TEST(y.empty());
y.merge(x);
BOOST_TEST(x.empty());
BOOST_TEST(y.size() == 1);
BOOST_TEST(y.count(10) == 1);
x.insert(10);
x.insert(50);
y.insert(70);
y.insert(80);
x.merge(y);
BOOST_TEST_EQ(x.size(), 4u);
BOOST_TEST_EQ(y.size(), 1u);
BOOST_TEST_EQ(x.count(10), 1u);
BOOST_TEST_EQ(x.count(50), 1u);
BOOST_TEST_EQ(x.count(70), 1u);
BOOST_TEST_EQ(x.count(80), 1u);
BOOST_TEST_EQ(y.count(10), 1u);
BOOST_TEST_EQ(y.count(50), 0u);
BOOST_TEST_EQ(y.count(70), 0u);
BOOST_TEST_EQ(y.count(80), 0u);
test::check_equivalent_keys(x);
test::check_equivalent_keys(y);
}
UNORDERED_AUTO_TEST(merge_multiset)
{
boost::unordered_multiset<int> x;
boost::unordered_multiset<int> y;
x.merge(y);
BOOST_TEST(x.empty());
BOOST_TEST(y.empty());
x.insert(10);
x.merge(y);
BOOST_TEST(x.size() == 1);
BOOST_TEST(x.count(10) == 1);
BOOST_TEST(y.empty());
y.merge(x);
BOOST_TEST(x.empty());
BOOST_TEST(y.size() == 1);
BOOST_TEST(y.count(10) == 1);
x.insert(10);
x.insert(50);
y.insert(70);
y.insert(80);
x.merge(y);
BOOST_TEST_EQ(x.size(), 5u);
BOOST_TEST_EQ(y.size(), 0u);
BOOST_TEST_EQ(x.count(10), 2u);
BOOST_TEST_EQ(x.count(50), 1u);
BOOST_TEST_EQ(x.count(70), 1u);
BOOST_TEST_EQ(x.count(80), 1u);
BOOST_TEST_EQ(y.count(10), 0u);
BOOST_TEST_EQ(y.count(50), 0u);
BOOST_TEST_EQ(y.count(70), 0u);
BOOST_TEST_EQ(y.count(80), 0u);
test::check_equivalent_keys(x);
test::check_equivalent_keys(y);
}
template <class X> void merge_empty_test(X*, test::random_generator generator)
{
test::check_instances check_;
test::random_values<X> v(1000, generator);
X x1(v.begin(), v.end()), x2;
x1.merge(x2);
test::check_container(x1, v);
BOOST_TEST(x2.empty());
test::check_equivalent_keys(x1);
test::check_equivalent_keys(x2);
}
template <class X>
void merge_into_empty_test(X*, test::random_generator generator)
{
test::check_instances check_;
test::random_values<X> v(1000, generator);
X x1, x2(v.begin(), v.end());
x1.merge(x2);
test::check_container(x1, v);
BOOST_TEST(x2.empty());
test::check_equivalent_keys(x1);
test::check_equivalent_keys(x2);
}
template <class X> void unique_merge_test(X*, test::random_generator generator)
{
test::check_instances check_;
test::random_values<X> v1(1000, generator);
test::random_values<X> v2(1000, generator);
v1.insert(v2.begin(), boost::next(v2.begin(), 100));
v2.insert(v1.begin(), boost::next(v1.begin(), 100));
X x1(v1.begin(), v1.end()), x2(v2.begin(), v2.end());
x1.merge(x2);
test::ordered<X> tracker1 = test::create_ordered(x1);
test::ordered<X> tracker2 = test::create_ordered(x2);
test::ordered<X> tracker_tmp = test::create_ordered(x2);
tracker1.insert(v1.begin(), v1.end());
tracker_tmp.insert(v2.begin(), v2.end());
for (BOOST_DEDUCED_TYPENAME test::ordered<X>::iterator it =
tracker_tmp.begin();
it != tracker_tmp.end(); ++it) {
if (!tracker1.insert(*it).second) {
tracker2.insert(*it);
}
}
tracker1.compare(x1);
tracker2.compare(x2);
test::check_equivalent_keys(x1);
test::check_equivalent_keys(x2);
}
template <class X> void equiv_merge_test(X*, test::random_generator generator)
{
test::check_instances check_;
test::random_values<X> v1(1000, generator);
test::random_values<X> v2(1000, generator);
v1.insert(v2.begin(), boost::next(v2.begin(), 100));
v2.insert(v1.begin(), boost::next(v1.begin(), 100));
X x1(v1.begin(), v1.end()), x2(v2.begin(), v2.end());
x1.merge(x2);
test::ordered<X> tracker1 = test::create_ordered(x1);
tracker1.insert(v1.begin(), v1.end());
tracker1.insert(v2.begin(), v2.end());
tracker1.compare(x1);
BOOST_TEST(x2.empty());
test::check_equivalent_keys(x1);
test::check_equivalent_keys(x2);
}
boost::unordered_set<test::movable, test::hash, test::equal_to,
std::allocator<test::movable> >* test_set_std_alloc;
boost::unordered_multimap<test::object, test::object, test::hash,
test::equal_to, std::allocator<test::object> >* test_multimap_std_alloc;
boost::unordered_set<test::object, test::hash, test::equal_to,
test::allocator1<test::object> >* test_set;
boost::unordered_multiset<test::movable, test::hash, test::equal_to,
test::allocator2<test::movable> >* test_multiset;
boost::unordered_map<test::movable, test::movable, test::hash, test::equal_to,
test::allocator2<test::movable> >* test_map;
boost::unordered_multimap<test::object, test::object, test::hash,
test::equal_to, test::allocator1<test::object> >* test_multimap;
using test::default_generator;
using test::generate_collisions;
UNORDERED_TEST(merge_empty_test,
((test_set_std_alloc)(test_multimap_std_alloc)(test_set)(test_multiset)(
test_map)(test_multimap))((default_generator)(generate_collisions)))
UNORDERED_TEST(merge_into_empty_test,
((test_set_std_alloc)(test_multimap_std_alloc)(test_set)(test_multiset)(
test_map)(test_multimap))((default_generator)(generate_collisions)))
UNORDERED_TEST(unique_merge_test,
((test_set_std_alloc)(test_set)(test_map))((default_generator)))
UNORDERED_TEST(equiv_merge_test, ((test_multimap_std_alloc)(test_multiset)(
test_multimap))((default_generator)))
}
RUN_TESTS()