Quetzal-CoaTL
The Coalescence Template Library
Loading...
Searching...
No Matches
OccupancySpectrum.hpp
1// Copyright 2021 Arnaud Becheler <abechele@umich.edu>
2
3/*********************************************************************** * This program is free software; you can
4 *redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free
5 *Software Foundation; either version 2 of the License, or * (at your option) any later version. *
6 * *
7 ***************************************************************************/
8
9#ifndef __OCCUPANCY_SPECTRUM_CLASS_H_INCLUDED__
10#define __OCCUPANCY_SPECTRUM_CLASS_H_INCLUDED__
11
12#include <random>
13#include <utility> // std::forward, std::move
14#include <vector>
15
16namespace quetzal
17{
18namespace coalescence
19{
20namespace occupancy_spectrum
21{
42{
43 private:
45 using occupancy_spectrum_type = std::vector<int>;
47 occupancy_spectrum_type M_j;
49 int k;
51 int N;
55 bool test_number_of_balls_conservation()
56 {
57 int sum = 0;
58 for (int i = 0; i < this->M_j.size(); ++i)
59 {
60 assert(this->M_j.at(i) >= 0);
61 sum += i * (this->M_j.at(i));
62 }
63 return (sum == this->k);
64 }
68 bool test_number_of_urns_conservation()
69 {
70 int sum = 0;
71 for (int i = 0; i < this->M_j.size(); ++i)
72 {
73 assert(this->M_j.at(i) >= 0);
74 sum += this->M_j.at(i);
75 }
76 return (sum == this->N);
77 }
78
79 public:
81 using value_type = typename occupancy_spectrum_type::value_type;
83 using iterator = typename occupancy_spectrum_type::iterator;
85 using const_iterator = typename occupancy_spectrum_type::const_iterator;
92 OccupancySpectrum(std::vector<int> const &v, int k, int N) : M_j(v), k(k), N(N)
93 {
94 assert(this->N >= 0);
95 assert(this->k >= 0);
96 assert(test_number_of_balls_conservation());
97 assert(test_number_of_urns_conservation());
98 }
105 OccupancySpectrum(std::vector<int> &&v, int k, int N) : M_j(std::move(v)), k(k), N(N)
106 {
107 assert(this->N >= 0);
108 assert(this->k >= 0);
109 assert(test_number_of_balls_conservation());
110 assert(test_number_of_urns_conservation());
111 }
121 OccupancySpectrum(const OccupancySpectrum &other) = default;
127 OccupancySpectrum(OccupancySpectrum &&other) noexcept : M_j(std::move(other.M_j)), N(other.N), k(other.k)
128 {
129 assert(this->N >= 0);
130 assert(this->k >= 0);
131 assert(test_number_of_balls_conservation());
132 assert(test_number_of_urns_conservation());
133 }
138 {
139 OccupancySpectrum otherCopy(other);
140 this->M_j = std::move(otherCopy.M_j);
141 this->N = otherCopy.N;
142 this->k = otherCopy.k;
143 return *this;
144 }
153 {
154 this->M_j = std::move(other.M_j);
155 this->N = other.N;
156 this->k = other.k;
157 return *this;
158 }
162 iterator begin()
163 {
164 return M_j.begin();
165 }
169 iterator end()
170 {
171 return M_j.end();
172 }
176 const_iterator begin() const
177 {
178 return M_j.begin();
179 }
183 const_iterator end() const
184 {
185 return M_j.end();
186 }
190 const_iterator cbegin() const
191 {
192 return M_j.cbegin();
193 }
197 const_iterator cend() const
198 {
199 return M_j.cend();
200 }
204 void erase(iterator first, iterator last)
205 {
206 M_j.erase(first, last);
207 }
211 bool empty() const
212 {
213 return M_j.empty();
214 }
215}; // end class OccupancySpectrum
216} // end namespace occupancy_spectrum
217} // end namespace coalescence
218} // end namespace quetzal
219
220#endif
An occupancy spectrum as defined in Becheler & Knowles, 2020.
Definition OccupancySpectrum.hpp:42
const_iterator begin() const
returns iterator to the beginning of the spectrum
Definition OccupancySpectrum.hpp:176
OccupancySpectrum & operator=(OccupancySpectrum &&other)
Move assignment operator.
Definition OccupancySpectrum.hpp:152
iterator end()
returns iterator to the end of the spectrum
Definition OccupancySpectrum.hpp:169
const_iterator cend() const
returns iterator to the end of the spectrum
Definition OccupancySpectrum.hpp:197
bool empty() const
Returns whether the spectrum is empty (i.e. whether its size is 0).
Definition OccupancySpectrum.hpp:211
OccupancySpectrum(std::vector< int > const &v, int k, int N)
Constructor by copy.
Definition OccupancySpectrum.hpp:92
OccupancySpectrum(OccupancySpectrum &&other) noexcept
Move constructor.
Definition OccupancySpectrum.hpp:127
iterator begin()
returns iterator to the beginning of the spectrum
Definition OccupancySpectrum.hpp:162
OccupancySpectrum(std::vector< int > &&v, int k, int N)
Move constructor.
Definition OccupancySpectrum.hpp:105
void erase(iterator first, iterator last)
erase a range of elements of the spectrum.
Definition OccupancySpectrum.hpp:204
const_iterator end() const
returns iterator to the end of the spectrum
Definition OccupancySpectrum.hpp:183
OccupancySpectrum & operator=(const OccupancySpectrum &other)
Copy assignment operator.
Definition OccupancySpectrum.hpp:137
OccupancySpectrum()=delete
Deleted default constructor.
const_iterator cbegin() const
returns iterator to the beginning of the spectrum
Definition OccupancySpectrum.hpp:190
OccupancySpectrum(const OccupancySpectrum &other)=default
Default copy constructor.
Simulation of coalescence-based models of molecular evolution.
Definition coalescence.hpp:21