blob: b5543b81d3a5df6a50842643d2db54688e52af1d [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001
Guido van Rossum3f5da241990-12-20 15:06:42 +00002/* Bitset primitives used by the parser generator */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003
Guido van Rossum3f5da241990-12-20 15:06:42 +00004#include "pgenheaders.h"
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00005#include "bitset.h"
6
7bitset
Thomas Wouters23c9e002000-07-22 19:20:54 +00008newbitset(int nbits)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00009{
10 int nbytes = NBYTES(nbits);
Anthony Baxter11490022006-04-11 05:39:14 +000011 bitset ss = (char *)PyObject_MALLOC(sizeof(BYTE) * nbytes);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000012
13 if (ss == NULL)
Guido van Rossum86bea461997-04-29 21:03:06 +000014 Py_FatalError("no mem for bitset");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000015
16 ss += nbytes;
17 while (--nbytes >= 0)
18 *--ss = 0;
19 return ss;
20}
21
22void
Thomas Wouters23c9e002000-07-22 19:20:54 +000023delbitset(bitset ss)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000024{
Neal Norwitz2c4e4f92006-04-10 06:42:25 +000025 PyObject_FREE(ss);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000026}
27
28int
Thomas Wouters23c9e002000-07-22 19:20:54 +000029addbit(bitset ss, int ibit)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000030{
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;
38}
39
40#if 0 /* Now a macro */
41int
Thomas Wouters23c9e002000-07-22 19:20:54 +000042testbit(bitset ss, int ibit)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000043{
44 return (ss[BIT2BYTE(ibit)] & BIT2MASK(ibit)) != 0;
45}
46#endif
47
48int
Thomas Wouters23c9e002000-07-22 19:20:54 +000049samebitset(bitset ss1, bitset ss2, int nbits)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000050{
51 int i;
52
53 for (i = NBYTES(nbits); --i >= 0; )
54 if (*ss1++ != *ss2++)
55 return 0;
56 return 1;
57}
58
59void
Thomas Wouters23c9e002000-07-22 19:20:54 +000060mergebitset(bitset ss1, bitset ss2, int nbits)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000061{
62 int i;
63
64 for (i = NBYTES(nbits); --i >= 0; )
65 *ss1++ |= *ss2++;
66}