IsoSpec  1.95
misc.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 <iostream>
20 #include <tuple>
21 #include <vector>
22 #include <fenv.h>
23 #include <string.h>
24 #include "isoMath.h"
25 
26 namespace IsoSpec
27 {
28 
29 inline double combinedSum(
30  const int* conf, const std::vector<double>** valuesContainer, int dimNumber
31 ){
32  double res = 0.0;
33  for(int i=0; i<dimNumber;i++)
34  res += (*(valuesContainer[i]))[conf[i]];
35  return res;
36 }
37 
38 inline int* getConf(void* conf)
39 {
40  return reinterpret_cast<int*>(
41  reinterpret_cast<char*>(conf) + sizeof(double)
42  );
43 }
44 
45 inline double getLProb(void* conf)
46 {
47  double ret = *reinterpret_cast<double*>(conf);
48  return ret;
49 }
50 
51 
52 inline double unnormalized_logProb(const int* conf, const double* logProbs, int dim)
53 {
54  double res = 0.0;
55 
56  int curr_method = fegetround();
57 
58  fesetround(FE_TOWARDZERO);
59 
60  for(int i=0; i < dim; i++)
61  res += minuslogFactorial(conf[i]);
62 
63  fesetround(FE_UPWARD);
64 
65  for(int i=0; i < dim; i++)
66  res += conf[i] * logProbs[i];
67 
68  fesetround(curr_method);
69 
70  return res;
71 }
72 
73 inline double mass(const int* conf, const double* masses, int dim)
74 {
75  double res = 0.0;
76 
77  for(int i=0; i < dim; i++)
78  {
79  res += conf[i] * masses[i];
80  }
81 
82  return res;
83 }
84 
85 
86 inline bool tupleCmp(
87  std::tuple<double,double,int*> t1,
88  std::tuple<double,double,int*> t2
89 ){
90  return std::get<1>(t1) > std::get<1>(t2);
91 }
92 
93 template<typename T> void printArray(const T* array, int size, const char* prefix = "")
94 {
95  if (strlen(prefix) > 0)
96  std::cout << prefix << " ";
97  for (int i=0; i<size; i++)
98  std::cout << array[i] << " ";
99  std::cout << std::endl;
100 }
101 
102 template<typename T> void printVector(const std::vector<T>& vec)
103 {
104  printArray<T>(vec.data(), vec.size());
105 }
106 
107 template<typename T> void printOffsets(const T** array, int size, const T* offset, const char* prefix = "")
108 {
109  if (strlen(prefix) > 0)
110  std::cout << prefix << " ";
111  for (int i=0; i<size; i++)
112  std::cout << array[i] - offset << " ";
113  std::cout << std::endl;
114 }
115 
116 template<typename T> void printNestedArray(const T** array, const int* shape, int size)
117 {
118  for (int i=0; i<size; i++)
119  printArray(array[i], shape[i]);
120  std::cout << std::endl;
121 }
122 
123 #define mswap(x, y) swapspace = x; x = y; y=swapspace;
124 
125 
127 void* quickselect(void** array, int n, int start, int end);
128 
129 
130 template <typename T> inline static T* array_copy(const T* A, int size)
131 {
132  T* ret = new T[size];
133  memcpy(ret, A, size*sizeof(T));
134  return ret;
135 }
136 
137 template<typename T> void dealloc_table(T* tbl, int dim)
138 {
139  for(int i=0; i<dim; i++)
140  {
141  delete tbl[i];
142  }
143  delete[] tbl;
144 }
145 
146 template<typename T> void realloc_append(T** array, T what, size_t old_array_size)
147 {
148  T* newT = new T[old_array_size+1];
149  memcpy(newT, *array, old_array_size*sizeof(T));
150  newT[old_array_size] = what;
151  delete[] *array;
152  *array = newT;
153 }
154 
155 } // namespace IsoSpec
156 
IsoSpec
Definition: allocator.cpp:21
IsoSpec::quickselect
void * quickselect(void **array, int n, int start, int end)
Quickly select the n'th positional statistic, including the weights.
Definition: misc.cpp:28