A toolkit for working with phylogenetic data.
v0.24.0
taxopath.hpp
Go to the documentation of this file.
1 #ifndef GENESIS_TAXONOMY_TAXOPATH_H_
2 #define GENESIS_TAXONOMY_TAXOPATH_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 <functional>
35 #include <stdexcept>
36 #include <string>
37 #include <vector>
38 
39 namespace genesis {
40 namespace taxonomy {
41 
42 // =================================================================================================
43 // Forward Declarations
44 // =================================================================================================
45 
46 class Taxon;
47 class Taxonomy;
48 
49 // =================================================================================================
50 // Taxopath
51 // =================================================================================================
52 
83 class Taxopath
84 {
85 public:
86 
87  // -------------------------------------------------------------------------
88  // Typedefs and Enums
89  // -------------------------------------------------------------------------
90 
91  typedef std::vector< std::string >::iterator iterator;
92  typedef std::vector< std::string >::const_iterator const_iterator;
93  typedef std::vector< std::string >::reverse_iterator reverse_iterator;
94  typedef std::vector< std::string >::const_reverse_iterator const_reverse_iterator;
95 
96  // -------------------------------------------------------------------------
97  // Constructors and Rule of Five
98  // -------------------------------------------------------------------------
99 
100  Taxopath() = default;
101  ~Taxopath() = default;
102 
109  explicit Taxopath( std::vector< std::string > const& elements )
110  : elements_( elements )
111  {}
112 
119  explicit Taxopath( std::vector< std::string > && elements )
120  : elements_( std::move( elements ))
121  {}
122 
123 
124  Taxopath( Taxopath const& ) = default;
125  Taxopath( Taxopath&& ) = default;
126 
127  Taxopath& operator= ( Taxopath const& ) = default;
128  Taxopath& operator= ( Taxopath&& ) = default;
129 
133  void swap( Taxopath& other )
134  {
135  using std::swap;
136  swap( elements_, other.elements_ );
137  }
138 
139  // -------------------------------------------------------------------------
140  // Accessors
141  // -------------------------------------------------------------------------
142 
146  bool empty() const
147  {
148  return elements_.empty();
149  }
150 
154  size_t size() const
155  {
156  return elements_.size();
157  }
158 
164  std::string const& at ( size_t index ) const
165  {
166  return elements_.at( index );
167  }
168 
174  std::string& at ( size_t index )
175  {
176  return elements_.at( index );
177  }
178 
184  std::string const& operator [] ( size_t index ) const
185  {
186  return elements_[ index ];
187  }
188 
194  std::string& operator [] ( size_t index )
195  {
196  return elements_[ index ];
197  }
198 
202  std::vector< std::string > const& elements() const
203  {
204  return elements_;
205  }
206 
210  std::vector< std::string > & elements()
211  {
212  return elements_;
213  }
214 
215 
216  // -------------------------------------------------------------------------
217  // Modifiers
218  // -------------------------------------------------------------------------
219 
226  void assign( std::vector< std::string > const& from )
227  {
228  elements_ = from;
229  }
230 
234  void push_back( std::string const& value )
235  {
236  elements_.push_back( value );
237  }
238 
242  void push_back( std::string&& value )
243  {
244  elements_.push_back( std::move( value ));
245  }
246 
252  std::string pop_back()
253  {
254  if( elements_.empty() ) {
255  throw std::out_of_range( "Cannot pop last element of empty Taxopath." );
256  }
257  auto last = elements_.back();
258  elements_.pop_back();
259  return last;
260  }
261 
265  void clear()
266  {
267  elements_.clear();
268  }
269 
270  // -------------------------------------------------------------------------
271  // Comparison
272  // -------------------------------------------------------------------------
273 
274  friend bool operator == ( Taxopath const & lhs, Taxopath const & rhs )
275  {
276  return lhs.elements_ == rhs.elements_;
277  }
278 
279  friend bool operator != ( Taxopath const & lhs, Taxopath const & rhs )
280  {
281  return lhs.elements_ != rhs.elements_;
282  }
283 
284  friend bool operator < ( Taxopath const & lhs, Taxopath const & rhs )
285  {
286  return lhs.elements_ < rhs.elements_;
287  }
288 
289  friend bool operator <= ( Taxopath const & lhs, Taxopath const & rhs )
290  {
291  return lhs.elements_ <= rhs.elements_;
292  }
293 
294  friend bool operator > ( Taxopath const & lhs, Taxopath const & rhs )
295  {
296  return lhs.elements_ > rhs.elements_;
297  }
298 
299  friend bool operator >= ( Taxopath const & lhs, Taxopath const & rhs )
300  {
301  return lhs.elements_ >= rhs.elements_;
302  }
303 
304  // -------------------------------------------------------------------------
305  // Iterators
306  // -------------------------------------------------------------------------
307 
311  iterator begin()
312  {
313  return elements_.begin();
314  }
315 
319  iterator end()
320  {
321  return elements_.end();
322  }
323 
327  const_iterator begin() const
328  {
329  return elements_.cbegin();
330  }
331 
335  const_iterator end() const
336  {
337  return elements_.cend();
338  }
339 
343  reverse_iterator rbegin()
344  {
345  return elements_.rbegin();
346  }
347 
351  reverse_iterator rend()
352  {
353  return elements_.rend();
354  }
355 
359  const_reverse_iterator rbegin() const
360  {
361  return elements_.crbegin();
362  }
363 
367  const_reverse_iterator rend() const
368  {
369  return elements_.crend();
370  }
371 
372  // -------------------------------------------------------------------------
373  // Data Members
374  // -------------------------------------------------------------------------
375 
376 private:
377 
378  std::vector< std::string > elements_;
379 };
380 
381 } // namespace taxonomy
382 } // namespace genesis
383 
384 // ================================================================================================
385 // Standard Hash Function
386 // ================================================================================================
387 
388 namespace std
389 {
395  template<>
396  struct hash<genesis::taxonomy::Taxopath>
397  {
399  using result_type = std::size_t;
400 
402  result_type result = 0;
403  for( auto const& e : t ) {
404  result ^= std::hash<std::string>{}( e );
405  }
406  return result;
407  }
408  };
409 }
410 
411 #endif // include guard
friend bool operator<=(Taxopath const &lhs, Taxopath const &rhs)
Definition: taxopath.hpp:289
friend bool operator>=(Taxopath const &lhs, Taxopath const &rhs)
Definition: taxopath.hpp:299
iterator end()
Return an iterator to the end of the taxonomic elements.
Definition: taxopath.hpp:319
friend bool operator==(Taxopath const &lhs, Taxopath const &rhs)
Definition: taxopath.hpp:274
std::string & at(size_t index)
Return the taxonomic element at a certain position.
Definition: taxopath.hpp:174
void assign(std::vector< std::string > const &from)
Replace the current elements of the Taxopath by a list of strings.
Definition: taxopath.hpp:226
void push_back(std::string &&value)
Add an element to the end of the Taxopath by moving it.
Definition: taxopath.hpp:242
void swap(SequenceSet &lhs, SequenceSet &rhs)
friend bool operator>(Taxopath const &lhs, Taxopath const &rhs)
Definition: taxopath.hpp:294
void clear()
Clear all taxonomic elements. This results in an empty Taxopath.
Definition: taxopath.hpp:265
STL namespace.
std::vector< std::string >::const_reverse_iterator const_reverse_iterator
Definition: taxopath.hpp:94
void push_back(std::string const &value)
Add an element to the end of the Taxopath by copying it.
Definition: taxopath.hpp:234
friend bool operator!=(Taxopath const &lhs, Taxopath const &rhs)
Definition: taxopath.hpp:279
std::vector< std::string >::reverse_iterator reverse_iterator
Definition: taxopath.hpp:93
Container namespace for all symbols of genesis in order to keep them separate when used as a library...
iterator begin()
Return an iterator to the beginning of the taxonomic elements.
Definition: taxopath.hpp:311
std::vector< std::string > & elements()
Return the elements of the Taxopath as a vector of strings.
Definition: taxopath.hpp:210
Taxopath(std::vector< std::string > &&elements)
Fill constructor that uses the given strings to initialize the taxonomic elements.
Definition: taxopath.hpp:119
reverse_iterator rbegin()
Return a reverse iterator to the reverse beginning of the taxonomic elements.
Definition: taxopath.hpp:343
friend bool operator<(Taxopath const &lhs, Taxopath const &rhs)
Definition: taxopath.hpp:284
reverse_iterator rend()
Return a reverse iterator to the reverse end of the taxonomic elements.
Definition: taxopath.hpp:351
bool empty() const
Return whether the Taxopath is empty, i.e., does not contain any elements.
Definition: taxopath.hpp:146
std::vector< std::string >::const_iterator const_iterator
Definition: taxopath.hpp:92
std::string pop_back()
Remove the last element of the Taxopath and return its value.
Definition: taxopath.hpp:252
std::string const & at(size_t index) const
Return the taxonomic element at a certain position.
Definition: taxopath.hpp:164
std::vector< std::string > const & elements() const
Return the elements of the Taxopath as a vector of strings.
Definition: taxopath.hpp:202
Helper class to store a taxonomic path.
Definition: taxopath.hpp:83
std::string const & operator[](size_t index) const
Return the taxonomic element at a certain position.
Definition: taxopath.hpp:184
size_t size() const
Return the number of elements of this Taxopath.
Definition: taxopath.hpp:154
Taxopath & operator=(Taxopath const &)=default
std::vector< std::string >::iterator iterator
Definition: taxopath.hpp:91
const_iterator begin() const
Return a const iterator to the beginning of the taxonomic elements.
Definition: taxopath.hpp:327
const_iterator end() const
Return a const iterator to the end of the taxonomic elements.
Definition: taxopath.hpp:335
const_reverse_iterator rend() const
Return a const reverse iterator to the reverse end of the taxonomic elements.
Definition: taxopath.hpp:367
Taxopath(std::vector< std::string > const &elements)
Fill constructor that uses the given strings to initialize the taxonomic elements.
Definition: taxopath.hpp:109
result_type operator()(argument_type const &t) const
Definition: taxopath.hpp:401
const_reverse_iterator rbegin() const
Return a const reverse iterator to the reverse beginning of the taxonomic elements.
Definition: taxopath.hpp:359
void swap(Taxopath &other)
Swap the elements of two Taxopaths.
Definition: taxopath.hpp:133