Quetzal-CoaTL
The Coalescence Template Library
Loading...
Searching...
No Matches
Support.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_SUPPORT_H_INCLUDED__
10#define __OCCUPANCY_SPECTRUM_SUPPORT_H_INCLUDED__
11
12#include "OccupancySpectrum.hpp"
13
14#include <utility>
15#include <vector>
16
17namespace quetzal
18{
19namespace coalescence
20{
21namespace occupancy_spectrum
22{
28{
29 public:
36 Support(int n, int m) : initial_n(n), initial_m(m)
37 {
38 std::cout << n << " " << m << std::endl;
39 }
41 using occupancy_spectrum_type = OccupancySpectrum;
52 template <class UnaryOperation> void generate(UnaryOperation op) const
53 {
54 std::vector<int> M_j(this->initial_n + 1);
55 f(this->initial_n, this->initial_m, this->initial_n, M_j, op);
56 }
57
58 private:
60 int initial_n;
62 int initial_m;
69 template <class Callback> void f(int n, int m, int j_max, std::vector<int> &v, Callback callback) const
70 {
71 if (m == 0 && n == 0)
72 {
73 // problem solved
74 callback(OccupancySpectrum(std::move(v), this->initial_n, this->initial_m));
75 return;
76 }
77 if (m != 0)
78 {
79 if (n == 0)
80 {
81 auto copy = v;
82 copy[0] = m; // here n = 0 et m = 0, so problem is solved
83 callback(OccupancySpectrum(std::move(copy), this->initial_n, this->initial_m));
84 return;
85 }
86 else
87 {
88 if (j_max > 0)
89 {
90 // vertical descent
91 for (int i = n / j_max; i >= 1; --i)
92 {
93 auto copy = v;
94 copy[j_max] = i;
95 if (m >= i)
96 {
97 int balls_left = n - i * j_max;
98 if (balls_left < j_max)
99 {
100 f(balls_left, m - i, balls_left, copy, callback);
101 }
102 else
103 {
104 f(balls_left, m - i, j_max - 1, copy, callback);
105 }
106 }
107 }
108 }
109 }
110 }
111 // horizontal descent
112 if (j_max == 0 && n > 0)
113 {
114 // not solvable
115 return;
116 }
117 auto copy = v;
118 copy[j_max] = 0;
119 f(n, m, j_max - 1, copy, callback);
120 }
121}; // end class Support
122} // namespace occupancy_spectrum
123} // namespace coalescence
124} // namespace quetzal
125
126#endif
An occupancy spectrum as defined in Becheler & Knowles, 2020.
Definition OccupancySpectrum.hpp:42
Set of occupancy spectrums resulting from throwing balls (the number of coalescing gene copies) into...
Definition Support.hpp:28
Support(int n, int m)
Constructor.
Definition Support.hpp:36
void generate(UnaryOperation op) const
Generate all possible configurations.
Definition Support.hpp:52
Simulation of coalescence-based models of molecular evolution.
Definition coalescence.hpp:21