File: BitMatrix.c

package info (click to toggle)
r-bioc-biostrings 2.42.1-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 14,652 kB
  • ctags: 721
  • sloc: ansic: 10,262; sh: 11; makefile: 2
file content (367 lines) | stat: -rw-r--r-- 8,625 bytes parent folder | download | duplicates (3)
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
/****************************************************************************
 *                                                                          *
 *                   Routines for BitMatrix manipulation                    *
 *                            Author: H. Pag\`es                            *
 *                                                                          *
 ****************************************************************************/
#include "Biostrings.h"
#include "S4Vectors_interface.h"
#include <S.h> /* for Salloc() */

#include <stdio.h>
#include <limits.h> /* for CHAR_BIT and ULONG_MAX */
#include <stdlib.h> /* for div() */


#define BITMATBYROW_NCOL (sizeof(int) * CHAR_BIT)

typedef IntAE BitMatByRow;

void _BitCol_set_val(BitCol *bitcol, BitWord val)
{
	div_t q;
	BitWord *bitword;
	int i1;

	q = div(bitcol->nbit, NBIT_PER_BITWORD);
	if (q.rem != 0)
		q.quot++;
	bitword = bitcol->bitword0;
	for (i1 = 0; i1 < q.quot; i1++)
		*(bitword++) = val;
	return;
}

BitCol _new_BitCol(int nbit, BitWord val)
{
	BitCol bitcol;
	div_t q;
	int nword;

	if (nbit <= 0)
		error("_new_BitCol(): nbit <= 0");
	q = div(nbit, NBIT_PER_BITWORD);
	nword = q.quot;
	if (q.rem != 0)
		nword++;
	bitcol.bitword0 = Salloc((long) nword, BitWord);
	bitcol.nword = nword;
	bitcol.nbit = nbit;
	_BitCol_set_val(&bitcol, val);
	return bitcol;
}

int _BitCol_get_bit(const BitCol *bitcol, int i)
{
	div_t q;
	BitWord *bitword;

	q = div(i, NBIT_PER_BITWORD);
	bitword = bitcol->bitword0 + q.quot;
	return (*bitword >> q.rem) & 1UL;
}

void _BitCol_set_bit(BitCol *bitcol, int i, int bit)
{
	div_t q;
	BitWord *bitword, mask;

	q = div(i, NBIT_PER_BITWORD);
	bitword = bitcol->bitword0 + q.quot;
	mask = 1UL << q.rem;
	if (bit)
		*bitword |= mask;
	else
		*bitword &= ~mask;
	return;
}

void _BitCol_A_gets_BimpliesA(BitCol *A, const BitCol *B)
{
	div_t q;
	BitWord *Abitword;
	const BitWord *Bbitword;
	int i1;

	if (A->nbit != B->nbit)
		error("_BitCol_A_gets_BimpliesA(): "
		      "'A' and 'B' are incompatible");
	q = div(A->nbit, NBIT_PER_BITWORD);
	if (q.rem != 0)
		q.quot++;
	Abitword = A->bitword0;
	Bbitword = B->bitword0;
	for (i1 = 0; i1 < q.quot; i1++)
		*(Abitword++) |= ~(*(Bbitword++));
	return;
}

/* WARNING: This is a 0-copy column extraction! */
BitCol _BitMatrix_get_col(const BitMatrix *bitmat, int j)
{
	BitCol bitcol;

	bitcol.bitword0 = bitmat->bitword00 + j * bitmat->nword_per_col;
	bitcol.nword = bitmat->nword_per_col;
	bitcol.nbit = bitmat->nrow;
	return bitcol;
}

void _BitMatrix_set_col(BitMatrix *bitmat, int j, const BitCol *bitcol)
{
	div_t q;
	BitWord *Lbitword;
	const BitWord *Rbitword;
	int i1;

	if (bitmat->nrow != bitcol->nbit)
		error("_BitMatrix_set_col(): "
		      "'bitmat' and 'bitcol' are incompatible");
	q = div(bitmat->nrow, NBIT_PER_BITWORD);
	if (q.rem != 0)
		q.quot++;
	Lbitword = bitmat->bitword00 + j * bitmat->nword_per_col;
	Rbitword = bitcol->bitword0;
	for (i1 = 0; i1 < q.quot; i1++)
		*(Lbitword++) = *(Rbitword++);
	return;
}


