A toolkit for working with phylogenetic data.
v0.20.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
edge.hpp
Go to the documentation of this file.
1 #ifndef GENESIS_TREE_TREE_EDGE_H_
2 #define GENESIS_TREE_TREE_EDGE_H_
3 
4 /*
5  Genesis - A toolkit for working with phylogenetic data.
6  Copyright (C) 2014-2018 Lucas Czech and HITS gGmbH
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 
36 
38 
39 #include <memory>
40 #include <string>
41 
42 namespace genesis {
43 namespace tree {
44 
45 // =================================================================================================
46 // Forward declarations
47 // =================================================================================================
48 
49 class Tree;
50 class TreeLink;
51 class TreeNode;
52 
53 class BaseEdgeData;
54 
55 // =================================================================================================
56 // Tree Edge
57 // =================================================================================================
58 
59 class TreeEdge
60 {
61 public:
62 
63  // ---------------------------------------------------------------------
64  // Typedefs and Enums
65  // ---------------------------------------------------------------------
66 
67  friend class Tree;
68 
69  // ---------------------------------------------------------------------
70  // Constructor and Rule of Five
71  // ---------------------------------------------------------------------
72 
74  : index_( 0 )
75  , link_p_( nullptr )
76  , link_s_( nullptr )
77  , data_( nullptr )
78  {}
79 
81  : index_( index )
82  , link_p_( primary_link )
83  , link_s_( secondary_link )
84  , data_( nullptr )
85  {}
86 
87  ~TreeEdge() = default;
88 
89  // avoid copy constructor and assignment operator.
90  // creating copies is maintained by Tree only.
91 
92  TreeEdge( TreeEdge const& ) = delete;
93  TreeEdge( TreeEdge&& ) = delete;
94 
95  TreeEdge& operator= ( TreeEdge const& ) = delete;
96  TreeEdge& operator= ( TreeEdge&& ) = delete;
97 
98  // ---------------------------------------------------------------------
99  // Accessors
100  // ---------------------------------------------------------------------
101 
105  size_t index() const
106  {
107  return index_;
108  }
109 
114  {
115  return *link_p_;
116  }
117 
121  TreeLink const& primary_link() const
122  {
123  return *link_p_;
124  }
125 
130  {
131  return *link_s_;
132  }
133 
137  TreeLink const& secondary_link() const
138  {
139  return *link_s_;
140  }
141 
146  {
147  return link_p_->node();
148  }
149 
153  TreeNode const& primary_node() const
154  {
155  return link_p_->node();
156  }
157 
162  {
163  return link_s_->node();
164  }
165 
169  TreeNode const& secondary_node() const
170  {
171  return link_s_->node();
172  }
173 
177  bool has_data() const
178  {
179  return data_.get() != nullptr;
180  }
181 
182  template< class EdgeDataType >
183  EdgeDataType& data()
184  {
185  return dynamic_cast< EdgeDataType& >( *data_ );
186  }
187 
188  template< class EdgeDataType >
189  EdgeDataType const& data() const
190  {
191  return dynamic_cast< EdgeDataType const& >( *data_ );
192  }
193 
194  template< class EdgeDataType >
195  EdgeDataType* data_cast()
196  {
197  return dynamic_cast< EdgeDataType* >( data_.get() );
198  }
199 
200  template< class EdgeDataType >
201  EdgeDataType const* data_cast() const
202  {
203  return dynamic_cast< EdgeDataType const* >( data_.get() );
204  }
205 
213  {
214  return data_.get();
215  }
216 
223  BaseEdgeData const* data_ptr() const
224  {
225  return data_.get();
226  }
227 
228  // ---------------------------------------------------------------------
229  // Modifiers
230  // ---------------------------------------------------------------------
231 
242  TreeEdge& reset_index( size_t val )
243  {
244  index_ = val;
245  return *this;
246  }
247 
257  {
258  link_p_ = val;
259  return *this;
260  }
261 
271  {
272  link_s_ = val;
273  return *this;
274  }
275 
284  TreeEdge& reset_data( std::unique_ptr< BaseEdgeData > data )
285  {
286  data_ = std::move( data );
287  return *this;
288  }
289 
290  // ---------------------------------------------------------------------
291  // Member Variables
292  // ---------------------------------------------------------------------
293 
294 private:
295 
296  size_t index_;
297 
298  TreeLink* link_p_;
299  TreeLink* link_s_;
300 
301  std::unique_ptr< BaseEdgeData > data_;
302 };
303 
304 } // namespace tree
305 } // namespace genesis
306 
307 #endif // include guard
TreeLink & secondary_link()
Return the TreeLink of this TreeEdge that points away from the root.
Definition: edge.hpp:129
size_t index() const
Return the index of this Edge.
Definition: edge.hpp:105
TreeEdge & operator=(TreeEdge const &)=delete
TreeNode & secondary_node()
Return the TreeNode of this TreeEdge that points away from the root.
Definition: edge.hpp:161
TreeEdge & reset_data(std::unique_ptr< BaseEdgeData > data)
Reset the data pointer of this TreeEdge.
Definition: edge.hpp:284
TreeNode const & secondary_node() const
Return the TreeNode of this TreeEdge that points away from the root.
Definition: edge.hpp:169
Provides some valuable additions to STD.
TreeNode & primary_node()
Return the TreeNode of this TreeEdge that points towards the root.
Definition: edge.hpp:145
BaseEdgeData * data_ptr()
Return a pointer to the data.
Definition: edge.hpp:212
Class for representing phylogenetic trees.
Definition: tree/tree.hpp:95
TreeLink const & primary_link() const
Return the TreeLink of this TreeEdge that points towards the root.
Definition: edge.hpp:121
TreeEdge & reset_index(size_t val)
Reset the internal index of this TreeEdge.
Definition: edge.hpp:242
bool has_data() const
Return true if the TreeEdge has a data object assigned to it.
Definition: edge.hpp:177
TreeNode const & primary_node() const
Return the TreeNode of this TreeEdge that points towards the root.
Definition: edge.hpp:153
TreeEdge & reset_primary_link(TreeLink *val)
Reset the internal pointer to the primary TreeLink of this TreeEdge.
Definition: edge.hpp:256
BaseEdgeData const * data_ptr() const
Return a const pointer to the data.
Definition: edge.hpp:223
EdgeDataType const & data() const
Definition: edge.hpp:189
TreeLink const & secondary_link() const
Return the TreeLink of this TreeEdge that points away from the root.
Definition: edge.hpp:137
TreeLink & primary_link()
Return the TreeLink of this TreeEdge that points towards the root.
Definition: edge.hpp:113
EdgeDataType const * data_cast() const
Definition: edge.hpp:201
Base class for storing data on Edges of a Tree.
Definition: edge_data.hpp:66
TreeEdge(size_t index, TreeLink *primary_link, TreeLink *secondary_link)
Definition: edge.hpp:80
EdgeDataType & data()
Definition: edge.hpp:183
EdgeDataType * data_cast()
Definition: edge.hpp:195
TreeEdge & reset_secondary_link(TreeLink *val)
Reset the internal pointer to the secondary TreeLink of this TreeEdge.
Definition: edge.hpp:270