Version: 1.0
collect_drag_stats.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_COLLECT_DRAG_STATISTICS_H_
8 #define GRAPHGEN_COLLECT_DRAG_STATISTICS_H_
9 
10 #include <algorithm>
11 #include <unordered_set>
12 
13 #include "conact_tree.h"
14 
15 // This class serves to efficiently collect information about each node/subtree of a tree starting from a node
16 // it both stores properties of each subtree (as string) and all the parents of each node.
18 
19  // Utility class to calculate and store (sub)trees properties
20  struct STreeProp {
21  std::string conditions_;
22  std::vector<BinaryDrag<conact>::node*> leaves_;
24 
26  conditions_ += rhs.conditions_;
27  copy(begin(rhs.leaves_), end(rhs.leaves_), back_inserter(leaves_));
28  return *this;
29  }
30 
31  bool equivalent(const STreeProp& rhs) {
32  if (conditions_ != rhs.conditions_)
33  return false;
34  for (size_t i = 0; i < leaves_.size(); ++i)
35  if (leaves_[i]->data.next != rhs.leaves_[i]->data.next
36  ||
37  (leaves_[i]->data.action & rhs.leaves_[i]->data.action) == 0)
38  return false;
39  return true;
40  }
41  };
42  std::unordered_map<BinaryDrag<conact>::node*, STreeProp> np_; // Associate to each tree node its properties (STreeProp)
43  std::unordered_map<BinaryDrag<conact>::node*, std::vector<BinaryDrag<conact>::node*>> parents_; // Associate to each tree node its parents (vector of nodes)
44 
47  for (const auto& t : bd.roots_) {
48  CollectStatsRec(t);
49  //CollectStatsRec(t->left);
50  //CollectStatsRec(t->right);
51  }
52  }
53 
54  // Collect information about each node/subtree of a tree starting from a node
56  auto it = np_.find(n);
57  if (it != end(np_))
58  return it->second;
59 
60  STreeProp sp;
61  sp.n_ = n;
62  if (n->isleaf()) {
63  sp.conditions_ = ".";
64  sp.leaves_.push_back(n);
65  }
66  else {
67  parents_[n->left].push_back(n);
68  parents_[n->right].push_back(n);
69  sp.conditions_ = n->data.condition;
70  sp += CollectStatsRec(n->left);
71  sp += CollectStatsRec(n->right);
72  }
73 
74  np_[n] = sp;
75  return sp;
76  }
77 };
78 
79 #endif // GRAPHGEN_COLLECT_DRAG_STATISTICS_H_
A BinaryDrag is the GRAPHGEN implementation of a Binary Directed Rooted Acyclic Graph (DRAG in short)
Definition: drag.h:28
std::vector< node * > roots_
Definition: drag.h:58
std::vector< BinaryDrag< conact >::node * > leaves_
STreeProp & operator+=(const STreeProp &rhs)
bool equivalent(const STreeProp &rhs)
BinaryDrag< conact >::node * n_
CollectDragStatistics(BinaryDrag< conact > &bd)
std::unordered_map< BinaryDrag< conact >::node *, STreeProp > np_
STreeProp CollectStatsRec(BinaryDrag< conact >::node *n)
std::unordered_map< BinaryDrag< conact >::node *, std::vector< BinaryDrag< conact >::node * > > parents_