Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r61785 - in sandbox/hash: boost boost/hash boost/hash/detail libs/hash/test
From: me22.ca+boost_at_[hidden]
Date: 2010-05-04 22:09:14


Author: smcmurray
Date: 2010-05-04 22:09:12 EDT (Tue, 04 May 2010)
New Revision: 61785
URL: http://svn.boost.org/trac/boost/changeset/61785

Log:
hash: allow use with Output Iterators, and add test
Text files modified:
   sandbox/hash/boost/hash.hpp | 1
   sandbox/hash/boost/hash/detail/exploder.hpp | 23 +++++++++++++---
   sandbox/hash/boost/hash/pack.hpp | 6 +++-
   sandbox/hash/libs/hash/test/pack.cpp | 52 +++++++++++++++++++++++++++++++++++++--
   4 files changed, 71 insertions(+), 11 deletions(-)

Modified: sandbox/hash/boost/hash.hpp
==============================================================================
--- sandbox/hash/boost/hash.hpp (original)
+++ sandbox/hash/boost/hash.hpp 2010-05-04 22:09:12 EDT (Tue, 04 May 2010)
@@ -9,7 +9,6 @@
 #ifndef BOOST_HASH_HPP
 #define BOOST_HASH_HPP
 
-#include <boost/hash/badhash.hpp>
 #include <boost/hash/cubehash.hpp>
 #include <boost/hash/md4.hpp>
 #include <boost/hash/md5.hpp>

Modified: sandbox/hash/boost/hash/detail/exploder.hpp
==============================================================================
--- sandbox/hash/boost/hash/detail/exploder.hpp (original)
+++ sandbox/hash/boost/hash/detail/exploder.hpp 2010-05-04 22:09:12 EDT (Tue, 04 May 2010)
@@ -11,8 +11,11 @@
 
 #include <boost/hash/stream_endian.hpp>
 #include <boost/hash/detail/unbounded_shift.hpp>
+#include <boost/integer.hpp>
 #include <boost/static_assert.hpp>
 
+#include <iterator>
+
 #include <climits>
 #include <cstring>
 
@@ -23,6 +26,16 @@
 // By definition, for all exploders, InputBits > OutputBits,
 // so we're taking one value and splitting it into many smaller values
 
+template <typename OutIter, int OutBits,
+ typename T = typename std::iterator_traits<OutIter>::value_type>
+struct outvalue_helper {
+ typedef T type;
+};
+template <typename OutIter, int OutBits>
+struct outvalue_helper<OutIter, OutBits, void> {
+ typedef typename uint_t<OutBits>::least type;
+};
+
 template <typename Endianness,
           int InputBits, int OutputBits,
           int k>
@@ -34,7 +47,7 @@
     template <typename InputValue, typename OutIter>
     static void step(InputValue const &x, OutIter &out) {
         int const shift = InputBits - (OutputBits+k);
- typedef typename std::iterator_traits<OutIter>::value_type OutValue;
+ typedef typename outvalue_helper<OutIter, OutputBits>::type OutValue;
         InputValue y = unbounded_shr<shift>(x);
         *out++ = OutValue(low_bits<OutputBits>(y));
     }
@@ -51,7 +64,7 @@
             OutputBits >= UnitBits ? k :
             InputBits >= UnitBits ? ku + (UnitBits-(OutputBits+kb)) :
                                      InputBits - (OutputBits+kb);
- typedef typename std::iterator_traits<OutIter>::value_type OutValue;
+ typedef typename outvalue_helper<OutIter, OutputBits>::type OutValue;
         InputValue y = unbounded_shr<shift>(x);
         *out++ = OutValue(low_bits<OutputBits>(y));
     }
@@ -68,7 +81,7 @@
             OutputBits >= UnitBits ? InputBits - (OutputBits+k) :
             InputBits >= UnitBits ? InputBits - (UnitBits+ku) + kb :
                                      kb;
- typedef typename std::iterator_traits<OutIter>::value_type OutValue;
+ typedef typename outvalue_helper<OutIter, OutputBits>::type OutValue;
         InputValue y = unbounded_shr<shift>(x);
         *out++ = OutValue(low_bits<OutputBits>(y));
     }
@@ -80,7 +93,7 @@
     template <typename InputValue, typename OutIter>
     static void step(InputValue const &x, OutIter &out) {
         int const shift = k;
- typedef typename std::iterator_traits<OutIter>::value_type OutValue;
+ typedef typename outvalue_helper<OutIter, OutputBits>::type OutValue;
         InputValue y = unbounded_shr<shift>(x);
         *out++ = OutValue(low_bits<OutputBits>(y));
     }
