Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 20 additions & 9 deletions include/boost/hana/map.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ Distributed under the Boost Software License, Version 1.0.
#include <boost/hana/fwd/map.hpp>

#include <boost/hana/all_of.hpp>
#include <boost/hana/at_key.hpp>
#include <boost/hana/basic_tuple.hpp>
#include <boost/hana/bool.hpp>
#include <boost/hana/concept/comparable.hpp>
Expand All @@ -39,7 +40,6 @@ Distributed under the Boost Software License, Version 1.0.
#include <boost/hana/functional/on.hpp>
#include <boost/hana/functional/partial.hpp>
#include <boost/hana/fwd/any_of.hpp>
#include <boost/hana/fwd/at_key.hpp>
#include <boost/hana/fwd/difference.hpp>
#include <boost/hana/fwd/erase_key.hpp>
#include <boost/hana/fwd/intersection.hpp>
Expand All @@ -53,6 +53,7 @@ Distributed under the Boost Software License, Version 1.0.
#include <boost/hana/optional.hpp>
#include <boost/hana/remove_if.hpp>
#include <boost/hana/second.hpp>
#include <boost/hana/set.hpp>
#include <boost/hana/unpack.hpp>
#include <boost/hana/value.hpp>

Expand Down Expand Up @@ -368,20 +369,30 @@ namespace boost { namespace hana {
return hana::false_c;
}

template <typename M1, typename M2>
struct compare_at_key {
const M1 &m1;
const M2 &m2;

template <typename K>
constexpr auto operator()(const K& key) const {
return hana::equal(hana::at_key(m1, key), hana::at_key(m2, key));
}
};

template <typename M1, typename M2>
static constexpr auto equal_helper(M1 const& m1, M2 const& m2, hana::true_) {
return hana::all_of(hana::keys(m1), hana::demux(equal)(
hana::partial(hana::find, m1),
hana::partial(hana::find, m2)
));
return hana::all_of(
hana::keys(m1),
equal_impl::compare_at_key<M1, M2>{m1, m2});
}

template <typename M1, typename M2>
static constexpr auto apply(M1 const& m1, M2 const& m2) {
return equal_impl::equal_helper(m1, m2, hana::bool_c<
decltype(hana::length(m1.storage))::value ==
decltype(hana::length(m2.storage))::value
>);
return equal_impl::equal_helper(m1, m2,
hana::to_set(hana::keys(m1)) ==
hana::to_set(hana::keys(m2))
);
}
};

Expand Down
8 changes: 8 additions & 0 deletions test/map/equal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@

#include <laws/base.hpp>
#include <support/minimal_product.hpp>

#include <memory>

namespace hana = boost::hana;


Expand Down Expand Up @@ -94,4 +97,9 @@ int main() {
!=
hana::make_map(p<1, 1>(), p<2, 2>())
);

// Check if non-copyable objects can be compared
auto map_unique_1 = hana::make_map(hana::make_pair(hana::integral_constant<int, 0>{}, std::make_unique<int>(0)));
auto map_unique_2 = hana::make_map(hana::make_pair(hana::integral_constant<int, 0>{}, std::make_unique<int>(0)));
BOOST_HANA_RUNTIME_CHECK(map_unique_1 != map_unique_2);
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe add map_unique_1 == map_unique_1. I am not seeing that hana::equal tries to short circuit identical types (or even address).

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good point.

}