From 25e845d1bee1bcd0353f8632ba9957a483310356 Mon Sep 17 00:00:00 2001
From: Paul Eggert <eggert@cs.ucla.edu>
Date: Thu, 5 Jun 2003 10:40:45 +0000
Subject: [PATCH 1/1] Import of 2003-06-08 libbitset
 <http://mail.gnu.org/archive/html/bison-patches/2003-06/msg00005.html>

---
 lib/bbitset.h   | 43 ++++++++++++++++++++++++++---------
 lib/libiberty.h | 60 ++++++-------------------------------------------
 2 files changed, 40 insertions(+), 63 deletions(-)

diff --git a/lib/bbitset.h b/lib/bbitset.h
index 26539f21..04bc487d 100644
--- a/lib/bbitset.h
+++ b/lib/bbitset.h
@@ -19,23 +19,30 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
 #ifndef _BBITSET_H
 #define _BBITSET_H
 
+#include "config.h"
 #include "libiberty.h"
 
 #include <stdbool.h>
 #include <limits.h>
+#include <sys/types.h>
 
-/* Currently we support three flavours of bitsets:
+/* Currently we support five flavours of bitsets:
    BITSET_ARRAY:  Array of bits (fixed size, fast for dense bitsets).
-   BITSET_LIST:   Linked list of array of bits (variable size, least storage
+                  Memory for bit array and bitset structure allocated
+                  contiguously.
+   BITSET_LIST:   Linked list of arrays of bits (variable size, least storage
 		  for large very sparse sets).
-   BITSET_TABLE:  Expandable table of pointers to array of bits
+   BITSET_TABLE:  Expandable table of pointers to arrays of bits
 		  (variable size, less storage for large sparse sets).
-
-   BITSET_STATS:  Wrapper bitset for internal use only.
+                  Faster than BITSET_LIST for random access.
+   BITSET_VARRAY: Variable array of bits (variable size, fast for 
+                  dense bitsets).
+   BITSET_STATS:  Wrapper bitset for internal use only.  Used for gathering
+                  statistics and/or better run-time checking.
 */
-enum bitset_type {BITSET_ARRAY, BITSET_LIST, BITSET_TABLE, BITSET_TYPE_NUM,
-		  BITSET_STATS};
-#define BITSET_TYPE_NAMES {"abitset", "lbitset", "ebitset"}
+enum bitset_type {BITSET_ARRAY, BITSET_LIST, BITSET_TABLE, BITSET_VARRAY,
+		  BITSET_TYPE_NUM, BITSET_STATS};
+#define BITSET_TYPE_NAMES {"abitset", "lbitset", "ebitset", "vbitset"}
 
 extern const char * const bitset_type_names[];
 
