|
Boost-Commit : |
Subject: [Boost-commit] svn:boost r77370 - sandbox/SOC/2011/checks/boost/checks
From: pierre.talbot.6114_at_[hidden]
Date: 2012-03-18 10:19:59
Author: trademark
Date: 2012-03-18 10:19:58 EDT (Sun, 18 Mar 2012)
New Revision: 77370
URL: http://svn.boost.org/trac/boost/changeset/77370
Log:
Add a size policy.
Put checksum algorithm description into a checksum structure.
Put checksum structure and size into a features structure.
Add traversal policy.
Added:
sandbox/SOC/2011/checks/boost/checks/checksum.hpp (contents, props changed)
Text files modified:
sandbox/SOC/2011/checks/boost/checks/amex.hpp | 25 ++++--
sandbox/SOC/2011/checks/boost/checks/basic_check_algorithm.hpp | 64 -----------------
sandbox/SOC/2011/checks/boost/checks/basic_checks.hpp | 146 +++++++--------------------------------
sandbox/SOC/2011/checks/boost/checks/ean.hpp | 38 ++++++---
sandbox/SOC/2011/checks/boost/checks/isbn.hpp | 33 +++++---
sandbox/SOC/2011/checks/boost/checks/luhn.hpp | 34 ++++----
sandbox/SOC/2011/checks/boost/checks/mastercard.hpp | 11 ++
sandbox/SOC/2011/checks/boost/checks/modulus11.hpp | 26 +++---
sandbox/SOC/2011/checks/boost/checks/modulus97.hpp | 39 +++++-----
sandbox/SOC/2011/checks/boost/checks/prechecksum.hpp | 8 ++
sandbox/SOC/2011/checks/boost/checks/upc.hpp | 21 +++-
sandbox/SOC/2011/checks/boost/checks/verhoeff.hpp | 38 ++++-----
sandbox/SOC/2011/checks/boost/checks/visa.hpp | 10 ++
sandbox/SOC/2011/checks/boost/checks/weighted_sum.hpp | 13 --
14 files changed, 198 insertions(+), 308 deletions(-)
Modified: sandbox/SOC/2011/checks/boost/checks/amex.hpp
==============================================================================
--- sandbox/SOC/2011/checks/boost/checks/amex.hpp (original)
+++ sandbox/SOC/2011/checks/boost/checks/amex.hpp 2012-03-18 10:19:58 EDT (Sun, 18 Mar 2012)
@@ -22,7 +22,8 @@
#include <boost/checks/filter.hpp>
#include <boost/checks/checkdigit.hpp>
#include <boost/checks/luhn.hpp>
-
+#include <boost/checks/checksum.hpp>
+
#include <boost/range/rbegin.hpp>
#include <boost/range/rend.hpp>
#include <boost/range/iterator_range.hpp>
@@ -35,12 +36,18 @@
namespace boost {
namespace checks{
+typedef features
+<
+ luhn,
+ AMEX_SIZE
+> amex;
+
/*!
\brief Validate a sequence according to the amex_check_algorithm type.
\pre check_seq is a valid range.
- \tparam check_range is a valid range type.
+ \tparam range is a valid range type.
\param check_seq is the sequence of value to check.
\throws std::invalid_argument if check_seq doesn't contain exactly AMEX_SIZE digits.
@@ -48,10 +55,10 @@
\returns @c true if the check digit is correct, @c false otherwise.
*/
-template <typename check_range>
-bool check_amex (const check_range& check_seq)
+template <typename range>
+bool check_amex (const range& x)
{
- return check_luhn<AMEX_SIZE>(check_seq);
+ return check_sequence<amex>(x);
}
/*!
@@ -59,7 +66,7 @@
\pre check_seq is a valid range.
- \tparam check_range is a valid range type.
+ \tparam range is a valid range type.
\param check_seq is the sequence of value to check.
\throws std::invalid_argument if check_seq doesn't contain exactly AMEX_SIZE_WITHOUT_CHECKDIGIT digits.
@@ -68,10 +75,10 @@
\returns The check digit. The check digit is in the range [0..9].
*/
-template <typename check_range>
-std::size_t compute_amex(const check_range& check_seq)
+template <typename range>
+std::size_t compute_amex(const range& x)
{
- return compute_luhn<AMEX_SIZE>(check_seq);
+ return compute_checkdigit<amex>(x);
}
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-03-18 10:19:58 EDT (Sun, 18 Mar 2012)
@@ -27,70 +27,6 @@
namespace boost{
namespace checks{
-
-typedef prechecksum<digit_filter, chartodigit> digit_prechecksum;
-
-typedef prechecksum<digitx_filter, chartodigitx> digitx_prechecksum;
-
-/*! \class basic_check_algorithm
- \brief The main check algorithm class that provides every static function that can be overloaded.\n Most of the functions must be re-implemented to have the desired behavior.
-
- \tparam traversal_direction must meet the iterator_direction concept requirements.
- \tparam checkdigit_size Helper functions to provide the same behavior on sequence with and without checkdigits. No "real" value in the sequence will be skipped.
-*/
-struct basic_check_algorithm
-{
- /*!
- \brief Validate the checksum.
-
- \param checksum is the checksum to validate.
-
- \returns @c true always (unless overloaded to check a sequence).
-
- \remarks This function should be overloaded if you want to check a sequence.
- */
- static bool validate_checksum(std::size_t checksum)
- {
- return true;
- }
-
- /*!
- \brief Compute the check digit of a sequence.
-
- \pre The type checkdigit must provides the default initialisation feature.
-
- \tparam checkdigit is the type of the check digit desired.
- \param checksum is the checksum used to extract the check digit.
-
- \returns default initialized value of checkdigit.
-
- \remarks This function should be overloaded if you want to compute the check digit of a sequence.
- */
- static std::size_t compute_checkdigit(std::size_t checksum)
- {
- return std::size_t();
- }
-
- /*!
- \brief Compute the check digit(s) of a sequence.
-
- \pre checkdigits must be a valid initialized iterator.
-
- \tparam checkdigits_iter must meet the OutputIterator requirements.
- \param checksum is the checksum used to extract the check digit(s).
- \param checkdigits is the iterator with which the check digit(s) will be written.
-
- \returns checkdigits.
-
- \remarks This function should be overloaded if you want your algorithm to compute more than one check digit (through it works for just one check digit too).
- */
- static std::pair<std::size_t, std::size_t> compute_multicheckdigit(std::size_t checksum)
- {
- return std::pair<std::size_t, std::size_t>();
- }
-};
-
-
}} // namespace boost namespace checks
#endif //BOOST_CHECKS_BASIC_CHECK_ALGO_HPP
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-03-18 10:19:58 EDT (Sun, 18 Mar 2012)
@@ -24,8 +24,6 @@
namespace boost {
namespace checks{
-struct no_prechecksum_tag {};
-
static const std::size_t bad_sequence = (std::size_t)-1;
/*!
@@ -44,50 +42,22 @@
\returns The checksum of the sequence calculated with algorithm.
*/
-template <typename Iterator>
-struct deref
-{
- Iterator &iter;
- deref(Iterator &iter) : iter(iter) { }
-
- std::size_t operator()() const
- {
- return *iter;
- }
-};
-
-template <template <class> class processor,
- std::size_t size_expected,
- typename seq_iterator,
+template <typename processor,
+ typename sizePolicy,
+ typename iterator,
typename counter_iter>
-std::size_t compute_checksum(seq_iterator seq_begin, seq_iterator seq_end, counter_iter &counter)
+std::size_t compute_checksum(iterator begin, iterator end, counter_iter &counter)
{
- typedef processor<deref<counter_iter> > counter_processor;
- counter_processor process = counter_processor(deref<counter_iter>(counter));
-
+ processor process;
std::size_t checksum = 0;
- for(; seq_begin != seq_end && *counter < size_expected; ++seq_begin, ++counter)
- checksum = process(checksum, *seq_begin);
+ for(; begin != end && sizePolicy::check(*counter); ++begin, ++counter)
+ checksum = process(checksum, *begin, *counter);
- if(*counter != size_expected || seq_begin != seq_end)
+ if(sizePolicy::overflow(*counter) || begin != end)
return bad_sequence;
return checksum;
}
-template <template <class> class processor,
- typename seq_iterator,
- typename counter_iter>
-std::size_t compute_checksum(seq_iterator seq_begin, seq_iterator seq_end, counter_iter &counter)
-{
- typedef processor<deref<counter_iter> > counter_processor;
- counter_processor process = counter_processor(deref<counter_iter>(counter));
-
- std::size_t checksum = 0;
- for(; seq_begin != seq_end; ++seq_begin, ++counter)
- checksum = process(checksum, *seq_begin);
- return checksum;
-}
-
/*!
\brief Validate a sequence according to algorithm.
@@ -101,49 +71,19 @@
\returns @c true if the checkdigit is correct, @c false otherwise.
*/
-template <template <class> class processor,
- typename UnaryPredicate,
- typename seq_iterator>
-bool check_sequence(seq_iterator seq_begin, seq_iterator seq_end)
+template <typename features,
+ typename range>
+bool check_sequence(range &x)
{
boost::checks::detail::simple_counter::type counter = boost::checks::detail::simple_counter()();
-
- std::size_t checksum = compute_checksum<processor>(seq_begin,
- seq_end,
- counter);
- return UnaryPredicate()(checksum);
-}
-
-/*!
- \brief Validate a sequence according to algorithm.
-
- \pre check_seq is a valid range.\n size_expected > 0 (enforced by static assert).
-
- \tparam algorithm is a set of static method use to translate, filter and calculate or verify the checkdigit.
- \tparam size_expected is the number of valid value expected in the sequence.
- \tparam check_range is a valid range type.
- \param check_seq is the sequence of value to check.
+ std::size_t checksum;
- \throws std::invalid_argument if check_seq doesn't contain size_expected valid values.
-
- \returns @c true if the checkdigit is correct, @c false otherwise.
-*/
-template <template <class> class processor,
- typename UnaryPredicate,
- std::size_t size_expected,
- typename seq_iterator>
-bool check_sequence(seq_iterator seq_begin, seq_iterator seq_end)
-{
- boost::checks::detail::simple_counter::type counter = boost::checks::detail::simple_counter()();
-
- // prechecksum_type prechecksum;
- std::size_t checksum = compute_checksum<processor,
- size_expected>(seq_begin,//prechecksum(seq_begin, seq_end),
- seq_end, //prechecksum(seq_end, seq_end),
- counter);
- if(checksum != bad_sequence)
- return UnaryPredicate()(checksum);
- return false;
+ checksum = compute_checksum<typename features::checksum::processor,
+ typename features::size_policy>
+ (features::begin(x), features::end(x), counter);
+ if(checksum == bad_sequence)
+ return false;
+ return typename features::checksum::validate_checkdigit()(checksum);
}
/*!
@@ -159,50 +99,22 @@
\returns The check digit of the type of a value in check_seq.
*/
-template <template <class> class processor,
- typename UnaryFunction,
- typename checkdigit,
- typename seq_iterator>
-std::size_t compute_checkdigit(seq_iterator seq_begin, seq_iterator seq_end)
+template <typename features,
+ typename range>
+std::size_t compute_checkdigit(range &x)
{
- typedef typename boost::checks::detail::skip_counter<checkdigit::pos, checkdigit::size> counter_type;
+ typedef typename boost::checks::detail::skip_counter<features::checksum::checkdigit_detail::pos, features::checksum::checkdigit_detail::size> counter_type;
typename counter_type::type counter = counter_type()();
- std::size_t checksum = compute_checksum<processor>(seq_begin,
- seq_end,
- counter);
- return UnaryFunction()(checksum);
-}
-
-/*!
- \brief Calculate the check digit of a sequence according to algorithm.
- \pre check_seq is a valid range.\n size_expected > 0 (enforced by static assert).
+ std::size_t checksum;
- \tparam algorithm is a set of static methods used to translate, filter and calculate or verify the checkdigit.
- \tparam size_expected is the number of valid value expected in the sequence.
- \tparam check_range is a valid range type.
- \param check_seq is the sequence of value to check.
+ checksum = compute_checksum<typename features::checksum::processor,
+ typename features::size_policy>
+ (features::begin(x), features::end(x), counter);
- \throws std::invalid_argument if check_seq doesn't contain size_expected valid values.
-
- \returns The check digit of the type of a value in check_seq.
-*/
-template <template <class> class processor,
- typename UnaryFunction,
- std::size_t size_expected,
- typename checkdigit,
- typename seq_iterator>
-std::size_t compute_checkdigit(seq_iterator seq_begin, seq_iterator seq_end)
-{
- typedef typename boost::checks::detail::skip_counter<checkdigit::pos, checkdigit::size> counter_type;
- typename counter_type::type counter = counter_type()();
- std::size_t checksum = compute_checksum<processor,
- size_expected>(seq_begin,
- seq_end,
- counter);
- if(checksum != bad_sequence)
- return UnaryFunction()(checksum);
- return bad_sequence;
+ if(checksum == bad_sequence)
+ return bad_sequence;
+ return typename features::checksum::make_checkdigit()(checksum);
}
} // namespace checks
Added: sandbox/SOC/2011/checks/boost/checks/checksum.hpp
==============================================================================
--- (empty file)
+++ sandbox/SOC/2011/checks/boost/checks/checksum.hpp 2012-03-18 10:19:58 EDT (Sun, 18 Mar 2012)
@@ -0,0 +1,135 @@
+// Boost checks/checksum.hpp header file
+// (C) Copyright Pierre Talbot 2012
+// Distributed under the Boost Software License, Version 1.0. (See
+// accompanying file LICENSE_1_0.txt or copy at
+// http://www.boost.org/LICENSE_1_0.txt
+// See http://www.boost.org for updates, documentation, and revision history.
+
+/*! \file
+ \brief
+*/
+
+#ifndef BOOST_CHECK_CHECKSUM_HPP
+#define BOOST_CHECK_CHECKSUM_HPP
+
+#ifdef _MSC_VER
+ #pragma once
+#endif
+
+#include <boost/range/rbegin.hpp>
+#include <boost/range/rend.hpp>
+#include <boost/range/iterator_range.hpp>
+
+namespace boost {
+ namespace checks{
+
+
+struct reverse_traversal
+{
+ template <typename Range>
+ struct iterator
+ {
+ typedef typename boost::range_reverse_iterator<Range>::type type;
+ };
+
+ template <typename Range>
+ static typename iterator<Range>::type begin(Range &x)
+ {
+ return boost::rbegin(x);
+ }
+
+ template <typename Range>
+ static typename iterator<Range>::type end(Range &x)
+ {
+ return boost::rend(x);
+ }
+};
+
+struct forward_traversal
+{
+ template <typename Range>
+ struct iterator
+ {
+ typedef typename boost::range_iterator<Range>::type type;
+ };
+
+ template <typename Range>
+ static typename iterator<Range>::type begin(Range &x)
+ {
+ return boost::begin(x);
+ }
+
+ template <typename Range>
+ static typename iterator<Range>::type end(Range &x)
+ {
+ return boost::end(x);
+ }
+};
+
+struct no_size_policy
+{
+ static bool check(std::size_t pos)
+ {
+ return true;
+ }
+ static bool overflow(std::size_t pos)
+ {
+ return false;
+ }
+};
+
+template <std::size_t size_expected>
+struct enforce_size_policy
+{
+ static bool check(std::size_t pos)
+ {
+ return pos < size_expected;
+ }
+ static bool overflow(std::size_t pos)
+ {
+ return pos != size_expected;
+ }
+};
+
+template
+<
+ typename TernaryFunction,
+ typename UnaryPredicate,
+ typename UnaryFunction,
+ typename Checkdigit = basic_checkdigit,
+ typename traversal = reverse_traversal
+>
+struct checksum : traversal
+{
+ typedef TernaryFunction processor;
+ typedef UnaryPredicate validate_checkdigit;
+ typedef UnaryFunction make_checkdigit;
+ typedef Checkdigit checkdigit_detail;
+};
+
+template
+<
+ typename Checksum,
+ std::size_t size_expected = 0
+>
+struct features : Checksum
+{
+ typedef Checksum checksum;
+ typedef enforce_size_policy<size_expected> size_policy;
+};
+
+template
+<
+ typename Checksum
+>
+struct features<Checksum, 0> : Checksum
+{
+ typedef Checksum checksum;
+ typedef no_size_policy size_policy;
+};
+
+
+} // namespace checks
+} // namespace boost
+
+#endif // BOOST_CHECK_CHECKSUM_HPP
Modified: sandbox/SOC/2011/checks/boost/checks/ean.hpp
==============================================================================
--- sandbox/SOC/2011/checks/boost/checks/ean.hpp (original)
+++ sandbox/SOC/2011/checks/boost/checks/ean.hpp 2012-03-18 10:19:58 EDT (Sun, 18 Mar 2012)
@@ -21,6 +21,7 @@
#include <boost/checks/basic_checks.hpp>
#include <boost/checks/modulus10.hpp>
#include <boost/checks/weighted_sum.hpp>
+#include <boost/checks/checksum.hpp>
#include <boost/range/rbegin.hpp>
#include <boost/range/rend.hpp>
@@ -43,6 +44,25 @@
*/
typedef weighted_sum<weight<1,3> > ean_processor;
+typedef checksum
+<
+ ean_processor,
+ mod10_validation,
+ mod10_checkdigit
+> ean;
+
+typedef features
+<
+ ean,
+ EAN13_SIZE
+> ean13;
+
+typedef features
+<
+ ean,
+ EAN8_SIZE
+> ean8;
+
/*!
\brief Validate a sequence according to the ean_check_algorithm type.
@@ -58,9 +78,7 @@
template <typename check_range>
bool check_ean13(const check_range& check_seq)
{
- return boost::checks::check_sequence<ean_processor::processor,
- mod10_validation,
- EAN13_SIZE> (boost::rbegin(check_seq), boost::rend(check_seq));
+ return check_sequence<ean13> (check_seq);
}
/*!
@@ -79,10 +97,7 @@
template <typename check_range>
std::size_t compute_ean13(const check_range& check_seq)
{
- return boost::checks::compute_checkdigit<ean_processor::processor,
- mod10_checkdigit,
- EAN13_SIZE,
- basic_checkdigit>(boost::rbegin(check_seq), boost::rend(check_seq));
+ return compute_checkdigit<ean13>(check_seq);
}
/*!
@@ -100,9 +115,7 @@
template <typename check_range>
bool check_ean8 (const check_range& check_seq)
{
- return boost::checks::check_sequence<ean_processor::processor,
- mod10_validation,
- EAN8_SIZE>(boost::rbegin(check_seq), boost::rend(check_seq));
+ return check_sequence<ean8>(check_seq);
}
/*!
@@ -121,10 +134,7 @@
template <typename check_range>
std::size_t compute_ean8(const check_range& check_seq)
{
- return boost::checks::compute_checkdigit<ean_processor::processor,
- mod10_checkdigit,
- EAN8_SIZE,
- basic_checkdigit>(boost::rbegin(check_seq), boost::rend(check_seq));
+ return compute_checkdigit<ean8>(check_seq);
}
Modified: sandbox/SOC/2011/checks/boost/checks/isbn.hpp
==============================================================================
--- sandbox/SOC/2011/checks/boost/checks/isbn.hpp (original)
+++ sandbox/SOC/2011/checks/boost/checks/isbn.hpp 2012-03-18 10:19:58 EDT (Sun, 18 Mar 2012)
@@ -21,6 +21,7 @@
#include <boost/checks/ean.hpp>
#include <boost/checks/modulus11.hpp>
#include <boost/checks/checkdigit.hpp>
+#include <boost/checks/checksum.hpp>
#include <boost/range/rbegin.hpp>
#include <boost/range/rend.hpp>
@@ -34,6 +35,8 @@
namespace boost {
namespace checks{
+typedef ean13 isbn13;
+
/*!
\brief Validate a sequence according to the isbn13_check_algorithm type.
@@ -46,10 +49,10 @@
\returns @c true if the check digit is correct, @c false otherwise.
*/
-template <typename check_range>
-bool check_isbn13 (const check_range& check_seq)
+template <typename range>
+bool check_isbn13 (const range& x)
{
- return check_ean13(check_seq);
+ return check_sequence<isbn13>(x);
}
/*!
@@ -65,12 +68,18 @@
\returns The check digit. The check digit is in the range [0..9].
*/
-template <typename check_range>
-std::size_t compute_isbn13 (const check_range& check_seq)
+template <typename range>
+std::size_t compute_isbn13 (const range& x)
{
- return compute_ean13(check_seq);
+ return compute_checkdigit<isbn13>(x);
}
+typedef features
+<
+ mod11,
+ ISBN10_SIZE
+> isbn10;
+
/*!
\brief Validate a sequence according to the mod11_check_algorithm type.
@@ -83,10 +92,10 @@
\returns @c true if the check digit is correct, @c false otherwise.
*/
-template <typename check_range>
-bool check_isbn10(const check_range& check_seq)
+template <typename range>
+bool check_isbn10(const range& x)
{
- return check_modulus11<ISBN10_SIZE>(check_seq);
+ return check_sequence<isbn10>(x);
}
/*!
@@ -102,10 +111,10 @@
\returns The check digit. The check digit is in the range [0..9,X].
*/
-template <typename check_range>
-std::size_t compute_isbn10(const check_range& check_seq)
+template <typename range>
+std::size_t compute_isbn10(const range& x)
{
- return compute_modulus11<ISBN10_SIZE>(check_seq);
+ return compute_checkdigit<isbn10>(x);
}
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-03-18 10:19:58 EDT (Sun, 18 Mar 2012)
@@ -23,6 +23,8 @@
#include <boost/range/rend.hpp>
#include <boost/range/iterator_range.hpp>
+#include <boost/checks/checksum.hpp>
+
namespace boost {
namespace checks{
@@ -36,19 +38,24 @@
\param checksum is the current checksum.
*/
-template <typename Function>
struct luhn_processor
{
- int weight;
- luhn_processor(Function counter) : weight((counter() & 1)^1) { }
+ luhn_processor() {}
- std::size_t operator()(std::size_t checksum, std::size_t value)
+ std::size_t operator()(std::size_t checksum, std::size_t value, std::size_t pos)
{
- std::size_t weighted_value = value << (weight ^= 1);
+ std::size_t weighted_value = value << (pos & 1);
return checksum + weighted_value % 10 + weighted_value / 10;
}
};
+typedef checksum
+<
+ luhn_processor,
+ mod10_validation,
+ mod10_checkdigit
+>
+luhn;
/*!
\brief Validate a sequence according to the luhn_check_algorithm type.
@@ -66,9 +73,7 @@
template <size_t size_expected, typename check_range>
bool check_luhn (const check_range& check_seq)
{
- return boost::checks::check_sequence<luhn_processor,
- mod10_validation,
- size_expected>(boost::rbegin(check_seq), boost::rend(check_seq));
+ return check_sequence<features<luhn, size_expected> >(check_seq);
}
/*!
@@ -86,9 +91,7 @@
template <typename check_range>
bool check_luhn (const check_range& check_seq)
{
- return boost::checks::check_sequence<luhn_processor,
- mod10_validation>
- (boost::rbegin(check_seq), boost::rend(check_seq));
+ return check_sequence<features<luhn> >(check_seq);
}
/*!
@@ -108,10 +111,7 @@
template <size_t size_expected, typename check_range>
std::size_t compute_luhn(const check_range& check_seq)
{
- return boost::checks::compute_checkdigit<luhn_processor,
- mod10_checkdigit,
- size_expected,
- basic_checkdigit>(boost::rbegin(check_seq), boost::rend(check_seq));
+ return compute_checkdigit<features<luhn, size_expected> >(check_seq);
}
/*!
@@ -130,9 +130,7 @@
template <typename check_range>
std::size_t compute_luhn (const check_range& check_seq)
{
- return boost::checks::compute_checkdigit<luhn_processor,
- mod10_checkdigit,
- basic_checkdigit>(boost::rbegin(check_seq), boost::rend(check_seq));
+ return compute_checkdigit<features<luhn> >(check_seq);
}
Modified: sandbox/SOC/2011/checks/boost/checks/mastercard.hpp
==============================================================================
--- sandbox/SOC/2011/checks/boost/checks/mastercard.hpp (original)
+++ sandbox/SOC/2011/checks/boost/checks/mastercard.hpp 2012-03-18 10:19:58 EDT (Sun, 18 Mar 2012)
@@ -19,6 +19,7 @@
#include <boost/range/rbegin.hpp>
#include <boost/range/rend.hpp>
#include <boost/range/iterator_range.hpp>
+#include <boost/checks/checksum.hpp>
#include <boost/checks/luhn.hpp>
#include <boost/checks/checkdigit.hpp>
@@ -31,6 +32,12 @@
namespace boost {
namespace checks{
+typedef features
+<
+ luhn,
+ MASTERCARD_SIZE
+> mastercard;
+
/*!
\brief Validate a sequence according to the mastercard_check_algorithm type.
@@ -47,7 +54,7 @@
template <typename check_range>
bool check_mastercard(const check_range& check_seq)
{
- return check_luhn<MASTERCARD_SIZE>(check_seq);
+ return check_sequence<mastercard>(check_seq);
}
/*!
@@ -67,7 +74,7 @@
template <typename check_range>
std::size_t compute_mastercard(const check_range& check_seq)
{
- return compute_luhn<MASTERCARD_SIZE>(check_seq);
+ return compute_checkdigit<mastercard>(check_seq);
}
Modified: sandbox/SOC/2011/checks/boost/checks/modulus11.hpp
==============================================================================
--- sandbox/SOC/2011/checks/boost/checks/modulus11.hpp (original)
+++ sandbox/SOC/2011/checks/boost/checks/modulus11.hpp 2012-03-18 10:19:58 EDT (Sun, 18 Mar 2012)
@@ -19,6 +19,7 @@
#include <boost/range/rbegin.hpp>
#include <boost/range/rend.hpp>
#include <boost/range/iterator_range.hpp>
+#include <boost/checks/checksum.hpp>
#include <boost/checks/weight.hpp>
#include <boost/checks/basic_checks.hpp>
@@ -79,6 +80,13 @@
*/
typedef weighted_sum<mod11_weight> mod11_processor;
+typedef checksum
+<
+ mod11_processor,
+ mod11_validation,
+ mod11_checkdigit
+> mod11;
+
/*!
\brief Validate a sequence according to the mod11_check_algorithm type.
@@ -95,9 +103,7 @@
template <size_t size_expected, typename check_range>
bool check_modulus11(const check_range& check_seq)
{
- return boost::checks::check_sequence<mod11_processor::processor,
- mod11_validation,
- size_expected>(boost::rbegin(check_seq), boost::rend(check_seq));
+ return boost::checks::check_sequence<features<mod11, size_expected> >(check_seq);
}
/*!
@@ -115,9 +121,7 @@
template <typename check_range>
bool check_modulus11(const check_range& check_seq)
{
- return check_sequence<mod11_processor::processor,
- mod11_validation>
- (boost::rbegin(check_seq), boost::rend(check_seq));
+ return check_sequence<features<mod11> >(check_seq);
}
/*!
@@ -137,10 +141,8 @@
template <std::size_t size_expected, typename check_range>
std::size_t compute_modulus11(const check_range& check_seq)
{
- return compute_checkdigit<mod11_processor::processor,
- mod11_checkdigit,
- size_expected,
- basic_checkdigit>(boost::rbegin(check_seq), boost::rend(check_seq));
+ return compute_checkdigit<features<mod11,
+ size_expected> >(check_seq);
}
/*!
@@ -159,9 +161,7 @@
template <typename check_range>
std::size_t compute_modulus11(const check_range& check_seq)
{
- return compute_checkdigit<mod11_processor::processor,
- mod11_checkdigit,
- basic_checkdigit>(boost::rbegin(check_seq), boost::rend(check_seq));
+ return compute_checkdigit<features<mod11> >(check_seq);
}
}} // namespace boost namespace checks
Modified: sandbox/SOC/2011/checks/boost/checks/modulus97.hpp
==============================================================================
--- sandbox/SOC/2011/checks/boost/checks/modulus97.hpp (original)
+++ sandbox/SOC/2011/checks/boost/checks/modulus97.hpp 2012-03-18 10:19:58 EDT (Sun, 18 Mar 2012)
@@ -20,6 +20,7 @@
#include <boost/preprocessor/repetition.hpp>
#include <boost/checks/weight.hpp>
#include <boost/checks/weighted_sum.hpp>
+#include <boost/checks/checksum.hpp>
namespace boost{
namespace checks{
@@ -73,23 +74,32 @@
typedef checkdigit<0, 2> mod97_10_checkdigit;
-template <typename Function>
struct mod97_10_processor
{
unsigned char weight;
- mod97_10_processor(Function counter) : weight(68)
+
+ mod97_10_processor() : weight(10)
{
- if(counter() != 0)
- weight = 10;
}
- std::size_t operator()(std::size_t checksum, std::size_t value)
+ std::size_t operator()(std::size_t checksum, std::size_t value, std::size_t pos)
{
- weight = weight * 10 % 97;
+ if(pos == 0)
+ weight = 1;
+ else
+ weight = weight * 10 % 97;
return checksum + value * weight;
}
};
+typedef checksum
+<
+ mod97_10_processor,
+ mod97_validation,
+ mod97_checkdigit,
+ mod97_10_checkdigit
+> mod97_10;
+
/*!
\brief Validate a sequence according to the mod97_10_check_algorithm type.
@@ -106,9 +116,7 @@
template <size_t size_expected, typename check_range>
bool check_mod97_10 (const check_range& check_seq)
{
- return check_sequence<mod97_10_processor,
- mod97_validation,
- size_expected>(boost::rbegin(check_seq), boost::rend(check_seq));
+ return check_sequence<features<mod97_10, size_expected> >(check_seq);
}
/*!
@@ -126,9 +134,7 @@
template <typename check_range>
bool check_mod97_10(const check_range& check_seq)
{
- return check_sequence<mod97_10_processor,
- mod97_validation>
- (boost::rbegin(check_seq), boost::rend(check_seq));
+ return check_sequence<features<mod97_10> >(check_seq);
}
/*!
@@ -150,10 +156,7 @@
template <size_t size_expected, typename check_range>
std::size_t compute_mod97_10(const check_range& check_seq)
{
- return compute_checkdigit<mod97_10_processor,
- mod97_checkdigit,
- mod97_10_checkdigit,
- size_expected>(boost::rbegin(check_seq), boost::rend(check_seq));
+ return compute_checkdigit<features<mod97_10, size_expected> >(check_seq);
}
/*!
@@ -174,9 +177,7 @@
template <typename check_range>
std::size_t compute_mod97_10(const check_range& check_seq)
{
- return compute_checkdigit<mod97_10_processor,
- mod97_checkdigit,
- mod97_10_checkdigit>(boost::rbegin(check_seq), boost::rend(check_seq));
+ return compute_checkdigit<features<mod97_10> >(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-03-18 10:19:58 EDT (Sun, 18 Mar 2012)
@@ -21,9 +21,15 @@
#include <boost/iterator/filter_iterator.hpp>
#include <boost/iterator/transform_iterator.hpp>
+#include <boost/checks/filter.hpp>
+#include <boost/checks/conversion.hpp>
+
namespace boost {
namespace checks{
+
+
+
template <
typename Prechecksum,
typename Iterator
@@ -124,6 +130,8 @@
}
};
+typedef prechecksum<digit_filter, chartodigit> digit_prechecksum;
+typedef prechecksum<digitx_filter, chartodigitx> digitx_prechecksum;
} // namespace checks
} // namespace boost
Modified: sandbox/SOC/2011/checks/boost/checks/upc.hpp
==============================================================================
--- sandbox/SOC/2011/checks/boost/checks/upc.hpp (original)
+++ sandbox/SOC/2011/checks/boost/checks/upc.hpp 2012-03-18 10:19:58 EDT (Sun, 18 Mar 2012)
@@ -19,6 +19,7 @@
#include <boost/range/rbegin.hpp>
#include <boost/range/rend.hpp>
#include <boost/range/iterator_range.hpp>
+#include <boost/checks/checksum.hpp>
#include <boost/checks/weight.hpp>
#include <boost/checks/basic_checks.hpp>
@@ -40,6 +41,17 @@
typedef weighted_sum<upc_weight> upc_processor;
+typedef features
+<
+ checksum
+ <
+ upc_processor,
+ mod10_validation,
+ mod10_checkdigit
+ >,
+ UPCA_SIZE
+> upca;
+
/*!
\brief Validate a sequence according to the upc_check_algorithm type.
@@ -55,9 +67,7 @@
template <typename check_range>
bool check_upca (const check_range& check_seq)
{
- return check_sequence<upc_processor::processor,
- mod10_validation,
- UPCA_SIZE>(boost::rbegin(check_seq), boost::rend(check_seq));
+ return check_sequence<upca>(check_seq);
}
/*!
@@ -76,10 +86,7 @@
template <typename check_range>
std::size_t compute_upca(const check_range& check_seq)
{
- return boost::checks::compute_checkdigit<upc_processor::processor,
- mod10_checkdigit,
- UPCA_SIZE,
- basic_checkdigit>(boost::rbegin(check_seq), boost::rend(check_seq));
+ return compute_checkdigit<upca>(check_seq);
}
}} // namespace boost namespace checks
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-03-18 10:19:58 EDT (Sun, 18 Mar 2012)
@@ -22,6 +22,7 @@
#include <boost/range/rbegin.hpp>
#include <boost/range/rend.hpp>
#include <boost/range/iterator_range.hpp>
+#include <boost/checks/checksum.hpp>
#include <boost/checks/weight.hpp>
#include <boost/checks/checkdigit.hpp>
@@ -82,23 +83,20 @@
\remarks This function use the classic table d and p of the Verhoeff algorithm.
*/
-template <typename Function>
struct verhoeff_processor
{
static const unsigned char d[10][10];
static const unsigned char p[8][10];
- Function counter;
- verhoeff_processor(Function counter) : counter(counter) { }
+ verhoeff_processor() { }
- std::size_t operator()(std::size_t checksum, std::size_t value)
+ std::size_t operator()(std::size_t checksum, std::size_t value, std::size_t pos)
{
- return d[checksum][p[counter() % 8][value]];
+ return d[checksum][p[pos % 8][value]];
}
};
-template <typename Function>
-const unsigned char verhoeff_processor<Function>::d[10][10] =
+const unsigned char verhoeff_processor::d[10][10] =
{
{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 },
{ 1, 2, 3, 4, 0, 6, 7, 8, 9, 5 },
@@ -112,8 +110,7 @@
{ 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 }
};
-template <typename Function>
-const unsigned char verhoeff_processor<Function>::p[8][10] =
+const unsigned char verhoeff_processor::p[8][10] =
{
{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 },
{ 1, 5, 7, 6, 2, 8, 3, 0, 9, 4 },
@@ -125,6 +122,13 @@
{ 7, 0, 4, 6, 9, 1, 3, 2, 5, 8 }
};
+typedef checksum
+<
+ verhoeff_processor,
+ verhoeff_validation,
+ verhoeff_checkdigit
+> verhoeff;
+
/*!
\brief Validate a sequence according to the verhoeff_check_algorithm type.
@@ -141,9 +145,7 @@
template <size_t size_expected, typename check_range>
bool check_verhoeff(const check_range& check_seq)
{
- return boost::checks::check_sequence<verhoeff_processor,
- verhoeff_validation,
- size_expected>(boost::rbegin(check_seq), boost::rend(check_seq));
+ return check_sequence<features<verhoeff, size_expected> >(check_seq);
}
/*!
@@ -161,8 +163,7 @@
template <typename check_range>
bool check_verhoeff(const check_range& check_seq)
{
- return boost::checks::check_sequence<verhoeff_processor,
- verhoeff_validation>(boost::rbegin(check_seq), boost::rend(check_seq));
+ return check_sequence<features<verhoeff_processor> >(check_seq);
}
/*!
@@ -182,10 +183,7 @@
template <size_t size_expected, typename check_range>
std::size_t compute_verhoeff(const check_range& check_seq)
{
- return compute_checkdigit<verhoeff_processor,
- verhoeff_checkdigit,
- size_expected,
- basic_checkdigit>(boost::rbegin(check_seq), boost::rend(check_seq));
+ return compute_checkdigit<features<verhoeff, size_expected> >(check_seq);
}
/*!
@@ -204,9 +202,7 @@
template <typename check_range>
std::size_t compute_verhoeff(const check_range& check_seq)
{
- return compute_checkdigit<verhoeff_processor,
- verhoeff_checkdigit,
- basic_checkdigit>(boost::rbegin(check_seq), boost::rend(check_seq));
+ return compute_checkdigit<features<verhoeff> >(check_seq);
}
}}
Modified: sandbox/SOC/2011/checks/boost/checks/visa.hpp
==============================================================================
--- sandbox/SOC/2011/checks/boost/checks/visa.hpp (original)
+++ sandbox/SOC/2011/checks/boost/checks/visa.hpp 2012-03-18 10:19:58 EDT (Sun, 18 Mar 2012)
@@ -19,6 +19,7 @@
#include <boost/range/rbegin.hpp>
#include <boost/range/rend.hpp>
#include <boost/range/iterator_range.hpp>
+#include <boost/checks/checksum.hpp>
#include <boost/checks/luhn.hpp>
#include <boost/checks/checkdigit.hpp>
@@ -30,6 +31,11 @@
namespace boost {
namespace checks{
+typedef features
+<
+ luhn,
+ VISA_SIZE
+> visa;
/*!
\brief Validate a sequence according to the visa_check_algorithm type.
@@ -47,7 +53,7 @@
template <typename check_range>
bool check_visa(const check_range& check_seq)
{
- return check_luhn<VISA_SIZE>(check_seq);
+ return check_sequence<visa>(check_seq);
}
/*!
@@ -67,7 +73,7 @@
template <typename check_range>
std::size_t compute_visa(const check_range& check_seq)
{
- return compute_luhn<VISA_SIZE>(check_seq);
+ return compute_checkdigit<visa>(check_seq);
}
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-03-18 10:19:58 EDT (Sun, 18 Mar 2012)
@@ -35,17 +35,10 @@
template <typename weight>
struct weighted_sum
{
- template <typename Function>
- struct processor
+ std::size_t operator()(std::size_t checksum, std::size_t value, std::size_t 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());
- }
- };
+ return checksum + value * weight::at(pos);
+ }
};
}}// 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