Version: 1.0
remove_equal_subtrees.h
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 #ifndef GRAPHGEN_REMOVE_EQUAL_SUBTREES_H_
8 #define GRAPHGEN_REMOVE_EQUAL_SUBTREES_H_
9 
10 #include <unordered_set>
11 
12 #include "conact_tree.h"
13 
23  std::unordered_map<std::string, BinaryDrag<conact>::node*> sp_; // string -> pointer
24  std::unordered_map<BinaryDrag<conact>::node*, std::string> ps_; // pointer -> string
25  uint nodes_ = 0, leaves_ = 0;
26 
28  for (auto& t : bd.roots_) {
30  }
31  }
32 
34  {
35  // Did we already find this node?
36  auto itps = ps_.find(n);
37  if (itps != end(ps_)) {
38  // Yes, return the string
39  return itps->second;
40  }
41 
42  std::string s;
43  if (n->isleaf()) {
44  ++leaves_;
45  //ss << setfill('0') << setw(3) << n->data.next;
46  //s = n->data.action.to_string() + ss.str();
47  const std::vector<uint>& a = n->data.actions();
48  s = '.' + std::to_string(a[0]);
49  for (std::size_t i = 1; i < a.size(); ++i) {
50  s += ',' + std::to_string(a[i]);
51  }
52  s += '-' + std::to_string(n->data.next);
53  }
54  else {
55  ++nodes_;
56  auto sl = RemoveEqualSubtreesRec(n->left);
57  auto sr = RemoveEqualSubtreesRec(n->right);
58  s = n->data.condition + sl + sr;
59  }
60 
61  auto it = sp_.find(s);
62  if (it == end(sp_)) {
63  sp_.insert({ s, n });
64  ps_.insert({ n, s });
65  }
66  else {
67  n = it->second;
68  if (n->isleaf()) {
69  --leaves_;
70  }
71  else {
72  --nodes_;
73  }
74  }
75  return s;
76  }
77 };
78 
79 #endif // GRAPHGEN_REMOVE_EQUAL_SUBTREES_H_
std::vector< node * > roots_
Definition: drag.h:58
uint32_t uint
This class allows to "remove" equal subtrees from a BinaryDrag.
std::string RemoveEqualSubtreesRec(BinaryDrag< conact >::node *&n)
std::unordered_map< std::string, BinaryDrag< conact >::node * > sp_
RemoveEqualSubtrees(BinaryDrag< conact > &bd)
std::unordered_map< BinaryDrag< conact >::node *, std::string > ps_