|
Boost-Commit : |
Subject: [Boost-commit] svn:boost r77045 - sandbox/SOC/2011/checks/boost/checks
From: pierre.talbot.6114_at_[hidden]
Date: 2012-02-16 17:39:47
Author: trademark
Date: 2012-02-16 17:39:46 EST (Thu, 16 Feb 2012)
New Revision: 77045
URL: http://svn.boost.org/trac/boost/changeset/77045
Log:
Change the function process to a struct processor taking a counter function as argument.
Text files modified:
sandbox/SOC/2011/checks/boost/checks/basic_check_algorithm.hpp | 13 +++++-
sandbox/SOC/2011/checks/boost/checks/basic_checks.hpp | 44 +++++++++++++++++--------
sandbox/SOC/2011/checks/boost/checks/luhn.hpp | 19 ++--------
sandbox/SOC/2011/checks/boost/checks/prechecksum.hpp | 8 ++--
sandbox/SOC/2011/checks/boost/checks/verhoeff.hpp | 67 +++++++++++++++++++++++----------------
sandbox/SOC/2011/checks/boost/checks/weighted_sum.hpp | 13 +++++-
6 files changed, 96 insertions(+), 68 deletions(-)
Modified: sandbox/SOC/2011/checks/boost/checks/basic_check_algorithm.hpp
==============================================================================
--- sandbox/SOC/2011/checks/boost/checks/basic_check_algorithm.hpp (original)
+++ sandbox/SOC/2011/checks/boost/checks/basic_check_algorithm.hpp 2012-02-16 17:39:46 EST (Thu, 16 Feb 2012)
@@ -99,10 +99,17 @@
\remarks This function should be overloaded if you want to calculate the checksum of a sequence.
*/
- static std::size_t process(std::size_t, std::size_t, std::size_t)
+ template <typename Function>
+ struct processor
{
- return std::size_t();
- }
+ Function counter;
+ processor(Function counter) : counter(counter) { }
+
+ std::size_t operator()(std::size_t checksum, std::size_t value)
+ {
+ return std::size_t();
+ }
+ };
};
Modified: sandbox/SOC/2011/checks/boost/checks/basic_checks.hpp
==============================================================================
--- sandbox/SOC/2011/checks/boost/checks/basic_checks.hpp (original)
+++ sandbox/SOC/2011/checks/boost/checks/basic_checks.hpp 2012-02-16 17:39:46 EST (Thu, 16 Feb 2012)
@@ -21,7 +21,6 @@
#include <boost/checks/detail/sequence_counter.hpp>
#include <boost/iterator/filter_iterator.hpp>
-
namespace boost {
namespace checks{
@@ -44,18 +43,33 @@
\returns The checksum of the sequence calculated with algorithm.
*/
+
+// Use bind and boost::ref instead ? But how to deduce type ?
+template <typename Iterator>
+struct deref
+{
+ Iterator &iter;
+ deref(Iterator &iter) : iter(iter) { }
+
+ std::size_t operator()() const
+ {
+ return *iter;
+ }
+};
+
template <typename algorithm,
std::size_t size_expected,
typename seq_iterator,
- typename counter_type>
-std::size_t compute_checksum(seq_iterator seq_begin, seq_iterator seq_end, counter_type &counter)
+ typename counter_iter>
+std::size_t compute_checksum(seq_iterator seq_begin, seq_iterator seq_end, counter_iter &counter)
{
+ typedef typename algorithm::template processor<deref<counter_iter> > processor;
+ processor process = processor(deref<counter_iter>(counter));
+
std::size_t checksum = 0;
- for(; seq_begin != seq_end && *counter < size_expected; ++seq_begin)
- {
- checksum = algorithm::process(checksum, *seq_begin, *counter);
- ++counter;
- }
+ for(; seq_begin != seq_end && *counter < size_expected; ++seq_begin, ++counter)
+ checksum = process(checksum, *seq_begin);
+
if(*counter != size_expected || seq_begin != seq_end)
return bad_sequence;
return checksum;
@@ -63,15 +77,15 @@
template <typename algorithm,
typename seq_iterator,
- typename counter_type>
-std::size_t compute_checksum(seq_iterator seq_begin, seq_iterator seq_end, counter_type &counter)
+ typename counter_iter>
+std::size_t compute_checksum(seq_iterator seq_begin, seq_iterator seq_end, counter_iter &counter)
{
+ typedef typename algorithm::template processor<deref<counter_iter> > processor;
+ processor process = processor(deref<counter_iter>(counter));
+
std::size_t checksum = 0;
- for(; seq_begin != seq_end; ++seq_begin)
- {
- checksum = algorithm::process(checksum, *seq_begin, *counter);
- ++counter;
- }
+ for(; seq_begin != seq_end; ++seq_begin, ++counter)
+ checksum = process(checksum, *seq_begin);
return checksum;
}
Modified: sandbox/SOC/2011/checks/boost/checks/luhn.hpp
==============================================================================
--- sandbox/SOC/2011/checks/boost/checks/luhn.hpp (original)
+++ sandbox/SOC/2011/checks/boost/checks/luhn.hpp 2012-02-16 17:39:46 EST (Thu, 16 Feb 2012)
@@ -49,29 +49,18 @@
\remarks This function become obsolete if you don't use luhn_weight. It is using operator "<<" to make internal multiplication.
*/
- static std::size_t process(std::size_t checksum, std::size_t value, std::size_t value_pos)
- {
- std::size_t weighted_value = value << (luhn_weight::at(value_pos) -1);
- return checksum + weighted_value % 10 + weighted_value / 10;
- }
- /*
template <typename Function>
struct processor
{
Function counter;
- bool weight;
- processor(Function counter) : counter(counter), weight(!(counter()&1)) { }
+ processor(Function counter) : counter(counter) { }
std::size_t operator()(std::size_t checksum, std::size_t value)
{
- return checksum + value << weight % 10 + value << weight / 10;
+ std::size_t weighted_value = value << (luhn_weight::at(counter()) -1);
+ return checksum + weighted_value % 10 + weighted_value / 10;
}
};
-
- validate_checksum <UnaryPredicate : for the checkdigit validation
- compute_checksum <UnaryFunction : for the computation of the checkdigit
-
-*/
};
/*!
@@ -108,7 +97,7 @@
template <typename check_range>
bool check_luhn (const check_range& check_seq)
{
- return boost::checks::check_sequence<luhn_algorithm, digit_prechecksum (boost::rbegin(check_seq), boost::rend(check_seq));
+ return boost::checks::check_sequence<luhn_algorithm, digit_prechecksum> (boost::rbegin(check_seq), boost::rend(check_seq));
}
/*!
Modified: sandbox/SOC/2011/checks/boost/checks/prechecksum.hpp
==============================================================================
--- sandbox/SOC/2011/checks/boost/checks/prechecksum.hpp (original)
+++ sandbox/SOC/2011/checks/boost/checks/prechecksum.hpp 2012-02-16 17:39:46 EST (Thu, 16 Feb 2012)
@@ -46,8 +46,8 @@
template <typename BaseIterator>
typename iterator<BaseIterator>::type operator()(BaseIterator b, BaseIterator e)
{
- return iterator<BaseIterator>::type(boost::filter_iterator<FilterPredicate, BaseIterator>(filter, b, e),
- converter);
+ return typename iterator<BaseIterator>::type(boost::filter_iterator<FilterPredicate, BaseIterator>(filter, b, e),
+ converter);
}
};
@@ -68,7 +68,7 @@
template <typename BaseIterator>
typename iterator<BaseIterator>::type operator()(BaseIterator b, BaseIterator e)
{
- return iterator<BaseIterator>::type(b, converter);
+ return typename iterator<BaseIterator>::type(b, converter);
}
};
@@ -88,7 +88,7 @@
template <typename BaseIterator>
typename iterator<BaseIterator>::type operator()(BaseIterator b, BaseIterator e)
{
- return iterator<BaseIterator>::type(filter, b, e);
+ return typename iterator<BaseIterator>::type(filter, b, e);
}
};
Modified: sandbox/SOC/2011/checks/boost/checks/verhoeff.hpp
==============================================================================
--- sandbox/SOC/2011/checks/boost/checks/verhoeff.hpp (original)
+++ sandbox/SOC/2011/checks/boost/checks/verhoeff.hpp 2012-02-16 17:39:46 EST (Thu, 16 Feb 2012)
@@ -49,37 +49,20 @@
\remarks This function use the classic table d and p of the Verhoeff algorithm.
*/
- static std::size_t process(std::size_t checksum, std::size_t value, std::size_t value_pos)
+ template <typename Function>
+ struct processor
{
- static const unsigned char d[10][10] =
- {
- { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 },
- { 1, 2, 3, 4, 0, 6, 7, 8, 9, 5 },
- { 2, 3, 4, 0, 1, 7, 8, 9, 5, 6 },
- { 3, 4, 0, 1, 2, 8, 9, 5, 6, 7 },
- { 4, 0, 1, 2, 3, 9, 5, 6, 7, 8 },
- { 5, 9, 8, 7, 6, 0, 4, 3, 2, 1 },
- { 6, 5, 9, 8, 7, 1, 0, 4, 3, 2 },
- { 7, 6, 5, 9, 8, 2, 1, 0, 4, 3 },
- { 8, 7, 6, 5, 9, 3, 2, 1, 0, 4 },
- { 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 }
- };
-
- static const unsigned char p[8][10] =
- {
- { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 },
- { 1, 5, 7, 6, 2, 8, 3, 0, 9, 4 },
- { 5, 8, 0, 3, 7, 9, 6, 1, 4, 2 },
- { 8, 9, 1, 6, 0, 4, 3, 5, 2, 7 },
- { 9, 4, 5, 3, 1, 2, 6, 8, 7, 0 },
- { 4, 2, 8, 6, 5, 7, 3, 9, 0, 1 },
- { 2, 7, 9, 3, 8, 0, 6, 4, 1, 5 },
- { 7, 0, 4, 6, 9, 1, 3, 2, 5, 8 }
- };
+ static const unsigned char d[10][10];
+ static const unsigned char p[8][10];
- return d[checksum][p[value_pos % 8][value]];
- }
+ Function counter;
+ processor(Function counter) : counter(counter) { }
+ std::size_t operator()(std::size_t checksum, std::size_t value)
+ {
+ return d[checksum][p[counter() % 8][value]];
+ }
+ };
/*!
\brief Validate the Verhoeff checksum.
@@ -110,6 +93,34 @@
}
};
+template <typename Function>
+const unsigned char verhoeff_algorithm::processor<Function>::d[10][10] =
+{
+ { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 },
+ { 1, 2, 3, 4, 0, 6, 7, 8, 9, 5 },
+ { 2, 3, 4, 0, 1, 7, 8, 9, 5, 6 },
+ { 3, 4, 0, 1, 2, 8, 9, 5, 6, 7 },
+ { 4, 0, 1, 2, 3, 9, 5, 6, 7, 8 },
+ { 5, 9, 8, 7, 6, 0, 4, 3, 2, 1 },
+ { 6, 5, 9, 8, 7, 1, 0, 4, 3, 2 },
+ { 7, 6, 5, 9, 8, 2, 1, 0, 4, 3 },
+ { 8, 7, 6, 5, 9, 3, 2, 1, 0, 4 },
+ { 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 }
+};
+
+template <typename Function>
+const unsigned char verhoeff_algorithm::processor<Function>::p[8][10] =
+{
+ { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 },
+ { 1, 5, 7, 6, 2, 8, 3, 0, 9, 4 },
+ { 5, 8, 0, 3, 7, 9, 6, 1, 4, 2 },
+ { 8, 9, 1, 6, 0, 4, 3, 5, 2, 7 },
+ { 9, 4, 5, 3, 1, 2, 6, 8, 7, 0 },
+ { 4, 2, 8, 6, 5, 7, 3, 9, 0, 1 },
+ { 2, 7, 9, 3, 8, 0, 6, 4, 1, 5 },
+ { 7, 0, 4, 6, 9, 1, 3, 2, 5, 8 }
+};
+
/*!
\brief Validate a sequence according to the verhoeff_check_algorithm type.
Modified: sandbox/SOC/2011/checks/boost/checks/weighted_sum.hpp
==============================================================================
--- sandbox/SOC/2011/checks/boost/checks/weighted_sum.hpp (original)
+++ sandbox/SOC/2011/checks/boost/checks/weighted_sum.hpp 2012-02-16 17:39:46 EST (Thu, 16 Feb 2012)
@@ -42,10 +42,17 @@
\param valid_value_counter is the number of valid values already counted (the current value is not included).\n This is also the position (above the valid values) of the current value analysed (0 <= valid_value_counter < n).
\param checksum is the current checksum.
*/
- static std::size_t process(std::size_t checksum, std::size_t value, std::size_t value_pos)
+ template <typename Function>
+ struct processor
{
- return checksum + value * weight::at(value_pos);
- }
+ Function counter;
+ processor(Function counter) : counter(counter) { }
+
+ std::size_t operator()(std::size_t checksum, std::size_t value)
+ {
+ return checksum + value * weight::at(counter());
+ }
+ };
};
}}// namespace boost namespace checks
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