souffle  2.0.2-371-g6315b36
Util.h
Go to the documentation of this file.
1 /*
2  * Souffle - A Datalog Compiler
3  * Copyright (c) 2020, The Souffle Developers. 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 Util.h
12  *
13  * @brief Interpreter Utilities.
14  ***********************************************************************/
15 
16 #pragma once
17 
18 #include "Global.h"
19 #include "souffle/RamTypes.h"
25 
26 namespace souffle::interpreter {
27 // clang-format off
28 
29 #define FOR_EACH_PROVENANCE(func, ...) \
30  func(Provenance, 2, __VA_ARGS__) \
31  func(Provenance, 3, __VA_ARGS__) \
32  func(Provenance, 4, __VA_ARGS__) \
33  func(Provenance, 5, __VA_ARGS__) \
34  func(Provenance, 6, __VA_ARGS__) \
35  func(Provenance, 7, __VA_ARGS__) \
36  func(Provenance, 8, __VA_ARGS__) \
37  func(Provenance, 9, __VA_ARGS__) \
38  func(Provenance, 10, __VA_ARGS__) \
39  func(Provenance, 11, __VA_ARGS__) \
40  func(Provenance, 12, __VA_ARGS__) \
41  func(Provenance, 13, __VA_ARGS__) \
42  func(Provenance, 14, __VA_ARGS__) \
43  func(Provenance, 15, __VA_ARGS__) \
44  func(Provenance, 16, __VA_ARGS__) \
45  func(Provenance, 17, __VA_ARGS__) \
46  func(Provenance, 18, __VA_ARGS__) \
47  func(Provenance, 19, __VA_ARGS__) \
48  func(Provenance, 20, __VA_ARGS__) \
49  func(Provenance, 21, __VA_ARGS__) \
50  func(Provenance, 22, __VA_ARGS__) \
51  func(Provenance, 23, __VA_ARGS__) \
52  func(Provenance, 24, __VA_ARGS__) \
53  func(Provenance, 25, __VA_ARGS__) \
54  func(Provenance, 26, __VA_ARGS__) \
55  func(Provenance, 27, __VA_ARGS__) \
56  func(Provenance, 28, __VA_ARGS__) \
57  func(Provenance, 29, __VA_ARGS__) \
58  func(Provenance, 30, __VA_ARGS__)
59 
60 
61 #define FOR_EACH_BTREE(func, ...)\
62  func(Btree, 0, __VA_ARGS__) \
63  func(Btree, 1, __VA_ARGS__) \
64  func(Btree, 2, __VA_ARGS__) \
65  func(Btree, 3, __VA_ARGS__) \
66  func(Btree, 4, __VA_ARGS__) \
67  func(Btree, 5, __VA_ARGS__) \
68  func(Btree, 6, __VA_ARGS__) \
69  func(Btree, 7, __VA_ARGS__) \
70  func(Btree, 8, __VA_ARGS__) \
71  func(Btree, 9, __VA_ARGS__) \
72  func(Btree, 10, __VA_ARGS__) \
73  func(Btree, 11, __VA_ARGS__) \
74  func(Btree, 12, __VA_ARGS__) \
75  func(Btree, 13, __VA_ARGS__) \
76  func(Btree, 14, __VA_ARGS__) \
77  func(Btree, 15, __VA_ARGS__) \
78  func(Btree, 16, __VA_ARGS__) \
79  func(Btree, 17, __VA_ARGS__) \
80  func(Btree, 18, __VA_ARGS__) \
81  func(Btree, 19, __VA_ARGS__) \
82  func(Btree, 20, __VA_ARGS__)
83 
84 // Brie is disabled for now.
85 #define FOR_EACH_BRIE(func, ...)
86  /* func(Brie, 0, __VA_ARGS__) \ */
87  /* func(Brie, 1, __VA_ARGS__) \ */
88  /* func(Brie, 2, __VA_ARGS__) \ */
89  /* func(Brie, 3, __VA_ARGS__) \ */
90  /* func(Brie, 4, __VA_ARGS__) \ */
91  /* func(Brie, 5, __VA_ARGS__) \ */
92  /* func(Brie, 6, __VA_ARGS__) \ */
93  /* func(Brie, 7, __VA_ARGS__) \ */
94  /* func(Brie, 8, __VA_ARGS__) \ */
95  /* func(Brie, 9, __VA_ARGS__) \ */
96  /* func(Brie, 10, __VA_ARGS__) \ */
97  /* func(Brie, 11, __VA_ARGS__) \ */
98  /* func(Brie, 12, __VA_ARGS__) \ */
99  /* func(Brie, 13, __VA_ARGS__) \ */
100  /* func(Brie, 14, __VA_ARGS__) \ */
101  /* func(Brie, 15, __VA_ARGS__) \ */
102  /* func(Brie, 16, __VA_ARGS__) \ */
103  /* func(Brie, 17, __VA_ARGS__) \ */
104  /* func(Brie, 18, __VA_ARGS__) \ */
105  /* func(Brie, 19, __VA_ARGS__) \ */
106  /* func(Brie, 20, __VA_ARGS__) */
107 
108 #define FOR_EACH_EQREL(func, ...)\
109  func(Eqrel, 2, __VA_ARGS__)
110 
111 #define FOR_EACH(func, ...) \
112  FOR_EACH_BTREE(func, __VA_ARGS__) \
113  FOR_EACH_BRIE(func, __VA_ARGS__) \
114  FOR_EACH_PROVENANCE(func, __VA_ARGS__) \
115  FOR_EACH_EQREL(func, __VA_ARGS__)
116 
117 // clang-format on
118 
119 /**
120  * A namespace enclosing utilities required by indices.
121  */
122 namespace index_utils {
123 
124 // -------- generic tuple comparator ----------
125 
126 template <unsigned... Columns>
127 struct comparator;
128 
129 template <unsigned First, unsigned... Rest>
130 struct comparator<First, Rest...> {
131  template <typename T>
132  int operator()(const T& a, const T& b) const {
133  return (a[First] < b[First]) ? -1 : ((a[First] > b[First]) ? 1 : comparator<Rest...>()(a, b));
134  }
135  template <typename T>
136  bool less(const T& a, const T& b) const {
137  return a[First] < b[First] || (a[First] == b[First] && comparator<Rest...>().less(a, b));
138  }
139  template <typename T>
140  bool equal(const T& a, const T& b) const {
141  return a[First] == b[First] && comparator<Rest...>().equal(a, b);
142  }
143 };
144 
145 template <>
146 struct comparator<> {
147  template <typename T>
148  int operator()(const T&, const T&) const {
149  return 0;
150  }
151  template <typename T>
152  bool less(const T&, const T&) const {
153  return false;
154  }
155  template <typename T>
156  bool equal(const T&, const T&) const {
157  return true;
158  }
159 };
160 
161 } // namespace index_utils
162 
163 /**
164  * The index class is utilized as a template-meta-programming structure
165  * to specify and realize indices.
166  *
167  * @tparam Columns ... the order in which elements of the relation to be indexed
168  * shell be considered by this index.
169  */
170 template <unsigned... Columns>
171 struct index {
172  // the comparator associated to this index
173  using comparator = index_utils::comparator<Columns...>;
174 };
175 
176 /**
177  * A namespace enclosing utilities required relations to handle indices.
178  */
179 namespace index_utils {
180 
181 // -- a utility extending a given index by another column --
182 // e.g. index<1,0> => index<1,0,2>
183 
184 template <typename Index, unsigned column>
185 struct extend;
186 
187 template <unsigned... Columns, unsigned Col>
188 struct extend<index<Columns...>, Col> {
189  using type = index<Columns..., Col>;
190 };
191 
192 // -- obtains a full index for a given arity --
193 
194 template <unsigned arity>
195 struct get_full_index {
196  using type = typename extend<typename get_full_index<arity - 1>::type, arity - 1>::type;
197 };
198 
199 template <>
200 struct get_full_index<0> {
201  using type = index<>;
202 };
203 
204 } // namespace index_utils
205 
206 template <size_t Arity>
208 
209 // The comparator to be used for B-tree nodes.
210 template <size_t Arity>
212 
213 // Alias for btree_set
214 template <size_t Arity>
216 
217 // Alias for Trie
218 template <size_t Arity>
219 using Brie = Trie<Arity>;
220 
221 // Updater for Provenance
222 template <size_t Arity>
223 struct ProvenanceUpdater {
224  void update(t_tuple<Arity>& old_t, const t_tuple<Arity>& new_t) {
225  old_t[Arity - 2] = new_t[Arity - 2];
226  old_t[Arity - 1] = new_t[Arity - 1];
227  }
228 };
229 
230 // Alias for Provenance
231 template <size_t Arity>
232 using Provenance = btree_set<t_tuple<Arity>, comparator<Arity>, std::allocator<t_tuple<Arity>>, 256,
235 
236 // Alias for Eqrel
237 // Note: require Arity = 2.
238 template <size_t Arity>
240 
241 }; // namespace souffle::interpreter
souffle::interpreter::index_utils::comparator
Definition: Util.h:132
souffle::interpreter::ProvenanceUpdater
Definition: Util.h:228
BTree.h
souffle::interpreter::comparator
typename index_utils::get_full_index< Arity >::type::comparator comparator
Definition: Util.h:216
souffle::interpreter::index_utils::get_full_index::type
typename extend< typename get_full_index< arity - 1 >::type, arity - 1 >::type type
Definition: Util.h:201
souffle::btree_set
A b-tree based set implementation.
Definition: BTree.h:2241
MiscUtil.h
Brie.h
souffle::Trie
Definition: Brie.h:79
souffle::interpreter::index
The index class is utilized as a template-meta-programming structure to specify and realize indices.
Definition: Util.h:176
Global.h
souffle::interpreter
Definition: BrieIndex.cpp:22
ContainerUtil.h
souffle::interpreter::ProvenanceUpdater::update
void update(t_tuple< Arity > &old_t, const t_tuple< Arity > &new_t)
Definition: Util.h:229
souffle::detail::default_strategy
Definition: BTree.h:235
souffle::Tuple
std::array< A, N > Tuple
Definition: RamTypes.h:36
souffle::interpreter::t_tuple
typename souffle::Tuple< RamDomain, Arity > t_tuple
Definition: Util.h:212
EquivalenceRelation.h
souffle::interpreter::index_utils::get_full_index
Definition: Util.h:200
b
l j a showGridBackground &&c b raw series this eventEmitter b
Definition: htmlJsChartistMin.h:15
RamTypes.h
souffle::interpreter::index_utils::extend
Definition: Util.h:190
souffle::EquivalenceRelation
Definition: EquivalenceRelation.h:50
std::type
ElementType type
Definition: span.h:640