Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 1 | /*********************************************************** |
Guido van Rossum | fd71b9e | 2000-06-30 23:50:40 +0000 | [diff] [blame] | 2 | Copyright (c) 2000, BeOpen.com. |
| 3 | Copyright (c) 1995-2000, Corporation for National Research Initiatives. |
| 4 | Copyright (c) 1990-1995, Stichting Mathematisch Centrum. |
| 5 | All rights reserved. |
Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 6 | |
Guido van Rossum | fd71b9e | 2000-06-30 23:50:40 +0000 | [diff] [blame] | 7 | See the file "Misc/COPYRIGHT" for information on usage and |
| 8 | redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES. |
Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 9 | ******************************************************************/ |
| 10 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 11 | /* Bitset primitives used by the parser generator */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 12 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 13 | #include "pgenheaders.h" |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 14 | #include "bitset.h" |
| 15 | |
| 16 | bitset |
Thomas Wouters | 23c9e00 | 2000-07-22 19:20:54 +0000 | [diff] [blame] | 17 | newbitset(int nbits) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 18 | { |
| 19 | int nbytes = NBYTES(nbits); |
Guido van Rossum | 86bea46 | 1997-04-29 21:03:06 +0000 | [diff] [blame] | 20 | bitset ss = PyMem_NEW(BYTE, nbytes); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 21 | |
| 22 | if (ss == NULL) |
Guido van Rossum | 86bea46 | 1997-04-29 21:03:06 +0000 | [diff] [blame] | 23 | Py_FatalError("no mem for bitset"); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 24 | |
| 25 | ss += nbytes; |
| 26 | while (--nbytes >= 0) |
| 27 | *--ss = 0; |
| 28 | return ss; |
| 29 | } |
| 30 | |
| 31 | void |
Thomas Wouters | 23c9e00 | 2000-07-22 19:20:54 +0000 | [diff] [blame] | 32 | delbitset(bitset ss) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 33 | { |
Guido van Rossum | 86bea46 | 1997-04-29 21:03:06 +0000 | [diff] [blame] | 34 | PyMem_DEL(ss); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 35 | } |
| 36 | |
| 37 | int |
Thomas Wouters | 23c9e00 | 2000-07-22 19:20:54 +0000 | [diff] [blame] | 38 | addbit(bitset ss, int ibit) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 39 | { |
| 40 | int ibyte = BIT2BYTE(ibit); |
| 41 | BYTE mask = BIT2MASK(ibit); |
| 42 | |
| 43 | if (ss[ibyte] & mask) |
| 44 | return 0; /* Bit already set */ |
| 45 | ss[ibyte] |= mask; |
| 46 | return 1; |
| 47 | } |
| 48 | |
| 49 | #if 0 /* Now a macro */ |
| 50 | int |
Thomas Wouters | 23c9e00 | 2000-07-22 19:20:54 +0000 | [diff] [blame] | 51 | testbit(bitset ss, int ibit) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 52 | { |
| 53 | return (ss[BIT2BYTE(ibit)] & BIT2MASK(ibit)) != 0; |
| 54 | } |
| 55 | #endif |
| 56 | |
| 57 | int |
Thomas Wouters | 23c9e00 | 2000-07-22 19:20:54 +0000 | [diff] [blame] | 58 | samebitset(bitset ss1, bitset ss2, int nbits) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 59 | { |
| 60 | int i; |
| 61 | |
| 62 | for (i = NBYTES(nbits); --i >= 0; ) |
| 63 | if (*ss1++ != *ss2++) |
| 64 | return 0; |
| 65 | return 1; |
| 66 | } |
| 67 | |
| 68 | void |
Thomas Wouters | 23c9e00 | 2000-07-22 19:20:54 +0000 | [diff] [blame] | 69 | mergebitset(bitset ss1, bitset ss2, int nbits) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 70 | { |
| 71 | int i; |
| 72 | |
| 73 | for (i = NBYTES(nbits); --i >= 0; ) |
| 74 | *ss1++ |= *ss2++; |
| 75 | } |