Quetzal-CoaTL
The Coalescence Template Library
Loading...
Searching...
No Matches
Network.hpp
1// Copyright 2021 Arnaud Becheler <abechele@umich.edu>
2
10
11#ifndef NETWORK_H_INCLUDED
12#define NETWORK_H_INCLUDED
13
14#include <vector>
15
16namespace quetzal
17{
18namespace coalescence
19{
20namespace container
21{
34template <class CellT> class Network
35{
36 private:
37 CellT m_cell;
38 std::vector<Network<CellT>> m_parents;
39 std::vector<Network<CellT>> m_children;
40
41 public:
47 Network() : m_parents(), m_cell(), m_children()
48 {
49 }
57 Network(const CellT &cell) : m_parents(), m_cell(cell), m_children()
58 {
59 }
67 Network(CellT &&cell) noexcept : m_parents(), m_cell(std::move(cell)), m_children()
68 {
69 }
73 unsigned int nb_children() const
74 {
75 return m_children.size();
76 }
80 unsigned int nb_parents() const
81 {
82 return m_parents.size();
83 }
91 const CellT &cell() const
92 {
93 return m_cell;
94 }
102 CellT &cell()
103 {
104 return m_cell;
105 }
106
114 Network<CellT> &add_child(CellT &&cell) noexcept
115 {
116 Network<CellT> subnetwork(std::move(cell));
117 subnetwork.add_parent(*this);
118 m_children.push_back(std::move(subnetwork));
119 return m_children.back();
120 }
129 {
130 subnetwork.add_parent(*this);
131 m_children.push_back(subnetwork);
132 return m_children.back();
133 }
142 {
143 m_parents.push_back(parent);
144 return m_parents.back();
145 }
151 bool has_parent() const
152 {
153 return !m_parents.empty();
154 }
160 bool has_children() const
161 {
162 return !m_children.empty();
163 }
173 template <typename UnaryOperation> void visit_cell_by_pre_order_DFS(UnaryOperation op) const
174 {
175 op(this->cell());
176 for (auto const &child : this->m_children)
177 {
178 child.visit_cells_by_pre_order_DFS(op);
179 }
180 }
189 template <class UnaryOperation> void visit_by_pre_order_DFS(UnaryOperation op)
190 {
191 op(*this);
192 for (auto &child : this->m_children)
193 {
194 child.visit_by_pre_order_DFS(op);
195 }
196 }
215 template <typename Op1, typename Op2, typename Op3>
216 void visit_by_generic_DFS(Op1 preorder, Op2 inorder, Op3 postorder)
217 {
218 preorder(*this);
219 for (auto &child : this->m_children)
220 {
221 child.visit_by_generic_DFS(preorder, inorder, postorder);
222 inorder(*this);
223 }
224 postorder(*this);
225 }
235 template <typename UnaryOperation> void visit_leaves_cells_by_DFS(UnaryOperation op) const
236 {
237 if (has_children())
238 {
239 for (auto const &child : this->m_children)
240 {
241 child.visit_leaves_cells_by_DFS(op);
242 }
243 }
244 else
245 {
246 op(this->cell());
247 }
248 }
249}; // end class Network
250} // end namespace container
251} // end namespace coalescence
252} // end namespace quetzal
253
254#endif
Data structure for representing phylogenetic network topologies where each node contains a data field...
Definition Network.hpp:35
Network< CellT > & add_child(CellT &&cell) noexcept
Add a subnetwork to the children list of the network, initialize the network.
Definition Network.hpp:114
const CellT & cell() const
Cell accessor.
Definition Network.hpp:91
unsigned int nb_parents() const
Returns the number of parents.
Definition Network.hpp:80
bool has_children() const
Checks whether the network has one or more child.
Definition Network.hpp:160
CellT & cell()
Cell accessor.
Definition Network.hpp:102
Network< CellT > & add_child(Network< CellT > &subnetwork)
Add a sub-network to the children list of the network.
Definition Network.hpp:128
Network(const CellT &cell)
Constructor with data copy.
Definition Network.hpp:57
unsigned int nb_children() const
Returns the number of children.
Definition Network.hpp:73
void visit_leaves_cells_by_DFS(UnaryOperation op) const
Applies a function object to the data member of each leave encountered along a depth first search alg...
Definition Network.hpp:235
Network()
Default constructor.
Definition Network.hpp:47
Network(CellT &&cell) noexcept
Constructor with data move.
Definition Network.hpp:67
void visit_cell_by_pre_order_DFS(UnaryOperation op) const
Applies a function object to the data member of each node encountered in a preorder depth first searc...
Definition Network.hpp:173
bool has_parent() const
Checks whether the network has a parent.
Definition Network.hpp:151
void visit_by_pre_order_DFS(UnaryOperation op)
Applies a function object to each node encountered in a depth first search algorithm.
Definition Network.hpp:189
void visit_by_generic_DFS(Op1 preorder, Op2 inorder, Op3 postorder)
Applies a function object to each node encountered in a depth first search algorithm.
Definition Network.hpp:216
Network< CellT > & add_parent(Network< CellT > &parent)
Add a itself as a sub-network in the list of the parent children.
Definition Network.hpp:141
Simulation of coalescence-based models of molecular evolution.
Definition coalescence.hpp:21