-
Notifications
You must be signed in to change notification settings - Fork 0
/
bitbangM.cpp
151 lines (128 loc) · 2.81 KB
/
bitbangM.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
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/* This file describes the API of an boolean symmetric matrix */
#include "bigint/BigIntegerLibrary.hh"
#include "bitbangM.h"
CBBMatrix::CBBMatrix(int N) :
_N(N), _M(0), _bitmasks(NULL), _last_value(0)
{
/* In this implementation the boolean matrix is
stored into one big integer with bitmasks used to address locations.
order is, first the strictly upper corner matrix row by row, then
the diagonal. For example N=4 matrix would be indexed as follows:
x/y| 0 1 2 3
---+---------
0 | 6 0 1 2
1 | 0 7 3 4
2 | 1 3 8 5
3 | 2 4 5 9
*/
int i = 0;
int j = 0;
int idx = 0;
BigUnsigned mask = 1;
int mask_count = (N*(N+1)/2);
// Dynamic allocation of bitmask table
_bitmasks = new BigUnsigned[mask_count];
// Strictly upper corner masks
for (i = 0 ; i < N ; i++)
{
for (j = i+1 ; j < N ; j++)
{
_last_value |= mask;
idx = get_mask_idx(i,j);
_bitmasks[idx] = mask;
//std::cerr << "DEBUG: set mask ("<< i << "," << j <<" at " << idx << " to value " << bigUnsignedToString(mask) << std::endl;
mask = mask << 1;
}
}
// Diagonal masks
for (i = 0 ; i < N ; i++)
{
idx = get_mask_idx(i,i);
_bitmasks[idx] = mask;
//std::cerr << "DEBUG: set mask ("<< i << "," << j <<" at " << idx << " to value " << bigUnsignedToString(mask) << std::endl;
mask = mask << 1;
}
}
CBBMatrix::~CBBMatrix()
{
delete [] _bitmasks;
}
int CBBMatrix::length()
{
return _N;
}
void CBBMatrix::print(std::ostream& iout)
{
int i = 0;
int j = 0;
iout << "M = [" << std::endl;
for (i = 0 ; i < _N ; i++)
{
for (j = 0 ; j < _N ; j++)
{
iout << (int)(get_value(i,j));
if (i!=_N-1 || j!=_N-1 )
iout << ", ";
}
iout << std::endl;
}
iout << "]" << std::endl;
}
bool CBBMatrix::next_value()
{
if (_M == _last_value)
{
return false;
}
else
{
_M+=1;
return true;
}
}
void CBBMatrix::jump_to_next_change_in(const int i, const int j)
{
// Set all prior to (i,j) to 1 so that the next call of
// next_value() will change (i,j).
int loop_idx = get_mask_idx(i, j)-1;
while (loop_idx>=0)
{
_M |= _bitmasks[loop_idx];
--loop_idx;
}
}
int CBBMatrix::get_mask_idx(const int i, const int j)
{
if (i==j)
{
return (_N*(_N-1)/2)+i;
}
else if (i>j)
{
return j*(_N-1)-int(j*(j+1)/2)+(i-1);
}
else
{
return i*(_N-1)-int(i*(i+1)/2)+(j-1);
}
}
bool CBBMatrix::get_value(const int i, const int j)
{
return ((_M & _bitmasks[get_mask_idx(i, j)]) != 0);
}
void CBBMatrix::set_value(const int i, const int j, const bool value)
{
if (value)
{
_M |= _bitmasks[get_mask_idx(i,j)];
}
// Is set but asked to reset
else if (get_value(i, j))
{
_M-=_bitmasks[get_mask_idx(i,j)];
}
}
void CBBMatrix::set_value(const int i, const int j, const int value)
{
set_value(i, j, value!=0);
}