pruner  0.0-99
tree_bones.hpp
1 #ifndef H_PRUNER
2 #include <memory>
3 #include <functional>
4 #include "typedefs.hpp"
5 #include "treeiterator_bones.hpp"
6 #endif
7 
8 #ifndef H_PRUNER_TREE_BONES
9 #define H_PRUNER_TREE_BONES 1
10 
11 #ifndef MAX_TREE_SIZE
12 #define MAX_TREE_SIZE 50000
13 #endif
14 
15 // Arbtrary set of arguments, this is the class that the creator function should
16 // inherit. Ideally it should have:
17 // - Model parameters can should be modifiable by a function
18 // - Additional data such as matrices and what not
19 // We start by declaring it, the user later on defines it
20 
26 
35 template <typename Data_Type = bool>
36 class Tree {
37 
38 protected:
39  bool is_dag_(int i = -1, int caller = -1, bool up_search = false);
40  void postorder_(uint i);
41  void postorder();
42  uint get_dist_tip2root_(uint start, uint count);
44 
45 
47  vv_uint parents;
49  vv_uint offspring;
50 
51  // Auxiliar variables
53  v_bool visited;
55  v_uint visit_counts;
56 
57  // Constant
58  uint N_NODES;
59  uint N_EDGES;
60  uint N_TIPS;
61 
63 
67  v_uint POSTORDER;
68 
70  v_uint TIPS;
71  v_uint DIST_TIPS2ROOT;
72 
73  friend Data_Type;
74  friend class TreeIterator< Data_Type >;
75 
76 public:
77 
79  Data_Type * args = nullptr;
80 
82 
88  std::function<void(Data_Type *, TreeIterator<Data_Type>&)> fun;
89 
91  void eval_fun() {
92 
93  if (fun)
94  fun(this->args, this->iter);
95 
96  return;
97  };
98 
99  // Creation ------------------------------------------------------------------
100  ~Tree() {
101  args = nullptr;
102  return;
103  };
104  Tree() {};
106 
114  Tree(const v_uint & parents_, const v_uint & offspring_, uint & out);
115 
116  // Getter --------------------------------------------------------------------
117 
118  // As pointers
119  const vv_uint * get_parents_ptr() const {return &this->parents;};
120  const vv_uint * get_offspring_ptr() const {return &this->offspring;};
121  const v_uint * get_postorder_ptr() const {return &this->POSTORDER;};
122 
123  // As data
124  vv_uint get_parents() const {return this->parents;};
125  vv_uint get_offspring() const {return this->offspring;};
126  v_uint get_postorder() const {return this->POSTORDER;};
127 
128  v_uint get_preorder() const;
129 
131  v_uint get_tips() const {return this->TIPS;};
132 
134  v_uint get_dist_tip2root();
135 
137  uint n_nodes() const {return this->N_NODES;};
139  uint n_edges() const {return this->N_EDGES;};
141  uint n_tips() const;
142 
144  int n_offspring(uint i) const;
145 
147  int n_parents(uint i) const;
148 
149  vv_uint get_edgelist() const;
150 
151  void print(bool details = true) const;
152 
153  // Checker functions ---------------------------------------------------------
154  bool is_dag();
155  bool is_connected() const {return this->POSTORDER.size() == this->N_NODES;};
156 
157  // Setters -------------------------------------------------------------------
158  void reset_visited() {
159  std::fill(this->visited.begin(), this->visited.end(), false);
160  std::fill(this->visit_counts.begin(), this->visit_counts.end(), 0u);
161  return ;
162  };
163 
164  uint set_postorder(const v_uint & POSTORDER_, bool check = true);
165 
166  // Pre-Post/order ------------------------------------------------------------
167 
169 
172  void prune_postorder();
173 
175 
178  void prune_postorder(v_uint & seq);
179 
181 
184  void prune_preorder();
186 
189  void prune_preorder(v_uint & seq);
190 
191 
192 };
193 
205 #endif
void prune_postorder()
Do the tree-traversal using the postorder.
Definition: tree_meat.hpp:385
v_uint POSTORDER
Postorder sequence.
Definition: tree_bones.hpp:67
vv_uint offspring
Each nodes&#39; offspring.
Definition: tree_bones.hpp:49
uint n_tips() const
Return the number of tips defined as nodes with no offspring.
Definition: tree_meat.hpp:467
int n_parents(uint i) const
Return the number of parents a given node has.
Definition: tree_meat.hpp:488
v_bool visited
List of already visited nodes (auxiliar)
Definition: tree_bones.hpp:53
std::function< void(Data_Type *, TreeIterator< Data_Type > &)> fun
Callable function during the the tree traversal.
Definition: tree_bones.hpp:88
void prune_preorder()
Do the tree-traversal using the preorder.
Definition: tree_meat.hpp:426
void eval_fun()
Evaluates the function by passing the arguments and the iterator.
Definition: tree_bones.hpp:91
uint n_edges() const
Returns the numner of edges.
Definition: tree_bones.hpp:139
v_uint get_tips() const
List of tips position indices.
Definition: tree_bones.hpp:131
Tree class.
Definition: tree_bones.hpp:36
v_uint visit_counts
Number of visits per node (auxilias)
Definition: tree_bones.hpp:55
vv_uint parents
Each nodes&#39; parents.
Definition: tree_bones.hpp:47
int n_offspring(uint i) const
Return the number of offsprings a given node has.
Definition: tree_meat.hpp:479
uint n_nodes() const
Returns the numner of nodes.
Definition: tree_bones.hpp:137
Data_Type * args
Arbitrary set of arguments.
Definition: tree_bones.hpp:79
Definition: treeiterator_bones.hpp:12
v_uint get_dist_tip2root()
Distance of tips to the closest root.
Definition: tree_meat.hpp:344
v_uint TIPS
List of tip (leaves) indices.
Definition: tree_bones.hpp:70