summaryrefslogtreecommitdiffstats
path: root/src/boost/libs/hana/test/_include/support/numeric.hpp
blob: 2aebe76640fd67a56b604edee1d0d6fd31680b20 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
// Copyright Louis Dionne 2013-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 TEST_SUPPORT_NUMERIC_HPP
#define TEST_SUPPORT_NUMERIC_HPP

#include <boost/hana/core/tag_of.hpp>
#include <boost/hana/eval.hpp>
#include <boost/hana/fwd/div.hpp>
#include <boost/hana/fwd/equal.hpp>
#include <boost/hana/fwd/eval_if.hpp>
#include <boost/hana/fwd/less.hpp>
#include <boost/hana/fwd/minus.hpp>
#include <boost/hana/fwd/mod.hpp>
#include <boost/hana/fwd/mult.hpp>
#include <boost/hana/fwd/negate.hpp>
#include <boost/hana/fwd/not.hpp>
#include <boost/hana/fwd/one.hpp>
#include <boost/hana/fwd/plus.hpp>
#include <boost/hana/fwd/while.hpp>
#include <boost/hana/fwd/zero.hpp>


struct numeric_type {
    constexpr explicit numeric_type(int v) : value(v) { }
    int value;
    constexpr operator int() const { return value; }
};

using Numeric = boost::hana::tag_of_t<numeric_type>;

struct numeric_t {
    constexpr numeric_type operator()(int x) const {
        return numeric_type{x};
    }
};
constexpr numeric_t numeric{};


namespace boost { namespace hana {
    //////////////////////////////////////////////////////////////////////////
    // Comparable
    //////////////////////////////////////////////////////////////////////////
    template <>
    struct equal_impl<Numeric, Numeric> {
        template <typename X, typename Y>
        static constexpr auto apply(X x, Y y)
        { return numeric(x.value == y.value); }
    };

    //////////////////////////////////////////////////////////////////////////
    // Orderable
    //////////////////////////////////////////////////////////////////////////
    template <>
    struct less_impl<Numeric, Numeric> {
        template <typename X, typename Y>
        static constexpr auto apply(X x, Y y) {
            // Workaround a _weird_ GCC bug:
            // error: parse error in template argument list
            //      bool cmp = (x.value < y.value);
            //                    ^
            int xv = x.value, yv = y.value;
            return numeric(xv < yv);
        }
    };

    //////////////////////////////////////////////////////////////////////////
    // Logical
    //////////////////////////////////////////////////////////////////////////
    template <>
    struct eval_if_impl<Numeric> {
        template <typename C, typename T, typename E>
        static constexpr auto apply(C const& c, T&& t, E&& e) {
            return c.value ? hana::eval(static_cast<T&&>(t))
                           : hana::eval(static_cast<E&&>(e));
        }
    };

    template <>
    struct not_impl<Numeric> {
        template <typename X>
        static constexpr auto apply(X x)
        { return numeric(!x.value); }
    };

    template <>
    struct while_impl<Numeric> {
        template <typename Pred, typename State, typename F>
        static constexpr auto apply(Pred pred, State state, F f)
            -> decltype(true ? f(state) : state)
        {
            if (pred(state))
                return hana::while_(pred, f(state), f);
            else
                return state;
        }
    };

    //////////////////////////////////////////////////////////////////////////
    // Monoid
    //////////////////////////////////////////////////////////////////////////
    template <>
    struct plus_impl<Numeric, Numeric> {
        template <typename X, typename Y>
        static constexpr auto apply(X x, Y y)
        { return numeric(x.value + y.value); }
    };

    template <>
    struct zero_impl<Numeric> {
        static constexpr auto apply()
        { return numeric(0); }
    };

    //////////////////////////////////////////////////////////////////////////
    // Group
    //
    // Define either one to select which MCD is used:
    //  BOOST_HANA_TEST_GROUP_NEGATE_MCD
    //  BOOST_HANA_TEST_GROUP_MINUS_MCD
    //
    // If neither is defined, the MCD used is unspecified.
    //////////////////////////////////////////////////////////////////////////
#if defined(BOOST_HANA_TEST_GROUP_NEGATE_MCD)
    template <>
    struct negate_impl<Numeric> {
        template <typename X>
        static constexpr auto apply(X x)
        { return numeric(-x.value); }
    };
#else
    template <>
    struct minus_impl<Numeric, Numeric> {
        template <typename X, typename Y>
        static constexpr auto apply(X x, Y y)
        { return numeric(x.value - y.value); }
    };
#endif

    //////////////////////////////////////////////////////////////////////////
    // Ring
    //////////////////////////////////////////////////////////////////////////
    template <>
    struct mult_impl<Numeric, Numeric> {
        template <typename X, typename Y>
        static constexpr auto apply(X x, Y y)
        { return numeric(x.value * y.value); }
    };

    template <>
    struct one_impl<Numeric> {
        static constexpr auto apply()
        { return numeric(1); }
    };

    //////////////////////////////////////////////////////////////////////////
    // EuclideanRing
    //////////////////////////////////////////////////////////////////////////
    template <>
    struct div_impl<Numeric, Numeric> {
        template <typename X, typename Y>
        static constexpr auto apply(X x, Y y)
        { return numeric(x.value / y.value); }
    };

    template <>
    struct mod_impl<Numeric, Numeric> {
        template <typename X, typename Y>
        static constexpr auto apply(X x, Y y)
        { return numeric(x.value % y.value); }
    };
}} // end namespace boost::hana

#endif //! TEST_SUPPORT_NUMERIC_HPP