Patch by Riccardo Marcangelo: Added a pop_back() member function which decreases the size of the bitset by one

This commit is contained in:
Marshall Clow
2014-09-15 08:29:05 -07:00
parent b151c97fd2
commit 225064d355
4 changed files with 63 additions and 0 deletions

View File

@@ -2,6 +2,7 @@
// Copyright (c) 2001 Jeremy Siek
// Copyright (c) 2003-2006, 2008 Gennaro Prota
// Copyright (c) 2014 Ahmed Charles
// Copyright (c) 2014 Riccardo Marcangelo
//
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
@@ -367,6 +368,20 @@ struct bitset_test {
BOOST_CHECK(b.size() == 0);
}
static void pop_back(const Bitset& lhs)
{
Bitset b(lhs);
b.pop_back();
BOOST_CHECK(b.size() == lhs.size() - 1);
for (std::size_t i = 0; i < b.size(); ++i)
BOOST_CHECK(b[i] == lhs[i]);
b.pop_back();
BOOST_CHECK(b.size() == lhs.size() - 2);
for (std::size_t j = 0; j < b.size(); ++j)
BOOST_CHECK(b[j] == lhs[j]);
}
static void append_bit(const Bitset& lhs)
{
Bitset b(lhs);

View File

@@ -2,6 +2,7 @@
// Copyright (c) 2001 Jeremy Siek
// Copyright (c) 2003-2006 Gennaro Prota
// Copyright (c) 2014 Ahmed Charles
// Copyright (c) 2014 Riccardo Marcangelo
//
// Copyright (c) 2014 Glen Joseph Fernandes
// glenfe at live dot com
@@ -379,6 +380,25 @@ void run_test_cases( BOOST_EXPLICIT_TEMPLATE_TYPE(Block) )
Tests::clear(a);
}
//=====================================================================
// Test pop back
{
boost::dynamic_bitset<Block> a(std::string("01"));
Tests::pop_back(a);
}
{
boost::dynamic_bitset<Block> a(std::string("10"));
Tests::pop_back(a);
}
{
const int size_to_fill_all_blocks = 4 * bits_per_block;
boost::dynamic_bitset<Block> a(size_to_fill_all_blocks, 255ul);
Tests::pop_back(a);
}
{
boost::dynamic_bitset<Block> a(long_string);
Tests::pop_back(a);
}
//=====================================================================
// Test append bit
{
boost::dynamic_bitset<Block> a;

View File

@@ -180,6 +180,7 @@ public:
void <a href=
"#resize">resize</a>(size_type num_bits, bool value = false);
void <a href="#clear">clear</a>();
void <a href="#pop_back">pop_back</a>();
void <a href="#push_back">push_back</a>(bool bit);
void <a href="#append1">append</a>(Block block);
@@ -834,6 +835,15 @@ void <a id="clear">clear</a>()
<b>Effects:</b> The size of the bitset becomes zero.<br />
<b>Throws:</b> nothing.
<hr />
<pre>
void <a id="pop_back">pop_back</a>();
</pre>
<b>Precondition:</b> <tt>!this-&gt;empty()</tt>.<br />
<b>Effects:</b> Decreases the size of the bitset by one.<br />
<b>Throws:</b> nothing.
<hr />
<pre>
void <a id="push_back">push_back</a>(bool value);

View File

@@ -6,6 +6,7 @@
//
// Copyright (c) 2014 Glen Joseph Fernandes
// glenfe at live dot com
// Copyright (c) 2014 Riccardo Marcangelo
//
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
@@ -227,6 +228,7 @@ public:
void resize(size_type num_bits, bool value = false);
void clear();
void push_back(bool bit);
void pop_back();
void append(Block block);
template <typename BlockInputIterator>
@@ -746,6 +748,22 @@ push_back(bool bit)
set(sz, bit);
}
template <typename Block, typename Allocator>
void dynamic_bitset<Block, Allocator>::
pop_back()
{
const size_type old_num_blocks = num_blocks();
const size_type required_blocks = calc_num_blocks(m_num_bits - 1);
if (required_blocks != old_num_blocks) {
m_bits.pop_back();
}
--m_num_bits;
m_zero_unused_bits();
}
template <typename Block, typename Allocator>
void dynamic_bitset<Block, Allocator>::
append(Block value) // strong guarantee