Add all() (#8445).

Includes test cases.
This commit is contained in:
Ahmed Charles
2014-02-22 18:02:07 -08:00
parent e8b37ccf10
commit 1b880102fe
4 changed files with 77 additions and 0 deletions

View File

@@ -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));

View File

@@ -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<Block> b;
Tests::all(b);
Tests::all(~b);
Tests::all(b.set());
Tests::all(b.reset());
}
{
boost::dynamic_bitset<Block> b(std::string("0"));
Tests::all(b);
Tests::all(~b);
Tests::all(b.set());
Tests::all(b.reset());
}
{
boost::dynamic_bitset<Block> b(long_string);
Tests::all(b);
Tests::all(~b);
Tests::all(b.set());
Tests::all(b.reset());
}
//=====================================================================
// Test b.any()
{
boost::dynamic_bitset<Block> b;

View File

@@ -202,6 +202,7 @@ public:
dynamic_bitset&amp; <a href="#flip2">flip</a>(size_type n);
dynamic_bitset&amp; <a href="#flip1">flip</a>();
bool <a href="#test">test</a>(size_type n) const;
bool <a href="#all">all</a>() const;
bool <a href="#any">any</a>() const;
bool <a href="#none">none</a>() const;
dynamic_bitset <a href="#op-not">operator~</a>() const;
@@ -1122,6 +1123,15 @@ size_type <a id="count">count</a>() const
set.<br />
<b>Throws:</b> nothing.
<hr />
<pre>
bool <a id="all">all</a>() const
</pre>
<b>Returns:</b> <tt>true</tt> if all bits in this bitset are set or
if <tt>size() == 0</tt>, and otherwise returns <tt>false</tt>.<br />
<b>Throws:</b> nothing.
<hr />
<pre>
bool <a id="any">any</a>() const

View File

@@ -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<Block, Allocator>::test(size_type pos) const
return m_unchecked_test(pos);
}
template <typename Block, typename Allocator>
bool dynamic_bitset<Block, Allocator>::all() const
{
if (empty()) {
return true;
}
const block_width_type extra_bits = count_extra_bits();
block_type const all_ones = ~static_cast<Block>(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<Block>(0) << extra_bits);
if (m_highest_block() != mask) {
return false;
}
}
return true;
}
template <typename Block, typename Allocator>
bool dynamic_bitset<Block, Allocator>::any() const
{