souffle  2.0.2-371-g6315b36
FunctionalUtil.h
Go to the documentation of this file.
1 /*
2  * Souffle - A Datalog Compiler
3  * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved
4  * Licensed under the Universal Permissive License v 1.0 as shown at:
5  * - https://opensource.org/licenses/UPL
6  * - <souffle root>/licenses/SOUFFLE-UPL.txt
7  */
8 
9 /************************************************************************
10  *
11  * @file FunctionalUtil.h
12  *
13  * @brief Datalog project utilities
14  *
15  ***********************************************************************/
16 
17 #pragma once
18 
19 #include <algorithm>
20 #include <functional>
21 #include <utility>
22 #include <vector>
23 
24 namespace souffle {
25 
26 // -------------------------------------------------------------------------------
27 // Functional Utils
28 // -------------------------------------------------------------------------------
29 
30 /**
31  * A functor comparing the dereferenced value of a pointer type utilizing a
32  * given comparator. Its main use case are sets of non-null pointers which should
33  * be ordered according to the value addressed by the pointer.
34  */
35 template <typename T, typename C = std::less<T>>
36 struct deref_less {
37  bool operator()(const T* a, const T* b) const {
38  return C()(*a, *b);
39  }
40 };
41 
42 // -------------------------------------------------------------------------------
43 // Lambda Utils
44 // -------------------------------------------------------------------------------
45 
46 namespace detail {
47 
48 template <typename T>
49 struct lambda_traits_helper;
50 
51 template <typename R>
52 struct lambda_traits_helper<R()> {
53  using result_type = R;
54 };
55 
56 template <typename R, typename A0>
57 struct lambda_traits_helper<R(A0)> {
58  using result_type = R;
59  using arg0_type = A0;
60 };
61 
62 template <typename R, typename A0, typename A1>
63 struct lambda_traits_helper<R(A0, A1)> {
64  using result_type = R;
65  using arg0_type = A0;
66  using arg1_type = A1;
67 };
68 
69 template <typename R, typename... Args>
70 struct lambda_traits_helper<R(Args...)> {
71  using result_type = R;
72 };
73 
74 template <typename R, typename C, typename... Args>
75 struct lambda_traits_helper<R (C::*)(Args...)> : public lambda_traits_helper<R(Args...)> {};
76 
77 template <typename R, typename C, typename... Args>
78 struct lambda_traits_helper<R (C::*)(Args...) const> : public lambda_traits_helper<R (C::*)(Args...)> {};
79 } // namespace detail
80 
81 /**
82  * A type trait enabling the deduction of type properties of lambdas.
83  * Those include so far:
84  * - the result type (result_type)
85  * - the first argument type (arg0_type)
86  */
87 template <typename Lambda>
88 struct lambda_traits : public detail::lambda_traits_helper<decltype(&Lambda::operator())> {};
89 
90 // -------------------------------------------------------------------------------
91 // General Algorithms
92 // -------------------------------------------------------------------------------
93 
94 /**
95  * A generic test checking whether all elements within a container satisfy a
96  * certain predicate.
97  *
98  * @param c the container
99  * @param p the predicate
100  * @return true if for all elements x in c the predicate p(x) is true, false
101  * otherwise; for empty containers the result is always true
102  */
103 template <typename Container, typename UnaryPredicate>
104 bool all_of(const Container& c, UnaryPredicate p) {
105  return std::all_of(c.begin(), c.end(), p);
106 }
107 
108 /**
109  * A generic test checking whether any elements within a container satisfy a
110  * certain predicate.
111  *
112  * @param c the container
113  * @param p the predicate
114  * @return true if there is an element x in c such that predicate p(x) is true, false
115  * otherwise; for empty containers the result is always false
116  */
117 template <typename Container, typename UnaryPredicate>
118 bool any_of(const Container& c, UnaryPredicate p) {
119  return std::any_of(c.begin(), c.end(), p);
120 }
121 
122 /**
123  * A generic test checking whether all elements within a container satisfy a
124  * certain predicate.
125  *
126  * @param c the container
127  * @param p the predicate
128  * @return true if for all elements x in c the predicate p(x) is true, false
129  * otherwise; for empty containers the result is always true
130  */
131 template <typename Container, typename UnaryPredicate>
132 bool none_of(const Container& c, UnaryPredicate p) {
133  return std::none_of(c.begin(), c.end(), p);
134 }
135 
136 /**
137  * Filter a vector to exclude certain elements.
138  */
139 template <typename A, typename F>
140 std::vector<A> filterNot(std::vector<A> xs, F&& f) {
141  xs.erase(std::remove_if(xs.begin(), xs.end(), std::forward<F>(f)), xs.end());
142  return xs;
143 }
144 
145 /**
146  * Filter a vector to include certain elements.
147  */
148 template <typename A, typename F>
149 std::vector<A> filter(std::vector<A> xs, F&& f) {
150  return filterNot(std::move(xs), [&](auto&& x) { return !f(x); });
151 }
152 
153 } // namespace souffle
souffle::filterNot
std::vector< A > filterNot(std::vector< A > xs, F &&f)
Filter a vector to exclude certain elements.
Definition: FunctionalUtil.h:146
souffle::lambda_traits
A type trait enabling the deduction of type properties of lambdas.
Definition: FunctionalUtil.h:94
souffle::deref_less::operator()
bool operator()(const T *a, const T *b) const
Definition: FunctionalUtil.h:49
souffle::detail::lambda_traits_helper
Definition: FunctionalUtil.h:55
souffle::filter
std::vector< A > filter(std::vector< A > xs, F &&f)
Filter a vector to include certain elements.
Definition: FunctionalUtil.h:155
souffle::any_of
bool any_of(const Container &c, UnaryPredicate p)
A generic test checking whether any elements within a container satisfy a certain predicate.
Definition: FunctionalUtil.h:124
souffle::detail::lambda_traits_helper< R()>::result_type
R result_type
Definition: FunctionalUtil.h:59
souffle::detail::lambda_traits_helper< R(A0)>::result_type
R result_type
Definition: FunctionalUtil.h:64
souffle::detail::lambda_traits_helper< R(A0)>::arg0_type
A0 arg0_type
Definition: FunctionalUtil.h:65
souffle::all_of
bool all_of(const Container &c, UnaryPredicate p)
A generic test checking whether all elements within a container satisfy a certain predicate.
Definition: FunctionalUtil.h:110
b
l j a showGridBackground &&c b raw series this eventEmitter b
Definition: htmlJsChartistMin.h:15
souffle
Definition: AggregateOp.h:25
souffle::none_of
bool none_of(const Container &c, UnaryPredicate p)
A generic test checking whether all elements within a container satisfy a certain predicate.
Definition: FunctionalUtil.h:138
souffle::detail::lambda_traits_helper< R(A0, A1)>::result_type
R result_type
Definition: FunctionalUtil.h:70
p
a horizontalBars(j=m=void 0===a.axisX.type?new c.AutoScaleAxis(c.Axis.units.x, b.normalized.series, o, c.extend({}, a.axisX,{highLow:d, referenceValue:0})):a.axisX.type.call(c, c.Axis.units.x, b.normalized.series, o, c.extend({}, a.axisX,{highLow:d, referenceValue:0})), l=n=void 0===a.axisY.type?new c.StepAxis(c.Axis.units.y, b.normalized.series, o,{ticks:k}):a.axisY.type.call(c, c.Axis.units.y, b.normalized.series, o, a.axisY)) var p
Definition: htmlJsChartistMin.h:15