A toolkit for working with phylogenetic data.
v0.24.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
bool empty() const
Return whether the TreeSet is empty.
Definition: tree_set.hpp:219
const_iterator end() const
Definition: tree_set.hpp:173
Tree & at(size_t index)
Definition: tree_set.hpp:178
void add(Tree const &tree, std::string const &name="")
Add a Tree with a name to the TreeSet.
Definition: tree_set.hpp:88
std::string const & name_at(size_t index) const
Definition: tree_set.hpp:138
typename std::vector< Tree >::iterator iterator
Definition: tree_set.hpp:56
void swap(SequenceSet &lhs, SequenceSet &rhs)
size_t size() const
Return the size of the TreeSet, i.e., the number of stored Trees.
Definition: tree_set.hpp:228
Container namespace for all symbols of genesis in order to keep them separate when used as a library...
void clear()
Clear the TreeSet and destroy all contained Trees.
Definition: tree_set.hpp:128
void add(Tree &&tree, std::string const &name="")
Add a Tree with a name to the TreeSet.
Definition: tree_set.hpp:99
Class for representing phylogenetic trees.
Definition: tree/tree.hpp:97
std::vector< Tree > const & trees() const
Definition: tree_set.hpp:198
std::vector< std::string > const & names() const
Definition: tree_set.hpp:149
void remove_at(size_t index)
Remove the Tree at a certain index position.
Definition: tree_set.hpp:111
Tree & operator[](const std::size_t index)
Definition: tree_set.hpp:188
const_iterator begin() const
Definition: tree_set.hpp:163
TreeSet & operator=(TreeSet const &)=default
std::shared_ptr< BaseOutputTarget > to_string(std::string &target_string)
Obtain an output target for writing to a string.
Header of Tree class.
typename std::vector< Tree >::const_iterator const_iterator
Definition: tree_set.hpp:57
void swap(TreeSet &other)
Definition: tree_set.hpp:72
Tree const & at(size_t index) const
Definition: tree_set.hpp:183