IsoSpec  1.95
operators.h
1 /*
2  * Copyright (C) 2015-2018 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 <string.h>
20 #include "conf.h"
21 #include "isoMath.h"
22 #include "misc.h"
23 
24 namespace IsoSpec
25 {
26 
27 class KeyHasher
28 {
29 private:
30  int dim;
31 public:
32  KeyHasher(int dim);
33 
34  inline std::size_t operator()(const int* conf) const
35  {
36  // Following Boost...
37  std::size_t seed = 0;
38  for(int i = 0; i < dim; ++i )
39  seed ^= conf[i] + 0x9e3779b9 + (seed << 6) + (seed >> 2);
40  return seed;
41  };
42 };
43 
44 
45 class ConfEqual
46 {
47 private:
48  int size;
49 public:
50  ConfEqual(int dim);
51 
52  inline bool operator()(const int* conf1, const int* conf2) const
53  {
54  // The memcmp() function returns zero if the two strings are identical, oth-
55  // erwise returns the difference between the first two differing bytes
56  // (treated as unsigned char values, so that `\200' is greater than `\0',
57  // for example). Zero-length strings are always identical. This behavior
58  // is not required by C and portable code should only depend on the sign of
59  // the returned value.
60  // sacred man of memcmp.
61  return memcmp(conf1, conf2, size) == 0;
62  }
63 };
64 
65 
66 class ConfOrder
67 {
68 //configurations comparator
69 public:
70  inline bool operator()(void* conf1,void* conf2) const
71  {
72  return *reinterpret_cast<double*>(conf1) < *reinterpret_cast<double*>(conf2);
73  };
74 };
75 
76 
77 
79 {
80 //configurations comparator
81  const double* logProbs;
82  int dim;
83 public:
84  ConfOrderMarginal(const double* logProbs, int dim);
85 
86  inline bool operator()(const Conf conf1, const Conf conf2)
87  {// Return true if conf1 is less probable than conf2.
88  return unnormalized_logProb(conf1,logProbs,dim) < unnormalized_logProb(conf2,logProbs,dim);
89  };
90 };
91 
93 {
94 //configurations comparator
95  const double* logProbs;
96  int dim;
97 public:
98  ConfOrderMarginalDescending(const double* logProbs, int dim);
99 
100  inline bool operator()(const Conf conf1, const Conf conf2)
101  {// Return true if conf1 is less probable than conf2.
102  return unnormalized_logProb(conf1,logProbs,dim) > unnormalized_logProb(conf2,logProbs,dim);
103  };
104 };
105 
106 template<typename T> class ReverseOrder
107 {
108 public:
109  inline ReverseOrder() {};
110  inline bool operator()(const T a,const T b) const { return a > b; };
111 };
112 
113 template<typename T> class TableOrder
114 {
115  const T* tbl;
116 public:
117  inline TableOrder(const T* _tbl) : tbl(_tbl) {};
118  inline bool operator()(unsigned int i, unsigned int j) { return tbl[i] < tbl[j]; };
119 };
120 
121 } // namespace IsoSpec
122 
123 #include "marginalTrek++.h"
124 
125 class PrecalculatedMarginal; // In case marginalTrek++.h us including us, and can't be included again...
126 
127 namespace IsoSpec
128 {
129 
131 {
132  PrecalculatedMarginal const* const* const T;
133 public:
134  OrderMarginalsBySizeDecresing(PrecalculatedMarginal const* const * const _T);
135  bool operator()(int m1, int m2);
136 };
137 
138 
139 } // namespace IsoSpec
140 
141 
142 
IsoSpec::TableOrder
Definition: operators.h:114
IsoSpec::PrecalculatedMarginal
Precalculated Marginal class.
Definition: marginalTrek++.h:214
IsoSpec::OrderMarginalsBySizeDecresing
Definition: operators.h:131
IsoSpec
Definition: allocator.cpp:22
IsoSpec::ConfOrderMarginal
Definition: operators.h:79
IsoSpec::KeyHasher
Definition: operators.h:28
IsoSpec::ConfEqual
Definition: operators.h:46
IsoSpec::ReverseOrder
Definition: operators.h:107
IsoSpec::ConfOrder
Definition: operators.h:67
IsoSpec::ConfOrderMarginalDescending
Definition: operators.h:93