From 1b880102fe339dd7ad589cb7ef26792ef76a9b08 Mon Sep 17 00:00:00 2001 From: Ahmed Charles Date: Sat, 22 Feb 2014 18:02:07 -0800 Subject: [PATCH] Add all() (#8445). Includes test cases. --- bitset_test.hpp | 12 +++++++ dyn_bitset_unit_tests3.cpp | 24 ++++++++++++++ dynamic_bitset.html | 10 ++++++ .../boost/dynamic_bitset/dynamic_bitset.hpp | 31 +++++++++++++++++++ 4 files changed, 77 insertions(+) diff --git a/bitset_test.hpp b/bitset_test.hpp index 26389a1..67851a8 100644 --- a/bitset_test.hpp +++ b/bitset_test.hpp @@ -712,6 +712,18 @@ struct bitset_test { BOOST_CHECK(Bitset(b).set().count() == b.size()); } + static void all(const Bitset& b) + { + BOOST_CHECK(b.all() == (b.count() == b.size())); + bool result = true; + for(std::size_t i = 0; i < b.size(); ++i) + if(!b[i]) { + result = false; + break; + } + BOOST_CHECK(b.all() == result); + } + static void any(const Bitset& b) { BOOST_CHECK(b.any() == (b.count() != 0)); diff --git a/dyn_bitset_unit_tests3.cpp b/dyn_bitset_unit_tests3.cpp index f698ad6..e607e21 100644 --- a/dyn_bitset_unit_tests3.cpp +++ b/dyn_bitset_unit_tests3.cpp @@ -1,6 +1,7 @@ // ----------------------------------------------------------- // Copyright (c) 2001 Jeremy Siek // Copyright (c) 2003-2006 Gennaro Prota +// Copyright (c) 2014 Ahmed Charles // // Distributed under the Boost Software License, Version 1.0. // (See accompanying file LICENSE_1_0.txt or copy at @@ -121,6 +122,29 @@ void run_test_cases( BOOST_EXPLICIT_TEMPLATE_TYPE(Block) ) Tests::size(b); } //===================================================================== + // Test b.all() + { + boost::dynamic_bitset b; + Tests::all(b); + Tests::all(~b); + Tests::all(b.set()); + Tests::all(b.reset()); + } + { + boost::dynamic_bitset b(std::string("0")); + Tests::all(b); + Tests::all(~b); + Tests::all(b.set()); + Tests::all(b.reset()); + } + { + boost::dynamic_bitset b(long_string); + Tests::all(b); + Tests::all(~b); + Tests::all(b.set()); + Tests::all(b.reset()); + } + //===================================================================== // Test b.any() { boost::dynamic_bitset b; diff --git a/dynamic_bitset.html b/dynamic_bitset.html index 1629802..c1850f8 100644 --- a/dynamic_bitset.html +++ b/dynamic_bitset.html @@ -202,6 +202,7 @@ public: dynamic_bitset& flip(size_type n); dynamic_bitset& flip(); bool test(size_type n) const; + bool all() const; bool any() const; bool none() const; dynamic_bitset operator~() const; @@ -1122,6 +1123,15 @@ size_type count() const set.
Throws: nothing. +
+
+bool all() const
+
+ +Returns: true if all bits in this bitset are set or +if size() == 0, and otherwise returns false.
+Throws: nothing. +
 bool any() const
diff --git a/include/boost/dynamic_bitset/dynamic_bitset.hpp b/include/boost/dynamic_bitset/dynamic_bitset.hpp
index 1845247..af43b60 100644
--- a/include/boost/dynamic_bitset/dynamic_bitset.hpp
+++ b/include/boost/dynamic_bitset/dynamic_bitset.hpp
@@ -278,6 +278,7 @@ public:
     dynamic_bitset& flip(size_type n);
     dynamic_bitset& flip();
     bool test(size_type n) const;
+    bool all() const;
     bool any() const;
     bool none() const;
     dynamic_bitset operator~() const;
@@ -1001,6 +1002,36 @@ bool dynamic_bitset::test(size_type pos) const
     return m_unchecked_test(pos);
 }
 
+template 
+bool dynamic_bitset::all() const
+{
+    if (empty()) {
+        return true;
+    }
+
+    const block_width_type extra_bits = count_extra_bits();
+    block_type const all_ones = ~static_cast(0);
+
+    if (extra_bits == 0) {
+        for (size_type i = 0, e = num_blocks(); i < e; ++i) {
+            if (m_bits[i] != all_ones) {
+                return false;
+            }
+        }
+    } else {
+        for (size_type i = 0, e = num_blocks() - 1; i < e; ++i) {
+            if (m_bits[i] != all_ones) {
+                return false;
+            }
+        }
+        block_type const mask = ~(~static_cast(0) << extra_bits);
+        if (m_highest_block() != mask) {
+            return false;
+        }
+    }
+    return true;
+}
+
 template 
 bool dynamic_bitset::any() const
 {