|
Boost-Commit : |
Subject: [Boost-commit] svn:boost r64822 - in sandbox/SOC/2010/bit_masks: boost/integer lib/integer/test/bitfield_vector_testing
From: bbartmanboost_at_[hidden]
Date: 2010-08-15 12:17:32
Author: bbartman
Date: 2010-08-15 12:17:23 EDT (Sun, 15 Aug 2010)
New Revision: 64822
URL: http://svn.boost.org/trac/boost/changeset/64822
Log:
Completed a large section of implementation along with testing for bitfield_vector however it's still incomplte but its now closer to a first working draft.
Text files modified:
sandbox/SOC/2010/bit_masks/boost/integer/bitfield_vector.hpp | 93 +++++++++++++++++-
sandbox/SOC/2010/bit_masks/lib/integer/test/bitfield_vector_testing/bitfield_vector_test.cpp | 201 +++++++++++++++++++++++++++++++++++++++
sandbox/SOC/2010/bit_masks/lib/integer/test/bitfield_vector_testing/test_utility.hpp | 1
3 files changed, 283 insertions(+), 12 deletions(-)
Modified: sandbox/SOC/2010/bit_masks/boost/integer/bitfield_vector.hpp
==============================================================================
--- sandbox/SOC/2010/bit_masks/boost/integer/bitfield_vector.hpp (original)
+++ sandbox/SOC/2010/bit_masks/boost/integer/bitfield_vector.hpp 2010-08-15 12:17:23 EDT (Sun, 15 Aug 2010)
@@ -495,19 +495,34 @@
typedef detail::const_proxy_reference_type<value_type,Width>
const_reference;
+ /** Constructable over an allocator and default constructor. */
explicit bitfield_vector(Allocator const& alloc = Allocator() )
:_base(alloc)
{ }
- explicit bitfield_vector(size_type n, T const& value= T(),
- Allocator const& = Allocator() );
+ /** fill with value constructor.
+ * Fills the vector with N items with value of value.
+ */
+ explicit bitfield_vector(size_type n,
+ value_type const& value = T(),
+ allocator_type const& alloc = Allocator()
+ )
+ :_base(alloc)
+ {
+ allocate_and_fill(n,value);
+ }
template <class InputIterator>
bitfield_vector(InputIterator first, InputIterator last,
Allocator const& = Allocator());
bitfield_vector(_self const& x );
- ~bitfield_vector();
+
+ // TODO Implement me!!!
+ // should just call clear maybe?
+ ~bitfield_vector() {
+
+ }
_self& operator=(_self const& x);
@@ -515,11 +530,21 @@
* types supported by this data structure.
*/
//@{
- iterator begin();
- const_iterator begin() const;
+ iterator begin() {
+ return iterator(this->_impl._start,0);
+ }
- iterator end();
- const_iterator end() const;
+ const_iterator begin() const {
+ return const_iterator(this->_impl._start,0);
+ }
+
+ iterator end() {
+ return begin() + (this->_impl._bits_in_use / Width);
+ }
+
+ const_iterator end() const {
+ return begin() + (this->_impl._bits_in_use / Width);
+ }
reverse_iterator rbegin();
const_reverse_iterator rbegin() const;
@@ -581,6 +606,60 @@
allocator_type get_allocator() const;
protected:
+
+ /** Inserts a value at the location
+ * of the iterator.
+ * TODO Implement this
+ */
+ template <typename Iter>
+ void _insert_at(Iter iter, value_type value) {
+ }
+
+ /** allocates a chunck of memory of the correct size and then
+ * correctly fills that memory with value for each of the
+ * bitfields within it.
+ */
+ void allocate_and_fill(size_type n, value_type value) {
+
+ // figure out exactly how many bits are going to be needed.
+ size_type min_allocation_size = n * Width;
+
+ // calculate that size in bytes.
+ min_allocation_size = (min_allocation_size/8)
+ + size_type((min_allocation_size%8)!=0);
+
+ // calculate the correct size of the allocation
+ // by getting the closest power of 2 greater then the number of bytes
+ // needed to stor n bitfields in.
+ size_type corrected_allocation_size = 1;
+ while(corrected_allocation_size < min_allocation_size) {
+ corrected_allocation_size *= 2;
+ }
+
+ // must save the original pointer so that I can maintain the state
+ // of this object and NOT leak memory.
+ pointer old_start = this->_impl._start;
+ try {
+ // allocate the necessary memory to hold all of the bitfields.
+ // This CAN throw std::bad_alloc
+ this->_impl._start = this->allocate_impl(corrected_allocation_size);
+ std::memset(this->_impl._start,0,corrected_allocation_size);
+ // this line will never throw because this is pointer arithmatic
+ // and integer assignment.
+ this->_impl._end = this->_impl._start + corrected_allocation_size;
+ }catch(...) {
+ this->_impl._start = old_start;
+ throw;
+ }
+
+ // once allocation is completed next comes filling the bitfields
+ // with val.
+ for(iterator it( begin()); it != end(); ++it) {
+ *it = value;
+ this->_impl._bits_in_use += Width;
+ }
+ }
+
void check_for_resizing() {
size_type size_of_alloc = (this->_impl._end - this->_impl._start);
difference_type remaing_bits = ((size_of_alloc*8) - this->_impl._bits_in_use);
Modified: sandbox/SOC/2010/bit_masks/lib/integer/test/bitfield_vector_testing/bitfield_vector_test.cpp
==============================================================================
--- sandbox/SOC/2010/bit_masks/lib/integer/test/bitfield_vector_testing/bitfield_vector_test.cpp (original)
+++ sandbox/SOC/2010/bit_masks/lib/integer/test/bitfield_vector_testing/bitfield_vector_test.cpp 2010-08-15 12:17:23 EDT (Sun, 15 Aug 2010)
@@ -16,12 +16,203 @@
* This is only done so that testing can be done easier.
*
*/
-template <typename T, std::size_t W>
-struct test_for_bitfield_vector
- :bitfield_vector<T, W>
-{ };
+typedef bitfield_vector<unsigned int, 3> bfv_1;
-int main() {
+template <typename T, std::size_t Width, typename Alloc, typename Bfv>
+struct test_for_bfv
+ :Bfv
+{
+ typedef Bfv _base;
+
+ /** Tests to make sure that I got all of the typedefs from within
+ * bitfield_vector correct and they are all correctly exposed.
+ */
+ static void test_typedefs() {
+ // iterator types.
+ BOOST_TEST((
+ is_same<
+ typename _base::iterator,
+ detail::bf_vector_iterator<T,Width>
+ >::value
+ ));
+
+ BOOST_TEST((
+ is_same<
+ typename _base::const_iterator,
+ detail::const_bf_vector_iterator<T,Width>
+ >::value
+ ));
+
+ BOOST_TEST((
+ is_same<
+ typename _base::reverse_iterator,
+ detail::bf_vector_reverse_iterator<T,Width>
+ >::value
+ ));
+
+ BOOST_TEST((
+ is_same<
+ typename _base::const_reverse_iterator,
+ detail::const_bf_vector_reverse_iterator<T,Width>
+ >::value
+ ));
+
+ // value_type
+ BOOST_TEST((
+ is_same<
+ typename _base::value_type,
+ T
+ >::value
+ ));
+
+ // allocator type
+ BOOST_TEST((
+ is_same<
+ typename _base::allocator_type,
+ Alloc
+ >::value
+ ));
+
+ // size_type
+ BOOST_TEST((
+ is_same<
+ typename _base::size_type,
+ std::size_t
+ >::value
+ ));
+
+ // difference_type
+ BOOST_TEST((
+ is_same<
+ typename _base::difference_type,
+ std::ptrdiff_t
+ >::value
+ ));
+
+ // pointer
+ BOOST_TEST((
+ is_same<
+ typename _base::pointer,
+ boost::detail::storage_ptr_t
+ >::value
+ ));
+
+ // const_pointer
+ typedef boost::detail::storage_t const* const_storage_ptr;
+ BOOST_TEST((
+ is_same<
+ typename _base::const_pointer,
+ const_storage_ptr
+ >::value
+ ));
+
+ // reference
+ BOOST_TEST((
+ is_same<
+ typename _base::reference,
+ detail::proxy_reference_type<T,Width>
+ >::value
+ ));
+
+ // const_reference
+ BOOST_TEST((
+ is_same<
+ typename _base::const_reference,
+ detail::const_proxy_reference_type<T,Width>
+ >::value
+ ));
+ }
+
+
+ // default constructor
+ test_for_bfv()
+ :_base()
+ { }
+
+ // default ctor test
+ void test_default_ctor() {
+ BOOST_TEST(this->_impl._start == 0 );
+ BOOST_TEST(this->_impl._end == 0 );
+ BOOST_TEST(this->_impl._bits_in_use == 0 );
+ }
+
+ // ctor over alloc
+ test_for_bfv(Alloc a)
+ :_base(a)
+ { }
+
+ // ctor over alloc test
+ void test_alloc_ctor() {
+ BOOST_TEST(this->_impl._start == 0 );
+ BOOST_TEST(this->_impl._end == 0 );
+ BOOST_TEST(this->_impl._bits_in_use == 0 );
+ }
+
+ // fill n with value ctor.
+ test_for_bfv(typename _base::size_type n, typename _base::value_type val )
+ :_base(n,val)
+ { }
+
+ void test_fill_with_n(typename _base::value_type val) {
+ typename _base::iterator it(this->_impl._start, 0);
+ typename _base::iterator it_end(this->_impl._start, 0);
+ it_end += this->_impl._bits_in_use / Width;
+ while(it != it_end) {
+ BOOST_TEST( *it == val);
+ ++it;
+ }
+ }
+};
+
+
+
+typedef test_for_bfv<
+ unsigned int,
+ 3,
+ std::allocator<unsigned char>,
+ bfv_1
+> test_type_1;
+
+template<typename Tester>
+void test_orchestrator() {
+
+ Tester::test_typedefs();
+
+ // default constructor
+ {
+ Tester t1;
+ t1.test_default_ctor();
+ }
+
+ // constructor over allocator
+ {
+ typename Tester::allocator_type alloc_temp;
+ Tester t1(alloc_temp);
+ t1.test_alloc_ctor();
+ }
+
+ // testing fill with N constructor.
+ {
+ Tester t1(4, 2);
+ t1.test_fill_with_n(2);
+ }
+ // regular begin and end.
+ {
+ }
+ // const begin and end
+ {
+ }
+ // reverse begin and end
+ {
+ }
+ // const reverse begine and end
+ {
+ }
+
+}
+
+int main() {
+ test_orchestrator<test_type_1>();
return boost::report_errors();
}
Modified: sandbox/SOC/2010/bit_masks/lib/integer/test/bitfield_vector_testing/test_utility.hpp
==============================================================================
--- sandbox/SOC/2010/bit_masks/lib/integer/test/bitfield_vector_testing/test_utility.hpp (original)
+++ sandbox/SOC/2010/bit_masks/lib/integer/test/bitfield_vector_testing/test_utility.hpp 2010-08-15 12:17:23 EDT (Sun, 15 Aug 2010)
@@ -13,6 +13,7 @@
#include <bitset>
#include <boost/integer/bit_width.hpp>
#include <sstream>
+#include <boost/type_traits/is_same.hpp>
#if defined(__GNUC__)
#include <cxxabi.h>
Boost-Commit list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk