-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbitwrap.cpp
More file actions
112 lines (95 loc) · 2.65 KB
/
bitwrap.cpp
File metadata and controls
112 lines (95 loc) · 2.65 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
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#include <vector>
#include <string>
#include <cmath>
#include <random>
#include <chrono>
class bitwrap {
private:
std::string astr = "0123456789abcdefghijklmnopqrstuvwxyz";
//reverse a string
std::string reverse(std::string str) {
std::string tmp = "";
int len = str.length();
for (int i = 1; i <= len; i++) {
tmp += str[len - i];
}
return tmp;
}
//get index of a string
int indexOf(std::string str, char chr) {
int res = 0;
for (int i = 0; i < str.length(); i++) {
if (str[i] == chr) res = i;
}
return res;
}
//toStrings with a radix
std::string toBase(int num, int base) {
std::string str = astr,
rstr = "";
int i = 0,
n = 0,
strlen = str.length();
while (n <= num) n = pow(base, i++);
i -= 2;
n = pow(base, i);
for (int c = i; c > -1; c--) {
int d = pow(base, c);
rstr += str[(num / d | 0) % base % strlen];
num = num % d;
}
return rstr;
}
//parseInt
int parseInt(std::string str, int radix) {
int base = 1,
res = 0;
std::string bstr = astr;
str = reverse(str);
for (char el : str) {
int n = indexOf(bstr, el);
res += n * base;
base *= radix;
}
return res;
}
std::string gbits(int side = 4) {
std::string tmp = "";
for (int i = 0; i < side; i++) {
tmp += astr[generator() % 2];
}
return tmp;
}
//slices a string
std::string slice(std::string str, int min = 0, int max = -1) {
if (max < 0) max = str.length();
std::string tmp = "";
for (int i = min; i < max; i++) tmp += str[i];
return tmp;
}
public:
std::mt19937 generator;
bitwrap() {
//cancer c++ sorry :(
unsigned seed = std::chrono::system_clock::now().time_since_epoch().count();
std::mt19937 gen(seed);
generator = gen;
}
~bitwrap() { }
std::vector<int> wrap(std::vector<int> arr) {
std::vector<int> tmp = {};
for (int el : arr) {
tmp.push_back(parseInt("1" + gbits(3) + toBase(el, 2) + gbits(), 2));
}
return tmp;
}
std::vector<int> unwrap(std::vector<int> arr) {
std::vector<int> tmp = {};
for (int el : arr) {
std::string bits = toBase(el, 2);
int blen = bits.length() - 8; //sum of both bits (4 + 4)
tmp.push_back( parseInt( slice(bits, 4, blen + 4), 2) );
}
return tmp;
}
};