Fleet  0.0.9
Inference in the LOT
FullLZEnumeration.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include "SubtreeEnumeration.h"
4 
16 template<typename Grammar_t>
18  Grammar_t* grammar;
19 
20 
21 public:
22  FullLZEnumeration(Grammar_t* g) : grammar(g) {}
23 
31  size_t compute_possible_referents(const Node& root, const nonterminal_t nt) {
32  size_t cnt = 0;
33  std::set<Node> unique;
34  for(auto& x : root) {
35  if(x.nt() == nt and
36  x.is_complete() and
37  (not unique.contains(x)) and
38  (not x.is_terminal())) {
39 
40  cnt++;
41  unique.insert(x);
42  //CERR "possible reference " TAB cnt TAB x.string() ENDL;
43  }
44  }
45  return cnt;
46  }
47 
48  Node& get_referent(const Node& root, size_t cnt, const nonterminal_t nt) {
49  std::set<Node> unique;
50  for(auto& x : root) {
51  if(x.nt() == nt and
52  x.is_complete() and
53  (not unique.contains(x)) and
54  (not x.is_terminal())) {
55 
56  // if we found it!
57  if(cnt == 0) return x;
58 
59  cnt--;
60  unique.insert(x);
61  }
62  }
63  assert(false && "*** You asked for a referent that was impossible!");
64  }
65 
72  [[nodiscard]] virtual Node toNode(IntegerizedStack& is, const nonterminal_t nt, Node& root) {
73 
74  // NOTE: The order of the next two chunks is important. If we check subtrees first, then we will
75  // enumerate references to subtrees before other terminals. If we flip the order, we get
76  // other terminals first.
77 
78  // otherwise let's see if it's a reference to a prior subtree of type nt
79  size_t possible_referents = compute_possible_referents(root, nt); // what could we have referenced?
80  if(is.get_value() < possible_referents) {
81  return get_referent(root, is.get_value(), nt);
82  }
83  is -= possible_referents;
84 
85  // if we encode a terminal
86  enumerationidx_t numterm = this->grammar->count_terminals(nt);
87  if(is.get_value() < numterm) {
88  return this->grammar->makeNode(this->grammar->get_rule(nt, is.get_value())); // whatever terminal we wanted
89  }
90  is -= numterm;
91 
92  // otherwise we unpack the kids' encoding:
93  auto ri = is.pop(this->grammar->count_nonterminals(nt)); // this already includes the shift from all the nonterminals
94  Rule* r = this->grammar->get_rule(nt, ri+numterm); // shift index from terminals (because they are first!)
95  Node out = this->grammar->makeNode(r);
96  int i=0;
97  for(auto v : is.split(r->N)) {
98 
99  // we assume we get a null root when root starts out -- so in that case,
100  // out is the root for everything below.
101  if(root.is_null()) {
102  out.set_child(i, toNode(v, r->type(i), out) ) ;
103  }
104  else {
105  out.set_child(i, toNode(v, r->type(i), root) ) ;
106  }
107 
108  i++;
109  }
110  // CERR "\t" << root TAB is TAB out ENDL;
111 
112  return out;
113 
114  }
115 
116  // Required here or else we get z converted implicity to IntegerizedStack
117  [[nodiscard]] virtual Node toNode(enumerationidx_t z, const nonterminal_t nt, Node& root) {
118  IntegerizedStack is(z);
119  return toNode(is, nt, root);
120  }
121 
122  // Required here or else we get z converted implicity to IntegerizedStack
123  [[nodiscard]] virtual Node toNode(enumerationidx_t z, const nonterminal_t nt) {
124  // defaultly create a null node to pass in.
125  Node n;
126 
127  IntegerizedStack is(z);
128  return toNode(is, nt, n);
129  }
130 
131 
132 
133  [[nodiscard]] virtual enumerationidx_t toInteger(const Node& root, const Node& n) {
134  assert(0);
135  }
136 
137 };
138 
139 
value_t pop()
Definition: IntegerizedStack.h:82
Node & get_referent(const Node &root, size_t cnt, const nonterminal_t nt)
Definition: FullLZEnumeration.h:48
virtual Node toNode(enumerationidx_t z, const nonterminal_t nt, Node &root)
Definition: FullLZEnumeration.h:117
Definition: Node.h:22
Enumerate subtrees of a given tree.
nonterminal_t type(size_t i) const
Definition: Rule.h:152
Definition: FullLZEnumeration.h:17
Definition: Rule.h:21
virtual enumerationidx_t toInteger(const Node &root, const Node &n)
Definition: FullLZEnumeration.h:133
value_t get_value() const
Definition: IntegerizedStack.h:124
size_t compute_possible_referents(const Node &root, const nonterminal_t nt)
Compute the number of things in n which could be referenced Here, we will require references to prior...
Definition: FullLZEnumeration.h:31
size_t N
Definition: Rule.h:29
bool is_null() const
Definition: Node.h:165
virtual Node toNode(enumerationidx_t z, const nonterminal_t nt)
Definition: FullLZEnumeration.h:123
FullLZEnumeration(Grammar_t *g)
Definition: FullLZEnumeration.h:22
unsigned short nonterminal_t
Definition: Nonterminal.h:4
void set_child(const size_t i, Node &n)
Definition: Node.h:88
size_t enumerationidx_t
Definition: IntegerizedStack.h:3
Definition: IntegerizedStack.h:12
virtual Node toNode(IntegerizedStack &is, const nonterminal_t nt, Node &root)
Convert to a node, using root as the root – NOTE that when root is null, we use out as the root...
Definition: FullLZEnumeration.h:72