A toolkit for working with phylogenetic data.
v0.18.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
node_links.hpp
Go to the documentation of this file.
1 #ifndef GENESIS_TREE_ITERATOR_NODE_LINKS_H_
2 #define GENESIS_TREE_ITERATOR_NODE_LINKS_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 
34 #include "genesis/tree/tree.hpp"
36 
37 #include <iterator>
38 
39 namespace genesis {
40 namespace tree {
41 
42 // =================================================================================================
43 // Forward Declarations
44 // =================================================================================================
45 
46 class Tree;
47 class TreeNode;
48 class TreeEdge;
49 class TreeLink;
50 
51 // =============================================================================
52 // Iterator Node Links
53 // =============================================================================
54 
55 template <typename LinkType, typename NodeType, typename EdgeType>
57 {
58 
59 public:
60 
61  // -----------------------------------------------------
62  // Typedefs
63  // -----------------------------------------------------
64 
65  using iterator_category = std::forward_iterator_tag;
67 
68  // -----------------------------------------------------
69  // Constructors and Rule of Five
70  // -----------------------------------------------------
71 
73  : start_( nullptr )
74  , link_( nullptr )
75  {}
76 
77  explicit IteratorNodeLinks( NodeType& node )
78  : start_( &node.primary_link() )
79  , link_( &node.primary_link() )
80  {}
81 
82  explicit IteratorNodeLinks( LinkType& link )
83  : start_( &link )
84  , link_( &link )
85  {}
86 
87  ~IteratorNodeLinks() = default;
88 
89  IteratorNodeLinks( IteratorNodeLinks const& ) = default;
90  IteratorNodeLinks( IteratorNodeLinks&& ) = default;
91 
92  IteratorNodeLinks& operator= ( IteratorNodeLinks const& ) = default;
94 
95  // -----------------------------------------------------
96  // Operators
97  // -----------------------------------------------------
98 
100  {
101  return *this;
102  }
103 
105  {
106  link_ = &link_->next();
107  if (link_ == start_) {
108  link_ = nullptr;
109  }
110  return *this;
111  }
112 
114  {
115  self_type tmp = *this;
116  ++(*this);
117  return tmp;
118  }
119 
120  bool operator == (const self_type &other) const
121  {
122  return other.link_ == link_;
123  }
124 
125  bool operator != (const self_type &other) const
126  {
127  return !(other == *this);
128  }
129 
130  // -----------------------------------------------------
131  // Members
132  // -----------------------------------------------------
133 
134  bool is_first_iteration() const
135  {
136  return link_ == start_;
137  }
138 
139  LinkType& link() const
140  {
141  return *link_;
142  }
143 
144  NodeType& node() const
145  {
146  return link_->node();
147  }
148 
149  EdgeType& edge() const
150  {
151  return link_->edge();
152  }
153 
154  LinkType& start_link() const
155  {
156  return *start_;
157  }
158 
159  // -----------------------------------------------------
160  // Data Members
161  // -----------------------------------------------------
162 
163 private:
164 
165  LinkType* const start_;
166  LinkType* link_;
167 };
168 
169 // =================================================================================================
170 // Node Links Wrapper Functions
171 // =================================================================================================
172 
173 template<typename ElementType>
175 node_links( ElementType const& element )
176 {
177  return {
180  };
181 }
182 
183 template<typename ElementType>
185 node_links( ElementType& element )
186 {
187  return {
190  };
191 }
192 
193 } // namespace tree
194 } // namespace genesis
195 
196 #endif // include guard
utils::Range< IteratorNodeLinks< TreeLink const, TreeNode const, TreeEdge const > > node_links(ElementType const &element)
Definition: node_links.hpp:175
Header of Tree class.