ʻOhana
Population structure, admixture history, and selection using learning methods.
jade.tree_path.hpp
1 /* -------------------------------------------------------------------------
2  Ohana
3  Copyright (c) 2015-2020 Jade Cheng (\___/)
4  Jade Cheng <info@jade-cheng.com> (='.'=)
5  ------------------------------------------------------------------------- */
6 
7 #ifndef JADE_TREE_PATH_HPP__
8 #define JADE_TREE_PATH_HPP__
9 
10 #include "jade.newick.hpp"
11 
12 namespace jade
13 {
14  ///
15  /// A template for a class that maintains a table of nodes indicating a
16  /// distance to the root of the tree.
17  ///
18  template <typename TValue>
20  {
21  public:
22  /// The value type.
23  typedef TValue value_type;
24 
25  /// The Newick node type.
27 
28  //
29  // Initialize a new instance of the class with no nodes.
30  //
31  inline basic_tree_path()
32  : _map ()
33  {
34  }
35 
36  ///
37  /// Initializes a new instance of the class by computing the set of
38  /// nodes in the path to the root. The specified node is included in the
39  /// result, but the root node is not. For example, computing a tree path
40  /// for node 'D' for the tree, "((B:2,(C:4,D:5)n1:3)n2:1)A;" results in
41  /// the container (D, n1, n2).
42  ///
43  explicit basic_tree_path(
44  const node_type & node) ///< The node to measure.
45  : _map ()
46  {
47  //
48  // Loop toward the root node, starting at the specified node, but
49  // do not include the root node in the result; in each iteration,
50  // accumulate the nodes traversed.
51  //
52  for (auto n = &node; !n->is_root(); n = n->get_parent())
53  _map[n->get_id()] = n;
54  }
55 
56  ///
57  /// \return The sum of the lengths for all nodes of this instance.
58  ///
60  {
61  auto sum = value_type(0);
62  for (const auto & pair : _map)
63  sum += pair.second->get_length();
64  return sum;
65  }
66 
67  ///
68  /// Computes the overlapped tree path of this instance and another
69  /// instance. This method returns only the nodes that exist in both
70  /// containers.
71  ///
72  /// \return A new tree path.
73  ///
75  const basic_tree_path & rhs) ///< The other tree path.
76  const
77  {
79 
80  for (const auto & pair: rhs._map)
81  if (_map.find(pair.first) != _map.end())
82  p._map[pair.first] = pair.second;
83 
84  return p;
85  }
86 
87  private:
88  std::map<int, const node_type *> _map;
89  };
90 }
91 
92 #endif // JADE_TREE_PATH_HPP__
jade::basic_tree_path::operator&
basic_tree_path operator&(const basic_tree_path &rhs) const
Computes the overlapped tree path of this instance and another instance. This method returns only the...
Definition: jade.tree_path.hpp:74
jade::basic_newick_node
A template class representing a node from a Newick tree.
Definition: jade.newick.hpp:19
jade::basic_tree_path
A template for a class that maintains a table of nodes indicating a distance to the root of the tree.
Definition: jade.tree_path.hpp:20
jade::basic_tree_path::get_length
value_type get_length() const
Definition: jade.tree_path.hpp:59
jade::basic_tree_path::basic_tree_path
basic_tree_path(const node_type &node)
Initializes a new instance of the class by computing the set of nodes in the path to the root....
Definition: jade.tree_path.hpp:43
jade::basic_tree_path::value_type
TValue value_type
The value type.
Definition: jade.tree_path.hpp:23
jade::basic_newick_node::is_root
bool is_root() const
Definition: jade.newick.hpp:422
jade::basic_tree_path::node_type
basic_newick_node< value_type > node_type
The Newick node type.
Definition: jade.tree_path.hpp:26