File: pred.hpp

package info (click to toggle)
spades 3.13.1+dfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 22,172 kB
  • sloc: cpp: 136,213; ansic: 48,218; python: 16,809; perl: 4,252; sh: 2,115; java: 890; makefile: 507; pascal: 348; xml: 303
file content (175 lines) | stat: -rw-r--r-- 3,955 bytes parent folder | download | duplicates (5)
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
#pragma once

#include "function_traits.hpp"

#include <memory>
#include <functional>

namespace func {

template<class T>
class AbstractPredicate {
public:
    typedef T checked_type;

    virtual bool Check(T t) const = 0;

    bool operator()(T t) const { return Check(t); }

    virtual ~AbstractPredicate() {}
};

template<typename T>
class TypedPredicate {
    struct TypedPredicateConcept {
        virtual ~TypedPredicateConcept() { };

        virtual bool operator()(T x) const = 0;
    };

    template<class P>
    struct TypedPredicateModel : TypedPredicateConcept {
        TypedPredicateModel(P p)
                : data_(std::move(p)) { }

        virtual bool operator()(T x) const override {
            return (bool) data_(x);
        }

        P data_;
    };

    std::shared_ptr<const TypedPredicateConcept> self_;

public:
    typedef T checked_type;

    template<typename P>
    TypedPredicate(P p)
            : self_(std::make_shared<TypedPredicateModel<P>>(std::move(p))) { }

    bool operator()(T x) const {
        return self_->operator()(x);
    }
};

template<typename T>
class AlwaysTrueOperator {
public:
    typedef T checked_type;

    bool operator()(T) const {
        return true;
    }
};

template<typename T>
class AlwaysFalseOperator {
    typedef T checked_type;

public:
    bool operator()(T) const {
        return false;
    }
};

template<typename T>
class AndOperator {
public:
    typedef T checked_type;

    AndOperator(TypedPredicate<T> lhs, TypedPredicate<T> rhs)
            : lhs_(std::move(lhs)),
              rhs_(std::move(rhs)) { }

    bool operator()(T x) const {
        return lhs_(x) && rhs_(x);
    }

private:
    const TypedPredicate<T> lhs_, rhs_;
};

template<typename T>
class OrOperator {
public:
    typedef T checked_type;

    OrOperator(TypedPredicate<T> lhs, TypedPredicate<T> rhs)
            : lhs_(std::move(lhs)), rhs_(std::move(rhs)) { }

    bool operator()(T x) const {
        return lhs_(x) || rhs_(x);
    }

private:
    const TypedPredicate<T> lhs_, rhs_;
};

template<typename T>
class NotOperator {
public:
    typedef T checked_type;

    NotOperator(const TypedPredicate<T> p)
            : p_(std::move(p)) { }

    bool operator()(T x) const {
        return !p_(x);
    }

private:
    const TypedPredicate<T> p_;
};

template<class P,
        bool = function_traits<P>::arity == 1 &&
               std::is_same<typename function_traits<P>::return_type, bool>::value>
struct is_predicate : public std::true_type {
};

template<class P>
struct is_predicate<P, false> : public std::false_type {
};

template<class TP1, class TP2,
        typename _T1 = typename function_traits<TP1>::template arg<0>::type,
        typename _T2 = typename function_traits<TP2>::template arg<0>::type,
        typename =
        typename std::enable_if<std::is_same<_T1, _T2>::value &&
                                is_predicate<TP1>::value && is_predicate<TP2>::value
        >::type>
TypedPredicate<_T1> And(TP1 lhs, TP2 rhs) {
    return AndOperator<_T1>(lhs, rhs);
}

template<class TP1, class TP2,
        typename _T1 = typename function_traits<TP1>::template arg<0>::type,
        typename _T2 = typename function_traits<TP2>::template arg<0>::type,
        typename =
        typename std::enable_if<std::is_same<_T1, _T2>::value &&
                                is_predicate<TP1>::value && is_predicate<TP2>::value
        >::type>
TypedPredicate<_T1> Or(TP1 lhs, TP2 rhs) {
    return OrOperator<_T1>(lhs, rhs);
}

template<class TP,
        typename _T = typename function_traits<TP>::template arg<0>::type,
        typename =
        typename std::enable_if<is_predicate<TP>::value>::type>
TypedPredicate<_T> Not(TP p) {
    return NotOperator<_T>(p);
}

template<class T>
TypedPredicate<T> AlwaysTrue() {
    return AlwaysTrueOperator<T>();
}

template<class T>
TypedPredicate<T> AlwaysFalse() {
    return AlwaysFalseOperator<T>();
}

} // namespace func