-
Notifications
You must be signed in to change notification settings - Fork 0
/
types.hpp
653 lines (583 loc) · 17.2 KB
/
types.hpp
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
#ifndef DURAK_TYPES_H
#define DURAK_TYPES_H
#include <string>
#include <vector>
#include <algorithm>
#include <type_traits>
#include <memory>
#include <iostream>
#include <unordered_map>
#include <map>
#include <set>
#include <sstream>
#include <iterator>
#include <functional>
using namespace std;
// 0. objects are the base class
// 1. concepts are singleton objects
// 2. an action changes state or returns something
// 3. a relation exists between two objects
struct Concept;
struct List;
struct Object {
static int idcount;
int id;
// Constructors
Object(int _id) : id(_id) {}
Object(const Object &o) : id(o.id) {}
Object();
Object(const string &type);
Object(const Concept &c);
// Operators
virtual bool operator==(const Object &other) const {
return id == other.id;
}
virtual bool operator!=(const Object &other) const {
return !(*this == other);
}
virtual bool operator<(const Object &other) const {
return id < other.id;
}
bool is(const Concept c) const;
bool is(const string &str) const;
void give(const Object obj) const;
void make(const Concept c) const;
void make(const string &str) const;
void unmake(const Concept &c) const;
void unmake(const string &str) const;
Object get(const Concept c) const;
Object get(const string &str) const;
void remove(const Concept c) const;
void remove(const string &str) const;
// Search
size_t get_sig() const;
// Printing and debug
friend ostream& operator<<(ostream &os, const Object &o);
void dump(ostream &os) const;
};
// Should go in source file
int Object::idcount = 0;
struct Concept : public Object {
Concept(const string &n);
Concept(int _id) : Object(_id) {}
Concept(const Object &o) : Object(o.id) {}
Concept(const Concept &c) : Object(c.id) {}
virtual bool operator==(const Concept &other) const {
return id == other.id;
}
virtual bool operator!=(const Concept &other) const {
return !(*this == other);
}
friend ostream& operator<<(ostream &os, const Concept &c);
};
// The set of all concepts
set<string> concepts;
// "name" map for concepts
// no storing extra data in "budget polymorphic" Object subclasses Concept and List
unordered_map<int,string> name_map;
unordered_map<string,int> rev_name_map;
// Concept of concepts
int CONCEPT_ID = -1;
// "list" map for lists
unordered_map<int,vector<int>> list_map;
// "is" and "has" maps
// for Object to Concepts and Object to Objects, respectively
multimap<int,int> is_map;
multimap<int,int> has_map;
// Hash operator for vector<int>
// https://stackoverflow.com/questions/20511347/a-good-hash-function-for-a-vector
struct int_vector_hasher {
std::size_t operator()(std::vector<int> const& vec) const {
std::size_t seed = vec.size();
for(auto x : vec) {
x = ((x >> 16) ^ x) * 0x45d9f3b;
x = ((x >> 16) ^ x) * 0x45d9f3b;
x = (x >> 16) ^ x;
seed ^= x + 0x9e3779b9 + (seed << 6) + (seed >> 2);
}
return seed;
}
};
// Get list of concepts for the object
size_t Object::get_sig() const {
auto range = is_map.equal_range(id);
vector<int> ids;
for (auto it = range.first; it != range.second; it++) {
ids.push_back(it->second);
}
sort(ids.begin(), ids.end());
return int_vector_hasher{}(ids);
}
void Object::dump(ostream &os) const {
os << *this << endl;
auto range = has_map.equal_range(id);
int i=0;
for (auto it = range.first; it != range.second; it++) {
Object(it->second).dump(os);
cout << endl;
}
}
ostream &operator<<(ostream &os, const Concept &c) {
os << name_map[c.id];
return os;
}
// Only for the static initializer
struct ConceptInit {
ConceptInit() {
name_map[CONCEPT_ID] = "concept";
rev_name_map["concept"] = CONCEPT_ID;
concepts.insert("concept");
}
} concept_init;
// History stack
vector<tuple<int, int, int, int>> history;
// History Operations
enum MapType : int {
HasMap, IsMap, ListMap
};
enum MapOp : int {
Add, Remove
};
// Functional interface to has, is, and list maps
// While searching for correct action, keep track of state history
void map_add(multimap<int,int> &map, MapType type, int a, int b, bool record) {
map.insert({a,b});
if (record)
history.push_back({type, MapOp::Add, a, b});
}
void map_remove(multimap<int,int> &map, MapType type, int a, int b, bool record) {
auto range = map.equal_range(a);
for (auto it = range.first; it != range.second; it++) {
if (it->second == b) {
if (record)
history.push_back({type, MapOp::Remove, a, b});
map.erase(it);
return;
}
}
}
void has_add(int a, int b, bool record = true) {
map_add(has_map, MapType::HasMap, a, b, record);
}
void is_add(int a, int b, bool record = true) {
map_add(is_map, MapType::IsMap, a, b, record);
}
void has_remove(int a, int b, bool record = true) {
map_remove(has_map, MapType::HasMap, a, b, record);
}
void is_remove(int a, int b, bool record = true) {
map_remove(is_map, MapType::IsMap, a, b, record);
}
bool list_contains(int a, int b) {
auto lst = list_map[a];
for (int i=0; i<lst.size(); i++) {
if (lst[i] == b) return true;
}
return false;
}
void list_add(int a, int b, bool record = true) {
if (list_contains(a, b)) return;
list_map[a].push_back(b);
if (record)
history.push_back({MapType::ListMap, MapOp::Add, a, b});
}
void list_remove(int a, int b, bool record = true) {
if (!list_contains(a, b)) return;
auto lst = list_map[a];
for (auto it = lst.begin(); it != lst.end(); it++) {
if (*it == b) {
lst.erase(it);
if (record)
history.push_back({MapType::ListMap, MapOp::Remove, a, b});
return;
}
}
}
class Exception {};
// Special objects and concepts
Concept null("null"); // null object
Concept boolean("bool");
Object nullobj("null"); // null
Object yes("bool"); // true
Object no("bool"); // false
ostream &operator<<(ostream &os, const Object &o) {
if (o.is("concept")) {
os << Concept(o);
return os;
}
if (o == nullobj) {
os << "nullobj";
return os;
}
auto range = is_map.equal_range(o.id);
int i=0;
for (auto it = range.first; it != range.second; it++) {
if (i++ > 0) os << ',';
os << name_map[it->second];
}
return os;
}
// Concepts are singletons
Concept::Concept(const string &n) : Object() {
if (concepts.count(n) != 0) {
// Keep object equality
id = rev_name_map[n];
} else {
name_map[id] = n;
rev_name_map[n] = id;
concepts.insert(n);
}
is_add(id, CONCEPT_ID);
}
// For concept objects
Object::Object() : id(idcount++) {}
// Objects of concrete type
Object::Object(const string &type) : id(idcount++) {
Concept c(type);
Concept o("object");
is_add(id, c.id);
is_add(id, o.id);
}
// Concrete type again
Object::Object(const Concept &c) : id(idcount++) {
Concept o("object");
is_add(id, c.id);
is_add(id, o.id);
}
bool Object::is(const Concept c) const {
auto range = is_map.equal_range(id);
for (auto it = range.first; it != range.second; it++) {
if (it->second == c.id) {
return true;
}
}
return false;
}
bool Object::is(const string &str) const {
return is(Concept(str));
}
void Object::give(const Object o) const {
has_add(id, o.id);
}
void Object::make(const string &str) const {
make(Concept(str));
}
void Object::make(const Concept c) const {
is_add(id, c.id);
}
void Object::unmake(const Concept &c) const {
is_remove(id, c.id);
}
void Object::unmake(const string &str) const {
unmake(Concept(str));
}
// Get object property
Object Object::get(const Concept c) const {
auto range = has_map.equal_range(id);
for (auto it = range.first; it != range.second; it++) {
auto obj = Object(it->second);
if (obj.is(c)) {
return obj;
}
}
return nullobj;
}
Object Object::get(const string &str) const {
return get(Concept(str));
}
// Remove object property
void Object::remove(const Concept c) const {
has_remove(id, c.id);
/*auto range = has_map.equal_range(id);
for (auto it = range.first; it != range.second; it++) {
auto obj = Object(it->second);
if (obj.is(c)) {
has_remove(obj.id, c.id);
return;
}
}*/
}
void Object::remove(const string &str) const {
remove(Concept(str));
}
vector<int> objvec2intvec(const vector<Object> &objs) {
vector<int> res;
for (size_t i=0; i<objs.size(); i++) {
res.push_back(objs[i].id);
}
return res;
}
vector<Object> intvec2objvec(const vector<int> &ints) {
vector<Object> res;
for (size_t i=0; i<ints.size(); i++) {
res.push_back(Object(ints[i]));
}
return res;
}
// Useful because it's indexed
struct List : public Object {
List(int _id) : Object(_id) {}
List(const Object &o) : Object(o.id) {}
List(const string &field_name = "") : Object("list") {
if (field_name.length() != 0) {
make(Concept(field_name));
}
}
virtual bool operator==(const List &other) const {
return list_map[id] == list_map[other.id];
}
virtual bool operator!=(const List &other) const {
return !(*this == other);
}
Object operator[](int idx) const {
vector<Object> objects = intvec2objvec(list_map[id]);
if (objects.size() > idx) {
return objects.at(idx);
}
throw Exception();
}
vector<Object> get_objects() {
return intvec2objvec(list_map[id]);
}
int size() const {
return list_map[id].size();
}
void add(Object obj) {
list_add(id, obj.id);
}
void remove(Object obj) {
list_remove(id, obj.id);
}
int index_of(Object obj) {
vector<Object> objects = intvec2objvec(list_map[id]);
for (size_t i=0; i<objects.size(); i++) {
if (objects[i].id == obj.id) {
return i;
}
}
return -1;
}
};
// Action function type
typedef Object (*ActionFn)(const vector<Object> &);
// Action data structures
unordered_map<int,int> act_res_map;
unordered_map<int,ActionFn> act_map;
unordered_map<int,vector<int>> act_args_map;
//unordered_map<int,vector<int>> act_inst_map;
// An action takes objects as arguments and returns some value
struct Action : public Object {
static Action no_action;
Action() : Object("action") {}
Action(int _id) : Object(_id) {}
Action(const Action &a) : Object(a.id) {}
Action(const string &name, const string &rtype, const vector<string> &atypes, ActionFn fn) : Object(name) {
make("action");
act_res_map[id] = Concept(rtype).id;
auto &act_args = act_args_map[id];
for (size_t i=0; i<atypes.size(); i++) {
act_args.push_back(Concept(atypes[i]).id);
}
act_map[id] = fn;
}
Action(Concept name, Concept rtype, const vector<int> &atypes, ActionFn fn) : Object(name) {
make("action");
act_res_map[id] = rtype.id;
act_args_map[id] = atypes;
act_map[id] = fn;
}
Object eval(vector<Object> &args) {
return act_map[id](args);
}
Concept get_concept() {
auto range = is_map.equal_range(id);
for (auto it = range.first; it != range.second; it++) {
int cid = it->second;
if (name_map[cid] != "object" and name_map[cid] != "action") {
return Concept(cid);
}
}
return null;
}
string get_name() {
return name_map[get_concept().id];
}
size_t get_args_size() const {
return act_args_map[id].size();
}
/*Action instantiate(const vector<Object> &args) {
if (!is_compatible(args))
throw na;
Action a(get_concept(), Concept(act_res_map[id]), act_args_map[id], act_map[id]);
inst_map[a.id] = objvec2intvec(args);
}*/
bool is_compatible_arg(int idx, Object arg) {
return arg.is(Concept(act_args_map[id][idx]));
}
bool is_compatible(const vector<Object> &args) {
auto &act_args = act_args_map[id];
if (args.size() != act_args.size()) {
return false;
}
for (size_t i=0; i<args.size(); i++) {
Concept type(act_args[i]);
Object arg(args[i]);
if (!arg.is(type)) {
return false;
}
}
return true;
}
};
Action Action::no_action;
// TODO not used
// typedef tuple<int,int,int,int> History;
unordered_map<int,int> node_res_map;
unordered_map<int,vector<int>> node_parent_map;
unordered_map<int,int> node_act_map;
unordered_map<int,vector<int>> node_sig_map;
typedef const function<ostream &(ostream &, const Object&)> value_print;
struct Node {
static int idcount;
int id;
Node(int i) : id(i) {}
Node(const Node &n) : id(n.id) {}
Node(const Object &r) : id(idcount++) {
node_res_map[id] = r.id;
node_act_map[id] = Action::no_action.id;
sign();
}
Node(const Action &a) : id(idcount++) {
node_res_map[id] = nullobj.id;
node_act_map[id] = a.id;
sign();
}
Node(const Object &r, const Action &a, const vector<int> &p)
: id(idcount++) {
node_res_map[id] = r.id;
node_act_map[id] = a.id;
node_parent_map[id] = p;
sign();
}
Object get_res() const {
return node_res_map[id];
}
Action get_act() const {
return node_act_map[id];
}
vector<int> &get_sig() const {
return node_sig_map[id];
}
vector<int> get_shallow() const {
vector<int> shallow{node_act_map[id]};
vector<int> &parents = node_parent_map[id];
for (size_t i=0; i<parents.size(); i++) {
int resid = node_res_map[parents[i]];
shallow.push_back(resid);
}
return shallow;
}
void sign() {
vector<int> sig;
vector<int> &parents = node_parent_map[id];
int resid = node_res_map[id];
int actid = node_act_map[id];
sig.push_back(Object(resid).get_sig());
sig.push_back(actid);
for (size_t i=0; i<parents.size(); i++) {
sig.insert(
sig.end(),
node_sig_map[parents[i]].begin(),
node_sig_map[parents[i]].end());
}
node_sig_map[id] = sig;
}
string sig_str() const {
stringstream str;
vector<int> sig = node_sig_map[id];
copy(sig.begin(), sig.end(), ostream_iterator<int>(str, " "));
return str.str();
}
Object eval() {
vector<Object> args;
vector<int> parents = node_parent_map[id];
Action act(node_act_map[id]);
for (size_t i=0; i<parents.size(); i++) {
Object res = node_res_map[parents[i]];
args.push_back(res);
}
return act.eval(args);
}
void remove() {
node_act_map.erase(id);
node_res_map.erase(id);
node_parent_map.erase(id);
node_sig_map.erase(id);
}
void print(ostream &os, size_t lvl = 0, value_print &vp = {}) {
Action act(node_act_map[id]);
Object res(node_res_map[id]);
vector<int> parents = node_parent_map[id];
for (size_t i=0; i<lvl; i++) {
os << '\t';
}
os << act.get_name() << " (" << res << ") ";
if (vp) vp(os, res);
os << endl;
for (size_t i=0; i<parents.size(); i++) {
Node(parents[i]).print(os, lvl+1, vp);
}
}
bool operator==(const Node &other) const {
return id == other.id;
}
};
int Node::idcount = 0;
void print_nodes(ostream &os, vector<Node> &nodes, value_print &vp = {}) {
for (size_t i=0; i<nodes.size(); i++) {
nodes[i].print(os, 0, vp);
}
}
void print_objs(ostream &os, vector<int> &ids) {
for (size_t i=0; i<ids.size(); i++) {
os << Object(ids[i]) << ' ';
}
os << endl;
}
# define MAKEACTION(a,ret,args) Action a##_action(#a,ret,args,a)
// Most general functions
Object get_field(const vector<Object> &args) {
return args[0].get(Concept(args[1].id));
}
MAKEACTION(get_field, "get-field", (vector<string>{"object", "concept"}));
// Expand list has special behavior in search code
Object expand_list(const vector<Object> &args) {
return args[0];
}
MAKEACTION(expand_list, "list", (vector<string>{"list"}));
/*
// A relation between two objects
struct Relation : public Object {
Object &from;
Object &to;
Relation(const string &typ, Object f, Object t) : Object(typ), from(f), to(t) {}
};
vector<Relation> relations;
// Convenience
struct Number : public Object {
int val;
Number(int v = 0) : Object("number"), val(v) {}
bool operator==(const Number &other) const {
return val == other.val;
}
bool operator!=(const Number &other) const {
return val != other.val;
}
bool operator>(const Number &other) const {
return val > other.val;
}
bool operator<(const Number &other) const {
return val < other.val;
}
};*/
#endif