]> Savannah Git Hosting - gnulib.git/commitdiff
bitset: use ffs where possible in array implementation
authorAkim Demaille <akim.demaille@gmail.com>
Mon, 16 Nov 2020 06:22:35 +0000 (07:22 +0100)
committerAkim Demaille <akim.demaille@gmail.com>
Wed, 18 Nov 2020 05:50:28 +0000 (06:50 +0100)
* lib/bitset/array.c (abitset_small_list): Use BITSET_FOR_EACH_BIT.

ChangeLog
lib/bitset/array.c

index 657768f3b55247248ea127bcb49c53856311e2c8..8ee603345f823f543b3504b2694ce3dc7da2df16 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,8 @@
+2020-11-17  Akim Demaille  <akim@lrde.epita.fr>
+
+       bitset: use ffs where possible in array implementation
+       * lib/bitset/array.c (abitset_small_list): Use BITSET_FOR_EACH_BIT.
+
 2020-11-17  Bruno Haible  <bruno@clisp.org>
 
        posixcheck: Don't enable GNULIB_POSIXCHECK in C++ mode.
index 6c5f7ed34f936d7513daebddbc47e9ed62116cc2..3f8bcca87d3d58cc4f3f89a6d993e29ed7fa5a41 100644 (file)
@@ -62,38 +62,25 @@ abitset_small_list (bitset src, bitset_bindex *list,
 
   word >>= bitno;
 
-  /* If num is 1, we could speed things up with a binary search
-     of the word of interest.  */
-
-  bitset_bindex count;
+  bitset_bindex count = 0;
+  /* Is there enough room to avoid checking in each iteration? */
   if (num >= BITSET_WORD_BITS)
     {
-      for (count = 0; word; bitno++)
-        {
-          if (word & 1)
-            list[count++] = bitno;
-          word >>= 1;
-        }
+      BITSET_FOR_EACH_BIT (pos, word)
+        list[count++] = bitno + pos;
+      *next = bitno + BITSET_WORD_BITS;
+      return count;
     }
   else
-    {
-      for (count = 0; word; bitno++)
-        {
-          if (word & 1)
-            {
-              list[count++] = bitno;
-              if (count >= num)
-                {
-                  bitno++;
-                  break;
-                }
-            }
-          word >>= 1;
-        }
-    }
-
-  *next = bitno;
-  return count;
+    BITSET_FOR_EACH_BIT (pos, word)
+      {
+        list[count++] = bitno + pos;
+        if (count >= num)
+          {
+            *next = bitno + pos + 1;
+            return count;
+          }
+      }
 }
 
 
@@ -205,9 +192,6 @@ abitset_list (bitset src, bitset_bindex *list,
       if (windex >= size)
         return 0;
 
-      /* If num is 1, we could speed things up with a binary search
-         of the current word.  */
-
       bitoff = windex * BITSET_WORD_BITS;
     }
   else