Version: 1.0
forest2dag.cpp
Go to the documentation of this file.
1 // Copyright (c) 2020, the GRAPHGEN contributors, as
2 // shown by the AUTHORS file. All rights reserved.
3 //
4 // Use of this source code is governed by a BSD-style
5 // license that can be found in the LICENSE file.
6 
7 #include "forest2dag.h"
8 
9 #include <sstream>
10 #include <iomanip>
11 
12 using namespace std;
13 
14 // Converts a tree into unique string exploiting memoization
16  auto it = ps_.find(n);
17  if (it != end(ps_))
18  return it->second;
19 
20  string s;
21  if (n->isleaf()) {
22  //char a_action[sizeof(n->data.action) + 1] = { 0 };
23  //memcpy(a_action, reinterpret_cast<char*>(&n->data.action), sizeof(n->data.action));
24  stringstream ss;
25  ss << setfill('0') << setw(3) << n->data.next;
26  s = n->data.action.to_string() + ss.str();
27  }
28  else
29  s = n->data.condition + Tree2String(n->left) + Tree2String(n->right);
30 
31  ps_[n] = s;
32  return s;
33 }
34 
35 // Recursively searches for equal subtrees inside the forest. It starts from a root of the forest
36 // and explore its subtrees. For each subtree, if there is an equal subtree (i.e. sp_ hash table
37 // contains the tree identifier string) the function updates the link, otherwise it updates the
38 // hash table with the "new" subtree. This must be repeated for every root of the forest (see Forest2Dag)
40  if (!n->isleaf()) {
41  auto s = Tree2String(n->left);
42 
43  auto it = sp_.find(s);
44  if (it == end(sp_)) {
45  sp_[s] = n->left;
46  FindAndLink(n->left);
47  }
48  else {
49  n->left = it->second;
50  }
51 
52  s = Tree2String(n->right);
53 
54  it = sp_.find(s);
55  if (it == end(sp_)) {
56  sp_[s] = n->right;
57  FindAndLink(n->right);
58  }
59  else {
60  n->right = it->second;
61  }
62  }
63 }
64 
65 // Calls FindAndLink for each root of the forest
66 //Forest2Dag::Forest2Dag(LineForestHandler& f) : f_(f) {
67 // // Conversion to DAG for the main forest
68 // for (auto& t : f_.trees_) {
69 // FindAndLink(t.GetRoot());
70 // }
71 //
72 // // Clean memoizing data structures
73 // ps_.clear();
74 // sp_.clear();
75 //
76 // // Conversion to DAG for the end forest
77 // for (auto& etg : f_.end_trees_) { // etg -> end trees groups
78 //
79 // if (f.separately) {
80 // // Clean memoizing data structures
81 // ps_.clear();
82 // sp_.clear();
83 // }
84 //
85 // for (auto& et : etg) {
86 // FindAndLink(et.GetRoot());
87 // }
88 // }
89 //}
A BinaryDrag is the GRAPHGEN implementation of a Binary Directed Rooted Acyclic Graph (DRAG in short)
Definition: drag.h:28
std::string Tree2String(BinaryDrag< conact >::node *n)
Definition: forest2dag.cpp:15
void FindAndLink(BinaryDrag< conact >::node *n)
Definition: forest2dag.cpp:39