17 #include "fixedEnvelopes.h"
22 template<
bool tgetlProbs,
bool tgetMasses,
bool tgetProbs,
bool tgetConfs>
void FixedEnvelope::reallocate_memory(
size_t new_size)
25 constexpr_if(tgetlProbs) { _lprobs = (
double*) realloc(_lprobs, new_size *
sizeof(
double)); tlprobs = _lprobs + _confs_no; }
26 constexpr_if(tgetMasses) { _masses = (
double*) realloc(_masses, new_size *
sizeof(
double)); tmasses = _masses + _confs_no; }
27 constexpr_if(tgetProbs) { _probs = (
double*) realloc(_probs, new_size *
sizeof(
double)); tprobs = _probs + _confs_no; }
28 constexpr_if(tgetConfs) { _confs = (
int*) realloc(_confs, new_size * allDimSizeofInt); tconfs = _confs + (allDim * _confs_no); }
32 template<
bool tgetlProbs,
bool tgetMasses,
bool tgetProbs,
bool tgetConfs>
void ThresholdFixedEnvelope::init(Iso&& iso)
34 IsoThresholdGenerator generator(std::move(iso), threshold, absolute);
36 size_t tab_size = generator.count_confs();
37 this->allDim = generator.getAllDim();
38 this->allDimSizeofInt = this->allDim *
sizeof(int);
40 this->reallocate_memory<tgetlProbs, tgetMasses, tgetProbs, tgetConfs>(tab_size);
42 while(generator.advanceToNextConfiguration())
43 store_conf<IsoThresholdGenerator, tgetlProbs, tgetMasses, tgetProbs, tgetConfs>(generator);
45 this->_confs_no = tab_size;
49 template<
bool tgetlProbs,
bool tgetMasses,
bool tgetProbs,
bool tgetConfs>
void TotalProbFixedEnvelope::init(Iso&& iso)
51 if(optimize && !tgetProbs)
53 throw std::logic_error(
"Cannot perform quicktrim if we're not computing probabilities");
55 IsoLayeredGenerator generator(std::move(iso), 1000, 1000,
true, std::min<double>(target_total_prob, 0.9999));
57 this->allDim = generator.getAllDim();
58 this->allDimSizeofInt = this->allDim*
sizeof(int);
61 this->reallocate_memory<tgetlProbs, tgetMasses, tgetProbs, tgetConfs>(ISOSPEC_INIT_TABLE_SIZE);
63 size_t last_switch = 0;
64 double prob_at_last_switch = 0.0;
65 double prob_so_far = 0.0;
69 while(generator.advanceToNextConfigurationWithinLayer())
71 this->
template addConf<tgetlProbs, tgetMasses, tgetProbs, tgetConfs>(generator);
72 prob_so_far += generator.prob();
73 if(prob_so_far >= target_total_prob)
77 while(generator.advanceToNextConfigurationWithinLayer())
78 this->
template addConf<tgetlProbs, tgetMasses, tgetProbs, tgetConfs>(generator);
85 if(prob_so_far >= target_total_prob)
88 last_switch = this->_confs_no;
89 prob_at_last_switch = prob_so_far;
90 }
while(generator.nextLayer(-3.0));
92 if(!optimize || prob_so_far <= target_total_prob)
101 int* conf_swapspace =
nullptr;
102 constexpr_if(tgetConfs)
103 conf_swapspace = (
int*) malloc(this->allDimSizeofInt);
105 size_t start = last_switch;
106 size_t end = this->_confs_no;
107 double sum_to_start = prob_at_last_switch;
112 size_t len = end - start;
113 #if ISOSPEC_BUILDING_R
114 size_t pivot = len/2 + start;
116 size_t pivot = rand() % len + start;
118 double pprob = this->_probs[pivot];
119 swap<tgetlProbs, tgetMasses, tgetProbs, tgetConfs>(pivot, end-1, conf_swapspace);
121 double new_csum = sum_to_start;
123 size_t loweridx = start;
124 for(
size_t ii=start; ii<end-1; ii++)
125 if(this->_probs[ii] > pprob)
127 swap<tgetlProbs, tgetMasses, tgetProbs, tgetConfs>(ii, loweridx, conf_swapspace);
128 new_csum += this->_probs[loweridx];
132 swap<tgetlProbs, tgetMasses, tgetProbs, tgetConfs>(end-1, loweridx, conf_swapspace);
135 if(new_csum < target_total_prob)
137 start = loweridx + 1;
138 sum_to_start = new_csum + this->_probs[loweridx];
144 constexpr_if(tgetConfs)
145 free(conf_swapspace);
147 if(end <= current_size/2)
149 this->
template reallocate_memory<tgetlProbs, tgetMasses, tgetProbs, tgetConfs>(end);
151 this->_confs_no = end;
154 template<
typename T>
void call_init(T* tabulator, Iso&& iso,
bool tgetlProbs,
bool tgetMasses,
bool tgetProbs,
bool tgetConfs)
163 tabulator->template init<true, true, true, true>(std::move(iso));
165 tabulator->template init<true, true, true, false>(std::move(iso));
170 tabulator->template init<true, true, false, true>(std::move(iso));
172 tabulator->template init<true, true, false, false>(std::move(iso));
180 tabulator->template init<true, false, true, true>(std::move(iso));
182 tabulator->template init<true, false, true, false>(std::move(iso));
187 tabulator->template init<true, false, false, true>(std::move(iso));
189 tabulator->template init<true, false, false, false>(std::move(iso));
200 tabulator->template init<false, true, true, true>(std::move(iso));
202 tabulator->template init<false, true, true, false>(std::move(iso));
207 tabulator->template init<false, true, false, true>(std::move(iso));
209 tabulator->template init<false, true, false, false>(std::move(iso));
217 tabulator->template init<false, false, true, true>(std::move(iso));
219 tabulator->template init<false, false, true, false>(std::move(iso));
224 tabulator->template init<false, false, false, true>(std::move(iso));
226 tabulator->template init<false, false, false, false>(std::move(iso));
232 template void call_init<TotalProbFixedEnvelope>(TotalProbFixedEnvelope* tabulator, Iso&& iso,
bool tgetlProbs,
bool tgetMasses,
bool tgetProbs,
bool tgetConfs);
233 template void call_init<ThresholdFixedEnvelope>(ThresholdFixedEnvelope* tabulator, Iso&& iso,
bool tgetlProbs,
bool tgetMasses,
bool tgetProbs,
bool tgetConfs);