bitset.c revision b9f8d6e54d72d108648a411174e57779c212871a
1/***********************************************************
2Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
3The Netherlands.
4
5                        All Rights Reserved
6
7Permission to use, copy, modify, and distribute this software and its
8documentation for any purpose and without fee is hereby granted,
9provided that the above copyright notice appear in all copies and that
10both that copyright notice and this permission notice appear in
11supporting documentation, and that the names of Stichting Mathematisch
12Centrum or CWI not be used in advertising or publicity pertaining to
13distribution of the software without specific, written prior permission.
14
15STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO
16THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
17FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE
18FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
19WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
20ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
21OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22
23******************************************************************/
24
25/* Bitset primitives used by the parser generator */
26
27#include "pgenheaders.h"
28#include "bitset.h"
29
30bitset
31newbitset(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
46void
47delbitset(ss)
48	bitset ss;
49{
50	DEL(ss);
51}
52
53int
54addbit(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 */
68int
69testbit(ss, ibit)
70	bitset ss;
71	int ibit;
72{
73	return (ss[BIT2BYTE(ibit)] & BIT2MASK(ibit)) != 0;
74}
75#endif
76
77int
78samebitset(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
90void
91mergebitset(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}
100