A toolkit for working with phylogenetic data.
v0.19.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
taxonomy/iterator/levelorder.hpp
Go to the documentation of this file.
1 #ifndef GENESIS_TAXONOMY_ITERATOR_LEVELORDER_H_
2 #define GENESIS_TAXONOMY_ITERATOR_LEVELORDER_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 
37 
38 #include <functional>
39 #include <queue>
40 
41 namespace genesis {
42 namespace taxonomy {
43 
44 // =================================================================================================
45 // Levelorder For Each
46 // =================================================================================================
47 
58 inline void levelorder_for_each(
59  Taxonomy& tax,
60  std::function< void( Taxon& )> fn,
61  bool include_inner_taxa = true
62 ) {
63  std::queue< Taxon* > taxa_queue;
64  for( auto& t : tax ) {
65  taxa_queue.push( &t );
66  }
67 
68  while( ! taxa_queue.empty() ) {
69  auto& cur = *taxa_queue.front();
70 
71  if( include_inner_taxa || cur.size() == 0 ) {
72  fn( cur );
73  }
74 
75  for( auto& t : cur ) {
76  taxa_queue.push( &t );
77  }
78  taxa_queue.pop();
79  }
80 }
81 
92 inline void levelorder_for_each(
93  Taxonomy const& tax,
94  std::function< void( Taxon const& )> fn,
95  bool include_inner_taxa = true
96 ) {
97  std::queue< Taxon const* > taxa_queue;
98  for( auto& t : tax ) {
99  taxa_queue.push( &t );
100  }
101 
102  while( ! taxa_queue.empty() ) {
103  auto const& cur = *taxa_queue.front();
104 
105  if( include_inner_taxa || cur.size() == 0 ) {
106  fn( cur );
107  }
108 
109  for( auto const& t : cur ) {
110  taxa_queue.push( &t );
111  }
112  taxa_queue.pop();
113  }
114 }
115 
116 } // namespace taxonomy
117 } // namespace genesis
118 
119 #endif // include guard
void levelorder_for_each(Taxonomy &tax, std::function< void(Taxon &)> fn, bool include_inner_taxa=true)
Apply a function to all taxa of the Taxonomy, traversing it in levelorder.
Store a Taxonomy, i.e., a nested hierarchy of Taxa.
Store a Taxon, i.e., an element in a Taxonomy, with its name, rank and sub-taxa.
Definition: taxon.hpp:76