cc-hash.c (fhash): The FILE name may be null.
[u/mdw/catacomb] / mpscan.h
index 6ec09cc..0d02519 100644 (file)
--- a/mpscan.h
+++ b/mpscan.h
@@ -1,13 +1,13 @@
 /* -*-c-*-
  *
- * $Id: mpscan.h,v 1.1 1999/09/03 08:41:12 mdw Exp $
+ * $Id: mpscan.h,v 1.5 2004/04/08 01:36:15 mdw Exp $
  *
  * Sequential bit scan of multiprecision integers
  *
  * (c) 1999 Straylight/Edgeware
  */
 
-/*----- Licensing notice --------------------------------------------------* 
+/*----- Licensing notice --------------------------------------------------*
  *
  * This file is part of Catacomb.
  *
  * it under the terms of the GNU Library General Public License as
  * published by the Free Software Foundation; either version 2 of the
  * License, or (at your option) any later version.
- * 
+ *
  * Catacomb 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 Library General Public License for more details.
- * 
+ *
  * You should have received a copy of the GNU Library General Public
  * License along with Catacomb; if not, write to the Free
  * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
  * MA 02111-1307, USA.
  */
 
-/*----- Revision history --------------------------------------------------* 
- *
- * $Log: mpscan.h,v $
- * Revision 1.1  1999/09/03 08:41:12  mdw
- * Initial import.
- *
- */
-
-#ifndef MPSCAN_H
-#define MPSCAN_H
+#ifndef CATACOMB_MPSCAN_H
+#define CATACOMB_MPSCAN_H
 
 #ifdef __cplusplus
   extern "C" {
 
 /*----- Header files ------------------------------------------------------*/
 
-#ifndef MPTYPES_H
-#  include "mptypes.h"
+#ifndef CATACOMB_MPW_H
+#  include "mpw.h"
 #endif
 
 /*----- Data structures ---------------------------------------------------*/
 
 typedef struct mpscan {
-  const mpw *v;                                /* Vector of words to scan */
+  const mpw *v, *vl;                   /* Vector of words to scan */
   mpw w;                               /* Current word to scan */
   int bits;                            /* Number of bits left in @w@ */
-  size_t len;                          /* Length of the vector in words */
 } mpscan;
 
-/*----- Functions provided ------------------------------------------------*/
+/*----- Right-to-left scanning --------------------------------------------*/
 
 /* --- @mpscan_initx@ --- *
  *
  * Arguments:  @mpscan *m@ = pointer to bitscanner structure
- *             @const mpw *v@ = vector of words to scan
- *             @size_t len@ = length of vector in words
+ *             @const mpw *v, *vl@ = vector of words to scan
  *
  * Returns:    ---
  *
- * Use:                Initializes a bitscanner from a low-level vector-and-length
+ * Use:                Initializes a bitscanner from a low-level base-and-limit
  *             representation of an integer.  Initially no bit is ready; you
  *             must call @mpscan_step@ before anything useful will come
  *             out.
  */
 
-#define MPSCAN_INITX(m_, v_, len_) do {                                        \
+#define MPSCAN_INITX(m_, v_, vl_) do {                                 \
   mpscan *_m = (m_);                                                   \
   _m->v = (v_);                                                                \
-  _m->len = (len_);                                                    \
+  _m->vl = (vl_);                                                      \
   _m->bits = 0;                                                                \
 } while (0)
 
-extern void mpscan_initx(mpscan */*m*/, const mpw */*v*/, size_t /*len*/);
+extern void mpscan_initx(mpscan */*m*/, const mpw */*v*/, const mpw */*vl*/);
 
 /* --- @mpscan_step@ --- *
  *
@@ -93,11 +83,9 @@ extern void mpscan_initx(mpscan */*m*/, const mpw */*v*/, size_t /*len*/);
  */
 
 #define MPSCAN_STEP(m)                                                 \
-  ((m)->bits ? ((m)->w >>= 1,                                          \
-               (m)->bits--, 1) :                                       \
-   (m)->len ? ((m)->len--,                                             \
-              (m)->w = *(m)->v++,                                      \
-              (m)->bits = MP_WBITS - 1, 1) :                           \
+  ((m)->bits ? ((m)->w >>= 1, (m)->bits--, 1) :                                \
+   (m)->v < (m)->vl ? ((m)->w = *(m)->v++,                             \
+                      (m)->bits = MPW_BITS - 1, 1) :                   \
    0)
 
 extern int mpscan_step(mpscan */*m*/);
@@ -116,6 +104,65 @@ extern int mpscan_step(mpscan */*m*/);
 
 extern int mpscan_bit(const mpscan */*m*/);
 
+/*----- Left-to right-scanning --------------------------------------------*/
+
+/* --- @mpscan_rinitx@ --- *
+ *
+ * Arguments:  @mpscan *m@ = pointer to bitscanner structure
+ *             @const mpw *v, *vl@ = vector of words to scan
+ *
+ * Returns:    ---
+ *
+ * Use:                Initializes a reverse bitscanner from a low-level
+ *             vector-and-length representation of an integer.  Initially no
+ *             bit is ready; you must call @mpscan_rstep@ before anything
+ *             useful will come out.
+ */
+
+#define MPSCAN_RINITX(m_, v_, vl_) do {                                        \
+  mpscan *_m = (m_);                                                   \
+  _m->v = (v_);                                                                \
+  _m->vl = (vl_);                                                      \
+  while (_m->vl > _m->v && !_m->vl[-1])                                        \
+    _m->vl--;                                                          \
+  _m->bits = 0;                                                                \
+} while (0)
+
+extern void mpscan_rinitx(mpscan */*m*/,
+                         const mpw */*v*/, const mpw */*vl*/);
+
+/* --- @mpscan_rstep@ --- *
+ *
+ * Arguments:  @mpscan *m@ = pointer to bitscanner
+ *
+ * Returns:    Nonzero if there is another bit to read.
+ *
+ * Use:                Steps on to the next bit in the integer.  The macro version
+ *             evaluates its argument multiple times.
+ */
+
+#define MPSCAN_RSTEP(m)                                                        \
+  ((m)->bits ? ((m)->w <<= 1, (m)->bits--, 1) :                                \
+   (m)->vl > (m)->v ? ((m)->w = *--(m)->vl,                            \
+                      (m)->bits = MPW_BITS - 1, 1) :                   \
+   0)
+
+extern int mpscan_rstep(mpscan */*m*/);
+
+/* --- @mpscan_rbit@ --- *
+ *
+ * Arguments:  @const mpscan *m@ = pointer to bitscanner
+ *
+ * Returns:    The value of the current bit.
+ *
+ * Use:                Reads the value of the current bit looked at by a
+ *             reverse bitscanner.
+ */
+
+#define MPSCAN_RBIT(m) (((m)->w >> (MPW_BITS - 1)) & 1)
+
+extern int mpscan_rbit(const mpscan */*m*/);
+
 /*----- That's all, folks -------------------------------------------------*/
 
 #ifdef __cplusplus