diff --git a/dynamic_bitset.html b/dynamic_bitset.html index 32ec373..9ccbef0 100644 --- a/dynamic_bitset.html +++ b/dynamic_bitset.html @@ -238,6 +238,7 @@ public: bool intersects(const dynamic_bitset& a) const; size_type find_first() const; + size_type find_first(size_type pos) const; size_type find_next(size_type pos) const; }; @@ -1373,6 +1374,15 @@ size_type find_first() const; Returns: the lowest index i such as bit i is set, or npos if *this has no on bits. +
+
+size_type find_first(size_type pos) const;
+
+ +Returns: the lowest index i greater or equal to +offset such as bit i is set, or npos if +no such index exists. +
 size_type find_next(size_type pos) const;
diff --git a/include/boost/dynamic_bitset/dynamic_bitset.hpp b/include/boost/dynamic_bitset/dynamic_bitset.hpp
index a2abfc9..06ec971 100644
--- a/include/boost/dynamic_bitset/dynamic_bitset.hpp
+++ b/include/boost/dynamic_bitset/dynamic_bitset.hpp
@@ -325,6 +325,7 @@ public:
 
     // lookup
     size_type find_first() const;
+    size_type find_first(size_type pos) const;
     size_type find_next(size_type pos) const;
 
 
@@ -1326,7 +1327,7 @@ to_ulong() const
 
   // Check for overflows. This may be a performance burden on very
   // large bitsets but is required by the specification, sorry
-  if (find_next(ulong_width - 1) != npos)
+  if (find_first(ulong_width) != npos)
     BOOST_THROW_EXCEPTION(std::overflow_error("boost::dynamic_bitset::to_ulong overflow"));
 
 
@@ -1486,17 +1487,12 @@ dynamic_bitset::find_first() const
     return m_do_find_from(0);
 }
 
-
 template 
 typename dynamic_bitset::size_type
-dynamic_bitset::find_next(size_type pos) const
+dynamic_bitset::find_first(size_type pos) const
 {
-
     const size_type sz = size();
-    if (pos >= (sz-1) || sz == 0)
-        return npos;
-
-    ++pos;
+    if (pos >= sz) return npos;
 
     const size_type blk = block_index(pos);
     const block_width_type ind = bit_index(pos);
@@ -1508,7 +1504,15 @@ dynamic_bitset::find_next(size_type pos) const
         pos + static_cast(detail::lowest_bit(fore))
         :
         m_do_find_from(blk + 1);
+}
 
+
+template 
+typename dynamic_bitset::size_type
+dynamic_bitset::find_next(size_type pos) const
+{
+    if (pos == npos) return npos;
+    return find_first(pos + 1);
 }
 
 
diff --git a/test/bitset_test.hpp b/test/bitset_test.hpp
index d53f595..758d44d 100644
--- a/test/bitset_test.hpp
+++ b/test/bitset_test.hpp
@@ -967,25 +967,25 @@ struct bitset_test {
     BOOST_TEST(b.intersects(a) == have_intersection);
   }
 
-  static void find_first(const Bitset& b)
+  static void find_first(const Bitset& b, typename Bitset::size_type offset = 0)
   {
-      // find first non-null bit, if any
-      typename Bitset::size_type i = 0;
-      while (i < b.size() && b[i] == 0)
-          ++i;
-
-      if (i == b.size())
-        BOOST_TEST(b.find_first() == Bitset::npos); // not found;
-      else {
-        BOOST_TEST(b.find_first() == i);
-        BOOST_TEST(b.test(i) == true);
-      }
+    // find first non-null bit from offset onwards, if any
+    typename Bitset::size_type i = offset;
+    while (i < b.size() && b[i] == 0)
+        ++i;
 
+    if (i >= b.size())
+      BOOST_TEST(b.find_first(offset) == Bitset::npos); // not found;
+    else {
+      BOOST_TEST(b.find_first(offset) == i);
+      BOOST_TEST(b.test(i) == true);
+    }
   }
 
-  static void find_next(const Bitset& b, typename Bitset::size_type prev)
+  static void find_pos(const Bitset& b, typename Bitset::size_type pos)
   {
-    BOOST_TEST(next_bit_on(b, prev) == b.find_next(prev));
+    find_first(b, pos);
+    BOOST_TEST(next_bit_on(b, pos) == b.find_next(pos));
   }
 
   static void operator_equal(const Bitset& a, const Bitset& b)
diff --git a/test/dyn_bitset_unit_tests3.cpp b/test/dyn_bitset_unit_tests3.cpp
index 5692c26..9d01b12 100644
--- a/test/dyn_bitset_unit_tests3.cpp
+++ b/test/dyn_bitset_unit_tests3.cpp
@@ -329,26 +329,26 @@ void run_test_cases( BOOST_EXPLICIT_TEMPLATE_TYPE(Block) )
       Tests::find_first(b);
   }
   //=====================================================================
-  // Test find_next
+  // Test find_next and offset find_first
   {
       // empty bitset
       bitset_type b;
 
       // check
-      Tests::find_next(b, 0);
-      Tests::find_next(b, 1);
-      Tests::find_next(b, 200);
-      Tests::find_next(b, b.npos);
+      Tests::find_pos(b, 0);
+      Tests::find_pos(b, 1);
+      Tests::find_pos(b, 200);
+      Tests::find_pos(b, b.npos);
   }
   {
       // bitset of size 1 (find_next can never find)
       bitset_type b(1, 1ul);
 
       // check
-      Tests::find_next(b, 0);
-      Tests::find_next(b, 1);
-      Tests::find_next(b, 200);
-      Tests::find_next(b, b.npos);
+      Tests::find_pos(b, 0);
+      Tests::find_pos(b, 1);
+      Tests::find_pos(b, 200);
+      Tests::find_pos(b, b.npos);
   }
   {
       // all-1s bitset
@@ -358,9 +358,9 @@ void run_test_cases( BOOST_EXPLICIT_TEMPLATE_TYPE(Block) )
       // check
       const typename bitset_type::size_type larger_than_size = 5 + b.size();
       for(typename bitset_type::size_type i = 0; i <= larger_than_size; ++i) {
-          Tests::find_next(b, i);
+          Tests::find_pos(b, i);
       }
-      Tests::find_next(b, b.npos);
+      Tests::find_pos(b, b.npos);
   }
   {
       // a bitset with 1s at block boundary only
@@ -379,9 +379,9 @@ void run_test_cases( BOOST_EXPLICIT_TEMPLATE_TYPE(Block) )
       // check
       const typename bitset_type::size_type larger_than_size = 5 + b.size();
       for (i = 0; i <= larger_than_size; ++i) {
-          Tests::find_next(b, i);
+          Tests::find_pos(b, i);
       }
-      Tests::find_next(b, b.npos);
+      Tests::find_pos(b, b.npos);
 
   }
   {
@@ -397,9 +397,9 @@ void run_test_cases( BOOST_EXPLICIT_TEMPLATE_TYPE(Block) )
       // check
       const typename bitset_type::size_type larger_than_size = 5 + b.size();
       for (i = 0; i <= larger_than_size; ++i) {
-          Tests::find_next(b, i);
+          Tests::find_pos(b, i);
       }
-      Tests::find_next(b, b.npos);
+      Tests::find_pos(b, b.npos);
 
   }
   //=====================================================================