A library for working with phylogenetic and population genetic data.
v0.27.0
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-2020 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
genesis::taxonomy::Taxon
Store a Taxon, i.e., an element in a Taxonomy, with its name, rank, ID and sub-taxa.
Definition: taxon.hpp:76
genesis::taxonomy::levelorder_for_each
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.
Definition: taxonomy/iterator/levelorder.hpp:58
taxonomy.hpp
range.hpp
genesis
Container namespace for all symbols of genesis in order to keep them separate when used as a library.
Definition: placement/formats/edge_color.cpp:42
genesis::taxonomy::Taxonomy
Store a Taxonomy, i.e., a nested hierarchy of Taxa.
Definition: taxonomy/taxonomy.hpp:96
taxon.hpp