105436638acc7c010349a69c3395f1a57c642dc62Ying Wang/* A simple, memory-efficient bitset implementation.
205436638acc7c010349a69c3395f1a57c642dc62Ying Wang
305436638acc7c010349a69c3395f1a57c642dc62Ying Wang   Copyright (C) 2009-2012 Free Software Foundation, Inc.
405436638acc7c010349a69c3395f1a57c642dc62Ying Wang
505436638acc7c010349a69c3395f1a57c642dc62Ying Wang   This file is part of Bison, the GNU Compiler Compiler.
605436638acc7c010349a69c3395f1a57c642dc62Ying Wang
705436638acc7c010349a69c3395f1a57c642dc62Ying Wang   This program is free software: you can redistribute it and/or modify
805436638acc7c010349a69c3395f1a57c642dc62Ying Wang   it under the terms of the GNU General Public License as published by
905436638acc7c010349a69c3395f1a57c642dc62Ying Wang   the Free Software Foundation, either version 3 of the License, or
1005436638acc7c010349a69c3395f1a57c642dc62Ying Wang   (at your option) any later version.
1105436638acc7c010349a69c3395f1a57c642dc62Ying Wang
1205436638acc7c010349a69c3395f1a57c642dc62Ying Wang   This program is distributed in the hope that it will be useful,
1305436638acc7c010349a69c3395f1a57c642dc62Ying Wang   but WITHOUT ANY WARRANTY; without even the implied warranty of
1405436638acc7c010349a69c3395f1a57c642dc62Ying Wang   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1505436638acc7c010349a69c3395f1a57c642dc62Ying Wang   GNU General Public License for more details.
1605436638acc7c010349a69c3395f1a57c642dc62Ying Wang
1705436638acc7c010349a69c3395f1a57c642dc62Ying Wang   You should have received a copy of the GNU General Public License
1805436638acc7c010349a69c3395f1a57c642dc62Ying Wang   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
1905436638acc7c010349a69c3395f1a57c642dc62Ying Wang
2005436638acc7c010349a69c3395f1a57c642dc62Ying Wang#ifndef SBITSET_H_
2105436638acc7c010349a69c3395f1a57c642dc62Ying Wang# define SBITSET_H_
2205436638acc7c010349a69c3395f1a57c642dc62Ying Wang
2305436638acc7c010349a69c3395f1a57c642dc62Ying Wangtypedef unsigned char *Sbitset;
2405436638acc7c010349a69c3395f1a57c642dc62Ying Wangtypedef size_t Sbitset__Index;
2505436638acc7c010349a69c3395f1a57c642dc62Ying Wang#define SBITSET__INDEX__CONVERSION_SPEC "zu"
2605436638acc7c010349a69c3395f1a57c642dc62Ying Wang
2705436638acc7c010349a69c3395f1a57c642dc62Ying Wang#define Sbitset__nbytes(NBITS) \
2805436638acc7c010349a69c3395f1a57c642dc62Ying Wang  (((NBITS) + CHAR_BIT - 1) / CHAR_BIT)
2905436638acc7c010349a69c3395f1a57c642dc62Ying Wang#define Sbitset__byteAddress(SELF, INDEX) \
3005436638acc7c010349a69c3395f1a57c642dc62Ying Wang  (((SELF) + (INDEX) / CHAR_BIT))
3105436638acc7c010349a69c3395f1a57c642dc62Ying Wang#define Sbitset__bit_mask(INDEX) \
3205436638acc7c010349a69c3395f1a57c642dc62Ying Wang  (1 << (CHAR_BIT - 1 - (INDEX) % CHAR_BIT))
3305436638acc7c010349a69c3395f1a57c642dc62Ying Wang#define Sbitset__last_byte_mask(NBITS) \
3405436638acc7c010349a69c3395f1a57c642dc62Ying Wang  (UCHAR_MAX << (CHAR_BIT - 1 - ((NBITS) - 1) % CHAR_BIT))
3505436638acc7c010349a69c3395f1a57c642dc62Ying Wang
3605436638acc7c010349a69c3395f1a57c642dc62Ying Wang/* nbits must not be 0.  */
3705436638acc7c010349a69c3395f1a57c642dc62Ying WangSbitset Sbitset__new (Sbitset__Index nbits);
3805436638acc7c010349a69c3395f1a57c642dc62Ying WangSbitset Sbitset__new_on_obstack (Sbitset__Index nbits,
3905436638acc7c010349a69c3395f1a57c642dc62Ying Wang                                 struct obstack *obstackp);
4005436638acc7c010349a69c3395f1a57c642dc62Ying Wangvoid Sbitset__delete (Sbitset self);
4105436638acc7c010349a69c3395f1a57c642dc62Ying Wang
4205436638acc7c010349a69c3395f1a57c642dc62Ying Wang#define Sbitset__test(SELF, INDEX)                                            \
4305436638acc7c010349a69c3395f1a57c642dc62Ying Wang  ((*Sbitset__byteAddress ((SELF), (INDEX)) & Sbitset__bit_mask (INDEX)) != 0)
4405436638acc7c010349a69c3395f1a57c642dc62Ying Wang
4505436638acc7c010349a69c3395f1a57c642dc62Ying Wangbool Sbitset__isEmpty (Sbitset self, Sbitset__Index nbits);
4605436638acc7c010349a69c3395f1a57c642dc62Ying Wang
4705436638acc7c010349a69c3395f1a57c642dc62Ying Wangvoid Sbitset__fprint(Sbitset self, Sbitset__Index nbits, FILE *file);
4805436638acc7c010349a69c3395f1a57c642dc62Ying Wang
4905436638acc7c010349a69c3395f1a57c642dc62Ying Wang#define Sbitset__set(SELF, INDEX)                                             \
5005436638acc7c010349a69c3395f1a57c642dc62Ying Wangdo {                                                                          \
5105436638acc7c010349a69c3395f1a57c642dc62Ying Wang  *Sbitset__byteAddress ((SELF), (INDEX)) =                                   \
5205436638acc7c010349a69c3395f1a57c642dc62Ying Wang    *Sbitset__byteAddress ((SELF), (INDEX)) | Sbitset__bit_mask (INDEX);      \
5305436638acc7c010349a69c3395f1a57c642dc62Ying Wang} while(0)
5405436638acc7c010349a69c3395f1a57c642dc62Ying Wang
5505436638acc7c010349a69c3395f1a57c642dc62Ying Wang#define Sbitset__reset(SELF, INDEX)                                           \
5605436638acc7c010349a69c3395f1a57c642dc62Ying Wangdo {                                                                          \
5705436638acc7c010349a69c3395f1a57c642dc62Ying Wang  *Sbitset__byteAddress ((SELF), (INDEX)) =                                   \
5805436638acc7c010349a69c3395f1a57c642dc62Ying Wang    *Sbitset__byteAddress ((SELF), (INDEX)) & ~Sbitset__bit_mask (INDEX);     \
5905436638acc7c010349a69c3395f1a57c642dc62Ying Wang} while(0)
6005436638acc7c010349a69c3395f1a57c642dc62Ying Wang
6105436638acc7c010349a69c3395f1a57c642dc62Ying Wang/* NBITS is the size of the bitset.  More than NBITS bits might be reset.  */
6205436638acc7c010349a69c3395f1a57c642dc62Ying Wang#define Sbitset__zero(SELF, NBITS)                                            \
6305436638acc7c010349a69c3395f1a57c642dc62Ying Wangdo {                                                                          \
6405436638acc7c010349a69c3395f1a57c642dc62Ying Wang  memset (SELF, 0, Sbitset__nbytes (NBITS));                                  \
6505436638acc7c010349a69c3395f1a57c642dc62Ying Wang} while(0)
6605436638acc7c010349a69c3395f1a57c642dc62Ying Wang
6705436638acc7c010349a69c3395f1a57c642dc62Ying Wang/* NBITS is the size of the bitset.  More than NBITS bits might be set.  */
6805436638acc7c010349a69c3395f1a57c642dc62Ying Wang#define Sbitset__ones(SELF, NBITS)                                            \
6905436638acc7c010349a69c3395f1a57c642dc62Ying Wangdo {                                                                          \
7005436638acc7c010349a69c3395f1a57c642dc62Ying Wang  memset (SELF, UCHAR_MAX, Sbitset__nbytes (NBITS));                          \
7105436638acc7c010349a69c3395f1a57c642dc62Ying Wang} while(0)
7205436638acc7c010349a69c3395f1a57c642dc62Ying Wang
7305436638acc7c010349a69c3395f1a57c642dc62Ying Wang/* NBITS is the size of every bitset.  More than NBITS bits might be set.  */
7405436638acc7c010349a69c3395f1a57c642dc62Ying Wang#define Sbitset__or(SELF, OTHER1, OTHER2, NBITS)                              \
7505436638acc7c010349a69c3395f1a57c642dc62Ying Wangdo {                                                                          \
7605436638acc7c010349a69c3395f1a57c642dc62Ying Wang  Sbitset ptr_self = (SELF);                                                  \
7705436638acc7c010349a69c3395f1a57c642dc62Ying Wang  Sbitset ptr_other1 = (OTHER1);                                              \
7805436638acc7c010349a69c3395f1a57c642dc62Ying Wang  Sbitset ptr_other2 = (OTHER2);                                              \
7905436638acc7c010349a69c3395f1a57c642dc62Ying Wang  Sbitset end_self = ptr_self + Sbitset__nbytes (NBITS);                      \
8005436638acc7c010349a69c3395f1a57c642dc62Ying Wang  for (; ptr_self < end_self; ++ptr_self, ++ptr_other1, ++ptr_other2)         \
8105436638acc7c010349a69c3395f1a57c642dc62Ying Wang    *ptr_self = *ptr_other1 | *ptr_other2;                                    \
8205436638acc7c010349a69c3395f1a57c642dc62Ying Wang} while(0)
8305436638acc7c010349a69c3395f1a57c642dc62Ying Wang
8405436638acc7c010349a69c3395f1a57c642dc62Ying Wang#define SBITSET__FOR_EACH(SELF, NBITS, ITER, INDEX)                           \
8505436638acc7c010349a69c3395f1a57c642dc62Ying Wang  for ((ITER) = (SELF); (ITER) < (SELF) + Sbitset__nbytes (NBITS); ++(ITER))  \
8605436638acc7c010349a69c3395f1a57c642dc62Ying Wang    if (*(ITER) != 0)                                                         \
8705436638acc7c010349a69c3395f1a57c642dc62Ying Wang      for ((INDEX) = ((ITER)-(SELF))*CHAR_BIT;                                \
8805436638acc7c010349a69c3395f1a57c642dc62Ying Wang           (INDEX) < (NBITS) && (SELF)+(INDEX)/CHAR_BIT < (ITER)+1;           \
8905436638acc7c010349a69c3395f1a57c642dc62Ying Wang           ++(INDEX))                                                         \
9005436638acc7c010349a69c3395f1a57c642dc62Ying Wang        if (((*ITER) & Sbitset__bit_mask (INDEX)) != 0)
9105436638acc7c010349a69c3395f1a57c642dc62Ying Wang
9205436638acc7c010349a69c3395f1a57c642dc62Ying Wang#endif /* !SBITSET_H_ */
93