From 225064d3558ced62db63ce23e108b85af2f09d07 Mon Sep 17 00:00:00 2001 From: Marshall Clow Date: Mon, 15 Sep 2014 08:29:05 -0700 Subject: [PATCH] Patch by Riccardo Marcangelo: Added a pop_back() member function which decreases the size of the bitset by one --- bitset_test.hpp | 15 ++++++++++++++ dyn_bitset_unit_tests1.cpp | 20 +++++++++++++++++++ dynamic_bitset.html | 10 ++++++++++ .../boost/dynamic_bitset/dynamic_bitset.hpp | 18 +++++++++++++++++ 4 files changed, 63 insertions(+) diff --git a/bitset_test.hpp b/bitset_test.hpp index ac47c04..463033a 100644 --- a/bitset_test.hpp +++ b/bitset_test.hpp @@ -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); diff --git a/dyn_bitset_unit_tests1.cpp b/dyn_bitset_unit_tests1.cpp index 31299d1..51b2ef9 100644 --- a/dyn_bitset_unit_tests1.cpp +++ b/dyn_bitset_unit_tests1.cpp @@ -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 a(std::string("01")); + Tests::pop_back(a); + } + { + boost::dynamic_bitset a(std::string("10")); + Tests::pop_back(a); + } + { + const int size_to_fill_all_blocks = 4 * bits_per_block; + boost::dynamic_bitset a(size_to_fill_all_blocks, 255ul); + Tests::pop_back(a); + } + { + boost::dynamic_bitset a(long_string); + Tests::pop_back(a); + } + //===================================================================== // Test append bit { boost::dynamic_bitset a; diff --git a/dynamic_bitset.html b/dynamic_bitset.html index d03a502..2f8d0d0 100644 --- a/dynamic_bitset.html +++ b/dynamic_bitset.html @@ -180,6 +180,7 @@ public: void resize(size_type num_bits, bool value = false); void clear(); + void pop_back(); void push_back(bool bit); void append(Block block); @@ -834,6 +835,15 @@ void clear() Effects: The size of the bitset becomes zero.
Throws: nothing. +
+
+void pop_back();
+
+ +Precondition: !this->empty().
+Effects: Decreases the size of the bitset by one.
+ Throws: nothing. +
 void push_back(bool value);
diff --git a/include/boost/dynamic_bitset/dynamic_bitset.hpp b/include/boost/dynamic_bitset/dynamic_bitset.hpp
index 4d6011c..f6002d8 100644
--- a/include/boost/dynamic_bitset/dynamic_bitset.hpp
+++ b/include/boost/dynamic_bitset/dynamic_bitset.hpp
@@ -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 
@@ -746,6 +748,22 @@ push_back(bool bit)
   set(sz, bit);
 }
 
+template 
+void dynamic_bitset::
+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 
 void dynamic_bitset::
 append(Block value) // strong guarantee