forked from williamslab/genetio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.cc
65 lines (52 loc) · 1.67 KB
/
util.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
// Library for I/O of genetic data
// Author: Amy Williams <alw289 cornell edu>
//
// This program is distributed under the terms of the GNU General Public License
#include <stdio.h>
#include <stdint.h>
#include <assert.h>
#include <string.h>
#include "util.h"
////////////////////////////////////////////////////////////////////////////////
// initialize static members
std::mt19937 RandGen::v;
std::uniform_real_distribution<> RandGen::dis01(0,1);
int intHashFunc(const int &key) {
// Take each 2 byte portion of the chunk and combine them:
int twoByteMask = (1 << 16) - 1;
int total = 0;
for(uint16_t i = 0; i < (sizeof(int) / 2); i++) {
// add in get the ith set of two bytes
total += (key >> (16 * i)) & twoByteMask;
total *= 3;
}
return total;
}
bool intEqualFunc(const int &v1, const int &v2) {
return v1 - v2 == 0;
}
int pairIntHashFunc(const PairIdx<int> &key) {
return 7 * key[0] + 13 * key[1];
}
bool pairIntEqualFunc(const PairIdx<int> &v1, const PairIdx<int> &v2) {
return v1[0] == v2[0] && v1[1] == v2[1];
}
bool stringcmp(char * const &s1, char * const &s2) {
return strcmp(s1, s2) == 0;
}
void RandGen::seed(bool autoSrand, std::mt19937::result_type &randSeed) {
if (!autoSrand) {
v.seed(randSeed); // seed with a given value
return;
}
// need to produce our own seed
randSeed = std::random_device().entropy();
if (randSeed == 0 && std::random_device().entropy() == 0) {
// std::random_device is not a real random number generator: fall back on
// using time to generate a seed:
timeval tv;
gettimeofday(&tv, NULL);
randSeed = tv.tv_sec + tv.tv_usec * 100000;
}
v.seed(randSeed); // do the seeding
}