@@ -78,10 +85,11 @@ enum bitset_ops {BITSET_OP_ZERO, BITSET_OP_ONES,
 
 struct bbitset_struct
 {
-  const struct bitset_vtable * vtable;
+  const struct bitset_vtable *vtable;
   bitset_windex cindex;		/* Cache word index.  */
   bitset_windex csize;		/* Cache size in words.  */
   bitset_word *cdata;		/* Cache data pointer.  */
+  bitset_bindex n_bits;		/* Number of bits.  */
   /* Perhaps we could sacrifice another word to indicate
      that the bitset is known to be zero, that a bit has been set
      in the cache, and that a bit has been cleared in the cache.
@@ -93,6 +101,14 @@ struct bbitset_struct
 typedef union bitset_union *bitset;
 
 
+/* Private accessor macros to bitset structure.  */
+#define BITSET_VTABLE_(SRC) (SRC)->b.vtable
+#define BITSET_CINDEX_(SRC) (SRC)->b.cindex
+#define BITSET_CDATA_(SRC) (SRC)->b.cdata
+#define BITSET_CSIZE_(SRC) (SRC)->b.csize
+#define BITSET_NBITS_(SRC) (SRC)->b.n_bits
+
+
 /* The contents of this structure should be considered private.  */
 struct bitset_vtable
 {
@@ -100,6 +116,7 @@ struct bitset_vtable
   void (*reset) PARAMS ((bitset, bitset_bindex));
   bool (*toggle) PARAMS ((bitset, bitset_bindex));
   bool (*test) PARAMS ((bitset, bitset_bindex));
+  bitset_bindex (*resize) PARAMS ((bitset, bitset_bindex));
   bitset_bindex (*size) PARAMS ((bitset));
   bitset_bindex (*count) PARAMS ((bitset));
 
@@ -138,7 +155,8 @@ struct bitset_vtable
   enum bitset_type type;
 };
 
-#define BITSET_COMPATIBLE_(BSET1, BSET2) ((BSET1)->b.vtable == (BSET2)->b.vtable)
+#define BITSET_COMPATIBLE_(BSET1, BSET2) \
+((BSET1)->b.vtable == (BSET2)->b.vtable)
 
 #define BITSET_CHECK2_(DST, SRC) \
 if (!BITSET_COMPATIBLE_ (DST, SRC)) abort ();
@@ -152,6 +170,9 @@ if (!BITSET_COMPATIBLE_ (DST, SRC1) || !BITSET_COMPATIBLE_ (DST, SRC2) \
     || !BITSET_COMPATIBLE_ (DST, SRC3)) abort ();
 
 
+/* Redefine number of bits in bitset DST.  */
+#define BITSET_RESIZE_(DST, SIZE) (DST)->b.vtable->resize (DST, SIZE)
+
 /* Return size in bits of bitset SRC.  */
 #define BITSET_SIZE_(SRC) (SRC)->b.vtable->size (SRC)
 
@@ -264,6 +285,8 @@ extern bool bitset_toggle_ PARAMS ((bitset, bitset_bindex));
 
 extern bitset_bindex bitset_count_ PARAMS ((bitset));
 
+extern bitset_bindex bitset_size_ PARAMS ((bitset));
+
 extern bool bitset_copy_ PARAMS ((bitset, bitset));
 
 extern void bitset_and_or_ PARAMS ((bitset, bitset, bitset, bitset));
diff --git a/lib/libiberty.h b/lib/libiberty.h
index 6276be6d..16615ef9 100644
--- a/lib/libiberty.h
+++ b/lib/libiberty.h
@@ -1,58 +1,12 @@
-/* Fake libiberty.h for Bison.
-   Copyright (C) 2002 Free Software Foundation, Inc.
+/* Dummy libiberty header file.  */
 
-   This program is free software; you can redistribute it and/or modify
-   it under the terms of the GNU General Public License as published by
-   the Free Software Foundation; either version 2, or (at your option)
-   any later version.
+#define PARAMS(X) X
 
-   This program is distributed in the hope that it will be useful,
-   but WITHOUT ANY WARRANTY; without even the implied warranty of
-   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-   GNU General Public License for more details.
+#define ATTRIBUTE_UNUSED __attribute__ ((__unused__))
 
-   You should have received a copy of the GNU General Public License
-   along with this program; if not, write to the Free Software Foundation,
-   Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
+#define xmalloc malloc
+#define xcalloc calloc
+#define xrealloc realloc
 
+#define xalloc_die()
 
-/* Bison depends on libiberty's implementation of bitsets, which
-   requires a `libiberty.h' file.  This file provides the minimum
-   services.  */
-
-#ifndef BISON_LIBIBERTY_H_
-# define BISON_LIBIBERTY_H_ 1
-
-# if HAVE_CONFIG_H
-#  include <config.h>
-# endif
-# include <stdlib.h>
-
-/* This file is the public interface to the bitset abstract data type.
-   Only use the functions and macros defined in this file.  */
-
-# ifndef PARAMS
-#  if defined PROTOTYPES || (defined __STDC__ && __STDC__)
-#   define PARAMS(Args) Args
-#  else
-#   define PARAMS(Args) ()
-#  endif
-# endif
-
-# ifndef __attribute__
-#  if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 8) || __STRICT_ANSI__
-#   define __attribute__(x)
-#  endif
-# endif
-
-# ifndef ATTRIBUTE_NORETURN
-#  define ATTRIBUTE_NORETURN __attribute__ ((__noreturn__))
-# endif
-
-# ifndef ATTRIBUTE_UNUSED
-#  define ATTRIBUTE_UNUSED __attribute__ ((__unused__))
-# endif
-
-# include "xalloc.h"
-
-#endif /* ! BISON_LIBIBERTY_H_ */
-- 
2.45.2