A toolkit for working with phylogenetic data.
v0.18.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
taxonomy/taxonomy.hpp
Go to the documentation of this file.
1 #ifndef GENESIS_TAXONOMY_TAXONOMY_H_
2 #define GENESIS_TAXONOMY_TAXONOMY_H_
3 
4 /*
5  Genesis - A toolkit for working with phylogenetic data.
6  Copyright (C) 2014-2017 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 
34 #include <string>
35 #include <vector>
36 
37 namespace genesis {
38 namespace taxonomy {
39 
40 // =================================================================================================
41 // Forward Declarations
42 // =================================================================================================
43 
44 class Taxon;
45 class Taxonomy;
46 void swap( Taxonomy& lhs, Taxonomy& rhs );
47 
48 // =================================================================================================
49 // Taxonomy
50 // =================================================================================================
51 
96 class Taxonomy
97 {
98 public:
99 
100  // -------------------------------------------------------------------------
101  // Typedefs and Enums
102  // -------------------------------------------------------------------------
103 
104  typedef std::vector<Taxon>::iterator iterator;
105  typedef std::vector<Taxon>::const_iterator const_iterator;
106 
107  // -------------------------------------------------------------------------
108  // Constructors and Rule of Five
109  // -------------------------------------------------------------------------
110 
111  Taxonomy() = default;
112  virtual ~Taxonomy() = default;
113 
114  Taxonomy( Taxonomy const& );
115  Taxonomy( Taxonomy&& );
116 
117  Taxonomy& operator= ( Taxonomy const& );
119 
120  friend void swap( Taxonomy& lhs, Taxonomy& rhs );
121 
122  // -------------------------------------------------------------------------
123  // Accessors
124  // -------------------------------------------------------------------------
125 
126  size_t size() const;
127 
128  bool has_child ( std::string name ) const;
129 
130  Taxon const& get_child ( std::string name ) const;
131  Taxon& get_child ( std::string name );
132 
133  Taxon const& operator [] ( std::string name ) const;
134  Taxon& operator [] ( std::string name );
135 
136  Taxon const& at ( size_t index ) const;
137  Taxon& at ( size_t index );
138 
139  Taxon const& operator [] ( size_t index ) const;
140  Taxon& operator [] ( size_t index );
141 
142  size_t index_of( std::string const& name ) const;
143 
144  // -------------------------------------------------------------------------
145  // Modifiers
146  // -------------------------------------------------------------------------
147 
148  Taxon& add_child( Taxon const& child );
149  Taxon& add_child( std::string const& name );
150 
151  void remove_child( std::string const& name );
152  void clear_children();
153 
154  // -------------------------------------------------------------------------
155  // Iterators
156  // -------------------------------------------------------------------------
157 
158  iterator begin();
159  iterator end();
160 
161  const_iterator begin() const;
162  const_iterator end() const;
163 
164  const_iterator cbegin() const;
165  const_iterator cend() const;
166 
167  // -------------------------------------------------------------------------
168  // Internal Implementation Details
169  // -------------------------------------------------------------------------
170 
171 protected:
172 
173  virtual Taxon& add_child_( Taxon const& child );
174 
175  void reset_parent_pointers_( Taxon* parent );
176 
177  // -------------------------------------------------------------------------
178  // Data Members
179  // -------------------------------------------------------------------------
180 
181 private:
182 
183  std::vector<Taxon> children_;
184 };
185 
186 } // namespace taxonomy
187 } // namespace genesis
188 
189 #endif // include guard
Taxonomy & operator=(Taxonomy const &)
Copy assigment operator.
Definition: taxonomy.cpp:75
iterator begin()
Return an iterator to the beginning of the child taxa.
Definition: taxonomy.cpp:290
Taxon & add_child(Taxon const &child)
Add a child Taxon as a copy of a given Taxon and return it.
Definition: taxonomy.cpp:233
Taxon const & at(size_t index) const
Return the child Taxon at the given index.
Definition: taxonomy.cpp:179
std::vector< Taxon >::const_iterator const_iterator
bool has_child(std::string name) const
Return whether an immediate child Taxon with the given name exists.
Definition: taxonomy.cpp:121
Taxon const & get_child(std::string name) const
Return the child Taxon with a given name if it exists, or throw otherwise.
Definition: taxonomy.cpp:135
virtual ~Taxonomy()=default
void reset_parent_pointers_(Taxon *parent)
Internal helper function that resets the parent pointer of all stored Taxa.
Definition: taxonomy.cpp:388
Store a Taxonomy, i.e., a nested hierarchy of Taxa.
std::vector< Taxon >::iterator iterator
size_t index_of(std::string const &name) const
Definition: taxonomy.cpp:214
const_iterator cend() const
Return a const iterator to the end of the child taxa.
Definition: taxonomy.cpp:330
Store a Taxon, i.e., an element in a Taxonomy, with its name, rank and sub-taxa.
Definition: taxon.hpp:76
void swap(Taxon &lhs, Taxon &rhs)
Swapperator for Taxon.
Definition: taxon.cpp:147
void remove_child(std::string const &name)
Remove a child Taxon with a certain name.
Definition: taxonomy.cpp:254
size_t size() const
Return the number of immediate child Taxa.
Definition: taxonomy.cpp:113
iterator end()
Return an iterator to the end of the child taxa.
Definition: taxonomy.cpp:298
const_iterator cbegin() const
Return a const iterator to the beginning of the child taxa.
Definition: taxonomy.cpp:322
friend void swap(Taxonomy &lhs, Taxonomy &rhs)
Swapperator for Taxonomy.
Definition: taxonomy.cpp:98
void clear_children()
Remove all children.
Definition: taxonomy.cpp:278
virtual Taxon & add_child_(Taxon const &child)
Virtual implementation function for adding a child Taxon.
Definition: taxonomy.cpp:352
Taxon const & operator[](std::string name) const
Return the child Taxon with a given name if it exists, or throw otherwise.
Definition: taxonomy.cpp:161