8000 compute the slices; TODO: unresolved inefficiency due to indexing a set · Z80coder/datalog-cpp@1fab6bd · GitHub
[go: up one dir, main page]

Skip to content

Commit 1fab6bd

Browse files
author
wright
committed
compute the slices; TODO: unresolved inefficiency due to indexing a set
1 parent c6b7017 commit 1fab6bd

File tree

2 files changed

+43
-32
lines changed

2 files changed

+43
-32
lines changed

src/Datalog.h

Lines changed: 40 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -72,29 +72,24 @@ struct Relation {
7272
const set<GroundType> tuples;
7373
};
7474

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)
7777
{
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 << "] ";
8681
return out;
8782
}
8883

8984
template<typename RELATION_TYPE, size_t ... Is>
9085
static void unbind(const typename RELATION_TYPE::AtomicType& tuple, index_sequence<Is...>) {
86+
// TODO: why can't we use the apply pattern everywhere?
9187
((get<Is>(tuple).getSym()->unbind()), ...);
9288
}
9389

9490
template<typename RELATION_TYPE>
9591
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> { });
9893
}
9994

10095
// bind 1 SymbolOrValue with 1 Value
@@ -127,18 +122,18 @@ static optional<typename RELATION_TYPE::GroundType> bind(
127122
if (successfulBinding) {
128123
{
129124
cout << "bound ";
130-
print<RELATION_TYPE>(cout, atom);
125+
print(cout, atom);
131126
cout << " to ";
132-
print<RELATION_TYPE>(cout, boundAtom);
127+
print(cout, boundAtom);
133128
cout << endl;
134129
}
135130
return boundAtom;
136131
}
137132
{
138133
cout << "failed to bind ";
139-
print<RELATION_TYPE>(cout, atom);
134+
print(cout, atom);
140135
cout << " with ";
141-
print<RELATION_TYPE>(cout, fact);
136+
print(cout, fact);
142137
cout << endl;
143138
}
144139
return nullopt;
@@ -150,8 +145,7 @@ static optional<typename RELATION_TYPE::GroundType> bind(
150145
typename RELATION_TYPE::AtomicType& atom,
151146
const typename RELATION_TYPE::GroundType& fact
152147
) {
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> { });
155149
}
156150

157151
// bind 1 atom with a relation (n facts)
@@ -181,6 +175,12 @@ template<typename ... RELATIONs>
181175
struct State {
182176
typedef tuple<RELATIONs...> RelationsType;
183177
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+
}
184184

185185
struct Iterator {
186186
Iterator(const RelationsType& relations) : sizes(relationSizes(relations)) {
@@ -191,15 +191,21 @@ struct State {
191191
}
192192

193193
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]);
196202
}
197203

198204
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;
203209
}
204210

205211
// Returns true if no more combinations of facts to iterate over
@@ -219,11 +225,13 @@ struct State {
219225
return false;
220226
}
221227

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> { });
226231
iterationFinished = next(0, true);
232+
cout << "slice = ";
233+
print(cout, slice);
234+
cout << endl;
227235
return slice;
228236
}
229237

@@ -243,8 +251,7 @@ struct State {
243251

244252
static array<unsigned int, tuple_size<RelationsType>::value> relationSizes(const RelationsType& relations) {
245253
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> { });
248255
return sizes;
249256
}
250257

@@ -298,8 +305,7 @@ static void apply(
298305
const typename RULE_TYPE::BodyType& body,
299306
const typename State<RELATIONs...>::RelationsType& relations
300307
) {
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> { });
303309
}
304310

305311
template<typename RULE_TYPE, typename ... RELATIONs>
@@ -308,12 +314,14 @@ static void apply(const RULE_TYPE& rule, const State<RELATIONs...>& state) {
308314
}
309315

310316

317+
#if 0
311318
template<typename RELATION_TYPE>
312319
static RELATION_TYPE merge(const RELATION_TYPE& r1, const RELATION_TYPE& r2) {
313320
set<typename RELATION_TYPE::GroundType> tuples { r1.tuples };
314321
tuples.insert(r2.tuples.begin(), r2.tuples.end());
315322
return RELATION_TYPE { tuples };
316323
}
324+
#endif
317325

318326
}
319327

tests/types_test.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,10 @@ int main() {
2020
String_Int r4 {{{ { "hello", 1 } }}};
2121
String_Int r5 {{{ {"hello", 1 }, { "world", 2 } }}};
2222
String_Int_String r6 {{{ { "hello", 1, "world" }, { "world", 2, "hello" }, { "world", 3, "world" } }}};
23+
24+
#if 0
2325
String_Int r7 = merge(r4, r5);
26+
#endif
2427

2528
}
2629

0 commit comments

Comments
 (0)
0