Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 1 | /*********************************************************** |
| 2 | Copyright 1991 by Stichting Mathematisch Centrum, Amsterdam, The |
| 3 | Netherlands. |
| 4 | |
| 5 | All Rights Reserved |
| 6 | |
| 7 | Permission to use, copy, modify, and distribute this software and its |
| 8 | documentation for any purpose and without fee is hereby granted, |
| 9 | provided that the above copyright notice appear in all copies and that |
| 10 | both that copyright notice and this permission notice appear in |
| 11 | supporting documentation, and that the names of Stichting Mathematisch |
| 12 | Centrum or CWI not be used in advertising or publicity pertaining to |
| 13 | distribution of the software without specific, written prior permission. |
| 14 | |
| 15 | STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO |
| 16 | THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND |
| 17 | FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE |
| 18 | FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 19 | WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 20 | ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT |
| 21 | OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 22 | |
| 23 | ******************************************************************/ |
| 24 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 25 | /* Bitset primitives used by the parser generator */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 26 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 27 | #include "pgenheaders.h" |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 28 | #include "bitset.h" |
| 29 | |
| 30 | bitset |
| 31 | newbitset(nbits) |
| 32 | int nbits; |
| 33 | { |
| 34 | int nbytes = NBYTES(nbits); |
| 35 | bitset ss = NEW(BYTE, nbytes); |
| 36 | |
| 37 | if (ss == NULL) |
| 38 | fatal("no mem for bitset"); |
| 39 | |
| 40 | ss += nbytes; |
| 41 | while (--nbytes >= 0) |
| 42 | *--ss = 0; |
| 43 | return ss; |
| 44 | } |
| 45 | |
| 46 | void |
| 47 | delbitset(ss) |
| 48 | bitset ss; |
| 49 | { |
| 50 | DEL(ss); |
| 51 | } |
| 52 | |
| 53 | int |
| 54 | addbit(ss, ibit) |
| 55 | bitset ss; |
| 56 | int ibit; |
| 57 | { |
| 58 | int ibyte = BIT2BYTE(ibit); |
| 59 | BYTE mask = BIT2MASK(ibit); |
| 60 | |
| 61 | if (ss[ibyte] & mask) |
| 62 | return 0; /* Bit already set */ |
| 63 | ss[ibyte] |= mask; |
| 64 | return 1; |
| 65 | } |
| 66 | |
| 67 | #if 0 /* Now a macro */ |
| 68 | int |
| 69 | testbit(ss, ibit) |
| 70 | bitset ss; |
| 71 | int ibit; |
| 72 | { |
| 73 | return (ss[BIT2BYTE(ibit)] & BIT2MASK(ibit)) != 0; |
| 74 | } |
| 75 | #endif |
| 76 | |
| 77 | int |
| 78 | samebitset(ss1, ss2, nbits) |
| 79 | bitset ss1, ss2; |
| 80 | int nbits; |
| 81 | { |
| 82 | int i; |
| 83 | |
| 84 | for (i = NBYTES(nbits); --i >= 0; ) |
| 85 | if (*ss1++ != *ss2++) |
| 86 | return 0; |
| 87 | return 1; |
| 88 | } |
| 89 | |
| 90 | void |
| 91 | mergebitset(ss1, ss2, nbits) |
| 92 | bitset ss1, ss2; |
| 93 | int nbits; |
| 94 | { |
| 95 | int i; |
| 96 | |
| 97 | for (i = NBYTES(nbits); --i >= 0; ) |
| 98 | *ss1++ |= *ss2++; |
| 99 | } |