A toolkit for working with phylogenetic data.
v0.19.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
node.hpp
Go to the documentation of this file.
1 #ifndef GENESIS_TREE_TREE_NODE_H_
2 #define GENESIS_TREE_TREE_NODE_H_
3 
4 /*
5  Genesis - A toolkit for working with phylogenetic data.
6  Copyright (C) 2014-2017 Lucas Czech
7 
8  This program is free software: you can redistribute it and/or modify
9  it under the terms of the GNU General Public License as published by
10  the Free Software Foundation, either version 3 of the License, or
11  (at your option) any later version.
12 
13  This program is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  GNU General Public License for more details.
17 
18  You should have received a copy of the GNU General Public License
19  along with this program. If not, see <http://www.gnu.org/licenses/>.
20 
21  Contact:
22  Lucas Czech <lucas.czech@h-its.org>
23  Exelixis Lab, Heidelberg Institute for Theoretical Studies
24  Schloss-Wolfsbrunnenweg 35, D-69118 Heidelberg, Germany
25 */
26 
35 
37 
38 #include <memory>
39 #include <string>
40 
41 namespace genesis {
42 namespace tree {
43 
44 // =================================================================================================
45 // Forward declarations
46 // =================================================================================================
47 
48 class Tree;
49 class TreeEdge;
50 class TreeLink;
51 
52 // =================================================================================================
53 // Tree Node
54 // =================================================================================================
55 
56 class TreeNode
57 {
58 public:
59 
60  // ---------------------------------------------------------------------
61  // Typedefs and Enums
62  // ---------------------------------------------------------------------
63 
64  friend class Tree;
65 
66  // ---------------------------------------------------------------------
67  // Constructor and Rule of Five
68  // ---------------------------------------------------------------------
69 
71  : index_( 0 )
72  , link_( nullptr )
73  , data_( nullptr )
74  {}
75 
77  : index_( index )
78  , link_( primary_link )
79  , data_( nullptr )
80  {}
81 
82  ~TreeNode() = default;
83 
84  // avoid copy constructor and assignment operator.
85  // creating copies is maintained by Tree only.
86 
87  TreeNode( TreeNode const& ) = delete;
88  TreeNode( TreeNode&& ) = delete;
89 
90  TreeNode& operator= ( TreeNode const& ) = delete;
91  TreeNode& operator= ( TreeNode&& ) = delete;
92 
93  // ---------------------------------------------------------------------
94  // Accessors
95  // ---------------------------------------------------------------------
96 
97  size_t index() const;
98 
100  TreeLink const& primary_link() const;
101 
102  TreeLink & link();
103  TreeLink const& link() const;
104 
105  bool has_data() const;
106 
107  template< class NodeDataType >
108  NodeDataType& data()
109  {
110  return dynamic_cast< NodeDataType& >( *data_ );
111  }
112 
113  template< class NodeDataType >
114  NodeDataType const& data() const
115  {
116  return dynamic_cast< NodeDataType const& >( *data_ );
117  }
118 
119  template< class NodeDataType >
120  NodeDataType* data_cast()
121  {
122  return dynamic_cast< NodeDataType* >( data_.get() );
123  }
124 
125  template< class NodeDataType >
126  NodeDataType const* data_cast() const
127  {
128  return dynamic_cast< NodeDataType const* >( data_.get() );
129  }
130 
132  BaseNodeData const* data_ptr() const;
133 
134  // ---------------------------------------------------------------------
135  // Modifiers
136  // ---------------------------------------------------------------------
137 
138  TreeNode& reset_index( size_t val );
139 
141 
142  TreeNode& reset_data( std::unique_ptr< BaseNodeData > data );
143 
144  // ---------------------------------------------------------------------
145  // Member Functions
146  // ---------------------------------------------------------------------
147 
148  size_t rank() const;
149  bool is_leaf() const;
150  bool is_inner() const;
151  bool is_root() const;
152 
153  std::string dump() const;
154 
155  // ---------------------------------------------------------------------
156  // Member Variables
157  // ---------------------------------------------------------------------
158 
159 private:
160 
161  size_t index_;
162  TreeLink* link_;
163 
164  std::unique_ptr< BaseNodeData > data_;
165 
166 };
167 
168 } // namespace tree
169 } // namespace genesis
170 
171 #endif // include guard
TreeNode(size_t index, TreeLink *primary_link)
Definition: node.hpp:76
TreeLink & link()
Return the TreeLink that points towards the root.
Definition: node.cpp:75
TreeNode & operator=(TreeNode const &)=delete
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
Provides some valuable additions to STD.
TreeLink & primary_link()
Return the TreeLink that points towards the root.
Definition: node.cpp:56
TreeNode & reset_index(size_t val)
Reset the internal index of this TreeNode.
Definition: node.cpp:135
NodeDataType const * data_cast() const
Definition: node.hpp:126
Class for representing phylogenetic trees.
Definition: tree/tree.hpp:95
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
NodeDataType const & data() const
Definition: node.hpp:114
TreeNode & reset_data(std::unique_ptr< BaseNodeData > data)
Reset the data pointer of this TreeNode.
Definition: node.cpp:163
NodeDataType & data()
Definition: node.hpp:108
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
NodeDataType * data_cast()
Definition: node.hpp:120