Orbiter 2022
Combinatorial Objects
bitmatrix.cpp
Go to the documentation of this file.
1/*
2 * bitmatrix.cpp
3 *
4 * Created on: Aug 15, 2019
5 * Author: betten
6 */
7
8
9
10#include "foundations.h"
11
12using namespace std;
13
14namespace orbiter {
15namespace layer1_foundations {
16namespace data_structures {
17
18
20{
21 m = 0;
22 n = 0;
23 N = 0;
24 data = NULL;
25}
26
28{
29 if (data) {
30 FREE_int((int *) data);
31 }
32 //freeself();
33}
34
35void bitmatrix::init(int m, int n, int verbose_level)
36{
37 int f_v = (verbose_level >= 1);
38 int n1, sz, i;
39
40 if (f_v) {
41 cout << "bitmatrix::init" << endl;
42 }
45 N = n >> 5; // 4 bytes = 32 bits = 2^5
46 n1 = N << 5;
47 if (n > n1) {
48 N++;
49 }
50 sz = m * N;
51 data = (uint32_t *) NEW_int(sz);
52 for (i = 0; i < m * N; i++) {
53 data[i] = 0;
54 }
55 if (f_v) {
56 cout << "bitmatrix::init done" << endl;
57 }
58}
59
61 field_theory::finite_field *F, long int start_value, int verbose_level)
62{
63 int f_v = (verbose_level >= 1);
64 int *v;
65 int i, j, n100;
66
67 if (f_v) {
68 cout << "bitmatrix::unrank_PG_elements_in_columns_consecutively" << endl;
69 }
70 if (F->q != 2) {
71 cout << "bitmatrix::unrank_PG_elements_in_columns_consecutively F->q != 2" << endl;
72 exit(1);
73 }
74 zero_out();
75 v = NEW_int(m);
76 n100 = n / 100 + 1;
77 for (j = 0; j < n; j++) {
78 if ((j % n100) == 0) {
79 cout << "bitmatrix::unrank_PG_elements_in_columns_consecutively " << j / n100 << " % done unranking" << endl;
80 }
81 F->PG_element_unrank_modified_lint(v, 1, m, start_value + j);
82 for (i = 0; i < m; i++) {
83 if (v[i]) {
84 m_ij(i, j, 1);
85 }
86 }
87 }
88 if (f_v) {
89 cout << "bitmatrix::unrank_PG_elements_in_columns_consecutively done" << endl;
90 }
91}
92
95 int *perms, unsigned int *PG_ranks, int verbose_level)
96{
97 int f_v = (verbose_level >= 1);
98 int f_vv = FALSE;
99 int *v;
100 int i;
101 int j;
102 int n100;
103 long int b;
104
105 if (f_v) {
106 cout << "bitmatrix::rank_PG_elements_in_columns" << endl;
107 }
108 if (F->q != 2) {
109 cout << "bitmatrix::rank_PG_elements_in_columns F->q != 2" << endl;
110 exit(1);
111 }
112 if (f_vv) {
113 cout << "perm=";
114 Int_vec_print(cout, perms, m);
115 cout << endl;
116 }
117 v = NEW_int(m);
118 n100 = n / 100 + 1;
119 for (j = 0; j < n; j++) {
120 if ((j % n100) == 0) {
121 cout << "bitmatrix::rank_PG_elements_in_columns "
122 << j / n100 << " % done ranking" << endl;
123 }
124 for (i = 0; i < m; i++) {
125 int a = perms[i];
126 //cout << "i=" << i << " j=" << j << " s_ij(i, j)=" << s_ij(i, j) << endl;
127 if (s_ij(i, j)) {
128 v[a] = 1;
129 }
130 else {
131 v[a] = 0;
132 }
133 }
134 //cout << "j=" << j << " v=";
135 //int_vec_print(cout, v, m);
136 //cout << endl;
137 F->PG_element_rank_modified_lint(v, 1, m, b);
138 PG_ranks[j] = (unsigned int) b;
139 }
140 FREE_int(v);
141 if (f_v) {
142 cout << "bitmatrix::rank_PG_elements_in_columns done" << endl;
143 }
144}
145
147{
148 int i, j, x;
149
150 for (i = 0; i < m; i++) {
151 for (j = 0; j < MIN(n, 10); j++) {
152 x = s_ij(i, j);
153 if (x)
154 cout << "1";
155 else
156 cout << "0";
157 }
158 cout << endl;
159 }
160 cout << endl;
161}
162
164{
165 int i;
166
167 for (i = 0; i < m * N; i++) {
168 data[i] = 0;
169 }
170}
171
172int bitmatrix::s_ij(int i, int j)
173{
174 int jj, bit;
175 uint32_t mask;
176
177 if (i < 0 || i >= m) {
178 cout << "bitmatrix::s_ij addressing error, i = " << i << ", m = " << m << endl;
179 exit(1);
180 }
181 if ( j < 0 || j >= n ) {
182 cout << "bitmatrix::s_ij addressing error, j = " << j << ", n = " << n << endl;
183 exit(1);
184 }
185 jj = j >> 5;
186 bit = j & 31;
187 mask = ((uint32_t) 1) << bit;
188 uint32_t &x = data[i * N + jj];
189 if (x & mask)
190 return 1;
191 else
192 return 0;
193}
194
195void bitmatrix::m_ij(int i, int j, int a)
196{
197 int jj, bit;
198 uint32_t mask;
199
200 if (i < 0 || i >= m) {
201 cout << "bitmatrix::m_ij addressing error, i = " << i << ", m = " << m << endl;
202 exit(1);
203 }
204 if ( j < 0 || j >= n ) {
205 cout << "bitmatrix::m_ij addressing error, j = " << j << ", n = " << n << endl;
206 exit(1);
207 }
208 jj = j >> 5;
209 bit = j & 31;
210 mask = ((uint32_t) 1) << bit;
211 uint32_t &x = data[i * N + jj];
212 if (a == 0) {
213 uint32_t not_mask = ~mask;
214 x &= not_mask;
215 }
216 else {
217 x |= mask;
218 }
219}
220
222 bitmatrix *Out, int verbose_level)
223{
224 int f_v = (verbose_level >= 1);
225 int f_vv = FALSE;
226 int i, j, h;
227
228 if (f_v) {
229 cout << "bitmatrix::mult_int_matrix_from_the_left" << endl;
230 }
231 if (An != m) {
232 cout << "bitmatrix::mult_int_matrix_from_the_left An != m" << endl;
233 cout << "An=" << An << endl;
234 cout << "m=" << m << endl;
235 exit(1);
236 }
237 if (Out->m != Am) {
238 cout << "bitmatrix::mult_int_matrix_from_the_left Out->m != Am" << endl;
239 cout << "Am=" << Am << endl;
240 cout << "Out->m=" << Out->m << endl;
241 exit(1);
242 }
243 if (Out->n != n) {
244 cout << "bitmatrix::mult_int_matrix_from_the_left Out->n != n" << endl;
245 cout << "n=" << n << endl;
246 cout << "Out->n=" << Out->n << endl;
247 exit(1);
248 }
249 Out->zero_out();
250 for (i = 0; i < Am; i++) {
251 if (f_vv) {
252 cout << "bitmatrix::mult_int_matrix_from_the_left row " << i << " : ";
253 }
254 for (j = 0; j < An; j++) {
255 if (A[i * An + j]) {
256 if (f_vv) {
257 cout << j << ", ";
258 }
259 for (h = 0; h < N; h++) {
260 Out->data[i * N + h] ^= data[j * N + h];
261 }
262 }
263 }
264 if (f_vv) {
265 cout << endl;
266 }
267 }
268
269
270 if (f_v) {
271 cout << "bitmatrix::mult_int_matrix_from_the_left done" << endl;
272 }
273}
274
275
276
277}}}
278
matrices over GF(2) stored as bitvectors
void init(int m, int n, int verbose_level)
Definition: bitmatrix.cpp:35
void unrank_PG_elements_in_columns_consecutively(field_theory::finite_field *F, long int start_value, int verbose_level)
Definition: bitmatrix.cpp:60
void mult_int_matrix_from_the_left(int *A, int Am, int An, bitmatrix *Out, int verbose_level)
Definition: bitmatrix.cpp:221
void rank_PG_elements_in_columns(field_theory::finite_field *F, int *perms, unsigned int *PG_ranks, int verbose_level)
Definition: bitmatrix.cpp:93
void PG_element_unrank_modified_lint(int *v, int stride, int len, long int a)
void PG_element_rank_modified_lint(int *v, int stride, int len, long int &a)
#define FREE_int(p)
Definition: foundations.h:640
#define MIN(x, y)
Definition: foundations.h:218
#define NEW_int(n)
Definition: foundations.h:625
#define FALSE
Definition: foundations.h:234
#define Int_vec_print(A, B, C)
Definition: foundations.h:685
the orbiter library for the classification of combinatorial objects