Python-2.7.3/Parser/bitset.c

No issues found

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