A library for working with phylogenetic and population genetic data.
v0.27.0
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-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 <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  size_t leaf_count() const;
167 
168  size_t inner_count() const;
169 
173  size_t node_count() const;
174 
178  long 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
genesis::tree::NewickBroker::iterator
std::deque< NewickBrokerElement >::iterator iterator
Definition: broker.hpp:114
genesis::tree::NewickBroker::const_reverse_iterator
std::deque< NewickBrokerElement >::const_reverse_iterator const_reverse_iterator
Definition: broker.hpp:117
genesis::tree::NewickBroker::operator[]
NewickBrokerElement & operator[](std::size_t index)
Provides index based array access to the nodes.
Definition: broker.cpp:187
genesis::tree::NewickBroker::reverse_iterator
std::deque< NewickBrokerElement >::reverse_iterator reverse_iterator
Definition: broker.hpp:116
genesis::tree::NewickBroker::pop_bottom
void pop_bottom()
Definition: broker.cpp:76
genesis::tree::NewickBroker::~NewickBroker
~NewickBroker()=default
genesis::tree::NewickBroker::top
NewickBrokerElement & top()
Returns a reference to the top node of the tree stack.
Definition: broker.cpp:213
genesis::tree::NewickBroker::clear
void clear()
Deletes all nodes from the broker.
Definition: broker.cpp:46
element.hpp
genesis::tree::NewickBroker::max_rank
long max_rank() const
Returns the highest rank of the nodes in the tree. assign_ranks() has to be called first.
Definition: broker.cpp:149
genesis::tree::NewickBroker::end
iterator end()
Returns an iterator to the end of the token list.
Definition: broker.cpp:247
genesis::tree::NewickBroker::validate
bool validate() const
Returns true iff the tree is valid. assign_ranks() has to be called first.
Definition: broker.cpp:302
genesis::tree::NewickBroker::push_top
void push_top(NewickBrokerElement const &node)
Definition: broker.cpp:51
genesis::tree::NewickBroker::dump
std::string dump() const
Return a readable string representation of the elements of the NewickBroker.
Definition: broker.cpp:327
genesis::tree::NewickBroker::begin
iterator begin()
Returns an iterator to the top of the stack.
Definition: broker.cpp:237
genesis::tree::NewickBroker::cbegin
const_iterator cbegin() const
Const version of begin().
Definition: broker.cpp:257
genesis::tree::NewickBroker::crbegin
const_reverse_iterator crbegin()
Const version of rbegin().
Definition: broker.cpp:287
genesis::tree::NewickBroker::rend
reverse_iterator rend()
Reverse version of end().
Definition: broker.cpp:277
genesis::tree::NewickBroker::at
NewickBrokerElement & at(std::size_t index)
Provides index based array access to the nodes, doing a boundary check first.
Definition: broker.cpp:197
genesis::tree::NewickBroker::inner_count
size_t inner_count() const
Definition: broker.cpp:136
genesis::tree::NewickBroker::pop_top
void pop_top()
Definition: broker.cpp:71
genesis::tree::NewickBroker::const_iterator
std::deque< NewickBrokerElement >::const_iterator const_iterator
Definition: broker.hpp:115
genesis::tree::NewickBroker::NewickBroker
NewickBroker()=default
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::NewickBroker
Stores a Newick tree in an intermediate format that can be further processed into a Tree.
Definition: broker.hpp:106
genesis::tree::NewickBroker::operator=
NewickBroker & operator=(NewickBroker const &)=default
genesis::tree::NewickBroker::assign_ranks
void assign_ranks() const
Iterate over the tree and assign ranks (= number of immediate children) to all nodes.
Definition: broker.cpp:85
genesis::tree::NewickBroker::empty
bool empty() const
Returns whether the stack is empty.
Definition: broker.cpp:173
genesis::tree::NewickBroker::crend
const_reverse_iterator crend()
Const version of rend().
Definition: broker.cpp:292
genesis::tree::NewickBroker::rbegin
reverse_iterator rbegin()
Returns a reverse iterator to the nodes on the stack.
Definition: broker.cpp:267
genesis::tree::NewickBroker::bottom
NewickBrokerElement & bottom()
Returns a reference to the bottom node of the tree stack.
Definition: broker.cpp:223
genesis::tree::NewickBroker::push_bottom
void push_bottom(NewickBrokerElement const &node)
Definition: broker.cpp:61
genesis::tree::NewickBrokerElement
Store the information for one element of a Newick tree.
Definition: element.hpp:60
genesis::tree::NewickBroker::is_bifurcating
bool is_bifurcating() const
Definition: broker.cpp:164
genesis::tree::NewickBroker::leaf_count
size_t leaf_count() const
Returns the number of leaf nodes in the tree. assign_ranks() has to be called first.
Definition: broker.cpp:122
genesis::tree::NewickBroker::size
size_t size() const
Returns the size of the stack, i.e. the number of nodes stored in the broker.
Definition: broker.cpp:178
genesis::tree::NewickBroker::cend
const_iterator cend() const
Const version of end().
Definition: broker.cpp:262
genesis::tree::NewickBroker::node_count
size_t node_count() const
Alias for size().
Definition: broker.cpp:144