@@ -72,29 +72,24 @@ struct Relation {
72
72
const set<GroundType> tuples;
73
73
};
74
74
75
- template <typename RELATION_TYPE >
76
- static ostream& print (ostream& out, const typename RELATION_TYPE::GroundType & s)
75
+ template <typename TUPLE_TYPE >
76
+ static ostream& print (ostream& out, const TUPLE_TYPE & s)
77
77
{
78
- apply ([&out](auto &&... args) {(( out << " [" << args << " ] " ), ...);}, s);
79
- return out;
80
- }
81
-
82
- template <typename RELATION_TYPE>
83
- static ostream& print (ostream& out, const typename RELATION_TYPE::AtomicType& s)
84
- {
85
- apply ([&out](auto &&... args) {(( out << " [" << args << " ] " ), ...);}, s);
78
+ out << " [" ;
79
+ apply ([&out](auto &&... args) {(( out << " [" << args << " ] " ), ...);}, s);
80
+ out << " ] " ;
86
81
return out;
87
82
}
88
83
89
84
template <typename RELATION_TYPE, size_t ... Is>
90
85
static void unbind (const typename RELATION_TYPE::AtomicType& tuple, index_sequence<Is...>) {
86
+ // TODO: why can't we use the apply pattern everywhere?
91
87
((get<Is>(tuple).getSym ()->unbind ()), ...);
92
88
}
93
89
94
90
template <typename RELATION_TYPE>
95
91
static void unbind (const typename RELATION_TYPE::AtomicType& tuple) {
96
- auto indexSequence = make_index_sequence<tuple_size<typename RELATION_TYPE::AtomicType>::value> { };
97
- unbind<RELATION_TYPE>(tuple, indexSequence);
92
+ unbind<RELATION_TYPE>(tuple, make_index_sequence<tuple_size<typename RELATION_TYPE::AtomicType>::value> { });
98
93
}
99
94
100
95
// bind 1 SymbolOrValue with 1 Value
@@ -127,18 +122,18 @@ static optional<typename RELATION_TYPE::GroundType> bind(
127
122
if (successfulBinding) {
128
123
{
129
124
cout << " bound " ;
130
- print<RELATION_TYPE> (cout, atom);
125
+ print (cout, atom);
131
126
cout << " to " ;
132
- print<RELATION_TYPE> (cout, boundAtom);
127
+ print (cout, boundAtom);
133
128
cout << endl;
134
129
}
135
130
return boundAtom;
136
131
}
137
132
{
138
133
cout << " failed to bind " ;
139
- print<RELATION_TYPE> (cout, atom);
134
+ print (cout, atom);
140
135
cout << " with " ;
141
- print<RELATION_TYPE> (cout, fact);
136
+ print (cout, fact);
142
137
cout << endl;
143
138
}
144
139
return nullopt;
@@ -150,8 +145,7 @@ static optional<typename RELATION_TYPE::GroundType> bind(
150
145
typename RELATION_TYPE::AtomicType& atom,
151
146
const typename RELATION_TYPE::GroundType& fact
152
147
) {
153
- auto indexSequence = make_index_sequence<tuple_size<typename RELATION_TYPE::GroundType>::value> { };
154
- return bind<RELATION_TYPE>(atom, fact, indexSequence);
148
+ return bind<RELATION_TYPE>(atom, fact, make_index_sequence<tuple_size<typename RELATION_TYPE::GroundType>::value> { });
155
149
}
156
150
157
151
// bind 1 atom with a relation (n facts)
@@ -181,6 +175,12 @@ template<typename ... RELATIONs>
181
175
struct State {
182
176
typedef tuple<RELATIONs...> RelationsType;
183
177
RelationsType relations;
178
+ typedef tuple<typename RELATIONs::GroundType const * ...> SliceType;
179
+
180
+ static ostream& print (ostream& out, const SliceType& slice) {
181
+ apply ([&out](auto &&... args) {(( datalog::print (out, *args), ...));}, slice);
182
+ return out;
183
+ }
184
184
185
185
struct Iterator {
186
186
Iterator (const RelationsType& relations) : sizes(relationSizes(relations)) {
@@ -191,15 +191,21 @@ struct State {
191
191
}
192
192
193
193
template <typename RELATION_TYPE>
194
- void pick (const RELATION_TYPE& relation, unsigned int relationIndex) {
195
- cout << " pick relation " << relationIndex << " with " << index[relationIndex] << endl;
194
+ void pick (
195
+ const RELATION_TYPE& relation,
196
+ typename RELATION_TYPE::GroundType const *& sliceElement,
197
+ unsigned int relationIndex
198
+ ) {
199
+ // cout << "pick relation " << relationIndex << " with " << index[relationIndex] << endl;
200
+ // TODO: inefficient
201
+ sliceElement = &*std::next (relation.tuples .begin (), index[relationIndex]);
196
202
}
197
203
198
204
template <size_t ... Is>
199
- void pick (const RelationsType& relations, RelationsType & slice, index_sequence<Is...>) {
200
- cout << " [" << endl;
201
- ((pick (get<Is>(relations), Is)), ...);
202
- cout << " ]" << endl;
205
+ void pick (const RelationsType& relations, SliceType & slice, index_sequence<Is...>) {
206
+ // cout << "[" << endl;
207
+ ((pick (get<Is>(relations), get<Is>(slice), Is)), ...);
208
+ // cout << "]" << endl;
203
209
}
204
210
205
211
// Returns true if no more combinations of facts to iterate over
@@ -219,11 +225,13 @@ struct State {
219
225
return false ;
220
226
}
221
227
222
- RelationsType next (const State& state) {
223
- RelationsType slice;
224
- auto indexSequence = make_index_sequence<tuple_size<RelationsType>::value> { };
225
- pick (state.relations , slice, indexSequence);
228
+ SliceType next (const State& state) {
229
+ SliceType slice;
230
+ pick (state.relations , slice, make_index_sequence<tuple_size<RelationsType>::value> { });
226
231
iterationFinished = next (0 , true );
232
+ cout << " slice = " ;
233
+ print (cout, slice);
234
+ cout << endl;
227
235
return slice;
228
236
}
229
237
@@ -243,8 +251,7 @@ struct State {
243
251
244
252
static array<unsigned int , tuple_size<RelationsType>::value> relationSizes (const RelationsType& relations) {
245
253
array<unsigned int , tuple_size<RelationsType>::value> sizes;
246
- auto indexSequence = make_index_sequence<tuple_size<RelationsType>::value> { };
247
- relationSizes (relations, sizes, indexSequence);
254
+ relationSizes (relations, sizes, make_index_sequence<tuple_size<RelationsType>::value> { });
248
255
return sizes;
249
256
}
250
257
@@ -298,8 +305,7 @@ static void apply(
298
305
const typename RULE_TYPE::BodyType& body,
299
306
const typename State<RELATIONs...>::RelationsType& relations
300
307
) {
301
- auto indexSequence = make_index_sequence<tuple_size<typename RULE_TYPE::BodyType>::value> { };
302
- apply<RULE_TYPE, RELATIONs...>(body, relations, indexSequence);
308
+ apply<RULE_TYPE, RELATIONs...>(body, relations, make_index_sequence<tuple_size<typename RULE_TYPE::BodyType>::value> { });
303
309
}
304
310
305
311
template <typename RULE_TYPE, typename ... RELATIONs>
@@ -308,12 +314,14 @@ static void apply(const RULE_TYPE& rule, const State<RELATIONs...>& state) {
308
314
}
309
315
310
316
317
+ #if 0
311
318
template<typename RELATION_TYPE>
312
319
static RELATION_TYPE merge(const RELATION_TYPE& r1, const RELATION_TYPE& r2) {
313
320
set<typename RELATION_TYPE::GroundType> tuples { r1.tuples };
314
321
tuples.insert(r2.tuples.begin(), r2.tuples.end());
315
322
return RELATION_TYPE { tuples };
316
323
}
324
+ #endif
317
325
318
326
}
319
327
0 commit comments