| 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 | |
|---|
| 27 | class TreeRelativePosition { |
|---|
| 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 | |
|---|
| 36 | static TreeRelativePosition unknown() { return TreeRelativePosition(); } |
|---|
| 37 | |
|---|
| 38 | explicit TreeRelativePosition(double relpos_sum_) // create leaf-info |
|---|
| 39 | : relpos_sum(relpos_sum_), |
|---|
| 40 | count(1) |
|---|
| 41 | {} |
|---|
| 42 | TreeRelativePosition(const TreeRelativePosition& p1, const TreeRelativePosition& p2) |
|---|
| 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 { |
|---|
| 49 | nt_assert(is_known()); |
|---|
| 50 | return relpos_sum / count; |
|---|
| 51 | } |
|---|
| 52 | int compare(const TreeRelativePosition &right) const { |
|---|
| 53 | return double_cmp(value(), right.value()); |
|---|
| 54 | } |
|---|
| 55 | }; |
|---|
| 56 | |
|---|
| 57 | class TreePositionLookup { |
|---|
| 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 { |
|---|
| 79 | /*! returns TreeRelativePosition of species 'name' inside tree. |
|---|
| 80 | */ |
|---|
| 81 | PosMap::const_iterator found = spos.find(name); |
|---|
| 82 | return |
|---|
| 83 | (found == spos.end()) |
|---|
| 84 | ? TreeRelativePosition::unknown() |
|---|
| 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 |
|---|