-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUtil.cpp
More file actions
66 lines (52 loc) · 1.5 KB
/
Util.cpp
File metadata and controls
66 lines (52 loc) · 1.5 KB
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
#include "Util.h"
#include <cmath>
#include <string>
using std::string;
string getBinStrFromInt(int i, int numChars)
{
string builder = string();
int _i = i;
while (_i != 0)
{
builder = ((_i % 2 == 0) ? "0" : "1") + builder;
_i /= 2;
}
int _charsDiff = numChars - builder.size();
if (_charsDiff > 0)
for (int i = 0; i < _charsDiff; i++)
builder = "0" + builder;
if (DEBUG_MODE)
printf("Got %s from %d\n", builder.c_str(), i);
return builder;
}
int getFirstMismatch(string str1, string str2, int offset)
{
for (int i = 0 + offset; i < str1.size() && i < str2.size(); i++)
if (str1.at(i) != str2.at(i))
return i;
return -1;
}
bool isAnotherMistmatch(std::string str1, std::string str2, int firstMismatch, int skip)
{
for (int i = firstMismatch + skip; i < str1.size() && i < str2.size(); i++)
if (str1.at(i) != str2.at(i))
return true;
return false;
}
bool consecutiveMismatches(std::string str1, std::string str2, int firstMismatch, int length)
{
for (int i = firstMismatch; (i < str1.size() && i < str2.size()) || (i < firstMismatch + length); i++)
if (str1.at(i) != str2.at(i))
return false;
return true;
}
unsigned int binStrToInt(string str)
{
unsigned int sum = 0;
for (int i = 0; i < str.size(); i++)
{
unsigned int _val = str[str.size() - 1 - i] == '1' ? 1 : 0;
sum += std::pow(2, i) * _val;
}
return sum;
}