Quetzal-CoaTL
The Coalescence Template Library
Loading...
Searching...
No Matches
Forest.hpp
1// Copyright 2021 Arnaud Becheler <abechele@umich.edu>
2
10
11#ifndef __FOREST_H_INCLUDED__
12#define __FOREST_H_INCLUDED__
13
14#include "assert.h"
15#include <set>
16#include <unordered_map>
17#include <vector>
18
19namespace quetzal
20{
21namespace coalescence
22{
23namespace container
24{
31template <typename Position, typename Tree> class Forest
32{
33 private:
37 using data_type = std::unordered_multimap<Position, Tree>;
39 using size_type = typename data_type::size_type;
40 data_type m_data;
41
42 public:
44 using position_type = Position;
46 using iterator = typename data_type::iterator;
48 using const_iterator = typename data_type::const_iterator;
54 Forest() : m_data()
55 {
56 }
63 Forest(const Forest<Position, Tree> &other) : m_data(other.m_data)
64 {
65 }
71 Forest(Forest<Position, Tree> &&other) noexcept : m_data(std::move(other.m_data))
72 {
73 }
79 {
80 Forest<Position, Tree> otherCopy(other);
81 *this = std::move(otherCopy);
82 return *this;
83 }
89 {
90 m_data = std::move(other.m_data);
91 return *this;
92 }
96 iterator begin()
97 {
98 return m_data.begin();
99 }
103 iterator end()
104 {
105 return m_data.end();
106 }
110 const_iterator begin() const
111 {
112 return m_data.begin();
113 }
117 const_iterator end() const
118 {
119 return m_data.end();
120 }
124 const_iterator cbegin() const
125 {
126 return m_data.cbegin();
127 }
131 const_iterator cend() const
132 {
133 return m_data.cend();
134 }
138 unsigned int nb_trees() const
139 {
140 return m_data.size();
141 }
145 unsigned int nb_trees(Position const &position) const
146 {
147 return m_data.count(position);
148 }
155 std::pair<const_iterator, const_iterator> trees_at_same_position(const Position &position) const
156 {
157 assert(m_data.count(position) > 0 && "no trees at position");
158 return m_data.equal_range(position);
159 }
166 std::pair<iterator, iterator> trees_at_same_position(const Position &position)
167 {
168 assert(m_data.count(position) > 0 && "no trees at position");
169 return m_data.equal_range(position);
170 }
171
177 iterator insert(Position const &position, Tree const &tree)
178 {
179 return m_data.insert(std::pair<Position, Tree>(position, tree));
180 }
186 iterator insert(Position const &position, Tree &&tree) noexcept
187 {
188 return m_data.insert(std::pair<Position, Tree>(position, std::move(tree)));
189 }
195 Forest<Position, Tree> &insert(Position const &position, std::vector<Tree> const &trees)
196 {
197 for (auto const &it : trees)
198 {
199 insert(position, it);
200 }
201 return *this;
202 }
210 Forest<Position, Tree> &insert(Position const &position, std::vector<Tree> &&trees) noexcept
211 {
212 for (auto &&it : trees)
213 {
214 insert(position, std::move(it));
215 }
216 return *this;
217 }
223 size_type erase(Position const &x)
224 {
225 return m_data.erase(x);
226 }
232 void erase(iterator first, iterator last)
233 {
234 m_data.erase(first, last);
235 }
241 std::set<Position> positions() const
242 {
243 std::set<Position> s;
244 for (auto const &it : m_data)
245 {
246 s.insert(it.first);
247 }
248 return s;
249 }
255 std::vector<Tree> get_all_trees() const
256 {
257 std::vector<Tree> trees;
258 trees.reserve(this->nb_trees());
259 for (auto &it : *this)
260 {
261 trees.push_back(it.second);
262 }
263 return trees;
264 }
265}; // end class Forest
266} // namespace container
267} // end namespace coalescence
268} // end namespace quetzal
269#endif
Collection of geo-localized coalescing trees.
Definition Forest.hpp:32
const_iterator end() const
returns iterator to the begining
Definition Forest.hpp:117
Forest< Position, Tree > & insert(Position const &position, std::vector< Tree > &&trees) noexcept
insert a vector of trees at a given position
Definition Forest.hpp:210
const_iterator begin() const
returns iterator to the begining
Definition Forest.hpp:110
Forest(const Forest< Position, Tree > &other)
Copy constructor.
Definition Forest.hpp:63
Forest< Position, Tree > & operator=(Forest< Position, Tree > &&other) noexcept
Move assignment operator.
Definition Forest.hpp:88
Forest(Forest< Position, Tree > &&other) noexcept
Move constructor.
Definition Forest.hpp:71
std::vector< Tree > get_all_trees() const
copies all trees in the forest
Definition Forest.hpp:255
unsigned int nb_trees(Position const &position) const
number of trees in the forest at a given position
Definition Forest.hpp:145
Forest< Position, Tree > & insert(Position const &position, std::vector< Tree > const &trees)
insert a vector of trees at a given position
Definition Forest.hpp:195
const_iterator cend() const
returns iterator to the begin
Definition Forest.hpp:131
iterator insert(Position const &position, Tree const &tree)
insert a new tree at a given position
Definition Forest.hpp:177
void erase(iterator first, iterator last)
remove elements from the Forest.
Definition Forest.hpp:232
std::set< Position > positions() const
positions in the forest
Definition Forest.hpp:241
unsigned int nb_trees() const
number of trees in the forest
Definition Forest.hpp:138
iterator begin()
returns iterator to the begin
Definition Forest.hpp:96
size_type erase(Position const &x)
erase a position from the Forest.
Definition Forest.hpp:223
Forest< Position, Tree > & operator=(const Forest< Position, Tree > &other)
Copy assignment operator.
Definition Forest.hpp:78
Forest()
Default constructor.
Definition Forest.hpp:54
iterator insert(Position const &position, Tree &&tree) noexcept
insert a new tree at a given position
Definition Forest.hpp:186
const_iterator cbegin() const
returns iterator to the begin
Definition Forest.hpp:124
std::pair< iterator, iterator > trees_at_same_position(const Position &position)
access to trees in the forest at a given position
Definition Forest.hpp:166
std::pair< const_iterator, const_iterator > trees_at_same_position(const Position &position) const
non-modifying access to trees in the forest at a given position
Definition Forest.hpp:155
iterator end()
returns iterator to the end
Definition Forest.hpp:103
Data structure for representing tree topologies where each node contains a data field.
Definition Tree.hpp:29
Simulation of coalescence-based models of molecular evolution.
Definition coalescence.hpp:21