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