map_difference: Parametrize value comparison
If map value type is lw_shared_ptr<T>, for example, we need to be able to pass our own value comparison function to the difference() function. Signed-off-by: Pekka Enberg <penberg@cloudius-systems.com>
This commit is contained in:
@@ -559,7 +559,9 @@ std::vector<const char*> ALL { KEYSPACES, COLUMNFAMILIES, COLUMNS, TRIGGERS, USE
|
||||
* that means that a keyspace had been recreated and dropped, and the recreated keyspace had never found a way
|
||||
* to this node
|
||||
*/
|
||||
auto diff = difference(before, after);
|
||||
auto diff = difference(before, after, [](const lw_shared_ptr<query::result_set>& x, const lw_shared_ptr<query::result_set>& y) -> bool {
|
||||
return *x == *y;
|
||||
});
|
||||
|
||||
for (auto&& entry : diff.entries_only_on_right) {
|
||||
if (!entry.second->empty()) {
|
||||
|
||||
@@ -39,12 +39,19 @@ struct map_difference {
|
||||
{ }
|
||||
};
|
||||
|
||||
template<typename Key, typename Tp, typename Compare, typename Alloc>
|
||||
template<typename Key,
|
||||
typename Tp,
|
||||
typename Compare = std::less<Key>,
|
||||
typename Eq = std::equal_to<Tp>,
|
||||
typename Alloc>
|
||||
inline
|
||||
map_difference<Key, Tp, Compare, Alloc>
|
||||
difference(const std::map<Key, Tp, Compare, Alloc>& left,
|
||||
const std::map<Key, Tp, Compare, Alloc>& right,
|
||||
const Compare& key_comp, const Alloc& alloc) {
|
||||
Compare key_comp,
|
||||
Eq equals = Eq(),
|
||||
Alloc alloc = Alloc())
|
||||
{
|
||||
map_difference<Key, Tp, Compare, Alloc> diff{key_comp, alloc};
|
||||
diff.entries_only_on_right = right;
|
||||
for (auto&& kv : left) {
|
||||
@@ -52,9 +59,9 @@ difference(const std::map<Key, Tp, Compare, Alloc>& left,
|
||||
auto&& it = right.find(left_key);
|
||||
if (it != right.end()) {
|
||||
diff.entries_only_on_right.erase(left_key);
|
||||
auto&& left_value = kv.second;
|
||||
auto&& right_value = it->second;
|
||||
if (left_value == right_value) {
|
||||
const Tp& left_value = kv.second;
|
||||
const Tp& right_value = it->second;
|
||||
if (equals(left_value, right_value)) {
|
||||
diff.entries_in_common.emplace(kv);
|
||||
} else {
|
||||
value_difference<Tp> value_diff{left_value, right_value};
|
||||
@@ -67,9 +74,9 @@ difference(const std::map<Key, Tp, Compare, Alloc>& left,
|
||||
return diff;
|
||||
}
|
||||
|
||||
template<typename Key, typename Tp, typename Compare, typename Alloc>
|
||||
template<typename Key, typename Tp, typename Compare, typename Eq, typename Alloc>
|
||||
inline
|
||||
map_difference<Key, Tp, Compare, Alloc>
|
||||
difference(const std::map<Key, Tp, Compare, Alloc>& left, const std::map<Key, Tp, Compare, Alloc>& right) {
|
||||
return difference(left, right, left.key_comp(), left.get_allocator());
|
||||
difference(const std::map<Key, Tp, Compare, Alloc>& left, const std::map<Key, Tp, Compare, Alloc>& right, Eq equals) {
|
||||
return difference(left, right, left.key_comp(), equals);
|
||||
}
|
||||
|
||||
@@ -17,7 +17,9 @@ BOOST_AUTO_TEST_CASE(both_empty) {
|
||||
map<int, int> left;
|
||||
map<int, int> right;
|
||||
|
||||
auto diff = difference(left, right);
|
||||
auto diff = difference(left, right, [](int x, int y) -> bool {
|
||||
return x == y;
|
||||
});
|
||||
|
||||
BOOST_REQUIRE(diff.entries_only_on_left.empty());
|
||||
BOOST_REQUIRE(diff.entries_only_on_right.empty());
|
||||
@@ -32,7 +34,9 @@ BOOST_AUTO_TEST_CASE(left_empty) {
|
||||
right.emplace(1, 100);
|
||||
right.emplace(2, 200);
|
||||
|
||||
auto diff = difference(left, right);
|
||||
auto diff = difference(left, right, [](int x, int y) -> bool {
|
||||
return x == y;
|
||||
});
|
||||
|
||||
BOOST_REQUIRE(diff.entries_only_on_left.empty());
|
||||
BOOST_REQUIRE(diff.entries_only_on_right == right);
|
||||
@@ -47,7 +51,9 @@ BOOST_AUTO_TEST_CASE(right_empty) {
|
||||
left.emplace(1, 100);
|
||||
left.emplace(2, 200);
|
||||
|
||||
auto diff = difference(left, right);
|
||||
auto diff = difference(left, right, [](int x, int y) -> bool {
|
||||
return x == y;
|
||||
});
|
||||
|
||||
BOOST_REQUIRE(diff.entries_only_on_left == left);
|
||||
BOOST_REQUIRE(diff.entries_only_on_right.empty());
|
||||
@@ -65,7 +71,9 @@ BOOST_AUTO_TEST_CASE(both_same) {
|
||||
right.emplace(1, 100);
|
||||
right.emplace(2, 200);
|
||||
|
||||
auto diff = difference(left, right);
|
||||
auto diff = difference(left, right, [](int x, int y) -> bool {
|
||||
return x == y;
|
||||
});
|
||||
|
||||
BOOST_REQUIRE(diff.entries_only_on_left.empty());
|
||||
BOOST_REQUIRE(diff.entries_only_on_right.empty());
|
||||
@@ -83,7 +91,9 @@ BOOST_AUTO_TEST_CASE(differing_values) {
|
||||
right.emplace(1, 1000);
|
||||
right.emplace(2, 2000);
|
||||
|
||||
auto diff = difference(left, right);
|
||||
auto diff = difference(left, right, [](int x, int y) -> bool {
|
||||
return x == y;
|
||||
});
|
||||
|
||||
BOOST_REQUIRE(diff.entries_only_on_left.empty());
|
||||
BOOST_REQUIRE(diff.entries_only_on_right.empty());
|
||||
|
||||
Reference in New Issue
Block a user