Skip to content

Commit 81b808a

Browse files
[SetTheoretic] Add SetTheoretic concept
It adds SetTheoretic concept for functions- union, intersection, difference, symmetric_difference. Closes #357
1 parent bcd7946 commit 81b808a

File tree

7 files changed

+136
-9
lines changed

7 files changed

+136
-9
lines changed

Diff for: example/misc/infinite_set.cpp

+22
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,12 @@
88
#include <boost/hana/functional/compose.hpp>
99
#include <boost/hana/functional/partial.hpp>
1010
#include <boost/hana/fwd/ap.hpp>
11+
#include <boost/hana/fwd/difference.hpp>
1112
#include <boost/hana/fwd/equal.hpp>
1213
#include <boost/hana/fwd/find_if.hpp>
14+
#include <boost/hana/fwd/intersection.hpp>
1315
#include <boost/hana/fwd/lift.hpp>
16+
#include <boost/hana/fwd/set.hpp>
1417
#include <boost/hana/fwd/union.hpp>
1518
#include <boost/hana/if.hpp>
1619
#include <boost/hana/is_subset.hpp>
@@ -48,6 +51,9 @@ constexpr auto doubleton(X x, Y y) {
4851
}
4952

5053
namespace boost { namespace hana {
54+
//////////////////////////////////////////////////////////////////////////
55+
// SetTheoretic
56+
//////////////////////////////////////////////////////////////////////////
5157
template <>
5258
struct union_impl<infinite_set_tag> {
5359
template <typename Xs, typename Ys>
@@ -56,6 +62,22 @@ namespace boost { namespace hana {
5662
}
5763
};
5864

65+
template <>
66+
struct difference_impl<infinite_set_tag> {
67+
template <typename Xs, typename Ys>
68+
static constexpr auto apply(Xs xs, Ys ys) {
69+
return difference_impl<set_tag>(xs, ys);
70+
}
71+
};
72+
73+
template <>
74+
struct intersection_impl<infinite_set_tag> {
75+
template <typename Xs, typename Ys>
76+
static constexpr auto apply(Xs xs, Ys ys) {
77+
return intersection_impl<set_tag>(xs, ys);
78+
}
79+
};
80+
5981
//////////////////////////////////////////////////////////////////////////
6082
// Comparable
6183
//////////////////////////////////////////////////////////////////////////

Diff for: include/boost/hana/concept.hpp

+1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ Distributed under the Boost Software License, Version 1.0.
3131
#include <boost/hana/concept/ring.hpp>
3232
#include <boost/hana/concept/searchable.hpp>
3333
#include <boost/hana/concept/sequence.hpp>
34+
#include <boost/hana/concept/set_theoretic.hpp>
3435
#include <boost/hana/concept/struct.hpp>
3536

3637
#endif // !BOOST_HANA_CONCEPT_HPP

Diff for: include/boost/hana/concept/set_theoretic.hpp

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*!
2+
@file
3+
Defines `boost::hana::SetTheoretic`.
4+
5+
@copyright Shreyans Doshi 2017
6+
Distributed under the Boost Software License, Version 1.0.
7+
(See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)
8+
*/
9+
10+
#ifndef BOOST_HANA_CONCEPT_SET_THEORETIC_HPP
11+
#define BOOST_HANA_CONCEPT_SET_THEORETIC_HPP
12+
13+
#include <boost/hana/fwd/concept/set_theoretic.hpp>
14+
15+
#include <boost/hana/config.hpp>
16+
#include <boost/hana/core/default.hpp>
17+
#include <boost/hana/core/tag_of.hpp>
18+
#include <boost/hana/detail/integral_constant.hpp>
19+
#include <boost/hana/difference.hpp>
20+
#include <boost/hana/intersection.hpp>
21+
#include <boost/hana/symmetric_difference.hpp>
22+
#include <boost/hana/union.hpp>
23+
24+
25+
BOOST_HANA_NAMESPACE_BEGIN
26+
template <typename STh>
27+
struct SetTheoretic
28+
: hana::integral_constant<bool,
29+
!is_default<difference_impl<typename tag_of<STh>::type>>::value &&
30+
!is_default<intersection_impl<typename tag_of<STh>::type>>::value &&
31+
!is_default<union_impl<typename tag_of<STh>::type>>::value
32+
>
33+
{ };
34+
BOOST_HANA_NAMESPACE_END
35+
36+
#endif // !BOOST_HANA_CONCEPT_SET_THEORETIC_HPP

Diff for: include/boost/hana/difference.hpp

+14-3
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ Distributed under the Boost Software License, Version 1.0.
1212

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

15+
#include <boost/hana/concept/set_theoretic.hpp>
1516
#include <boost/hana/config.hpp>
1617
#include <boost/hana/core/dispatch.hpp>
1718
#include <boost/hana/erase_key.hpp>
@@ -21,11 +22,21 @@ BOOST_HANA_NAMESPACE_BEGIN
2122
//! @cond
2223
template <typename Xs, typename Ys>
2324
constexpr auto difference_t::operator()(Xs&& xs, Ys&& ys) const {
24-
using S = typename hana::tag_of<Xs>::type;
25-
using Difference = BOOST_HANA_DISPATCH_IF(difference_impl<S>,
26-
true
25+
using TX = typename hana::tag_of<Xs>::type;
26+
using TY = typename hana::tag_of<Ys>::type;
27+
using Difference = BOOST_HANA_DISPATCH_IF(difference_impl<TX>,
28+
hana::SetTheoretic<TX>::value &&
29+
std::is_same<TX, TY>::value
2730
);
2831

32+
#ifndef BOOST_HANA_CONFIG_DISABLE_CONCEPT_CHECKS
33+
static_assert(hana::SetTheoretic<TX>::value,
34+
"hana::difference(xs, ys) requires 'xs' to be SetTheoretic");
35+
36+
static_assert(std::is_same<TX, TY>::value,
37+
"hana::difference(xs, ys) requires 'xs' and 'ys' to be of the same type");
38+
#endif
39+
2940
return Difference::apply(static_cast<Xs&&>(xs), static_cast<Ys&&>(ys));
3041
}
3142
//! @endcond

Diff for: include/boost/hana/fwd/concept/set_theoretic.hpp

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*!
2+
@file
3+
Forward declares `boost::hana::SetTheoretic`.
4+
5+
@copyright Shreyans Doshi 2017
6+
Distributed under the Boost Software License, Version 1.0.
7+
(See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)
8+
*/
9+
10+
#ifndef BOOST_HANA_FWD_CONCEPT_SET_THEORETIC_HPP
11+
#define BOOST_HANA_FWD_CONCEPT_SET_THEORETIC_HPP
12+
13+
#include <boost/hana/config.hpp>
14+
15+
16+
BOOST_HANA_NAMESPACE_BEGIN
17+
//! @ingroup group-concepts
18+
//! @defgroup group-SetTheoretic SetTheoretic
19+
//! The `SetTheoretic` concept represents data structures supporting
20+
//! algebra of sets.
21+
//!
22+
//! Minimal complete definition
23+
//! ---------------------------
24+
//! `union_`, `intersection`, `difference` and `symmetric_difference`
25+
//!
26+
//! Concrete models
27+
//! ---------------
28+
//! `hana::set`, `hana::map`
29+
//!
30+
//!
31+
template <typename STh>
32+
struct SetTheoretic;
33+
BOOST_HANA_NAMESPACE_END
34+
35+
#endif // !BOOST_HANA_FWD_CONCEPT_SET_THEORETIC_HPP

Diff for: include/boost/hana/intersection.hpp

+14-3
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ Distributed under the Boost Software License, Version 1.0.
1212

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

15+
#include <boost/hana/concept/set_theoretic.hpp>
1516
#include <boost/hana/config.hpp>
1617
#include <boost/hana/core/dispatch.hpp>
1718

@@ -20,11 +21,21 @@ BOOST_HANA_NAMESPACE_BEGIN
2021
//! @cond
2122
template <typename Xs, typename Ys>
2223
constexpr auto intersection_t::operator()(Xs&& xs, Ys&& ys) const {
23-
using S = typename hana::tag_of<Xs>::type;
24-
using Intersection = BOOST_HANA_DISPATCH_IF(intersection_impl<S>,
25-
true
24+
using TX = typename hana::tag_of<Xs>::type;
25+
using TY = typename hana::tag_of<Ys>::type;
26+
using Intersection = BOOST_HANA_DISPATCH_IF(intersection_impl<TX>,
27+
hana::SetTheoretic<TX>::value &&
28+
std::is_same<TX, TY>::value
2629
);
2730

31+
#ifndef BOOST_HANA_CONFIG_DISABLE_CONCEPT_CHECKS
32+
static_assert(hana::SetTheoretic<TX>::value,
33+
"hana::intersection(xs, ys) requires 'xs' to be SetTheoretic");
34+
35+
static_assert(std::is_same<TX, TY>::value,
36+
"hana::intersection(xs, ys) requires 'xs' and 'ys' to be of the same type");
37+
#endif
38+
2839
return Intersection::apply(static_cast<Xs&&>(xs), static_cast<Ys&&>(ys));
2940
}
3041
//! @endcond

Diff for: include/boost/hana/union.hpp

+14-3
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ Distributed under the Boost Software License, Version 1.0.
1212

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

15+
#include <boost/hana/concept/set_theoretic.hpp>
1516
#include <boost/hana/config.hpp>
1617
#include <boost/hana/core/dispatch.hpp>
1718

@@ -20,11 +21,21 @@ BOOST_HANA_NAMESPACE_BEGIN
2021
//! @cond
2122
template <typename Xs, typename Ys>
2223
constexpr auto union_t::operator()(Xs&& xs, Ys&& ys) const {
23-
using S = typename hana::tag_of<Xs>::type;
24-
using Union = BOOST_HANA_DISPATCH_IF(union_impl<S>,
25-
true
24+
using TX = typename hana::tag_of<Xs>::type;
25+
using TY = typename hana::tag_of<Ys>::type;
26+
using Union = BOOST_HANA_DISPATCH_IF(union_impl<TX>,
27+
hana::SetTheoretic<TX>::value &&
28+
std::is_same<TX, TY>::value
2629
);
2730

31+
#ifndef BOOST_HANA_CONFIG_DISABLE_CONCEPT_CHECKS
32+
static_assert(hana::SetTheoretic<TX>::value,
33+
"hana::union_(xs, ys) requires 'xs' to be SetTheoretic");
34+
35+
static_assert(std::is_same<TX, TY>::value,
36+
"hana::union_(xs, ys) requires 'xs' and 'ys' to be of the same type");
37+
#endif
38+
2839
return Union::apply(static_cast<Xs&&>(xs), static_cast<Ys&&>(ys));
2940
}
3041
//! @endcond

0 commit comments

Comments
 (0)