1 | n/a | |
---|
2 | n/a | /* Bitset primitives used by the parser generator */ |
---|
3 | n/a | |
---|
4 | n/a | #include "pgenheaders.h" |
---|
5 | n/a | #include "bitset.h" |
---|
6 | n/a | |
---|
7 | n/a | bitset |
---|
8 | n/a | newbitset(int nbits) |
---|
9 | n/a | { |
---|
10 | n/a | int nbytes = NBYTES(nbits); |
---|
11 | n/a | bitset ss = (char *)PyObject_MALLOC(sizeof(BYTE) * nbytes); |
---|
12 | n/a | |
---|
13 | n/a | if (ss == NULL) |
---|
14 | n/a | Py_FatalError("no mem for bitset"); |
---|
15 | n/a | |
---|
16 | n/a | ss += nbytes; |
---|
17 | n/a | while (--nbytes >= 0) |
---|
18 | n/a | *--ss = 0; |
---|
19 | n/a | return ss; |
---|
20 | n/a | } |
---|
21 | n/a | |
---|
22 | n/a | void |
---|
23 | n/a | delbitset(bitset ss) |
---|
24 | n/a | { |
---|
25 | n/a | PyObject_FREE(ss); |
---|
26 | n/a | } |
---|
27 | n/a | |
---|
28 | n/a | int |
---|
29 | n/a | addbit(bitset ss, int ibit) |
---|
30 | n/a | { |
---|
31 | n/a | int ibyte = BIT2BYTE(ibit); |
---|
32 | n/a | BYTE mask = BIT2MASK(ibit); |
---|
33 | n/a | |
---|
34 | n/a | if (ss[ibyte] & mask) |
---|
35 | n/a | return 0; /* Bit already set */ |
---|
36 | n/a | ss[ibyte] |= mask; |
---|
37 | n/a | return 1; |
---|
38 | n/a | } |
---|
39 | n/a | |
---|
40 | n/a | #if 0 /* Now a macro */ |
---|
41 | n/a | int |
---|
42 | n/a | testbit(bitset ss, int ibit) |
---|
43 | n/a | { |
---|
44 | n/a | return (ss[BIT2BYTE(ibit)] & BIT2MASK(ibit)) != 0; |
---|
45 | n/a | } |
---|
46 | n/a | #endif |
---|
47 | n/a | |
---|
48 | n/a | int |
---|
49 | n/a | samebitset(bitset ss1, bitset ss2, int nbits) |
---|
50 | n/a | { |
---|
51 | n/a | int i; |
---|
52 | n/a | |
---|
53 | n/a | for (i = NBYTES(nbits); --i >= 0; ) |
---|
54 | n/a | if (*ss1++ != *ss2++) |
---|
55 | n/a | return 0; |
---|
56 | n/a | return 1; |
---|
57 | n/a | } |
---|
58 | n/a | |
---|
59 | n/a | void |
---|
60 | n/a | mergebitset(bitset ss1, bitset ss2, int nbits) |
---|
61 | n/a | { |
---|
62 | n/a | int i; |
---|
63 | n/a | |
---|
64 | n/a | for (i = NBYTES(nbits); --i >= 0; ) |
---|
65 | n/a | *ss1++ |= *ss2++; |
---|
66 | n/a | } |
---|