A library for working with phylogenetic and population genetic data.
v0.27.0
tree_set.hpp
Go to the documentation of this file.
1 #ifndef GENESIS_TREE_TREE_SET_H_
2 #define GENESIS_TREE_TREE_SET_H_
3 
4 /*
5  Genesis - A toolkit for working with phylogenetic data.
6  Copyright (C) 2014-2019 Lucas Czech and HITS gGmbH
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"
35 
36 #include <cassert>
37 #include <stdexcept>
38 #include <string>
39 #include <vector>
40 
41 namespace genesis {
42 namespace tree {
43 
44 // =================================================================================================
45 // Tree Set
46 // =================================================================================================
47 
48 class TreeSet
49 {
50 public:
51 
52  // -------------------------------------------------------------------------
53  // Typedefs
54  // -------------------------------------------------------------------------
55 
56  using iterator = typename std::vector<Tree>::iterator;
57  using const_iterator = typename std::vector<Tree>::const_iterator;
58 
59  // -------------------------------------------------------------------------
60  // Constructors and Rule of Five
61  // -------------------------------------------------------------------------
62 
63  TreeSet() = default;
64  ~TreeSet() = default;
65 
66  TreeSet( TreeSet const& ) = default;
67  TreeSet( TreeSet&& ) = default;
68 
69  TreeSet& operator= ( TreeSet const& ) = default;
70  TreeSet& operator= ( TreeSet&& ) = default;
71 
72  void swap( TreeSet& other )
73  {
74  using std::swap;
75  swap( names_, other.names_ );
76  swap( trees_, other.trees_ );
77  }
78 
79  // -------------------------------------------------------------------------
80  // Modifiers
81  // -------------------------------------------------------------------------
82 
88  void add( Tree const& tree, std::string const& name = "" )
89  {
90  names_.push_back( name );
91  trees_.push_back( tree );
92  }
93 
99  void add( Tree&& tree, std::string const& name = "" )
100  {
101  names_.push_back( name );
102  trees_.push_back( std::move( tree ));
103  }
104 
111  void remove_at( size_t index )
112  {
113  if( index >= trees_.size() ) {
114  throw std::invalid_argument(
115  "Cannot remove element at index " + std::to_string( index ) + " from TreeSet with " +
116  std::to_string( trees_.size() ) + " trees."
117  );
118  }
119  assert( names_.size() == trees_.size() );
120 
121  names_.erase( names_.begin() + index );
122  trees_.erase( trees_.begin() + index );
123  }
124 
128  void clear()
129  {
130  names_.clear();
131  trees_.clear();
132  }
133 
134  // -------------------------------------------------------------------------
135  // Name Accessors
136  // -------------------------------------------------------------------------
137 
138  std::string const& name_at( size_t index ) const
139  {
140  if( index >= names_.size() ) {
141  throw std::invalid_argument(
142  "Cannot access element at index " + std::to_string( index ) + " from TreeSet with " +
143  std::to_string( trees_.size() ) + " trees."
144  );
145  }
146  return names_[index];
147  }
148 
149  std::vector<std::string> const& names() const
150  {
151  return names_;
152  }
153 
154  // -------------------------------------------------------------------------
155  // Tree Accessors
156  // -------------------------------------------------------------------------
157 
159  {
160  return trees_.begin();
161  }
162 
164  {
165  return trees_.cbegin();
166  }
167 
169  {
170  return trees_.end();
171  }
172 
174  {
175  return trees_.cend();
176  }
177 
178  Tree& at( size_t index )
179  {
180  return trees_.at(index);
181  }
182 
183  Tree const& at( size_t index ) const
184  {
185  return trees_.at(index);
186  }
187 
188  Tree& operator [] (const std::size_t index)
189  {
190  return trees_[index];
191  }
192 
193  Tree const& operator [] (const std::size_t index) const
194  {
195  return trees_[index];
196  }
197 
198  std::vector<Tree> const& trees() const
199  {
200  return trees_;
201  }
202 
207  operator std::vector<Tree> const&() const
208  {
209  return trees_;
210  }
211 
212  // -------------------------------------------------------------------------
213  // General Properties
214  // -------------------------------------------------------------------------
215 
219  bool empty() const
220  {
221  assert( names_.empty() == trees_.empty() );
222  return trees_.empty();
223  }
224 
228  size_t size() const
229  {
230  assert( names_.size() == trees_.size() );
231  return trees_.size();
232  }
233 
234  // -------------------------------------------------------------------------
235  // Data Members
236  // -------------------------------------------------------------------------
237 
238 private:
239 
240  std::vector<std::string> names_;
241  std::vector<Tree> trees_;
242 
243 };
244 
245 } // namespace tree
246 } // namespace genesis
247 
248 #endif // include guard
genesis::tree::TreeSet::add
void add(Tree const &tree, std::string const &name="")
Add a Tree with a name to the TreeSet.
Definition: tree_set.hpp:88
genesis::placement::swap
void swap(Sample &lhs, Sample &rhs)
Definition: sample.cpp:104
genesis::tree::TreeSet::operator=
TreeSet & operator=(TreeSet const &)=default
genesis::tree::TreeSet::swap
void swap(TreeSet &other)
Definition: tree_set.hpp:72
genesis::tree::TreeSet::end
const_iterator end() const
Definition: tree_set.hpp:173
genesis::tree::TreeSet::TreeSet
TreeSet()=default
genesis::tree::TreeSet::size
size_t size() const
Return the size of the TreeSet, i.e., the number of stored Trees.
Definition: tree_set.hpp:228
tree.hpp
Header of Tree class.
genesis::tree::TreeSet::name_at
std::string const & name_at(size_t index) const
Definition: tree_set.hpp:138
genesis::tree::TreeSet::clear
void clear()
Clear the TreeSet and destroy all contained Trees.
Definition: tree_set.hpp:128
genesis::tree::TreeSet::end
iterator end()
Definition: tree_set.hpp:168
genesis::tree::TreeSet::at
Tree & at(size_t index)
Definition: tree_set.hpp:178
genesis::tree::TreeSet::iterator
typename std::vector< Tree >::iterator iterator
Definition: tree_set.hpp:56
genesis::population::to_string
std::string to_string(GenomeLocus const &locus)
Definition: functions/genome_locus.hpp:48
genesis::tree::TreeSet
Definition: tree_set.hpp:48
genesis::tree::Tree
Class for representing phylogenetic trees.
Definition: tree/tree.hpp:97
genesis::tree::TreeSet::add
void add(Tree &&tree, std::string const &name="")
Add a Tree with a name to the TreeSet.
Definition: tree_set.hpp:99
genesis::tree::TreeSet::~TreeSet
~TreeSet()=default
genesis::tree::TreeSet::trees
std::vector< Tree > const & trees() const
Definition: tree_set.hpp:198
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::tree::TreeSet::begin
const_iterator begin() const
Definition: tree_set.hpp:163
genesis::tree::TreeSet::remove_at
void remove_at(size_t index)
Remove the Tree at a certain index position.
Definition: tree_set.hpp:111
genesis::tree::TreeSet::names
std::vector< std::string > const & names() const
Definition: tree_set.hpp:149
genesis::tree::TreeSet::begin
iterator begin()
Definition: tree_set.hpp:158
genesis::tree::TreeSet::const_iterator
typename std::vector< Tree >::const_iterator const_iterator
Definition: tree_set.hpp:57
genesis::tree::TreeSet::at
Tree const & at(size_t index) const
Definition: tree_set.hpp:183
genesis::tree::TreeSet::empty
bool empty() const
Return whether the TreeSet is empty.
Definition: tree_set.hpp:219
genesis::tree::TreeSet::operator[]
Tree & operator[](const std::size_t index)
Definition: tree_set.hpp:188