A library for working with phylogenetic and population genetic data.
v0.27.0
nexus/document.cpp
Go to the documentation of this file.
1 /*
2  Genesis - A toolkit for working with phylogenetic data.
3  Copyright (C) 2014-2017 Lucas Czech
4 
5  This program is free software: you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation, either version 3 of the License, or
8  (at your option) any later version.
9 
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with this program. If not, see <http://www.gnu.org/licenses/>.
17 
18  Contact:
19  Lucas Czech <lucas.czech@h-its.org>
20  Exelixis Lab, Heidelberg Institute for Theoretical Studies
21  Schloss-Wolfsbrunnenweg 35, D-69118 Heidelberg, Germany
22 */
23 
32 
33 namespace genesis {
34 namespace utils {
35 
36 // =================================================================================================
37 // Accessors
38 // =================================================================================================
39 
40 bool NexusDocument::has_block( std::string block_name ) const
41 {
42  return get_block( block_name ) != nullptr;
43 }
44 
45 NexusBlock const* NexusDocument::get_block( std::string block_name ) const
46 {
47  // Could use std::find_if instead, but checking iterators is also a bit tedious...
48  for( auto& bptr : data_ ) {
49  if( bptr->block_name() == block_name ) {
50  return bptr.get();
51  }
52  }
53  return nullptr;
54 }
55 
56 NexusBlock* NexusDocument::get_block( std::string block_name )
57 {
58  // Identical to the const version of this function... a bit ugly!
59  for( auto& bptr : data_ ) {
60  if( bptr->block_name() == block_name ) {
61  return bptr.get();
62  }
63  }
64  return nullptr;
65 }
66 
67 // =================================================================================================
68 // Modifiers
69 // =================================================================================================
70 
71 NexusBlock* NexusDocument::set_block( std::unique_ptr<NexusBlock> block )
72 {
73  // First check if we already have a block with this name.
74  // If so, replace it and return its pointer.
75  for( auto& bptr : data_ ) {
76  if( bptr->block_name() == block->block_name() ) {
77  bptr = std::move(block);
78  return bptr.get();
79  }
80  }
81 
82  // If there is no block with this name yet, create one.
83  data_.push_back( std::move(block) );
84  return data_.back().get();
85 }
86 
87 // void NexusDocument::set_block_trees( Trees const& trees )
88 // {
89 // for( auto& bptr : data_ ) {
90 // if( bptr->block_name() == trees.block_name() ) {
91 // bptr = utils::make_unique<Trees>(trees);
92 // return;
93 // }
94 // }
95 // data_.push_back( utils::make_unique<Trees>(trees) );
96 // }
97 //
98 // void NexusDocument::set_block_taxa( Taxa const& taxa )
99 // {
100 // data_.push_back( utils::make_unique<Taxa>(taxa) );
101 // }
102 
103 // const_iterator NexusDocument::begin() const
104 // {
105 //
106 // }
107 //
108 // const_iterator NexusDocument::end() const
109 // {
110 //
111 // }
112 
113 } // namespace utils
114 } // namespace genesis
genesis::utils::NexusBlock
Definition: block.hpp:47
genesis::utils::NexusDocument::has_block
bool has_block(std::string block_name) const
Definition: nexus/document.cpp:40
genesis::utils::NexusDocument::get_block
NexusBlock const * get_block(std::string block_name) const
Definition: nexus/document.cpp:45
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
document.hpp
genesis::utils::NexusDocument::set_block
NexusBlock * set_block(std::unique_ptr< NexusBlock > block)
Definition: nexus/document.cpp:71