@@ -91,7 +104,7 @@
                      InputBits, OutputBits, k> {
     template <typename InputValue, typename OutIter>
     static void step(InputValue const &x, OutIter &out) {
- typedef typename std::iterator_traits<OutIter>::value_type OutValue;
+ typedef typename outvalue_helper<OutIter, OutputBits>::type OutValue;
         BOOST_STATIC_ASSERT(sizeof(InputValue)*CHAR_BIT == InputBits);
         BOOST_STATIC_ASSERT(sizeof(OutValue)*CHAR_BIT == OutputBits);
         OutValue value;

Modified: sandbox/hash/boost/hash/pack.hpp
==============================================================================
--- sandbox/hash/boost/hash/pack.hpp (original)
+++ sandbox/hash/boost/hash/pack.hpp 2010-05-04 22:09:12 EDT (Tue, 04 May 2010)
@@ -162,7 +162,8 @@
                        OutIter out) {
         size_t out_n = in_n/(OutputBits/InputBits);
         while (out_n--) {
- typedef typename std::iterator_traits<OutIter>::value_type OutValue;
+ typedef typename detail::outvalue_helper<OutIter, OutputBits>::type
+ OutValue;
             OutValue value = OutValue();
             detail::imploder<Endianness, InputBits, OutputBits>
              ::implode(in, value);
@@ -174,7 +175,8 @@
     static void pack(InIter in, InIter in_e,
                      OutIter out) {
         while (in != in_e) {
- typedef typename std::iterator_traits<OutIter>::value_type OutValue;
+ typedef typename detail::outvalue_helper<OutIter, OutputBits>::type
+ OutValue;
             OutValue value = OutValue();
             detail::imploder<Endianness, InputBits, OutputBits>
              ::implode(in, value);

Modified: sandbox/hash/libs/hash/test/pack.cpp
==============================================================================
--- sandbox/hash/libs/hash/test/pack.cpp (original)
+++ sandbox/hash/libs/hash/test/pack.cpp 2010-05-04 22:09:12 EDT (Tue, 04 May 2010)
@@ -1,12 +1,18 @@
 
-#include <cassert>
-#include <cstdio>
-
 #include <boost/array.hpp>
 #include <boost/cstdint.hpp>
 #include <boost/hash/pack.hpp>
 
+#include <sstream>
+#include <iterator>
+
+#include <cassert>
+#include <cstdio>
+
 using boost::array;
+using boost::int8_t;
+using boost::int16_t;
+using boost::int32_t;
 using namespace boost::hash;
 using namespace boost::hash::stream_endian;
 
@@ -678,5 +684,45 @@
     test_implodebl();
     test_implodell();
 
+ {
+ using namespace std;
+ istringstream iss("-1 -2 -4 -8");
+ ostringstream oss;
+ pack<big_bit, 4, 16>(istream_iterator<int>(iss), istream_iterator<int>(),
+ ostream_iterator<int>(oss, " "));
+ printf("%s\n", oss.str().c_str());
+ assert(oss.str() == "65224 ");
+ }
+
+ {
+ using namespace std;
+ istringstream iss("-1 -2 -4 -8");
+ ostringstream oss;
+ pack<little_bit, 4, 16>(istream_iterator<int>(iss), istream_iterator<int>(),
+ ostream_iterator<int>(oss, " "));
+ printf("%s\n", oss.str().c_str());
+ assert(oss.str() == "36079 ");
+ }
+
+ {
+ using namespace std;
+ istringstream iss("-312");
+ ostringstream oss;
+ pack<big_bit, 16, 4>(istream_iterator<int>(iss), istream_iterator<int>(),
+ ostream_iterator<int>(oss, " "));
+ printf("%s\n", oss.str().c_str());
+ assert(oss.str() == "15 14 12 8 ");
+ }
+
+ {
+ using namespace std;
+ istringstream iss("-29457");
+ ostringstream oss;
+ pack<little_bit, 16, 4>(istream_iterator<int>(iss), istream_iterator<int>(),
+ ostream_iterator<int>(oss, " "));
+ printf("%s\n", oss.str().c_str());
+ assert(oss.str() == "15 14 12 8 ");
+ }
+
 }
 


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