Add capacity(), reserve(), and shrink_to_fit().

This commit is contained in:
ricky65
2014-11-24 17:09:22 +00:00
committed by Ahmed Charles
parent 1c5e30fe49
commit 340822f979
4 changed files with 164 additions and 6 deletions

View File

@@ -2,7 +2,7 @@
// Copyright (c) 2001 Jeremy Siek
// Copyright (c) 2003-2006, 2008 Gennaro Prota
// Copyright (c) 2014 Ahmed Charles
// Copyright (c) 2014 Riccardo Marcangelo
// Copyright (c) 2014 Riccardo Marcangelo
//
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
@@ -732,6 +732,67 @@ struct bitset_test {
BOOST_CHECK(Bitset(b).set().count() == b.size());
}
static void capacity_test_one(const Bitset& lhs)
{
//empty bitset
Bitset b(lhs);
BOOST_CHECK(b.capacity() == 0);
}
static void capacity_test_two(const Bitset& lhs)
{
//bitset constructed with size "100"
Bitset b(lhs);
BOOST_CHECK(b.capacity() >= 100);
b.resize(200);
BOOST_CHECK(b.capacity() >= 200);
}
static void reserve_test_one(const Bitset& lhs)
{
//empty bitset
Bitset b(lhs);
b.reserve(16);
BOOST_CHECK(b.capacity() >= 16);
}
static void reserve_test_two(const Bitset& lhs)
{
//bitset constructed with size "100"
Bitset b(lhs);
BOOST_CHECK(b.capacity() >= 100);
b.reserve(60);
BOOST_CHECK(b.size() == 100);
BOOST_CHECK(b.capacity() >= 100);
b.reserve(160);
BOOST_CHECK(b.size() == 100);
BOOST_CHECK(b.capacity() >= 160);
}
static void shrink_to_fit_test_one(const Bitset& lhs)
{
//empty bitset
Bitset b(lhs);
b.shrink_to_fit();
BOOST_CHECK(b.size() == 0);
BOOST_CHECK(b.capacity() == 0);
}
static void shrink_to_fit_test_two(const Bitset& lhs)
{
//bitset constructed with size "100"
Bitset b(lhs);
b.shrink_to_fit();
BOOST_CHECK(b.capacity() >= 100);
BOOST_CHECK(b.size() == 100);
b.reserve(200);
BOOST_CHECK(b.capacity() >= 200);
BOOST_CHECK(b.size() == 100);
b.shrink_to_fit();
BOOST_CHECK(b.capacity() < 200);
BOOST_CHECK(b.size() == 100);
}
static void all(const Bitset& b)
{
BOOST_CHECK(b.all() == (b.count() == b.size()));

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
//
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
@@ -122,6 +123,36 @@ void run_test_cases( BOOST_EXPLICIT_TEMPLATE_TYPE(Block) )
Tests::size(b);
}
//=====================================================================
// Test b.capacity()
{
boost::dynamic_bitset<Block> b;
Tests::capacity_test_one(b);
}
{
boost::dynamic_bitset<Block> b(100);
Tests::capacity_test_two(b);
}
//=====================================================================
// Test b.reserve()
{
boost::dynamic_bitset<Block> b;
Tests::reserve_test_one(b);
}
{
boost::dynamic_bitset<Block> b(100);
Tests::reserve_test_two(b);
}
//=====================================================================
// Test b.shrink_to_fit()
{
boost::dynamic_bitset<Block> b;
Tests::shrink_to_fit_test_one(b);
}
{
boost::dynamic_bitset<Block> b(100);
Tests::shrink_to_fit_test_two(b);
}
//=====================================================================
// Test b.all()
{
boost::dynamic_bitset<Block> b;

View File

@@ -7,6 +7,7 @@
Copyright (c) 2001 Jeremy Siek
Copyright (c) 2003-2004, 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
@@ -219,6 +220,9 @@ public:
size_type <a href="#num_blocks">num_blocks</a>() const noexcept;
size_type <a href="#max_size">max_size</a>() const noexcept;
bool <a href="#empty">empty</a>() const noexcept;
size_type <a href="#capacity">capacity</a>() const noexcept;
void <a href="#reserve">reserve</a>(size_type num_bits);
void <a href="#shrink_to_fit">shrink_to_fit</a>();
bool <a href="#is_subset_of">is_subset_of</a>(const dynamic_bitset&amp; a) const;
bool <a href="#is_proper_subset_of">is_proper_subset_of</a>(const dynamic_bitset&amp; a) const;
@@ -843,7 +847,7 @@ void <a id="pop_back">pop_back</a>();
<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);
@@ -1125,6 +1129,40 @@ bool <a id="empty">empty</a>() const;
otherwise. <i>Note</i>: not to be confused with <tt>none()</tt>, that has
different semantics.
<hr />
<pre>
size_type <a id="capacity">capacity</a>() const;
</pre>
<b>Returns:</b> The total number of elements that <tt>*this</tt> can hold without requiring
reallocation.<br />
<b>Throws:</b> nothing.
<hr />
<pre>
void <a id="reserve">reserve</a>(size_type num_bits);
</pre>
<b>Effects:</b> A directive that informs the bitset of a planned change in size, so that it can
manage the storage allocation accordingly. After reserve(), capacity() is greater or equal to the
argument of reserve() if reallocation happens; and equal to the previous value of capacity() otherwise.
Reallocation happens at this point if and only if the current capacity is less than the argument of
reserve(). <br />
<i>Note:</i> It does not change the size() of the bitset.<br />
<b>Postcondtitions:</b> <tt>this->capacity() >= num_bits</tt>.<br />
<b>Throws:</b> An allocation error if memory is exhausted
(<tt>std::bad_alloc</tt> if <tt>Allocator=std::allocator</tt>).
<hr />
<pre>
void <a id="shrink_to_fit">shrink_to_fit</a>();
</pre>
<b>Effects:</b> shrink_to_fit() is a request to reduce memory use by removing unused capacity.<br />
<i>Note:</i> It does not change the size() of the bitset.<br />
<b>Throws:</b> An allocation error if memory is exhausted
(<tt>std::bad_alloc</tt> if <tt>Allocator=std::allocator</tt>).
<hr />
<pre>
size_type <a id="count">count</a>() const
@@ -1682,6 +1720,10 @@ href="mailto:cda@freshsources.com">cda@freshsources.com</a>)<br
<td>Copyright &copy; 2014</td>
<td>Glen Fernandes (<a href="mailto:glenfe@live.com">glenfe@live.com</a>)</td>
</tr>
<tr>
<td>Copyright &copy; 2014</td>
<td>Riccardo Marcangelo (<a href="mailto:ricky.65@outlook.com">ricky.65@outlook.com</a>)</td>
</tr>
</table>
<br />
<div class="legalnotice">

