Skip to content

Commit

Permalink
[SetTheoretic] Add SetTheoretic concept
Browse files Browse the repository at this point in the history
It adds SetTheoretic concept for functions-
union, intersection, difference, symmetric_difference.

Closes #357
  • Loading branch information
shreyans800755 committed Feb 21, 2020
1 parent bcd7946 commit 81b808a
Show file tree
Hide file tree
Showing 7 changed files with 136 additions and 9 deletions.
22 changes: 22 additions & 0 deletions example/misc/infinite_set.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,12 @@
#include <boost/hana/functional/compose.hpp>
#include <boost/hana/functional/partial.hpp>
#include <boost/hana/fwd/ap.hpp>
#include <boost/hana/fwd/difference.hpp>
#include <boost/hana/fwd/equal.hpp>
#include <boost/hana/fwd/find_if.hpp>
#include <boost/hana/fwd/intersection.hpp>
#include <boost/hana/fwd/lift.hpp>
#include <boost/hana/fwd/set.hpp>
#include <boost/hana/fwd/union.hpp>
#include <boost/hana/if.hpp>
#include <boost/hana/is_subset.hpp>
Expand Down Expand Up @@ -48,6 +51,9 @@ constexpr auto doubleton(X x, Y y) {
}

namespace boost { namespace hana {
//////////////////////////////////////////////////////////////////////////
// SetTheoretic
//////////////////////////////////////////////////////////////////////////
template <>
struct union_impl<infinite_set_tag> {
template <typename Xs, typename Ys>
Expand All @@ -56,6 +62,22 @@ namespace boost { namespace hana {
}
};

template <>
struct difference_impl<infinite_set_tag> {
template <typename Xs, typename Ys>
static constexpr auto apply(Xs xs, Ys ys) {
return difference_impl<set_tag>(xs, ys);
}
};

template <>
struct intersection_impl<infinite_set_tag> {
template <typename Xs, typename Ys>
static constexpr auto apply(Xs xs, Ys ys) {
return intersection_impl<set_tag>(xs, ys);
}
};

//////////////////////////////////////////////////////////////////////////
// Comparable
//////////////////////////////////////////////////////////////////////////
Expand Down
1 change: 1 addition & 0 deletions include/boost/hana/concept.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ Distributed under the Boost Software License, Version 1.0.
#include <boost/hana/concept/ring.hpp>
#include <boost/hana/concept/searchable.hpp>
#include <boost/hana/concept/sequence.hpp>
#include <boost/hana/concept/set_theoretic.hpp>
#include <boost/hana/concept/struct.hpp>

#endif // !BOOST_HANA_CONCEPT_HPP
36 changes: 36 additions & 0 deletions include/boost/hana/concept/set_theoretic.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*!
@file
Defines `boost::hana::SetTheoretic`.
@copyright Shreyans Doshi 2017
Distributed under the Boost Software License, Version 1.0.
(See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)
*/

#ifndef BOOST_HANA_CONCEPT_SET_THEORETIC_HPP
#define BOOST_HANA_CONCEPT_SET_THEORETIC_HPP

#include <boost/hana/fwd/concept/set_theoretic.hpp>

#include <boost/hana/config.hpp>
#include <boost/hana/core/default.hpp>
#include <boost/hana/core/tag_of.hpp>
#include <boost/hana/detail/integral_constant.hpp>
#include <boost/hana/difference.hpp>
#include <boost/hana/intersection.hpp>
#include <boost/hana/symmetric_difference.hpp>
#include <boost/hana/union.hpp>


BOOST_HANA_NAMESPACE_BEGIN
template <typename STh>
struct SetTheoretic
: hana::integral_constant<bool,
!is_default<difference_impl<typename tag_of<STh>::type>>::value &&
!is_default<intersection_impl<typename tag_of<STh>::type>>::value &&
!is_default<union_impl<typename tag_of<STh>::type>>::value
>
{ };
BOOST_HANA_NAMESPACE_END

#endif // !BOOST_HANA_CONCEPT_SET_THEORETIC_HPP
17 changes: 14 additions & 3 deletions include/boost/hana/difference.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ Distributed under the Boost Software License, Version 1.0.

#include <boost/hana/fwd/difference.hpp>

#include <boost/hana/concept/set_theoretic.hpp>
#include <boost/hana/config.hpp>
#include <boost/hana/core/dispatch.hpp>
#include <boost/hana/erase_key.hpp>
Expand All @@ -21,11 +22,21 @@ BOOST_HANA_NAMESPACE_BEGIN
//! @cond
template <typename Xs, typename Ys>
constexpr auto difference_t::operator()(Xs&& xs, Ys&& ys) const {
using S = typename hana::tag_of<Xs>::type;
using Difference = BOOST_HANA_DISPATCH_IF(difference_impl<S>,
true
using TX = typename hana::tag_of<Xs>::type;
using TY = typename hana::tag_of<Ys>::type;
using Difference = BOOST_HANA_DISPATCH_IF(difference_impl<TX>,
hana::SetTheoretic<TX>::value &&
std::is_same<TX, TY>::value
);

#ifndef BOOST_HANA_CONFIG_DISABLE_CONCEPT_CHECKS
static_assert(hana::SetTheoretic<TX>::value,
"hana::difference(xs, ys) requires 'xs' to be SetTheoretic");

static_assert(std::is_same<TX, TY>::value,
"hana::difference(xs, ys) requires 'xs' and 'ys' to be of the same type");
#endif

return Difference::apply(static_cast<Xs&&>(xs), static_cast<Ys&&>(ys));
}
//! @endcond
Expand Down
35 changes: 35 additions & 0 deletions include/boost/hana/fwd/concept/set_theoretic.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*!
@file
Forward declares `boost::hana::SetTheoretic`.
@copyright Shreyans Doshi 2017
Distributed under the Boost Software License, Version 1.0.
(See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)
*/

#ifndef BOOST_HANA_FWD_CONCEPT_SET_THEORETIC_HPP
#define BOOST_HANA_FWD_CONCEPT_SET_THEORETIC_HPP

#include <boost/hana/config.hpp>


BOOST_HANA_NAMESPACE_BEGIN
//! @ingroup group-concepts
//! @defgroup group-SetTheoretic SetTheoretic
//! The `SetTheoretic` concept represents data structures supporting
//! algebra of sets.
//!
//! Minimal complete definition
//! ---------------------------
//! `union_`, `intersection`, `difference` and `symmetric_difference`
//!
//! Concrete models
//! ---------------
//! `hana::set`, `hana::map`
//!
//!
template <typename STh>
struct SetTheoretic;
BOOST_HANA_NAMESPACE_END

#endif // !BOOST_HANA_FWD_CONCEPT_SET_THEORETIC_HPP
17 changes: 14 additions & 3 deletions include/boost/hana/intersection.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ Distributed under the Boost Software License, Version 1.0.

#include <boost/hana/fwd/intersection.hpp>

#include <boost/hana/concept/set_theoretic.hpp>
#include <boost/hana/config.hpp>
#include <boost/hana/core/dispatch.hpp>

Expand All @@ -20,11 +21,21 @@ BOOST_HANA_NAMESPACE_BEGIN
//! @cond
template <typename Xs, typename Ys>
constexpr auto intersection_t::operator()(Xs&& xs, Ys&& ys) const {
using S = typename hana::tag_of<Xs>::type;
using Intersection = BOOST_HANA_DISPATCH_IF(intersection_impl<S>,
true
using TX = typename hana::tag_of<Xs>::type;
using TY = typename hana::tag_of<Ys>::type;
using Intersection = BOOST_HANA_DISPATCH_IF(intersection_impl<TX>,
hana::SetTheoretic<TX>::value &&
std::is_same<TX, TY>::value
);

#ifndef BOOST_HANA_CONFIG_DISABLE_CONCEPT_CHECKS
static_assert(hana::SetTheoretic<TX>::value,
"hana::intersection(xs, ys) requires 'xs' to be SetTheoretic");

static_assert(std::is_same<TX, TY>::value,
"hana::intersection(xs, ys) requires 'xs' and 'ys' to be of the same type");
#endif

return Intersection::apply(static_cast<Xs&&>(xs), static_cast<Ys&&>(ys));
}
//! @endcond
Expand Down
17 changes: 14 additions & 3 deletions include/boost/hana/union.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ Distributed under the Boost Software License, Version 1.0.

#include <boost/hana/fwd/union.hpp>

#include <boost/hana/concept/set_theoretic.hpp>
#include <boost/hana/config.hpp>
#include <boost/hana/core/dispatch.hpp>

Expand All @@ -20,11 +21,21 @@ BOOST_HANA_NAMESPACE_BEGIN
//! @cond
template <typename Xs, typename Ys>
constexpr auto union_t::operator()(Xs&& xs, Ys&& ys) const {
using S = typename hana::tag_of<Xs>::type;
using Union = BOOST_HANA_DISPATCH_IF(union_impl<S>,
true
using TX = typename hana::tag_of<Xs>::type;
using TY = typename hana::tag_of<Ys>::type;
using Union = BOOST_HANA_DISPATCH_IF(union_impl<TX>,
hana::SetTheoretic<TX>::value &&
std::is_same<TX, TY>::value
);

#ifndef BOOST_HANA_CONFIG_DISABLE_CONCEPT_CHECKS
static_assert(hana::SetTheoretic<TX>::value,
"hana::union_(xs, ys) requires 'xs' to be SetTheoretic");

static_assert(std::is_same<TX, TY>::value,
"hana::union_(xs, ys) requires 'xs' and 'ys' to be of the same type");
#endif

return Union::apply(static_cast<Xs&&>(xs), static_cast<Ys&&>(ys));
}
//! @endcond
Expand Down

0 comments on commit 81b808a

Please sign in to comment.