Fleet  0.0.9
Inference in the LOT
SubtreeEnumeration.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include "BasicEnumeration.h"
4 
12 template<typename Grammar_t>
14 public:
15  Grammar_t* grammar;
16 
17  SubtreeEnumeration(Grammar_t* g) : grammar(g) {}
18 
24  [[nodiscard]] enumerationidx_t count(const Node& n) {
25  // how many possible connected, partial subtrees do I have
26  // (e.g. including a path back to my root)
27 
28  if(n.is_null())
29  return 1; // only one tree there!
30 
31  enumerationidx_t k=1;
32  for(const auto& c : n.get_children() ){
33  // for each child independently, I can either make them null, or I can include them
34  // and choose one of their own subtrees
35  k *= count(c);
36  }
37  return k+1; // +1 because I could make the root null
38  }
39 
46  [[nodiscard]] virtual Node toNode(IntegerizedStack& is, const Node& frm) {
47  if(is.get_value() == 0) {
48  return Node();
49  }
50  else {
51  is -= 1; // is encodes tree+1
52  Node out = this->grammar->makeNode(frm.rule); // copy the first level
53  for(size_t i=0;i<out.nchildren();i++) {
54  auto cz = count(frm.child(i));
55  out.set_child(i, toNode(is.pop(cz), frm.child(i)));
56  }
57  return out;
58  }
59  }
60 
61  // Required here or else we get z converted implicity to IntegerizedStack
62  [[nodiscard]] virtual Node toNode(enumerationidx_t z, const Node& frm) {
63  IntegerizedStack is(z);
64  return toNode(is, frm);
65  }
66 
67 
68  [[nodiscard]] virtual enumerationidx_t toInteger(const Node& n, const Node& frm) {
69  if(n.is_null()) {
70  return 0;
71  }
72  else {
73  // else it's a real construction
74 
75  const int N = n.nchildren();
76  assert( (int)frm.nchildren() == N);
77 
79 
80  // We have to go in reverse order here, remember, since that's hwo things are unpacked
81  for(int i=N-1;i>=0;i--) {
82 
83  // the most we can pop from a child is the number of partial trees they have
84  auto cx = count(frm.child(i));
85  auto k = toInteger(n.child(i), frm.child(i));
86 
87  is.push(k, cx);
88 
89  //COUT ">>" TAB k TAB cx TAB is.get_value() TAB "[" << i << "] " << n.child(i) << " in " << n.string() ENDL;
90  }
91 
92  // 1+ becuase it wasn't null
93  return 1+is.get_value();
94  }
95 
96  }
97 
98 };
value_t pop()
Definition: IntegerizedStack.h:82
virtual Node toNode(enumerationidx_t z, const Node &frm)
Definition: SubtreeEnumeration.h:62
SubtreeEnumeration(Grammar_t *g)
Definition: SubtreeEnumeration.h:17
decltype(children) & get_children()
Definition: BaseNode.h:168
Definition: Node.h:22
enumerationidx_t count(const Node &n)
How many partial subtrees are there?
Definition: SubtreeEnumeration.h:24
Definition: SubtreeEnumeration.h:13
Grammar_t * grammar
Definition: SubtreeEnumeration.h:15
value_t get_value() const
Definition: IntegerizedStack.h:124
virtual enumerationidx_t toInteger(const Node &n, const Node &frm)
Definition: SubtreeEnumeration.h:68
bool is_null() const
Definition: Node.h:165
this_t & child(const size_t i)
Definition: BaseNode.h:175
const Rule * rule
Definition: Node.h:32
void set_child(const size_t i, Node &n)
Definition: Node.h:88
virtual Node toNode(IntegerizedStack &is, const Node &frm)
Convert to the is&#39;th partial subtree of frm.
Definition: SubtreeEnumeration.h:46
size_t enumerationidx_t
Definition: IntegerizedStack.h:3
Definition: IntegerizedStack.h:12
size_t nchildren() const
Definition: BaseNode.h:208