A toolkit for working with phylogenetic data.
v0.19.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
node.cpp
Go to the documentation of this file.
1 /*
2  Genesis - A toolkit for working with phylogenetic data.
3  Copyright (C) 2014-2017 Lucas Czech
4 
5  This program is free software: you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation, either version 3 of the License, or
8  (at your option) any later version.
9 
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with this program. If not, see <http://www.gnu.org/licenses/>.
17 
18  Contact:
19  Lucas Czech <lucas.czech@h-its.org>
20  Exelixis Lab, Heidelberg Institute for Theoretical Studies
21  Schloss-Wolfsbrunnenweg 35, D-69118 Heidelberg, Germany
22 */
23 
32 
35 
36 #include <assert.h>
37 
38 namespace genesis {
39 namespace tree {
40 
41 // =================================================================================================
42 // Accessors
43 // =================================================================================================
44 
48 size_t TreeNode::index() const
49 {
50  return index_;
51 }
52 
57 {
58  return *link_;
59 }
60 
65 {
66  return *link_;
67 }
68 
76 {
77  return *link_;
78 }
79 
86 TreeLink const& TreeNode::link() const
87 {
88  return *link_;
89 }
90 
94 bool TreeNode::has_data() const
95 {
96  return data_.get() != nullptr;
97 }
98 
106 {
107  return data_.get();
108 }
109 
117 {
118  return data_.get();
119 }
120 
121 // =================================================================================================
122 // Modifiers
123 // =================================================================================================
124 
136 {
137  index_ = val;
138  return *this;
139 }
140 
150 {
151  link_ = val;
152  return *this;
153 }
154 
163 TreeNode& TreeNode::reset_data( std::unique_ptr< BaseNodeData > data )
164 {
165  data_ = std::move( data );
166  return *this;
167 }
168 
169 // =================================================================================================
170 // Member Functions
171 // =================================================================================================
172 
176 size_t TreeNode::rank() const
177 {
178  int rank = -1;
179  TreeLink* link = link_;
180 
181  do {
182  ++rank;
183  link = &link->next();
184  } while( link != link_ );
185 
186  // We add at least 1 to the initial value of the rank, so this is valid.
187  return static_cast<size_t>(rank);
188 }
189 
193 bool TreeNode::is_leaf() const
194 {
195  return link_->is_leaf();
196 }
197 
201 bool TreeNode::is_inner() const
202 {
203  return link_->is_inner();
204 }
205 
209 bool TreeNode::is_root() const
210 {
211  // The link_ is always the one pointing towards the root. Also, the edge of that link always has
212  // the primary link set to that it points towards the root.
213  // At the root itself, however, this means we are pointing to ourselves. Use this to check
214  // if this node is the root.
215  return &link_->edge().primary_link() == link_;
216 }
217 
223 std::string TreeNode::dump() const
224 {
225  // throw std::runtime_error( "Not implemented" );
226  // return "";
227  return "Base node dump";
228  // return "Rank: " + std::to_string(rank()) + " \t " + "name: " + this->data.name;
229 }
230 
231 } // namespace tree
232 } // namespace genesis
TreeLink & link()
Return the TreeLink that points towards the root.
Definition: node.cpp:75
TreeNode & reset_primary_link(TreeLink *val)
Reset the internal pointer to the TreeLink of this TreeNode.
Definition: node.cpp:149
Base class for storing data on Nodes of a Tree.
Definition: node_data.hpp:66
bool is_root() const
True iff the node is the root of its Tree.
Definition: node.cpp:209
size_t index() const
Return the index of this Node.
Definition: node.cpp:48
TreeLink & primary_link()
Return the TreeLink that points towards the root.
Definition: node.cpp:56
TreeLink & primary_link()
Return the TreeLink of this TreeEdge that points towards the root.
Definition: edge.cpp:53
TreeNode & reset_index(size_t val)
Reset the internal index of this TreeNode.
Definition: node.cpp:135
bool is_leaf() const
True iff the node is a leaf/tip.
Definition: node.cpp:193
BaseNodeData * data_ptr()
Return a pointer to the data.
Definition: node.cpp:105
TreeNode & reset_data(std::unique_ptr< BaseNodeData > data)
Reset the data pointer of this TreeNode.
Definition: node.cpp:163
size_t rank() const
Rank of the node, i.e. how many immediate children it has.
Definition: node.cpp:176
bool has_data() const
Return true if the TreeNode has a data object assigned to it.
Definition: node.cpp:94
std::string dump() const
Returns a one-line dump summary of the data of this node.
Definition: node.cpp:223
bool is_inner() const
True iff the node is an inner node.
Definition: node.cpp:201