forked from HIITMetagenomics/dsm-framework
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Tools.cpp
95 lines (79 loc) · 1.84 KB
/
Tools.cpp
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/*
* Collection of basic tools and defines
*/
#include "Tools.h"
#include <cstdio>
time_t Tools::startTime;
void Tools::StartTimer()
{
startTime = time(NULL);
}
double Tools::GetTime()
{
time_t stopTime = time(NULL);
return difftime( stopTime, startTime );
}
uchar * Tools::GetRandomString(unsigned min, unsigned max, unsigned &alphabetSize)
{
unsigned len = std::rand() % (max - min) + min;
alphabetSize = std::rand() % 26 + 1;
uchar* temp = new uchar[len + 2];
for (unsigned i = 0; i < len; i++)
temp[i] = 97 + std::rand() % alphabetSize;
temp[len] = 0u ;temp[len+1] = '\0';
return temp;
}
void Tools::PrintBitSequence(ulong *A, ulong len)
{
for(ulong i = 0; i < len; i++)
if (GetField(A, 1, i))
std::cout << "1";
else
std::cout << "0";
std::cout << "\n";
}
unsigned Tools::FloorLog2(ulong i)
{
unsigned b = 0;
if (i == 0)
return 0;
while (i)
{
b++;
i >>= 1;
}
return b - 1;
}
unsigned Tools::CeilLog2(ulong i)
{
unsigned j = FloorLog2(i);
if ((ulong)(1lu << j) != i)
return j + 1;
return j;
}
uchar * Tools::GetFileContents(char *filename, ulong maxSize)
{
std::ifstream::pos_type posSize;
std::ifstream file ((char *)filename, std::ios::in|std::ios::binary|std::ios::ate);
if (file.is_open())
{
posSize = file.tellg();
ulong size = posSize;
if (maxSize != 0 && size > maxSize)
size = maxSize;
char *memblock = new char [size + 1];
file.seekg (0, std::ios::beg);
file.read (memblock, size);
memblock[size] = '\0';
file.close();
return (uchar *)memblock;
}
else
return 0;
}
unsigned Tools::bits (ulong n)
{ unsigned b = 0;
while (n)
{ b++; n >>= 1; }
return b;
}