-
Notifications
You must be signed in to change notification settings - Fork 1
/
rdfstore_bits.c
580 lines (518 loc) · 14.5 KB
/
rdfstore_bits.c
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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
/*
##############################################################################
# Copyright (c) 2000-2006 All rights reserved
# Alberto Reggiori <areggiori@webweaving.org>
# Dirk-Willem van Gulik <dirkx@webweaving.org>
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
#
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in
# the documentation and/or other materials provided with the
# distribution.
#
# 3. The end-user documentation included with the redistribution,
# if any, must include the following acknowledgment:
# "This product includes software developed by
# Alberto Reggiori <areggiori@webweaving.org> and
# Dirk-Willem van Gulik <dirkx@webweaving.org>."
# Alternately, this acknowledgment may appear in the software itself,
# if and wherever such third-party acknowledgments normally appear.
#
# 4. All advertising materials mentioning features or use of this software
# must display the following acknowledgement:
# This product includes software developed by the University of
# California, Berkeley and its contributors.
#
# 5. Neither the name of the University nor the names of its contributors
# may be used to endorse or promote products derived from this software
# without specific prior written permission.
#
# 6. Products derived from this software may not be called "RDFStore"
# nor may "RDFStore" appear in their names without prior written
# permission.
#
# THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
# ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
# STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
# OF THE POSSIBILITY OF SUCH DAMAGE.
#
# ====================================================================
#
# This software consists of work developed by Alberto Reggiori and
# Dirk-Willem van Gulik. The RDF specific part is based based on public
# domain software written at the Stanford University Database Group by
# Sergey Melnik. For more information on the RDF API Draft work,
# please see <http://www-db.stanford.edu/~melnik/rdf/api.html>
# The DBMS TCP/IP server part is based on software originally written
# by Dirk-Willem van Gulik for Web Weaving Internet Engineering m/v Enschede,
# The Netherlands.
#
##############################################################################
$Id: rdfstore_bits.c,v 1.16 2006/06/19 10:10:21 areggiori Exp $
*/
#include <sys/types.h>
#include <sys/time.h>
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <string.h>
#include <assert.h>
#include "dbms.h"
#include "dbms_compat.h"
#include "rdfstore.h"
#include "rdfstore_bits.h"
#include "rdfstore_log.h"
/* within p->size / p->bits
* set at -bit- position 'at'
* the masked bits to value
*
* return the modified bits
*/
/*
* #define RDFSTORE_DEBUG_BITS
*/
int rdfstore_bits_setmask(
unsigned int * size,
unsigned char * bits,
unsigned int at,
unsigned int mask,
unsigned int value,
unsigned int max
) {
register int depth,change;
if (mask == 0) return(0);
/* auto extend if needed... */
if ( (at/8) >= *size) {
unsigned int n=*size;
unsigned int s= STEP * ( 1 + at/8/STEP );
if (s>max) {
fprintf(stderr, "Too many bit=%d byte=%d %d of %d\n",
at, at/8, s, max);
exit(1);
};
*size = s;
bzero(bits+n, s-n);
};
/* x x x x x x as stored
* 0 0 1 1 1 0 mask
* 0 0 0 1 1 0 value
*/
mask <<= at % 8;
value <<= at % 8;
at /= 8;
change =0; depth = 0;
do {
register unsigned char d,c;
if (at>=max) {
fprintf(stderr,"Uncontrolled overflow %d of %d\n",
at, max);
exit(1);
};
c = bits[ at ];
d = ( c & (~ mask) ) | value;
if (d != c) {
bits[ at ] = d;
change |= (d ^ c) << depth;
};
at ++;
depth += 8;
mask >>= 8;
value >>= 8;
} while ((mask) && (at < *size ));
return (change);
};
/* Return the record number (bit number / 4) of the
* first record from record 'at' onwards which has
* a bit set within the mask.
*/
unsigned int
rdfstore_bits_getfirstrecord (
unsigned int size, /* in bytes */
unsigned char * bits, /* bit array */
unsigned int at, /* as record no (bits/4) */
unsigned char mask /* 0000 to 1111 */
) {
unsigned mask2 = mask << 4;
unsigned i = at >> 1;
unsigned char c = bits[i];
assert(mask < 16);
assert(mask != 0);
if (at & 1)
c &= 0xF0;
do {
if ((c & 0x0f) & mask)
return 2*i+0;
if ((c & 0xf0) & mask2)
return 2*i+1;
c = bits[ ++i ];
} while (i < size);
return size*2;
}
/*
* rdfstore_bits_isanyset - returns != 0 if any bit in the bitmask is set
* in addition it returns the positions of the first bit set in at
*
*/
int rdfstore_bits_isanyset(
unsigned int * size,
unsigned char * bits,
unsigned int * at,
unsigned char mask
) {
register unsigned rest=0;
rest = ( *at % 8 );
mask = mask << rest;
*at /= 8;
while ((mask) && (*at < *size)) {
register int c = bits[ *at ] & mask;
if (c) {
(*at) *=8;
(*at) += rest;
return c;
};
(*at)++;
};
return 0;
};
/*
* returns the first bit set from (and including) at in the
* bit array of size bytes. If no bit set is found after
* the size-est byte of bits; size*8 is returned (i.e. the number
* of * last bit (not byte)+1; starting from zero.
*
* size in bytes
* bits unsigned array of bytes with 8 bits each
* at location in bits.
* return location in bits (or size*8).
*
*/
unsigned int rdfstore_bits_getfirstsetafter (
unsigned int size,
unsigned char * bits,
unsigned int at
) {
register unsigned int i = at >> 3;
register unsigned char c = bits[ i ];
/* first byte is special; skip over the bits
* before 'at'.
*/
c &= ( 0xFF << (at & 0x7 ));
do {
if (c) {
i <<= 3;
#define _RX(x) if (c & (1<<x)) return (i+x);
_RX(0); _RX(1); _RX(2); _RX(3);
_RX(4); _RX(5); _RX(6);
return (i+7);
#undef _RX
}
i++;
c = bits[i];
} while (i < size);
/* Fail; return bits+1. */
return size<<3;
}
/* slightly tricky bin-ops; in that it can cope with 'infinitive
* lenght type tricks.. Returns the len of the changed bitseq.
*/
/*
* exor - Exor's to bitvectors to each other, ba of length la and bb of len lb
*
* returns result in bc (should be preallocated) and length as function result
*/
unsigned int rdfstore_bits_exor (
unsigned int la, unsigned char * ba,
unsigned int lb, unsigned char * bb,
unsigned char * bc
)
{
register unsigned int len,i;
/* set in a, but not set in b
* a b -> a|b ^ b
* 0 0 0 0
* 0 1 1 0
* 1 0 1 1
* 1 1 1 0
*/
#if 0
A real EXOR does
00 -> 0
10 -> 1
01 -> 1
11 -> 0
#endif
for(len=0,i=0; (i<la) || (i<lb); i++) {
register unsigned char a = ( i>=la ) ? 0 : ba[i];
register unsigned char b = ( i>=lb ) ? 0 : bb[i];
#if 0
/* real exor */
register unsigned char c = a ^ b;
#endif
register unsigned char c = (a | b) ^ b;
if (c) len = i+1;
bc[i] = c;
};
return len;
};
/*
* or - Or's to bitvectors to each other, ba of length la and bb of length lb
*
* returns result in bc (should be preallocated) and length as function result
*/
unsigned int rdfstore_bits_or (
unsigned int la, unsigned char * ba,
unsigned int lb, unsigned char * bb,
unsigned char * bc
)
{
register unsigned int len,i;
for(len=0,i=0; (i<la) || (i<lb); i++) {
register unsigned char a = ( i>=la ) ? 0 : ba[i];
register unsigned char b = ( i>=lb ) ? 0 : bb[i];
register unsigned char c = (a | b);
if (c) len = i+1;
bc[i] = c;
};
return len;
};
/*
* and - And's to bitvectors to each other, ba of length la and bb of length lb
*
* returns result in bc (should be preallocated) and length as function result
*/
unsigned int rdfstore_bits_and (
unsigned int la, unsigned char * ba,
unsigned int lb, unsigned char * bb,
unsigned char * bc
)
{
register unsigned int len,i;
for(len=0,i=0; (i<la) && (i<lb); i++) {
register unsigned char a = ( i>=la ) ? 0 : ba[i];
register unsigned char b = ( i>=lb ) ? 0 : bb[i];
register unsigned char c = (a & b);
if (c) len = i+1;
bc[i] = c;
};
return len;
};
/*
* not - Not's a bitvector ba of length la
*
* returns result in bb (should be preallocated) and length as function result
*/
unsigned int rdfstore_bits_not (
unsigned int la, unsigned char * ba,
unsigned char * bb
)
{
register unsigned int len,i;
for(len=0,i=0; (i<la) ; i++) {
register unsigned char a = ( i>=la ) ? 0 : ba[i];
register unsigned char b = ~ a;
if (b) len = i+1;
bb[i] = b;
};
return len;
};
/*
* shorten - removes the top zero bits of the bitvector
*
* returns length of bitvector (without trailing zeroes) as bytes as function
* result
*/
unsigned int rdfstore_bits_shorten(
unsigned int la, unsigned char * ba
)
{
while( ( la >0 ) && (ba[la-1] == 0) ) la--;
return(la);
};
/* n = 6 - size of a record.
* A = row of records; at 1 bit wide.
* lenght in bytes, not bits !
* B = row of records; each 6 bits wide.
* lenght in bytes, not bits !
* M = mask of 6 bits.
* no lenght
* OUT:
* bc filled
* returns number of bytes in use.
*
*/
unsigned int rdfstore_bits_and2(
int n,
unsigned int la, unsigned char * ba,
unsigned int lb, unsigned char * bb,
unsigned char mask,
unsigned char * bc
)
{
unsigned int i = 0;
int endbit = la * 8;
assert(n <= 8); /* up to 8 bits - see q+1 below */
assert(mask < (1<<n)); /* Mask cannot be bigger than N bits */
bzero(bc,la); /* Out array of length A max */
/* If B has less records than A has bits; shorten A
*/
if (lb * 8 / n < endbit)
endbit = (lb * 8 / n) * 8;
#ifdef RDFSTORE_DEBUG_BITS
{
int i,j=0;
printf("rdfstore_bits_and2 la=%d lb=%d endbit=%d endbyte=%d\n",(int)la,(int)lb,endbit,endbit/8);
printf("rdfstore_bits_and2 ba -->'");
for(i=0;i<8*la;i++) {
printf("Rec %d %c\n", i,(ba[i>>3] & (1<<(i&7))) ? '1':'0');
};
printf("'\n");
printf("rdfstore_bits_and2 bb -->'");
for(i=0;i<8*lb;i++) {
if (i % n == 0) {
int a = 0;
if (j<8*la) a= ba[j>>3] & (1<<(j&7));
printf("Rec %d A=%d ",j,a ? '1':'0');
j++;
};
printf("%c", (bb[i>>3] & (1<<(i&7))) ? '1':'0');
if (i % n == n-1) printf("\n");
};
printf("'\n");
printf("rdfstore_bits_and2 mask -->'");
for(i=0;i<8;i++) {
printf("%c", (mask & (1<<(i&7))) ? '1':'0');
};
printf("'\n");
}
#endif
for(i=0; i < endbit ; i++) {
/* Check if bit 'i' is set or not
*/
if (ba[ i>>3 ] & (1<<(i & 7))) {
unsigned int p = n * i; /* bit number where the record starts */
unsigned int q = p >> 3; /* byte number. */
unsigned int r = p & 7; /* bit offset in the byte */
unsigned int record;
/* fetch N bits from the B. Note 8 bits max now; if we have
* records of more than 8 bits; then add q + 2.
*/
record = (((bb[ q + 1 ] << 8) + bb[ q ]) >> (r));
/* If there is one or more bits in the record set; within
* the mask; set a bit at recno in the output.
*/
if (record & mask) /* and2 */
bc[ i >> 3 ] |= (1 << ( i & 7));
};
};
#ifdef RDFSTORE_DEBUG_BITS
{
int j;
printf("rdfstore_bits_or2 bc -->'");
for(j=0;j<8*(i>>3);j++) {
printf("Rec %d %c\n", j,(bc[j>>3] & (1<<(j&7))) ? '1':'0');
};
printf("'\n");
};
#endif
/* Return the lenght in bytes, not bits */
return i >> 3;
};
/* n = 6 - size of a record.
* A = row of records; at 1 bit wide.
* lenght in bytes, not bits !
* B = row of records; each 6 bits wide.
* lenght in bytes, not bits !
* M = mask of 6 bits.
* no lenght
* OUT:
* bc filled
* returns number of bytes in use.
*
*/
unsigned int rdfstore_bits_or2(
int n,
unsigned int la, unsigned char * ba,
unsigned int lb, unsigned char * bb,
unsigned char mask,
unsigned char * bc
)
{
unsigned int i = 0;
int endbit = la * 8;
assert(n <= 8); /* up to 8 bits - see q+1 below */
assert(mask < (1<<n)); /* Mask cannot be bigger than N bits */
bzero(bc,la); /* Out array of length A max */
/* If B has less records than A has bits; shorten A
*/
if (lb * 8 / n < endbit)
endbit = (lb * 8 / n) * 8;
#ifdef RDFSTORE_DEBUG_BITS
{
int i,j=0;
printf("rdfstore_bits_or2 la=%d lb=%d endbit=%d endbyte=%d\n",(int)la,(int)lb,endbit,endbit/8);
printf("rdfstore_bits_or2 ba -->'");
for(i=0;i<8*la;i++) {
printf("Rec %d %c\n", i,(ba[i>>3] & (1<<(i&7))) ? '1':'0');
};
printf("'\n");
printf("rdfstore_bits_or2 bb -->'");
for(i=0;i<8*lb;i++) {
if (i % n == 0) {
int a = 0;
if (j<8*la) a= ba[j>>3] & (1<<(j&7));
printf("Rec %d A=%d ",j,a ? '1':'0');
j++;
};
printf("%c", (bb[i>>3] & (1<<(i&7))) ? '1':'0');
if (i % n == n-1) printf("\n");
};
printf("'\n");
printf("rdfstore_bits_or2 mask -->'");
for(i=0;i<8;i++) {
printf("%c", (mask & (1<<(i&7))) ? '1':'0');
};
printf("'\n");
}
#endif
for(i=0; i < endbit ; i++) {
unsigned int p = n * i; /* bit number where the record starts */
unsigned int q = p >> 3; /* byte number. */
unsigned int r = p & 7; /* bit offset in the byte */
unsigned int record;
/* fetch N bits from the B. Note 8 bits max now; if we have
* records of more than 8 bits; then add q + 2.
*/
record = (((bb[ q + 1 ] << 8) + bb[ q ]) >> (r));
/* If there is one or more bits in the record set; within
* the mask; set a bit at recno in the output.
*/
if ( (ba[ i>>3 ] & (1<<(i & 7))) | (record & mask) ) /* or2 */
bc[ i >> 3 ] |= (1 << ( i & 7));
};
#ifdef RDFSTORE_DEBUG_BITS
{
int j;
printf("rdfstore_bits_or2 bc -->'");
for(j=0;j<8*(i>>3);j++) {
printf("Rec %d %c\n", j,(bc[j>>3] & (1<<(j&7))) ? '1':'0');
};
printf("'\n");
};
#endif
/* Return the lenght in bytes, not bits */
return i >> 3;
};