1f70e43a073b36c6f6e9894c01025243a77a452d4Guido van Rossum
23f5da24ea304e674a9abbdcffc4d671e32aa70f1Guido van Rossum/* Bitset primitives used by the parser generator */
385a5fbbdfea617f6cc8fae82c9e8c2b5c424436dGuido van Rossum
43f5da24ea304e674a9abbdcffc4d671e32aa70f1Guido van Rossum#include "pgenheaders.h"
585a5fbbdfea617f6cc8fae82c9e8c2b5c424436dGuido van Rossum#include "bitset.h"
685a5fbbdfea617f6cc8fae82c9e8c2b5c424436dGuido van Rossum
785a5fbbdfea617f6cc8fae82c9e8c2b5c424436dGuido van Rossumbitset
823c9e0024af99379ae517b016b874d57127e9a97Thomas Woutersnewbitset(int nbits)
985a5fbbdfea617f6cc8fae82c9e8c2b5c424436dGuido van Rossum{
10c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou    int nbytes = NBYTES(nbits);
11c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou    bitset ss = (char *)PyObject_MALLOC(sizeof(BYTE) *  nbytes);
12c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou
13c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou    if (ss == NULL)
14c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou        Py_FatalError("no mem for bitset");
15c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou
16c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou    ss += nbytes;
17c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou    while (--nbytes >= 0)
18c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou        *--ss = 0;
19c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou    return ss;
2085a5fbbdfea617f6cc8fae82c9e8c2b5c424436dGuido van Rossum}
2185a5fbbdfea617f6cc8fae82c9e8c2b5c424436dGuido van Rossum
2285a5fbbdfea617f6cc8fae82c9e8c2b5c424436dGuido van Rossumvoid
2323c9e0024af99379ae517b016b874d57127e9a97Thomas Woutersdelbitset(bitset ss)
2485a5fbbdfea617f6cc8fae82c9e8c2b5c424436dGuido van Rossum{
25c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou    PyObject_FREE(ss);
2685a5fbbdfea617f6cc8fae82c9e8c2b5c424436dGuido van Rossum}
2785a5fbbdfea617f6cc8fae82c9e8c2b5c424436dGuido van Rossum
2885a5fbbdfea617f6cc8fae82c9e8c2b5c424436dGuido van Rossumint
2923c9e0024af99379ae517b016b874d57127e9a97Thomas Woutersaddbit(bitset ss, int ibit)
3085a5fbbdfea617f6cc8fae82c9e8c2b5c424436dGuido van Rossum{
31c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou    int ibyte = BIT2BYTE(ibit);
32c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou    BYTE mask = BIT2MASK(ibit);
33c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou
34c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou    if (ss[ibyte] & mask)
35c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou        return 0; /* Bit already set */
36c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou    ss[ibyte] |= mask;
37c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou    return 1;
3885a5fbbdfea617f6cc8fae82c9e8c2b5c424436dGuido van Rossum}
3985a5fbbdfea617f6cc8fae82c9e8c2b5c424436dGuido van Rossum
4085a5fbbdfea617f6cc8fae82c9e8c2b5c424436dGuido van Rossum#if 0 /* Now a macro */
4185a5fbbdfea617f6cc8fae82c9e8c2b5c424436dGuido van Rossumint
4223c9e0024af99379ae517b016b874d57127e9a97Thomas Wouterstestbit(bitset ss, int ibit)
4385a5fbbdfea617f6cc8fae82c9e8c2b5c424436dGuido van Rossum{
44c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou    return (ss[BIT2BYTE(ibit)] & BIT2MASK(ibit)) != 0;
4585a5fbbdfea617f6cc8fae82c9e8c2b5c424436dGuido van Rossum}
4685a5fbbdfea617f6cc8fae82c9e8c2b5c424436dGuido van Rossum#endif
4785a5fbbdfea617f6cc8fae82c9e8c2b5c424436dGuido van Rossum
4885a5fbbdfea617f6cc8fae82c9e8c2b5c424436dGuido van Rossumint
4923c9e0024af99379ae517b016b874d57127e9a97Thomas Wouterssamebitset(bitset ss1, bitset ss2, int nbits)
5085a5fbbdfea617f6cc8fae82c9e8c2b5c424436dGuido van Rossum{
51c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou    int i;
52c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou
53c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou    for (i = NBYTES(nbits); --i >= 0; )
54c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou        if (*ss1++ != *ss2++)
55c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou            return 0;
56c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou    return 1;
5785a5fbbdfea617f6cc8fae82c9e8c2b5c424436dGuido van Rossum}
5885a5fbbdfea617f6cc8fae82c9e8c2b5c424436dGuido van Rossum
5985a5fbbdfea617f6cc8fae82c9e8c2b5c424436dGuido van Rossumvoid
6023c9e0024af99379ae517b016b874d57127e9a97Thomas Woutersmergebitset(bitset ss1, bitset ss2, int nbits)
6185a5fbbdfea617f6cc8fae82c9e8c2b5c424436dGuido van Rossum{
62c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou    int i;
63c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou
64c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou    for (i = NBYTES(nbits); --i >= 0; )
65c83ea137d7e717f764e2f31fc2544f522de7d857Antoine Pitrou        *ss1++ |= *ss2++;
6685a5fbbdfea617f6cc8fae82c9e8c2b5c424436dGuido van Rossum}
67