home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / pyth_os2.zip / python-1.0.2 / Parser / bitset.c < prev    next >
C/C++ Source or Header  |  1994-01-02  |  2KB  |  100 lines

  1. /***********************************************************
  2. Copyright 1991, 1992, 1993, 1994 by Stichting Mathematisch Centrum,
  3. Amsterdam, The 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.  
  25. /* Bitset primitives used by the parser generator */
  26.  
  27. #include "pgenheaders.h"
  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. }
  100.