-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtree_semantics.h
3109 lines (2884 loc) · 104 KB
/
tree_semantics.h
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
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* tree_semantics.h
*
* Created on: Jul 21, 2015
* Author: asaparov
*/
#ifndef TREE_SEMANTICS_H_
#define TREE_SEMANTICS_H_
#include "parser.h"
#include <atomic>
#include <core/map.h>
#include <limits.h>
#include <math.h>
using namespace core;
#define LABEL_EITHER (UINT_MAX - 3)
#define LABEL_EMPTY (UINT_MAX - 2)
#define LABEL_WILDCARD_TREE (UINT_MAX - 1)
#define LABEL_WILDCARD (UINT_MAX)
#define CHECK_ARITY /* comment this to disable arity checking */
//#define INTERSECTION_CACHE /* comment this to disable the intersection cache */
//#define PRECOMPUTE_HASH /* comment this to disable precomputation of hash values */
/* TODO: these are for profiling; delete them */
/*#include <core/timer.h>
double profiler_intersect_node = 0.0;
double profiler_intersect_first_null = 0.0;
double profiler_intersect_second_null = 0.0;
double profiler_intersect_first_wildcard_tree = 0.0;
double profiler_intersect_second_wildcard_tree = 0.0;
double profiler_intersect_first_wildcard = 0.0;
double profiler_intersect_second_wildcard = 0.0;
double profiler_intersect_first_either = 0.0;
double profiler_intersect_second_either = 0.0;
double profiler_intersect_hash = 0.0;
double profiler_tree_semantics_equality = 0.0;*/
/* TODO: these are for debugging; delete them */
bool semantics_debug_flag = false;
unsigned int semantics_debug = 0;
/* forward declarations */
template<typename V> struct tree_semantics_trie;
#if defined(CHECK_ARITY)
enum arity {
TREE_SEMANTICS_ARITY_ZERO,
TREE_SEMANTICS_ARITY_UNARY,
TREE_SEMANTICS_ARITY_BINARY,
TREE_SEMANTICS_ARITY_EITHER
};
array<arity> arity_table = array<arity>(16);
inline bool min_arity_zero(unsigned int label) {
if (label == LABEL_WILDCARD || label >= arity_table.length)
return true;
return (arity_table[label] == TREE_SEMANTICS_ARITY_EITHER || arity_table[label] == TREE_SEMANTICS_ARITY_ZERO);
}
inline bool arity_one(unsigned int label) {
if (label == LABEL_WILDCARD || label >= arity_table.length)
return true;
return (arity_table[label] != TREE_SEMANTICS_ARITY_ZERO);
}
inline bool min_arity_two(unsigned int label) {
if (label == LABEL_WILDCARD || label >= arity_table.length)
return false;
return arity_table[label] == TREE_SEMANTICS_ARITY_BINARY;
}
inline bool max_arity_two(unsigned int label) {
if (label == LABEL_WILDCARD || label >= arity_table.length)
return true;
return (arity_table[label] == TREE_SEMANTICS_ARITY_EITHER || arity_table[label] == TREE_SEMANTICS_ARITY_BINARY);
}
#else /* CHECK_ARITY */
constexpr bool min_arity_zero(unsigned int label) { return true; }
constexpr bool arity_one(unsigned int label) { return true; }
constexpr bool min_arity_two(unsigned int label) { return false; }
constexpr bool max_arity_two(unsigned int label) { return true; }
#endif /* CHECK_ARITY */
struct tree_semantics {
unsigned int label;
tree_semantics* left_child;
tree_semantics* right_child;
unsigned int* excluded;
unsigned int excluded_count;
mutable std::atomic_uint reference_count;
#if defined(PRECOMPUTE_HASH)
unsigned int hash_value;
#endif
template<typename V> using trie = tree_semantics_trie<V>;
tree_semantics() : left_child(NULL), right_child(NULL), excluded_count(0), reference_count(1) { }
explicit tree_semantics(unsigned int label) :
label(label), left_child(NULL), right_child(NULL), excluded_count(0), reference_count(1)
{
recompute_hash();
}
tree_semantics(const tree_semantics& src) : reference_count(1) {
if (!initialize(src))
exit(EXIT_FAILURE);
}
~tree_semantics() { free(); }
inline void operator = (const tree_semantics& src) {
reference_count = 1;
if (!initialize(src))
exit(EXIT_FAILURE);
}
/* for debugging */
inline bool is_valid(hash_map<const tree_semantics*, unsigned int>& reference_counts) const
{
bool contains; unsigned int index;
if (!reference_counts.check_size()) return false;
unsigned int& count = reference_counts.get(this, contains, index);
if (!contains) {
reference_counts.table.keys[index] = this;
reference_counts.values[index] = 1;
reference_counts.table.size++;
} else count++;
if (reference_count == 0) return false;
if (left_child != NULL && !left_child->is_valid(reference_counts)) return false;
if (right_child != NULL && !right_child->is_valid(reference_counts)) return false;
if (excluded_count > 0 && (label == LABEL_WILDCARD || label == LABEL_WILDCARD_TREE))
return false;
return true;
}
inline bool check_reference_counts(const hash_map<const tree_semantics*, unsigned int>& reference_counts) const {
bool contains;
unsigned int expected = reference_counts.get(this, contains);
if (!contains) return false;
if (expected != reference_count) {
fprintf(stderr, "tree_semantics.check_reference_counts ERROR:"
" Reference counter is invalid. Expected %u but counter "
"is currently %u.\n", expected, reference_count.load());
return false;
}
if (left_child != NULL && !left_child->check_reference_counts(reference_counts))
return false;
if (right_child != NULL && !right_child->check_reference_counts(reference_counts))
return false;
return true;
}
inline bool exclude(unsigned int item)
{
if (excluded_count == 0) excluded = NULL;
unsigned int* new_excluded = (unsigned int*) realloc(excluded, sizeof(double) * (excluded_count + 1));
if (new_excluded == NULL) {
fprintf(stderr, "tree_semantics.exclude ERROR: Out of memory.\n");
return false;
}
excluded = new_excluded;
unsigned int index = strict_linear_search(excluded, item, 0, excluded_count);
if (index > 0 && excluded[index - 1] == item)
return true;
shift_right(excluded, excluded_count, index);
excluded[index] = item;
excluded_count++;
return true;
}
inline bool exclude(const unsigned int* items, unsigned int item_count)
{
unsigned int* excluded_union = (unsigned int*) malloc(
sizeof(unsigned int) * (excluded_count + item_count));
if (excluded_union == NULL) {
fprintf(stderr, "tree_semantics.exclude ERROR: Out of memory.\n");
return false;
}
unsigned int excluded_union_count = 0;
set_union(excluded_union, excluded_union_count, excluded, excluded_count, items, item_count);
if (excluded_union_count == 0) {
core::free(excluded_union);
return true;
} else if (excluded_count > 0)
core::free(excluded);
excluded = excluded_union;
excluded_count = excluded_union_count;
return true;
}
enum feature {
FEATURE_NULL = 0,
FEATURE_LABEL = 1,
FEATURE_LABEL_LEFT = 2,
FEATURE_LABEL_RIGHT = 3
};
enum function_type {
FUNCTION_EMPTY = 0,
FUNCTION_IDENTITY = 1,
FUNCTION_LEFT = 2,
FUNCTION_RIGHT = 3
};
struct function {
function_type type;
constexpr function(const function_type& type) : type(type) { }
static inline unsigned int hash(const function& f) {
return default_hash(f.type);
}
static inline bool is_empty(const function& f) {
return f.type == FUNCTION_EMPTY;
}
static inline void set_empty(function& f) {
f.type = FUNCTION_EMPTY;
}
};
template<typename Stream>
static inline bool print(const function& func, Stream& out) {
switch (func.type) {
case tree_semantics::FUNCTION_IDENTITY:
return core::print("identity", out);
case tree_semantics::FUNCTION_LEFT:
return core::print("left", out);
case tree_semantics::FUNCTION_RIGHT:
return core::print("right", out);
default:
fprintf(stderr, "print ERROR: Unrecognized tree_semantics::function type.\n");
return false;
}
}
inline void recompute_hash() {
#if defined(PRECOMPUTE_HASH)
hash_value = compute_hash(*this);
#endif
}
static inline unsigned int hash(const tree_semantics& logical_form) {
#if defined(PRECOMPUTE_HASH)
return logical_form.hash_value;
#else
return compute_hash(logical_form);
#endif
}
static inline unsigned int compute_hash(const tree_semantics& logical_form) {
unsigned int hash_value = default_hash(logical_form.label);
if (logical_form.left_child != NULL)
hash_value ^= hash(*logical_form.left_child);
if (logical_form.right_child != NULL)
hash_value ^= hash(*logical_form.right_child);
if (logical_form.excluded_count > 0)
hash_value ^= default_hash(logical_form.excluded, logical_form.excluded_count);
return hash_value;
}
static inline bool is_empty(const tree_semantics& logical_form) {
return logical_form.label == 0;
}
static inline void move(const tree_semantics& src, tree_semantics& dst) {
dst.label = src.label;
dst.left_child = src.left_child;
dst.right_child = src.right_child;
dst.excluded_count = src.excluded_count;
dst.excluded = src.excluded;
dst.reference_count = src.reference_count.load();
#if defined(PRECOMPUTE_HASH)
dst.hash_value = src.hash_value;
#endif
}
static inline void swap(tree_semantics& first, tree_semantics& second) {
core::swap(first.label, second.label);
core::swap(first.left_child, second.left_child);
core::swap(first.right_child, second.right_child);
core::swap(first.excluded, second.excluded);
core::swap(first.excluded_count, second.excluded_count);
first.reference_count = second.reference_count.exchange(first.reference_count);
#if defined(PRECOMPUTE_HASH)
core::swap(first.hash_value, second.hash_value);
#endif
}
static inline void free(tree_semantics& logical_form) {
logical_form.free();
}
static constexpr function default_function() {
return FUNCTION_EMPTY;
}
static constexpr bool is_feature_pruneable(feature f) {
return true;
}
static const function functions[];
static const pair<function, function> transformations[];
static const double log_function_count;
static const double log_transformation_count;
private:
inline bool initialize(const tree_semantics& src) {
label = src.label;
left_child = src.left_child;
right_child = src.right_child;
excluded_count = src.excluded_count;
if (excluded_count > 0) {
excluded = (unsigned int*) malloc(sizeof(unsigned int) * excluded_count);
if (excluded == NULL) {
fprintf(stderr, "tree_semantics.initialize ERROR: Out of memory.\n");
return false;
}
memcpy(excluded, src.excluded, sizeof(unsigned int) * excluded_count);
}
if (left_child != NULL)
left_child->reference_count++;
if (right_child != NULL)
right_child->reference_count++;
#if defined(PRECOMPUTE_HASH)
hash_value = src.hash_value;
#endif
return true;
}
inline void free() {
reference_count--;
if (reference_count == 0) {
if (excluded_count > 0)
core::free(excluded);
if (left_child != NULL) {
free(*left_child);
if (left_child->reference_count == 0)
core::free(left_child);
}
if (right_child != NULL) {
free(*right_child);
if (right_child->reference_count == 0)
core::free(right_child);
}
}
}
};
const tree_semantics::function tree_semantics::functions[] = { FUNCTION_IDENTITY, FUNCTION_LEFT, FUNCTION_RIGHT };
const pair<tree_semantics::function, tree_semantics::function> tree_semantics::transformations[] = {
{FUNCTION_IDENTITY, FUNCTION_LEFT}, {FUNCTION_LEFT, FUNCTION_IDENTITY},
{FUNCTION_IDENTITY, FUNCTION_RIGHT}, {FUNCTION_LEFT, FUNCTION_RIGHT}
//{FUNCTION_IDENTITY, FUNCTION_LEFT}, {FUNCTION_LEFT, FUNCTION_IDENTITY}
};
const double tree_semantics::log_function_count = log(array_length(tree_semantics::functions));
const double tree_semantics::log_transformation_count = log(array_length(tree_semantics::transformations));
tree_semantics WILDCARD_TREE = tree_semantics(LABEL_WILDCARD_TREE);
tree_semantics EMPTY_TREE = tree_semantics(LABEL_EMPTY);
static inline void initialize_any(tree_semantics& logical_form) {
logical_form.label = LABEL_WILDCARD_TREE;
logical_form.excluded_count = 0;
logical_form.reference_count = 1;
logical_form.left_child = NULL;
logical_form.right_child = NULL;
#if defined(PRECOMPUTE_HASH)
logical_form.hash_value = WILDCARD_TREE.hash_value;
#endif
}
static inline void initialize_empty(tree_semantics& logical_form) {
logical_form.label = LABEL_EMPTY;
logical_form.excluded_count = 0;
logical_form.reference_count = 1;
logical_form.left_child = NULL;
logical_form.right_child = NULL;
#if defined(PRECOMPUTE_HASH)
logical_form.hash_value = EMPTY_TREE.hash_value;
#endif
}
#if defined(INTERSECTION_CACHE)
struct intersection_cache {
hash_map<pair<tree_semantics, tree_semantics>, tree_semantics*> map;
intersection_cache() : map(16384) { }
~intersection_cache() {
for (auto entry : map) {
free(entry.key.key);
free(entry.key.value);
if (entry.value != NULL) {
free(*entry.value);
if (entry.value->reference_count == 0)
free(entry.value);
}
}
}
};
intersection_cache intersections;
#endif
inline bool is_excluded(const unsigned int* excluded, unsigned int excluded_count, unsigned int item) {
return index_of(item, excluded, excluded_count) < excluded_count;
}
inline bool is_excluded(const tree_semantics& logical_form, unsigned int item) {
return is_excluded(logical_form.excluded, logical_form.excluded_count, item);
}
template<typename Stream, typename Printer>
inline bool print(const tree_semantics& tree, Stream& out, Printer& printer) {
bool success = true;
if (tree.left_child == NULL) {
success &= print(tree.label, out, printer);
if (tree.right_child != NULL) {
fprintf(stderr, "print WARNING: Unary node is right-leaning.\n");
success &= print(*tree.right_child, out, printer);
}
} else {
success &= print(tree.label, out, printer);
if (tree.left_child != NULL) {
success &= print('(', out) && print(*tree.left_child, out, printer);
if (tree.right_child != NULL)
success &= print(',', out) && print(*tree.right_child, out, printer);
success &= print(')', out);
}
}
return success;
}
template<typename Stream>
inline bool read(tree_semantics::function& function, Stream& stream) {
unsigned char c;
if (!read(c, stream)) return false;
function.type = static_cast<tree_semantics::function_type>(c);
return true;
}
template<typename Stream>
inline bool write(const tree_semantics::function& function, Stream& stream) {
return write((unsigned char) function.type, stream);
}
template<typename Stream>
inline bool read(tree_semantics::feature& feature, Stream& stream) {
unsigned char c;
if (!read(c, stream)) return false;
feature = static_cast<tree_semantics::feature>(c);
return true;
}
template<typename Stream>
inline bool write(const tree_semantics::feature& feature, Stream& stream) {
return write((unsigned char) feature, stream);
}
#if defined(CHECK_ARITY)
inline bool check_arity(unsigned int label, const tree_semantics& logical_form)
{
if (label >= arity_table.length) {
return true;
} else if (arity_table[label] == TREE_SEMANTICS_ARITY_ZERO) {
if (logical_form.left_child != NULL
&& (logical_form.left_child->label != LABEL_WILDCARD_TREE || is_excluded(*logical_form.left_child, LABEL_EMPTY)))
return false;
if (logical_form.right_child != NULL
&& (logical_form.right_child->label != LABEL_WILDCARD_TREE || is_excluded(*logical_form.right_child, LABEL_EMPTY)))
return false;
return true;
} else if (arity_table[label] == TREE_SEMANTICS_ARITY_UNARY) {
if (logical_form.left_child == NULL)
return false;
if (logical_form.right_child != NULL
&& (logical_form.right_child->label != LABEL_WILDCARD_TREE || is_excluded(*logical_form.right_child, LABEL_EMPTY)))
return false;
return true;
} else if (arity_table[label] == TREE_SEMANTICS_ARITY_BINARY) {
if (logical_form.left_child == NULL)
return false;
if (logical_form.right_child == NULL)
return false;
return true;
} else {
return true;
}
}
#else
constexpr bool check_arity(unsigned int label, const tree_semantics& logical_form) { return true; }
#endif
/* forward declarations */
bool operator != (const tree_semantics& first, const tree_semantics& second);
template<bool FirstReferenceable, bool SecondReferenceable>
bool intersect(tree_semantics*& dst, const tree_semantics* first, const tree_semantics* second);
void set_union(tree_semantics*& dst, const tree_semantics* first, const tree_semantics* second);
bool operator == (const tree_semantics& first, const tree_semantics& second)
{
if (first.label != second.label)
return false;
#if defined(PRECOMPUTE_HASH)
if (first.hash_value != second.hash_value)
return false;
#endif
if (first.excluded_count != second.excluded_count)
return false;
/* we assume the excluded array is sorted */
for (unsigned int i = 0; i < first.excluded_count; i++)
if (first.excluded[i] != second.excluded[i])
return false;
/* compare child nodes */
if (first.left_child == NULL) {
if (second.left_child != NULL)
return false;
} else if (second.left_child == NULL) {
return false;
} else if (first.left_child != second.left_child
&& !(*first.left_child == *second.left_child)) {
return false;
}
if (first.right_child == NULL) {
if (second.right_child != NULL)
return false;
} else if (second.right_child == NULL) {
return false;
} else if (first.right_child != second.right_child
&& !(*first.right_child == *second.right_child)) {
return false;
}
return true;
}
inline bool operator != (const tree_semantics& first, const tree_semantics& second) {
return !(first == second);
}
constexpr bool operator == (const tree_semantics::function& first, const tree_semantics::function& second) {
return first.type == second.type;
}
constexpr bool operator != (const tree_semantics::function& first, const tree_semantics::function& second) {
return first.type != second.type;
}
constexpr bool operator < (const tree_semantics::function& first, const tree_semantics::function& second) {
return first.type < second.type;
}
inline void new_tree_without_excluded(
tree_semantics& dst, unsigned int label,
tree_semantics* left_child, tree_semantics* right_child)
{
dst.label = label;
dst.reference_count = 1;
dst.left_child = left_child;
dst.right_child = right_child;
if (left_child != NULL) left_child->reference_count++;
if (right_child != NULL) right_child->reference_count++;
}
inline bool new_tree(tree_semantics& dst, unsigned int label,
tree_semantics* left_child, tree_semantics* right_child,
const unsigned int* excluded, unsigned int excluded_count)
{
new_tree_without_excluded(dst, label, left_child, right_child);
dst.excluded_count = excluded_count;
if (excluded_count > 0) {
dst.excluded = (unsigned int*) malloc(sizeof(unsigned int) * excluded_count);
if (dst.excluded == NULL) {
fprintf(stderr, "new_tree ERROR: Insufficient memory for excluded set.\n");
dst.excluded_count = 0;
free(dst);
return false;
}
memcpy(dst.excluded, excluded, sizeof(unsigned int) * excluded_count);
}
dst.recompute_hash();
return true;
}
inline tree_semantics* new_empty_tree(unsigned int label)
{
tree_semantics* logical_form = (tree_semantics*) malloc(sizeof(tree_semantics));
if (logical_form == NULL) {
fprintf(stderr, "new_tree ERROR: Out of memory.\n");
return NULL;
}
logical_form->label = label;
logical_form->reference_count = 1;
return logical_form;
}
inline tree_semantics* new_tree(unsigned int label,
tree_semantics* left_child, tree_semantics* right_child,
const unsigned int* excluded, unsigned int excluded_count)
{
tree_semantics* logical_form = (tree_semantics*) malloc(sizeof(tree_semantics));
if (logical_form == NULL) {
fprintf(stderr, "new_tree ERROR: Out of memory.\n");
return NULL;
} else if (!new_tree(*logical_form, label, left_child, right_child, excluded, excluded_count)) {
free(logical_form);
return NULL;
}
return logical_form;
}
inline tree_semantics* new_tree_without_excluded(
unsigned int label, tree_semantics* left_child, tree_semantics* right_child)
{
tree_semantics* logical_form = (tree_semantics*) malloc(sizeof(tree_semantics));
if (logical_form == NULL) {
fprintf(stderr, "new_tree ERROR: Out of memory.\n");
return NULL;
}
new_tree_without_excluded(*logical_form, label, left_child, right_child);
return logical_form;
}
inline tree_semantics* copy(const tree_semantics& src) {
return new_tree(src.label, src.left_child, src.right_child, src.excluded, src.excluded_count);
}
inline tree_semantics* copy_without_excluded(const tree_semantics& src) {
return new_tree(src.label, src.left_child, src.right_child, NULL, 0);
}
const tree_semantics* apply(tree_semantics::function function, const tree_semantics& src)
{
switch (function.type) {
case tree_semantics::FUNCTION_IDENTITY:
return &src;
case tree_semantics::FUNCTION_LEFT:
return src.left_child;
case tree_semantics::FUNCTION_RIGHT:
return src.right_child;
default:
fprintf(stderr, "apply ERROR: Unrecognized transformation function.\n");
exit(EXIT_FAILURE);
}
}
bool apply(tree_semantics::function function, const tree_semantics& src, tree_semantics& dst)
{
switch (function.type) {
case tree_semantics::FUNCTION_IDENTITY:
dst = src;
break;
case tree_semantics::FUNCTION_LEFT:
if (src.label == LABEL_WILDCARD_TREE) {
dst = WILDCARD_TREE;
return true;
} else if (src.left_child == NULL) return false;
dst = *src.left_child;
break;
case tree_semantics::FUNCTION_RIGHT:
if (src.label == LABEL_WILDCARD_TREE) {
dst = WILDCARD_TREE;
return true;
} else if (src.right_child == NULL) return false;
dst = *src.right_child;
break;
default:
fprintf(stderr, "apply ERROR: Unrecognized transformation function.\n");
return false;
}
return true;
}
bool intersect_excluded(tree_semantics& intersection,
const tree_semantics& first, const tree_semantics& second)
{
intersection.excluded_count = 0;
unsigned int excluded_count = first.excluded_count + second.excluded_count;
if (excluded_count > 0) {
intersection.excluded = (unsigned int*) malloc(sizeof(unsigned int) * excluded_count);
if (intersection.excluded == NULL) {
fprintf(stderr, "intersect_excluded ERROR: Unable to compute union of excluded sets.\n");
return false;
}
set_union(intersection.excluded, intersection.excluded_count,
first.excluded, first.excluded_count, second.excluded, second.excluded_count);
}
return true;
}
inline bool intersect_children(tree_semantics& intersection,
const tree_semantics& first, const tree_semantics& second)
{
/* intersect the children */
intersection.left_child = NULL;
intersection.right_child = NULL;
if (!intersect<true, true>(intersection.left_child, first.left_child, second.left_child)
|| !intersect<true, true>(intersection.right_child, first.right_child, second.right_child))
{
free(intersection);
return false;
}
return true;
}
template<bool IntersectDescendants>
inline bool intersect_node(tree_semantics& dst, const tree_semantics* first, const tree_semantics* second)
{
//timer stopwatch_function;
#if !defined(NDEBUG)
if ((first != NULL && first->label == LABEL_EITHER && first->excluded_count < 2)
|| (second != NULL && second->label == LABEL_EITHER && second->excluded_count < 2))
fprintf(stderr, "intersect_node WARNING: Invalid union-type tree_semantics structures.\n");
#endif
/* intersect the labels */
if (first == NULL || first->label == LABEL_EMPTY) {
//timer stopwatch;
if (second == NULL || second->label == LABEL_EMPTY) {
/* don't need to check exclusions */
} else if (second->label == LABEL_WILDCARD || second->label == LABEL_WILDCARD_TREE) {
if (is_excluded(*second, LABEL_EMPTY)) {
//profiler_intersect_first_null += stopwatch.nanoseconds();
//profiler_intersect_node += stopwatch_function.nanoseconds();
return false;
}
} else if (second->label == LABEL_EITHER) {
if (!is_excluded(*second, LABEL_EMPTY))
return false;
} else {
return false;
}
dst.label = LABEL_EMPTY; dst.reference_count = 1; dst.excluded_count = 0;
dst.left_child = NULL; dst.right_child = NULL;
return true;
//profiler_intersect_first_null += stopwatch.nanoseconds();
//profiler_intersect_node += stopwatch_function.nanoseconds();
} else if (second == NULL || second->label == LABEL_EMPTY) {
//timer stopwatch;
if (first->label == LABEL_WILDCARD || first->label == LABEL_WILDCARD_TREE) {
if (is_excluded(*first, LABEL_EMPTY)) {
//profiler_intersect_second_null += stopwatch.nanoseconds();
//profiler_intersect_node += stopwatch_function.nanoseconds();
return false;
}
} else if (first->label == LABEL_EITHER) {
if (!is_excluded(*first, LABEL_EMPTY))
return false;
} else {
return false;
}
dst.label = LABEL_EMPTY; dst.reference_count = 1; dst.excluded_count = 0;
dst.left_child = NULL; dst.right_child = NULL;
//profiler_intersect_second_null += stopwatch.nanoseconds();
//profiler_intersect_node += stopwatch_function.nanoseconds();
return true;
} else if (first->label == LABEL_WILDCARD_TREE) {
//timer stopwatch;
if (second->label == LABEL_WILDCARD || second->label == LABEL_WILDCARD_TREE) {
if (IntersectDescendants) {
dst = *second;
if (!dst.exclude(first->excluded, first->excluded_count))
exit(EXIT_FAILURE);
} else {
dst.label = second->label;
dst.reference_count = 1;
dst.excluded_count = 0;
if (first->excluded_count + second->excluded_count > 0) {
dst.excluded = (unsigned int*) malloc(
sizeof(unsigned int) * (first->excluded_count + second->excluded_count));
set_union(dst.excluded, dst.excluded_count,
first->excluded, first->excluded_count,
second->excluded, second->excluded_count);
}
}
} else if (second->label == LABEL_EITHER) {
if (IntersectDescendants) {
dst = *second;
set_subtract(dst.excluded, dst.excluded_count, first->excluded, first->excluded_count);
} else {
dst.label = second->label;
dst.reference_count = 1;
dst.excluded_count = 0;
dst.excluded = (unsigned int*) malloc(sizeof(unsigned int) * second->excluded_count);
set_subtract(dst.excluded, dst.excluded_count,
second->excluded, second->excluded_count,
first->excluded, first->excluded_count);
}
if (dst.excluded_count == 0) {
free(dst.excluded); free(dst);
//profiler_intersect_first_wildcard_tree += stopwatch.nanoseconds();
//profiler_intersect_node += stopwatch_function.nanoseconds();
return false;
} else if (dst.excluded_count == 1) {
dst.label = dst.excluded[0];
free(dst.excluded);
dst.excluded_count = 0;
}
} else {
if (is_excluded(*first, second->label)) {
//profiler_intersect_first_wildcard_tree += stopwatch.nanoseconds();
//profiler_intersect_node += stopwatch_function.nanoseconds();
return false;
}
if (IntersectDescendants) {
dst = *second;
} else {
dst.label = second->label;
dst.reference_count = 1;
dst.excluded_count = 0;
}
}
//profiler_intersect_first_wildcard_tree += stopwatch.nanoseconds();
//profiler_intersect_node += stopwatch_function.nanoseconds();
return true;
} else if (second->label == LABEL_WILDCARD_TREE) {
//timer stopwatch;
if (first->label == LABEL_WILDCARD) {
if (IntersectDescendants) {
dst = *first;
if (!dst.exclude(second->excluded, second->excluded_count))
exit(EXIT_FAILURE);
} else {
dst.label = first->label;
dst.reference_count = 1;
dst.excluded_count = 0;
if (first->excluded_count + second->excluded_count > 0) {
dst.excluded = (unsigned int*) malloc(
sizeof(unsigned int) * (first->excluded_count + second->excluded_count));
set_union(dst.excluded, dst.excluded_count,
first->excluded, first->excluded_count,
second->excluded, second->excluded_count);
}
}
} else if (first->label == LABEL_EITHER) {
if (IntersectDescendants) {
dst = *first;
set_subtract(dst.excluded, dst.excluded_count, second->excluded, second->excluded_count);
} else {
dst.label = first->label;
dst.reference_count = 1;
dst.excluded_count = 0;
dst.excluded = (unsigned int*) malloc(sizeof(unsigned int) * first->excluded_count);
set_subtract(dst.excluded, dst.excluded_count,
first->excluded, first->excluded_count,
second->excluded, second->excluded_count);
}
if (dst.excluded_count == 0) {
free(dst.excluded); free(dst);
//profiler_intersect_second_wildcard_tree += stopwatch.nanoseconds();
//profiler_intersect_node += stopwatch_function.nanoseconds();
return false;
} else if (dst.excluded_count == 1) {
dst.label = dst.excluded[0];
free(dst.excluded);
dst.excluded_count = 0;
}
} else {
if (is_excluded(*second, first->label)) {
//profiler_intersect_second_wildcard_tree += stopwatch.nanoseconds();
//profiler_intersect_node += stopwatch_function.nanoseconds();
return false;
}
if (IntersectDescendants) {
dst = *first;
} else {
dst.label = first->label;
dst.reference_count = 1;
dst.excluded_count = 0;
}
}
//profiler_intersect_second_wildcard_tree += stopwatch.nanoseconds();
//profiler_intersect_node += stopwatch_function.nanoseconds();
return true;
} else if (first->label == LABEL_WILDCARD) {
//timer stopwatch;
if (second->label == LABEL_WILDCARD) {
dst.label = LABEL_WILDCARD;
if (!intersect_excluded(dst, *first, *second))
exit(EXIT_FAILURE);
} else if (second->label == LABEL_EITHER) {
dst.label = LABEL_EITHER;
dst.excluded = (unsigned int*) malloc(sizeof(unsigned int) * second->excluded_count);
dst.excluded_count = 0;
set_subtract(dst.excluded, dst.excluded_count,
second->excluded, second->excluded_count,
first->excluded, first->excluded_count);
if (dst.excluded_count == 0) {
free(dst.excluded);
//profiler_intersect_first_wildcard += stopwatch.nanoseconds();
//profiler_intersect_node += stopwatch_function.nanoseconds();
return false;
} else if (dst.excluded_count == 1) {
dst.label = dst.excluded[0];
free(dst.excluded);
dst.excluded_count = 0;
}
} else {
if (is_excluded(*first, second->label)) {
//profiler_intersect_first_wildcard += stopwatch.nanoseconds();
//profiler_intersect_node += stopwatch_function.nanoseconds();
return false;
}
dst.label = second->label;
dst.excluded_count = 0;
}
//profiler_intersect_first_wildcard += stopwatch.nanoseconds();
//profiler_intersect_node += stopwatch_function.nanoseconds();
} else if (second->label == LABEL_WILDCARD) {
//timer stopwatch;
if (first->label == LABEL_EITHER) {
dst.label = LABEL_EITHER;
dst.excluded = (unsigned int*) malloc(sizeof(unsigned int) * first->excluded_count);
dst.excluded_count = 0;
set_subtract(dst.excluded, dst.excluded_count,
first->excluded, first->excluded_count,
second->excluded, second->excluded_count);
if (dst.excluded_count == 0) {
free(dst.excluded);
//profiler_intersect_second_wildcard += stopwatch.nanoseconds();
//profiler_intersect_node += stopwatch_function.nanoseconds();
return false;
} else if (dst.excluded_count == 1) {
dst.label = dst.excluded[0];
free(dst.excluded);
dst.excluded_count = 0;
}
} else {
if (is_excluded(*second, first->label)) {
//profiler_intersect_second_wildcard += stopwatch.nanoseconds();
//profiler_intersect_node += stopwatch_function.nanoseconds();
return false;
}
dst.label = first->label;
dst.excluded_count = 0;
}
//profiler_intersect_second_wildcard += stopwatch.nanoseconds();
//profiler_intersect_node += stopwatch_function.nanoseconds();
} else if (first->label == LABEL_EITHER) {
//timer stopwatch;
if (second->label == LABEL_EITHER) {
dst.label = LABEL_EITHER;
dst.excluded = (unsigned int*) malloc(sizeof(unsigned int) * max(first->excluded_count, second->excluded_count));
dst.excluded_count = 0;
set_intersect(dst.excluded, dst.excluded_count,
first->excluded, first->excluded_count,
second->excluded, second->excluded_count);
if (dst.excluded_count == 0) {
free(dst.excluded);
//profiler_intersect_first_either += stopwatch.nanoseconds();
//profiler_intersect_node += stopwatch_function.nanoseconds();
return false;
} else if (dst.excluded_count == 1) {
dst.label = dst.excluded[0];
free(dst.excluded);
dst.excluded_count = 0;
}
} else {
if (!is_excluded(*first, second->label)) {
//profiler_intersect_first_either += stopwatch.nanoseconds();
//profiler_intersect_node += stopwatch_function.nanoseconds();
return false;
}
dst.label = second->label;
dst.excluded_count = 0;