IsoSpec  1.95
fixedEnvelopes.h
1 /*
2  * Copyright (C) 2015-2019 Mateusz Łącki and Michał Startek.
3  *
4  * This file is part of IsoSpec.
5  *
6  * IsoSpec is free software: you can redistribute it and/or modify
7  * it under the terms of the Simplified ("2-clause") BSD licence.
8  *
9  * IsoSpec is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12  *
13  * You should have received a copy of the Simplified BSD Licence
14  * along with IsoSpec. If not, see <https://opensource.org/licenses/BSD-2-Clause>.
15  */
16 
17 #pragma once
18 
19 #include <stdlib.h>
20 
21 #include "isoSpec++.h"
22 
23 #define ISOSPEC_INIT_TABLE_SIZE 1024
24 
25 namespace IsoSpec
26 {
27 
28 class ISOSPEC_EXPORT_SYMBOL FixedEnvelope {
29 protected:
30  double* _masses;
31  double* _lprobs;
32  double* _probs;
33  int* _confs;
34  size_t _confs_no;
35  int allDim;
36 
37 public:
38  FixedEnvelope() : _masses(nullptr),
39  _lprobs(nullptr),
40  _probs(nullptr),
41  _confs(nullptr),
42  _confs_no(0)
43  {};
44 
45  virtual ~FixedEnvelope()
46  {
47  if( _masses != nullptr ) free(_masses);
48  if( _lprobs != nullptr ) free(_lprobs);
49  if( _probs != nullptr ) free(_probs);
50  if( _confs != nullptr ) free(_confs);
51  };
52 
53  inline size_t confs_no() const { return _confs_no; };
54  inline int getAllDim() const { return allDim; };
55 
56  inline const double* lprobs() { return _lprobs; };
57  inline const double* masses() { return _masses; };
58  inline const double* probs() { return _probs; };
59  inline const int* confs() { return _confs; };
60 
61  inline double* release_lprobs() { double* ret = _lprobs; _lprobs = nullptr; return ret; };
62  inline double* release_masses() { double* ret = _masses; _masses = nullptr; return ret; };
63  inline double* release_probs() { double* ret = _probs; _probs = nullptr; return ret; };
64  inline int* release_confs() { int* ret = _confs; _confs = nullptr; return ret; };
65 
66 
67  inline double mass(size_t i) { return _masses[i]; };
68  inline double lprob(size_t i) { return _lprobs[i]; };
69  inline double prob(size_t i) { return _probs[i]; };
70  inline const int* conf(size_t i) { return _confs + i*allDim; };
71 
72 protected:
73  double* tmasses;
74  double* tlprobs;
75  double* tprobs;
76  int* tconfs;
77 
78  int allDimSizeofInt;
79 
80  template<typename T, bool tgetlProbs, bool tgetMasses, bool tgetProbs, bool tgetConfs> ISOSPEC_FORCE_INLINE void store_conf(T& generator)
81  {
82  constexpr_if(tgetlProbs) { *tlprobs = generator.lprob(); tlprobs++; };
83  constexpr_if(tgetMasses) { *tmasses = generator.mass(); tmasses++; };
84  constexpr_if(tgetProbs) { *tprobs = generator.prob(); tprobs++; };
85  constexpr_if(tgetConfs) { generator.get_conf_signature(tconfs); tconfs += allDim; };
86  }
87 
88  template<bool tgetlProbs, bool tgetMasses, bool tgetProbs, bool tgetConfs> void reallocate_memory(size_t new_size);
89 };
90 
91 template<typename T> void call_init(T* tabulator, Iso&& iso, bool tgetlProbs, bool tgetMasses, bool tgetProbs, bool tgetConfs);
92 
93 class ISOSPEC_EXPORT_SYMBOL ThresholdFixedEnvelope : public FixedEnvelope
94 {
95  const double threshold;
96  const bool absolute;
97 public:
98  ThresholdFixedEnvelope(Iso&& iso, double _threshold, bool _absolute, bool tgetConfs = false, bool tgetlProbs = false, bool tgetMasses = true, bool tgetProbs = true) :
99  FixedEnvelope(),
100  threshold(_threshold),
101  absolute(_absolute)
102  {
103  call_init<ThresholdFixedEnvelope>(this, std::move(iso), tgetlProbs, tgetMasses, tgetProbs, tgetConfs);
104  }
105 
106  inline ThresholdFixedEnvelope(const Iso& iso, double _threshold, bool _absolute, bool tgetConfs = false, bool tgetlProbs = false, bool tgetMasses = true, bool tgetProbs = true) :
107  ThresholdFixedEnvelope(Iso(iso, false), _threshold, _absolute, tgetConfs, tgetlProbs, tgetMasses, tgetProbs) {};
108 
109  virtual ~ThresholdFixedEnvelope() {};
110 
111 private:
112  template<bool tgetlProbs, bool tgetMasses, bool tgetProbs, bool tgetConfs> void init(Iso&& iso);
113 
114  template<typename T> friend void call_init(T* tabulator, Iso&& iso, bool tgetlProbs, bool tgetMasses, bool tgetProbs, bool tgetConfs);
115 };
116 
117 
118 class ISOSPEC_EXPORT_SYMBOL TotalProbFixedEnvelope : public FixedEnvelope
119 {
120  const bool optimize;
121  double target_total_prob;
122  size_t current_size;
123 
124 public:
125  TotalProbFixedEnvelope(Iso&& iso, double _target_total_prob, bool _optimize, bool tgetConfs = false, bool tgetlProbs = false, bool tgetMasses = true, bool tgetProbs = true) :
126  FixedEnvelope(),
127  optimize(_optimize),
128  target_total_prob(_target_total_prob >= 1.0 ? std::numeric_limits<double>::infinity() : _target_total_prob),
129  current_size(ISOSPEC_INIT_TABLE_SIZE)
130  {
131  if(_target_total_prob <= 0.0)
132  return;
133 
134  call_init(this, std::move(iso), tgetlProbs, tgetMasses, tgetProbs || _optimize, tgetConfs);
135 
136  if(!tgetProbs && optimize)
137  {
138  free(_probs);
139  _probs = nullptr;
140  }
141  }
142 
143  inline TotalProbFixedEnvelope(const Iso& iso, double _target_total_prob, bool _optimize, bool tgetConfs = false, bool tgetlProbs = false, bool tgetMasses = true, bool tgetProbs = true) :
144  TotalProbFixedEnvelope(Iso(iso, false), _target_total_prob, _optimize, tgetConfs, tgetlProbs, tgetMasses, tgetProbs) {};
145 
146  virtual ~TotalProbFixedEnvelope() {};
147 
148 private:
149 
150  template<bool tgetlProbs, bool tgetMasses, bool tgetProbs, bool tgetConfs> void init(Iso&& iso);
151 
152  template<bool tgetlProbs, bool tgetMasses, bool tgetProbs, bool tgetConfs> void swap([[maybe_unused]] size_t idx1, [[maybe_unused]] size_t idx2, [[maybe_unused]] int* conf_swapspace)
153  {
154  constexpr_if(tgetlProbs) std::swap<double>(this->_lprobs[idx1], this->_lprobs[idx2]);
155  constexpr_if(tgetProbs) std::swap<double>(this->_probs[idx1], this->_probs[idx2]);
156  constexpr_if(tgetMasses) std::swap<double>(this->_masses[idx1], this->_masses[idx2]);
157  constexpr_if(tgetConfs)
158  {
159  int* c1 = this->_confs + (idx1*this->allDim);
160  int* c2 = this->_confs + (idx2*this->allDim);
161  memcpy(conf_swapspace, c1, this->allDimSizeofInt);
162  memcpy(c1, c2, this->allDimSizeofInt);
163  memcpy(c2, conf_swapspace, this->allDimSizeofInt);
164  }
165  }
166 
167 
168  template<bool tgetlProbs, bool tgetMasses, bool tgetProbs, bool tgetConfs> void addConf(IsoLayeredGenerator& generator)
169  {
170  if(this->_confs_no == this->current_size)
171  {
172  this->current_size *= 2;
173  this->template reallocate_memory<tgetlProbs, tgetMasses, tgetProbs, tgetConfs>(this->current_size);
174  }
175 
176  this->template store_conf<IsoLayeredGenerator, tgetlProbs, tgetMasses, tgetProbs, tgetConfs>(generator);
177  this->_confs_no++;
178  }
179 
180  template<typename T> friend void call_init(T* tabulator, Iso&& iso, bool tgetlProbs, bool tgetMasses, bool tgetProbs, bool tgetConfs);
181 
182 };
183 
184 
185 } // namespace IsoSpec
186 
IsoSpec::ThresholdFixedEnvelope
Definition: fixedEnvelopes.h:93
IsoSpec::TotalProbFixedEnvelope
Definition: fixedEnvelopes.h:118
IsoSpec
Definition: allocator.cpp:21
IsoSpec::Iso
The Iso class for the calculation of the isotopic distribution.
Definition: isoSpec++.h:53
IsoSpec::FixedEnvelope
Definition: fixedEnvelopes.h:28
IsoSpec::IsoLayeredGenerator
Definition: isoSpec++.h:393