A toolkit for working with phylogenetic data.
v0.18.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
broker.hpp
Go to the documentation of this file.
1 #ifndef GENESIS_TREE_FORMATS_NEWICK_BROKER_H_
2 #define GENESIS_TREE_FORMATS_NEWICK_BROKER_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 <deque>
35 #include <stdexcept>
36 #include <string>
37 #include <vector>
38 
40 
41 namespace genesis {
42 namespace tree {
43 
44 // =================================================================================================
45 // NewickBroker
46 // =================================================================================================
47 
107 {
108 public:
109 
110  // -------------------------------------------------------------------------
111  // Typedefs and Enums
112  // -------------------------------------------------------------------------
113 
114  typedef std::deque<NewickBrokerElement>::iterator iterator;
115  typedef std::deque<NewickBrokerElement>::const_iterator const_iterator;
116  typedef std::deque<NewickBrokerElement>::reverse_iterator reverse_iterator;
117  typedef std::deque<NewickBrokerElement>::const_reverse_iterator const_reverse_iterator;
118 
119  // -------------------------------------------------------------------------
120  // Constructor and Rule of Five
121  // -------------------------------------------------------------------------
122 
123  NewickBroker() = default;
124  ~NewickBroker() = default;
125 
126  NewickBroker(NewickBroker const&) = default;
127  NewickBroker(NewickBroker&&) = default;
128 
129  NewickBroker& operator= (NewickBroker const&) = default;
130  NewickBroker& operator= (NewickBroker&&) = default;
131 
132  // -------------------------------------------------------------------------
133  // Modifiers
134  // -------------------------------------------------------------------------
135 
139  void clear();
140 
141  void push_top (NewickBrokerElement const& node);
142  void push_top (NewickBrokerElement&& node);
143 
144  void push_bottom (NewickBrokerElement const& node);
145  void push_bottom (NewickBrokerElement&& node);
146 
147  void pop_top();
148  void pop_bottom();
149 
150  // -------------------------------------------------------------------------
151  // State Functions
152  // -------------------------------------------------------------------------
153 
161  void assign_ranks() const;
162 
166  int leaf_count() const;
167 
168  int inner_count() const;
169 
173  int node_count() const;
174 
178  int max_rank() const;
179 
180  bool is_bifurcating() const;
181 
182  // -------------------------------------------------------------------------
183  // Properties
184  // -------------------------------------------------------------------------
185 
189  bool empty() const;
190 
194  size_t size() const;
195 
196  // -------------------------------------------------------------------------
197  // Element Access
198  // -------------------------------------------------------------------------
199 
214  NewickBrokerElement & operator [] (std::size_t index);
215  NewickBrokerElement const& operator [] (std::size_t index) const;
216 
222  NewickBrokerElement & at(std::size_t index);
223  NewickBrokerElement const& at(std::size_t index) const;
224 
235  NewickBrokerElement const& top() const;
236 
243  NewickBrokerElement const& bottom() const;
244 
245  // -------------------------------------------------------------------------
246  // Iteration
247  // -------------------------------------------------------------------------
248 
252  iterator begin();
253 
257  const_iterator begin() const;
258 
262  iterator end();
263 
267  const_iterator end() const;
268 
272  const_iterator cbegin() const;
273 
277  const_iterator cend() const;
278 
283 
288 
293 
298 
303 
308 
309  // -------------------------------------------------------------------------
310  // Dump and Debug
311  // -------------------------------------------------------------------------
312 
324  bool validate() const;
325 
329  std::string dump() const;
330 
331  // -------------------------------------------------------------------------
332  // Data Members
333  // -------------------------------------------------------------------------
334 
335 private:
336 
337  std::deque<NewickBrokerElement> stack_;
338 };
339 
340 } // namespace tree
341 } // namespace genesis
342 
343 #endif // include guard
reverse_iterator rend()
Reverse version of end().
Definition: broker.cpp:274
NewickBrokerElement & at(std::size_t index)
Provides index based array access to the nodes, doing a boundary check first.
Definition: broker.cpp:194
std::deque< NewickBrokerElement >::reverse_iterator reverse_iterator
Definition: broker.hpp:116
reverse_iterator rbegin()
Returns a reverse iterator to the nodes on the stack.
Definition: broker.cpp:264
bool empty() const
Returns whether the stack is empty.
Definition: broker.cpp:170
std::deque< NewickBrokerElement >::const_reverse_iterator const_reverse_iterator
Definition: broker.hpp:117
bool is_bifurcating() const
Definition: broker.cpp:161
void assign_ranks() const
Iterate over the tree and assign ranks (= number of immediate children) to all nodes.
Definition: broker.cpp:85
void push_top(NewickBrokerElement const &node)
Definition: broker.cpp:51
const_iterator cbegin() const
Const version of begin().
Definition: broker.cpp:254
NewickBrokerElement & bottom()
Returns a reference to the bottom node of the tree stack.
Definition: broker.cpp:220
Stores a Newick tree in an intermediate format that can be further processed into a Tree...
Definition: broker.hpp:106
size_t size() const
Returns the size of the stack, i.e. the number of nodes stored in the broker.
Definition: broker.cpp:175
int inner_count() const
Definition: broker.cpp:136
const_reverse_iterator crbegin()
Const version of rbegin().
Definition: broker.cpp:284
int leaf_count() const
Returns the number of leaf nodes in the tree. assign_ranks() has to be called first.
Definition: broker.cpp:122
NewickBrokerElement & top()
Returns a reference to the top node of the tree stack.
Definition: broker.cpp:210
const_iterator cend() const
Const version of end().
Definition: broker.cpp:259
std::string dump() const
Return a readable string representation of the elements of the NewickBroker.
Definition: broker.cpp:324
int node_count() const
Alias for size().
Definition: broker.cpp:141
int max_rank() const
Returns the highest rank of the nodes in the tree. assign_ranks() has to be called first...
Definition: broker.cpp:146
NewickBroker & operator=(NewickBroker const &)=default
bool validate() const
Returns true iff the tree is valid. assign_ranks() has to be called first.
Definition: broker.cpp:299
const_reverse_iterator crend()
Const version of rend().
Definition: broker.cpp:289
void clear()
Deletes all nodes from the broker.
Definition: broker.cpp:46
NewickBrokerElement & operator[](std::size_t index)
Provides index based array access to the nodes.
Definition: broker.cpp:184
iterator end()
Returns an iterator to the end of the token list.
Definition: broker.cpp:244
void push_bottom(NewickBrokerElement const &node)
Definition: broker.cpp:61
std::deque< NewickBrokerElement >::const_iterator const_iterator
Definition: broker.hpp:115
iterator begin()
Returns an iterator to the top of the stack.
Definition: broker.cpp:234
std::deque< NewickBrokerElement >::iterator iterator
Definition: broker.hpp:114
Store the information for one element of a Newick tree.
Definition: element.hpp:60