/*
 * _BitMatrix_set_val() could also be implemented as:
 *   for (j = 0; j < bitmat->ncol; j++) {
 *     bitcol = _BitMatrix_get_col(bitmat, j);
 *     _BitCol_set_val(&bitcol, val);
 *   }
 * but the implementation below is faster (only 1 call to div()).
 */
void _BitMatrix_set_val(BitMatrix *bitmat, BitWord val)
{
	div_t q;
	int i1, j;
	BitWord *bitword0, *bitword;

	q = div(bitmat->nrow, NBIT_PER_BITWORD);
	if (q.rem != 0)
		q.quot++;
	for (j = 0, bitword0 = bitmat->bitword00;
	     j < bitmat->ncol;
	     j++, bitword0 += bitmat->nword_per_col)
	{
		bitword = bitword0;
		for (i1 = 0; i1 < q.quot; i1++)
			*(bitword++) = val;
	}
	return;
}

BitMatrix _new_BitMatrix(int nrow, int ncol, BitWord val)
{
	BitMatrix bitmat;
	div_t q;
	int nword_per_col, nword;

	if (nrow <= 0 || ncol <= 0)
		error("_new_BitMatrix(): nrow <= 0 || ncol <= 0");
	q = div(nrow, NBIT_PER_BITWORD);
	nword_per_col = q.quot;
	if (q.rem != 0)
		nword_per_col++;
	nword = nword_per_col * ncol;
	bitmat.bitword00 = Salloc((long) nword, BitWord);
	bitmat.nword_per_col = nword_per_col;
	bitmat.nrow = nrow;
	bitmat.ncol = ncol;
	_BitMatrix_set_val(&bitmat, val);
	return bitmat;
}

/*
 * _BitMatrix_get_bit() could also be implemented as:
 *   bitcol = _BitMatrix_get_col(bitmat, j);
 *   return _BitCol_get_bit(&bitcol, i);
 */
int _BitMatrix_get_bit(const BitMatrix *bitmat, int i, int j)
{
	div_t q;
	BitWord *bitword;

	q = div(i, NBIT_PER_BITWORD);
	bitword = bitmat->bitword00 + j * bitmat->nword_per_col + q.quot;
	return (*bitword >> q.rem) & 1UL;
}

/*
 * _BitMatrix_set_bit() could also be implemented as:
 *   bitcol = _BitMatrix_get_col(bitmat, j);
 *   _BitCol_set_bit(&bitcol, i, bit);
 */
void _BitMatrix_set_bit(BitMatrix *bitmat, int i, int j, int bit)
{
	div_t q;
	BitWord *bitword, mask;

	q = div(i, NBIT_PER_BITWORD);
	bitword = bitmat->bitword00 + j * bitmat->nword_per_col + q.quot;
	mask = 1UL << q.rem;
	if (bit)
		*bitword |= mask;
	else
		*bitword &= ~mask;
	return;
}

void _BitMatrix_Rrot1(BitMatrix *bitmat)
{
	div_t q;
	BitWord *bitwordNE, *Lbitword, *Rbitword;
	int i1, j;

	if (bitmat->ncol == 0)
		error("_BitMatrix_Rrot1(): bitmat->ncol == 0");
	q = div(bitmat->nrow, NBIT_PER_BITWORD);
	if (q.rem != 0)
		q.quot++;
	bitwordNE = bitmat->bitword00 + (bitmat->ncol - 1) * bitmat->nword_per_col;
	for (i1 = 0; i1 < q.quot; i1++) {
		Rbitword = bitwordNE + i1;
		Lbitword = Rbitword - bitmat->nword_per_col;
		for (j = 1; j < bitmat->ncol; j++) {
			*Rbitword = *Lbitword;
			Rbitword = Lbitword;
			Lbitword -= bitmat->nword_per_col;
		}
		*Rbitword = ULONG_MAX;
	}
	return;
}

