ARB
tree_position.h
Go to the documentation of this file.
1 // ============================================================ //
2 // //
3 // File : tree_position.h //
4 // Purpose : provides relative position in tree //
5 // //
6 // Coded by Ralf Westram (coder@reallysoft.de) in July 2016 //
7 // http://www.arb-home.de/ //
8 // //
9 // ============================================================ //
10 
11 #ifndef TREE_POSITION_H
12 #define TREE_POSITION_H
13 
14 #ifndef _GLIBCXX_MAP
15 #include <map>
16 #endif
17 #ifndef _GLIBCXX_STRING
18 #include <string>
19 #endif
20 #ifndef TREENODE_H
21 #include <TreeNode.h>
22 #endif
23 #ifndef NT_LOCAL_H
24 #include "NT_local.h"
25 #endif
26 
28  // describes relative position of a given subtree in a tree
29 
30  double relpos_sum; // sum of all involved relative positions
31  size_t count; // amount of positions involved
32 
33  TreeRelativePosition() : relpos_sum(0), count(0) {} // = no position
34 public:
35 
37 
38  explicit TreeRelativePosition(double relpos_sum_) // create leaf-info
39  : relpos_sum(relpos_sum_),
40  count(1)
41  {}
43  : relpos_sum(p1.relpos_sum + p2.relpos_sum),
44  count(p1.count + p2.count)
45  {}
46 
47  bool is_known() const { return count; }
48  double value() const {
50  return relpos_sum / count;
51  }
52  int compare(const TreeRelativePosition &right) const {
53  return double_cmp(value(), right.value());
54  }
55 };
56 
58  // provides relative position of species inside a tree
59 
60  typedef std::map<std::string, unsigned> PosMap;
61  PosMap spos;
62 
63  void fillFromTree(const TreeNode *node) {
64  if (node->is_leaf()) {
65  size_t pos = spos.size();
66  spos[node->name] = pos;
67  }
68  else {
69  fillFromTree(node->get_leftson());
70  fillFromTree(node->get_rightson());
71  }
72  }
73 public:
74  explicit TreePositionLookup(const TreeNode *tree) {
75  if (tree) fillFromTree(tree);
76  }
77 
78  TreeRelativePosition relative(const char *name) const {
81  PosMap::const_iterator found = spos.find(name);
82  return
83  (found == spos.end())
85  : TreeRelativePosition(found->second/double(spos.size()-1));
86  }
87 };
88 
89 #else
90 #error tree_position.h included twice
91 #endif // TREE_POSITION_H
double value() const
Definition: tree_position.h:48
bool is_known() const
Definition: tree_position.h:47
static TreeRelativePosition unknown()
Definition: tree_position.h:36
int compare(const TreeRelativePosition &right) const
Definition: tree_position.h:52
TreePositionLookup(const TreeNode *tree)
Definition: tree_position.h:74
#define nt_assert(cond)
Definition: NT_local.h:27
bool is_leaf() const
Definition: TreeNode.h:171
char * name
Definition: TreeNode.h:134
TreeRelativePosition(double relpos_sum_)
Definition: tree_position.h:38
TreeRelativePosition(const TreeRelativePosition &p1, const TreeRelativePosition &p2)
Definition: tree_position.h:42
CONSTEXPR_INLINE int double_cmp(const double d1, const double d2)
Definition: arbtools.h:182
TreeRelativePosition relative(const char *name) const
Definition: tree_position.h:78