View File

@@ -305,6 +305,9 @@ public:
size_type num_blocks() const BOOST_NOEXCEPT;
size_type max_size() const BOOST_NOEXCEPT;
bool empty() const BOOST_NOEXCEPT;
size_type capacity() const BOOST_NOEXCEPT;
void reserve(size_type num_bits);
void shrink_to_fit();
bool is_subset_of(const dynamic_bitset& a) const;
bool is_proper_subset_of(const dynamic_bitset& a) const;
@@ -750,15 +753,15 @@ push_back(bool bit)
template <typename Block, typename Allocator>
void dynamic_bitset<Block, Allocator>::
pop_back()
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_bits.pop_back();
}
--m_num_bits;
m_zero_unused_bits();
}
@@ -1267,6 +1270,27 @@ inline bool dynamic_bitset<Block, Allocator>::empty() const BOOST_NOEXCEPT
return size() == 0;
}
template <typename Block, typename Allocator>
inline typename dynamic_bitset<Block, Allocator>::size_type
dynamic_bitset<Block, Allocator>::capacity() const BOOST_NOEXCEPT
{
return m_bits.capacity() * bits_per_block;
}
template <typename Block, typename Allocator>
inline void dynamic_bitset<Block, Allocator>::reserve(size_type num_bits)
{
m_bits.reserve(calc_num_blocks(num_bits));
}
template <typename Block, typename Allocator>
void dynamic_bitset<Block, Allocator>::shrink_to_fit()
{
if (m_bits.size() < m_bits.capacity()) {
buffer_type(m_bits).swap(m_bits);
}
}
template <typename Block, typename Allocator>
bool dynamic_bitset<Block, Allocator>::
is_subset_of(const dynamic_bitset<Block, Allocator>& a) const