void _BitMatrix_grow1rows(BitMatrix *bitmat, const BitCol *bitcol)
{
	div_t q;
	BitWord *Lbitword, Rbitword, ret;
	int i1, j;

	if (bitmat->nrow != bitcol->nbit)
		error("_BitMatrix_grow1rows(): "
		      "'bitmat' and 'bitcol' are incompatible");
	q = div(bitmat->nrow, NBIT_PER_BITWORD);
	if (q.rem != 0)
		q.quot++;
	for (i1 = 0; i1 < q.quot; i1++) {
		Lbitword = bitmat->bitword00 + i1;
		Rbitword = bitcol->bitword0[i1];
		for (j = 0; j < bitmat->ncol; j++) {
			ret = *Lbitword & Rbitword; // and
			*Lbitword |= Rbitword; // or
			Rbitword = ret;
			Lbitword += bitmat->nword_per_col;
		}
	}
	return;
}



/*****************************************************************************
 * Testing and debugging stuff
 */

static void BitMatrix_tr(BitMatrix *in, BitMatByRow *out)
{
	BitWord rbit, *bitword;
	int i1, i2, i, j, cbit;

	if (in->nrow != IntAE_get_nelt(out))
		error("BitMatrix_tr(): 'in' and 'out' are incompatible");
	if (in->ncol >= BITMATBYROW_NCOL)
		error("BitMatrix_tr(): 'in' has too many columns");
	for (i1 = i = 0; i1 < in->nword_per_col; i1++) {
		for (i2 = 0, rbit = 1UL;
		     i2 < NBIT_PER_BITWORD;
		     i2++, i++, rbit <<= 1)
		{
			if (i >= in->nrow)
				return;
			out->elts[i] = 0;
			for (j = 0, bitword = in->bitword00 + i1, cbit = 1;
			     j < in->ncol;
			     j++, bitword += in->nword_per_col, cbit <<= 1) {
				if (*bitword & rbit)
					out->elts[i] += cbit;
			}
		}
	}
	return;
}

static void BitMatrix_print(BitMatrix *bitmat)
{
	BitMatByRow *bitmat_byrow;
	int i, *row, j, cbit, bit;

	bitmat_byrow = new_IntAE(bitmat->nrow, bitmat->nrow, 0);
	BitMatrix_tr(bitmat, bitmat_byrow);
	for (i = 0, row = bitmat_byrow->elts; i < bitmat->nrow; i++, row++) {
		Rprintf("%4d: ", i);
		for (j = 0, cbit = 1; j < bitmat->ncol; j++, cbit <<= 1) {
			bit = (*row & cbit) != 0;
			Rprintf("%d", bit);
		}
		Rprintf(" (%d)\n", *row);
	}
	return;
}

/*
static void BitMatrix_addcol(BitMatrix *bitmat, const BitCol *bitcol)
{
	BitWord *Lbitword, Rbitword, ret;
	int i1, j;

	if (bitmat->nrow != bitcol->nbit)
		error("BitMatrix_addcol(): "
		      "'bitmat' and 'bitcol' are incompatible");
	for (i1 = 0; i1 < bitmat->nword_per_col; i1++) {
		Lbitword = bitmat->bitword00 + i1;
		Rbitword = bitcol->bitword0[i1];
		for (j = 0; j < bitmat->ncol; j++) {
			ret = *Lbitword & Rbitword; // and
			*Lbitword ^= Rbitword; // xor
			Rbitword = ret;
			Lbitword += bitmat->nword_per_col;
		}
		if (Rbitword)
			warning("integer overflow in BitMatrix_addcol()");
	}
	return;
}

static void testing1(BitMatrix *bitmat, BitCol *bitcol)
{
	int n, p;

	for (n = 0; n < 1000000; n++) {
		_BitMatrix_set_val(bitmat, 0UL);
		for (p = 0; p < 25; p++)
			BitMatrix_addcol(bitmat, bitcol);
	}
	//BitMatrix_print(&bitmat);
	return;
}

static void testing2(BitMatByRow *bitmat_byrow)
{
	int n, p, i, *row;

	for (n = 0; n < 1000000; n++) {
		IntAE_set_val(bitmat_byrow, 0);
		for (i = 0, row = bitmat_byrow->elts; i < bitmat_byrow->nelt; i++)
			for (p = 0; p < 25; p++)
				(*row)++;
	}
	return;
}
*/