A toolkit for working with phylogenetic data.
v0.19.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
eulertour.hpp
Go to the documentation of this file.
1 #ifndef GENESIS_TREE_ITERATOR_EULERTOUR_H_
2 #define GENESIS_TREE_ITERATOR_EULERTOUR_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 // Euler Tour Iterator
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 IteratorEulertour (Tree& tree)
78  : start_( &tree.root_link() )
79  , link_( &tree.root_link() )
80  {}
81 
82  explicit IteratorEulertour (Tree const& tree)
83  : start_( &tree.root_link() )
84  , link_( &tree.root_link() )
85  {}
86 
87  explicit IteratorEulertour (NodeType& node)
88  : start_( &node.primary_link() )
89  , link_( &node.primary_link() )
90  {}
91 
92  explicit IteratorEulertour (LinkType& link)
93  : start_( &link )
94  , link_( &link )
95  {}
96 
97  ~IteratorEulertour() = default;
98 
99  IteratorEulertour( IteratorEulertour const& ) = default;
100  IteratorEulertour( IteratorEulertour&& ) = default;
101 
102  IteratorEulertour& operator= ( IteratorEulertour const& ) = default;
104 
105  // -----------------------------------------------------
106  // Operators
107  // -----------------------------------------------------
108 
110  {
111  return *this;
112  }
113 
115  {
116  link_ = &link_->outer().next();
117  if (link_ == start_) {
118  link_ = nullptr;
119  }
120  return *this;
121  }
122 
124  {
125  self_type tmp = *this;
126  ++(*this);
127  return tmp;
128  }
129 
130  bool operator == (const self_type &other) const
131  {
132  return other.link_ == link_;
133  }
134 
135  bool operator != (const self_type &other) const
136  {
137  return !(other == *this);
138  }
139 
140  // -----------------------------------------------------
141  // Members
142  // -----------------------------------------------------
143 
144  bool is_first_iteration() const
145  {
146  return link_ == start_;
147  }
148 
149  LinkType& link() const
150  {
151  return *link_;
152  }
153 
154  NodeType& node() const
155  {
156  return link_->node();
157  }
158 
159  EdgeType& edge() const
160  {
161  return link_->edge();
162  }
163 
164  LinkType& start_link() const
165  {
166  return *start_;
167  }
168 
169  NodeType& start_node() const
170  {
171  return start_->node();
172  }
173 
174  // -----------------------------------------------------
175  // Data Members
176  // -----------------------------------------------------
177 
178 private:
179 
180  LinkType* const start_;
181  LinkType* link_;
182 };
183 
184 // =================================================================================================
185 // Euler Tour Wrapper Functions
186 // =================================================================================================
187 
188 template<typename ElementType>
190 eulertour( ElementType const& element )
191 {
192  return {
195  };
196 }
197 
198 template<typename ElementType>
200 eulertour( ElementType& element )
201 {
202  return {
205  };
206 }
207 
208 } // namespace tree
209 } // namespace genesis
210 
211 #endif // include guard
IteratorEulertour(LinkType &link)
Definition: eulertour.hpp:92
IteratorEulertour & operator=(IteratorEulertour const &)=default
bool operator!=(const self_type &other) const
Definition: eulertour.hpp:135
IteratorEulertour(NodeType &node)
Definition: eulertour.hpp:87
utils::Range< IteratorEulertour< TreeLink const, TreeNode const, TreeEdge const > > eulertour(ElementType const &element)
Definition: eulertour.hpp:190
IteratorEulertour(Tree const &tree)
Definition: eulertour.hpp:82
Class for representing phylogenetic trees.
Definition: tree/tree.hpp:95
LinkType & start_link() const
Definition: eulertour.hpp:164
Header of Tree class.
NodeType & start_node() const
Definition: eulertour.hpp:169
bool operator==(const self_type &other) const
Definition: eulertour.hpp:130
std::forward_iterator_tag iterator_category
Definition: eulertour.hpp:65