blob: 02572c28d42adf642382d1ff5b4640b08a58bf75 (
plain) (
blame)
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
113
114
115
116
117
118
119
120
121
|
/*
* bit_utils.h
*
* Translation of bit_utils.py to c++
*
* Created on: Feb 19, 2017
* Author: S. Lukic
*/
#ifndef BIT_UTILS_H_
#define BIT_UTILS_H_
#include <vector>
#include <string>
#include <cassert>
#include <cstdio>
#include <iostream>
#include "Eigen/Dense"
unsigned rev_int(unsigned n, unsigned l) {
unsigned j=0;
for (unsigned i=0; i<l; i++) {
unsigned b = n&1;
n >>= 1;
j = (j<<1) | b;
}
return j;
}
/* "bits" and "dibits" are coded as unsigned integers.
* This is an arbitrary decision.
* Eigen::VectorXi objects are used for python lists
* containing numbers and for Numpy ndarrays.
*/
Eigen::VectorXi * bits_to_dibits(Eigen::VectorXi &bits) {
Eigen::VectorXi *d = new Eigen::VectorXi;
for (unsigned i=0; i<bits.size()/2; i++) {
(*d)(i) = ((bits(i*2)&1)<<1) + (bits(i*2+1)&1);
}
return d;
}
Eigen::VectorXi* dibits_to_bits(Eigen::VectorXi &dibits) {
Eigen::VectorXi *b = new Eigen::VectorXi;
for (unsigned i=0; i<dibits.size(); i++) {
unsigned char d = dibits(i);
(*b)(i*2) = (d>>1)&1;
(*b)(i*2+1) = d&1;
}
return b;
}
// Tested OK
Eigen::VectorXi *mk_array(unsigned long long n, unsigned l) {
Eigen::VectorXi *a = new Eigen::VectorXi(l);
for(int i=l-1; i>=0; i--) {
(*a)(i) = n&1;
n >>= 1;
}
return a;
}
unsigned long long mk_int(Eigen::VectorXi a) {
unsigned long long res = 0ULL;
for (unsigned i=0; i< a.size(); i++) {
res *= 2ULL;
res += (unsigned long long)(a(i) & 1);
}
return res;
}
std::string mk_str(Eigen::VectorXi a) {
std::string res = "";
char buffer[10];
for (unsigned i=0; i<a.size(); i++) {
sprintf(buffer, "%d", (a(i) & 1));
res.append(buffer);
}
return res;
}
unsigned check_l(Eigen::VectorXi a, Eigen::VectorXi b) {
unsigned ans = 0;
assert(a.size()==b.size());
for (unsigned i=0; i<a.size(); i++) {
if (a(i) == b(i)) ans++;
}
return ans;
}
Eigen::VectorXi *fixup(Eigen::VectorXi a) {
Eigen::VectorXi *res;
for (unsigned i=0; i<a.size(); i++) {
if (a(i) == 3) { (*res)(i) = 1; }
else { (*res)(i) = 3; }
}
return res;
}
unsigned find_sym(Eigen::VectorXi pattern, Eigen::VectorXi symbols) {
for (unsigned i=0; i < (pattern.size()-symbols.size()); i++) {
if (symbols.segment(i, pattern.size()) == pattern) return i;
}
return -1;
}
void print_array(const Eigen::VectorXi *a) {
for (unsigned i=0; i<a->size()-1; i++) {
std::cout << (*a)(i) << ", ";
}
std::cout << (*a)(a->size()-1) << ",";
std::cout << std::endl;
}
#endif /* BIT_UTILS_H_ */
|