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