Compare commits

...

52 Commits

Author SHA1 Message Date
0c86f9a11a 2nd draft with zero errors from Inspect. 2015-01-28 17:06:04 +00:00
b82f5a792b Added examples in /example folder (contain snippets for Quickbook), and updates to add typedef static_gcd_t for common factor 2015-01-27 14:27:08 +00:00
2de3837ca6 First commit of extensively revised documentation including examples for all items and using Quickbook code snippets in the text. TODO Compile failure in common_factor_ct example. Derived html files NOT saved. 2015-01-27 11:53:20 +00:00
c5370a9705 Fix ambiguous 'detail' errors under msvc-8.0. 2015-01-18 20:04:09 +02:00
8dae82faeb Replace math with integer, untabify. 2015-01-13 20:53:02 +02:00
1a72919419 Add common_factor_test to test/Jamfile, switch to lightweight_test. 2015-01-13 20:33:50 +02:00
acf272ee7d Move common_factor headers to integer/ 2015-01-13 19:55:58 +02:00
7ada3ee926 Reapply final math-gcd.qbk commit. 2015-01-13 19:49:06 +02:00
4622a12f01 Added licence info.
[SVN r84266]
2015-01-13 19:42:24 +02:00
88b3ac5619 Change to quickbook 1.6. Change to use chapters at the top level. Big search and replace on link names.
[SVN r84201]
2015-01-13 19:42:11 +02:00
ae8ee599ac Reorganise directory structure to make things easier to find and maintain.
[SVN r84143]
2015-01-13 19:42:11 +02:00
9677dbd035 Added missing #include <iostream>
[SVN r81697]
2015-01-13 19:42:10 +02:00
e394f8fd86 Change PDF URL to SF download page. Add optional index generation.
[SVN r67545]
2015-01-13 19:42:10 +02:00
95a4f1e235 Update docs to match code.
[SVN r63409]
2015-01-13 19:42:09 +02:00
f194e652ab Fix links to pdfs in the maths documentation.
[SVN r45861]
2015-01-13 19:42:09 +02:00
e64047b7c7 Fix typo in example. Added links to PDF versions of the docs. Regenerated all the docs to fix people links.
[SVN r43346]
2015-01-13 19:42:08 +02:00
dcf16f30bc Added refactored docs for existing Boost.Math libraries.
[SVN r39851]
2015-01-13 19:42:08 +02:00
b70c89a3ff Remove BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
Process #ifndef...#else...#endif blocks.

[SVN r86245]
2015-01-13 19:41:33 +02:00
9f43b6a7d6 Removed missed usage of deprecated macros in Boost.Math
[SVN r81586]
2015-01-13 19:41:32 +02:00
fecb6b5509 Fix numeric_limits specialization.
[SVN r79419]
2015-01-13 19:41:32 +02:00
eea434b0f1 eliminated test_case_template.hpp
[SVN r74735]
2015-01-13 19:41:31 +02:00
72b569f84f MSVC warning suppression.
[SVN r69509]
2015-01-13 19:41:31 +02:00
ce4bd6cc98 Fix signed/unsigned comparison warnings when char is unsigned.
[SVN r63496]
2015-01-13 19:41:31 +02:00
d38557f5f3 Make the compile time gcd facilities work with greatest integer types, and be mpl compatible.
[SVN r63408]
2015-01-13 19:41:30 +02:00
5174291e08 Fix failures when used with an expression-template enabled number type such as the gmpxx clases. Add additional concept check for integer code using gmp classes. Fixes #4139.
[SVN r61837]
2015-01-13 19:41:29 +02:00
7c528c325e Tweaked files to pass inspection report; one file had some min/max violations; but all had to have 'boostinspect:nolicense' set because a copyrighter, Paul Moore, has disappeared, so his files can never be changed to the main Boost license
[SVN r47847]
2015-01-13 19:41:29 +02:00
bdafb07012 Replace a couple of non-ascii symbols.
[SVN r46943]
2015-01-13 19:41:28 +02:00
fe60fee95b Fix more inspect report issues, and regenerated the docs.
[SVN r46839]
2015-01-13 19:41:28 +02:00
e20431e713 Changed long long to boost::long_long_type and unsigned long long to boost::ulong_long_type. A couple of other typo corrections, to get the code compiling with g++ -pedantic.
[SVN r44877]
2015-01-13 19:41:27 +02:00
217fa37de2 Fix min/max usage violation.
[SVN r43145]
2015-01-13 19:41:27 +02:00
307fee457a Fix Borland infinite looping issue.
[SVN r39723]
2015-01-13 19:41:27 +02:00
a9d91361fa Undo previous fix, it causes regressions.
[SVN r37022]
2015-01-13 19:41:26 +02:00
f1076ff42c STLport workaround.
[SVN r37001]
2015-01-13 19:41:26 +02:00
18a37cbab2 various fixes for old compilers
[SVN r36259]
2015-01-13 19:41:25 +02:00
80b59192ae Made the GCD specializations for '(unsigned) long long' and '(unsigned) __int64' mutually exclusive, following the lead of <boost/cstdint.hpp>.
[SVN r35999]
2015-01-13 19:41:25 +02:00
1f5f827a5e Changed GCD for built-in integer types to use the binary-GCD algorithm
[SVN r35850]
2015-01-13 19:41:25 +02:00
56b287a590 Added custom integer types, signed and unsigned, with and without std::numeric_limits<> specializations, to test
[SVN r35830]
2015-01-13 19:41:24 +02:00
c8faa83d47 Changed test to use Boost.Test's unit test system
[SVN r35815]
2015-01-13 19:41:24 +02:00
c041451a54 Added missing 'typename'
[SVN r27219]
2015-01-13 19:41:23 +02:00
6e6179b932 Replaced BOOST_TEST
[SVN r27050]
2015-01-13 19:41:23 +02:00
791b547d25 Changed files to BSL based on permissions in blanket-permission.txt
[SVN r25451]
2015-01-13 19:41:22 +02:00
cd696b2f6a License update
[SVN r24180]
2015-01-13 19:41:22 +02:00
fadad77a39 Merged fix from release branch to main trunk.
[SVN r15607]
2015-01-13 19:41:22 +02:00
9880cf69dc Adjusted for moving the Compile-Time and Run-Time GCD and LCM components to new, separate headers
[SVN r14273]
2015-01-13 19:41:21 +02:00
56bd3fd0b3 Moved the Compile-Time and Run-Time components to separate headers
[SVN r14272]
2015-01-13 19:41:21 +02:00
bb16ad8ea0 Added tests for unsigned int types
[SVN r13685]
2015-01-13 19:41:20 +02:00
82ad7a9edb Added fixes for the case that BOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS is defined.
[SVN r13684]
2015-01-13 19:41:20 +02:00
6a8080014a Integrated MSVC fixes from Daryle
[SVN r12012]
2015-01-13 19:41:19 +02:00
f6738d210c fix errors (from Daryle Walker)
[SVN r11935]
2015-01-13 19:41:19 +02:00
5f810b2754 Daryle Walker's GCD library submission
[SVN r11814]
2015-01-13 19:41:18 +02:00
7c2b4a799d Merge pull request #5 from danieljames/metadata
Create metadata file.
2014-08-18 09:07:49 -07:00
dcfac1423f Add metadata file. 2014-08-18 14:59:48 +01:00
27 changed files with 5251 additions and 303 deletions

View File

@ -1,12 +1,88 @@
# jamfile to build Boost.Integer Documentation.
# Copyright John Maddock 2005. Use, modification, and distribution are
# subject to 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)
using quickbook ;
# Revised to include Doxygen and Autoindex, and code snippets from examples.
# Copyright Paul A. Bristow 2015
using boostbook ;
using doxygen ; # Required if you want to use Doxygen.
using quickbook ;
using auto-index ;
path-constant here : . ; # convenient to refer to files in the same directory as this jamfile.v2
path-constant parent : .. ; # Parent directory, /libs/integer/
if --enable-index in [ modules.peek : ARGV ]
{
ECHO "Building the Integer docs with automatic index generation enabled." ;
using auto-index ;
project integer_doc : requirements
<auto-index>on
<auto-index-script>integer.idx
<auto-index-prefix>.
<auto-index-verbose>on
<format>html:<auto-index-internal>on
<format>html:<xsl:param>generate.index=0
<format>pdf:<auto-index-internal>on
<format>pdf:<xsl:param>index.on.type=1
<quickbook-define>enable_index ;
}
else
{
project integer_doc ;
ECHO "Building the Integer docs with automatic index generation disabled. Try building with --enable-index." ;
}
xml integer
:
integer.qbk # This is 'root' Quickbook file (that may include other .qbk files).
;
doxygen autodoc
:
[ glob $(here)/../include/boost/*.hpp ]
[ glob $(here)/../include/boost/integer/*.hpp ]
# but could also include this and switch Boost.Integer C++ reference info to include implementation details (or not) using Doxygen macro DETAIL.
# See http://www.stack.nl/~dimitri/doxygen/manual/commands.html#cmdcond
# and http://www.stack.nl/~dimitri/doxygen/manual/config.html#cfg_enabled_sections
# by adding this line below with other Doxygen parameters
# <doxygen:param>ENABLED_SECTIONS="DETAIL"
# Or setting this macro value ENABLED_SECTIONS="DETAIL" in /doxygen/integer_doxyfile.txt for Standalone Doxygen documentaation.
# This might be useful for maintainers.
:
<doxygen:param>PROJECT_NAME="Integer"
<doxygen:param>RECURSIVE=NO # Specify all folders and files above explicitly.
<doxygen:param>ENABLE_PREPROCESSING=YES
<doxygen:param>EXPAND_ONLY_PREDEF=YES
<doxygen:param>EXTRACT_ALL=NO
<doxygen:param>EXTRACT_PRIVATE=NO
<doxygen:param>HIDE_UNDOC_MEMBERS=YES
<doxygen:param>MACRO_EXPANSION=YES
<doxygen:param>SORT_MEMBER_DOCS=NO
<doxygen:param>SHOW_INCLUDE_FILES=NO
<doxygen:param>MAX_INITIALIZER_LINES=0
<doxygen:param>VERBATIM_HEADERS=NO
<doxygen:param>WARNINGS=NO # If WARN_IF_UNDOCUMENTED is set to YES, then doxygen will generate warnings for undocumented members.
# If EXTRACT_ALL is set to YES then this flag will automatically be disabled.
<doxygen:param>WARN_IF_UNDOCUMENTED=YES # If WARN_IF_UNDOCUMENTED is set to YES,
# then doxygen will generate warnings for all undocumented members.
<doxygen:param>WARN_IF_DOC_ERROR=YES # If WARN_IF_DOC_ERROR is set to YES, Doxygen will generate warnings for
# potential errors in the documentation.
<doxygen:param>WARN_LOGFILE=AutoDoxywarnings.log # This may not be empty (usually not a good sign!), depending on options chosen.
# Much better to send message to a logfile than the default stderr.
# and make sure that there are no Doxygen errors or significant warnings in the log file.
#<reftitle>"Reference" # Default is "Reference" but helpful to specify library.
<xsl:param>"boost.doxygen.reftitle=Boost.Integer C++ Reference"
# See Doxygen configuration for detailed explanation of these options.
# Usually these match those in /doc/doxygen/integer_doxyfile.txt.
;
xml integer : integer.qbk ;
boostbook standalone
:
integer
@ -14,6 +90,7 @@ boostbook standalone
# HTML options first:
# Use graphics not text for navigation:
<xsl:param>navig.graphics=1
<xsl:param>chapter.autolabel=0 # No Chapter numbering.
# How far down we chunk nested sections, basically all of them:
<xsl:param>chunk.section.depth=1
# Don't put the first section on the same page as the TOC:
@ -26,7 +103,7 @@ boostbook standalone
<xsl:param>generate.section.toc.level=4
# Path for links to Boost:
<xsl:param>boost.root=../../../..
# PDF Options:
# TOC Generation: this is needed for FOP-0.9 and later:
<xsl:param>fop1.extensions=0
@ -47,10 +124,20 @@ boostbook standalone
# better use SVG's instead:
<format>pdf:<xsl:param>admon.graphics.extension=".svg"
<format>pdf:<xsl:param>admon.graphics.path=$(boost-images)/
<format>pdf:<xsl:param>boost.url.prefix=http://www.boost.org/doc/libs/release/libs/regex/doc/html
# Use Arial for PDF.
<format>pdf:<xsl:param>title.font.family="sans-serif"
<format>pdf:<xsl:param>body.font.family="sans-serif"
<format>pdf:<xsl:param>monospace.font.family="sans-serif"
<dependency>autodoc # Add Doxygen info.
;
install pdfinstall : standalone/<format>pdf : <location>. <install-type>PDF ;
# install pdfinstall : standalone/<format>pdf : <location>. <install-type>PDF <name>boost.pdf ;
install pdf-install : standalone : <install-type>PDF <location>. <name>integer.pdf ;
explicit pdfinstall ;

View File

File diff suppressed because it is too large Load Diff

247
doc/gcd/math-gcd.qbk Normal file
View File

@ -0,0 +1,247 @@
[mathpart gcd_lcm Integer Utilities (Greatest Common Divisor and Least Common Multiple)]
[section Introduction]
The class and function templates in <boost/math/common_factor.hpp>
provide run-time and compile-time evaluation of the greatest common divisor
(GCD) or least common multiple (LCM) of two integers.
These facilities are useful for many numeric-oriented generic
programming problems.
[endsect]
[section Synopsis]
namespace boost
{
namespace math
{
template < typename IntegerType >
class gcd_evaluator;
template < typename IntegerType >
class lcm_evaluator;
template < typename IntegerType >
IntegerType gcd( IntegerType const &a, IntegerType const &b );
template < typename IntegerType >
IntegerType lcm( IntegerType const &a, IntegerType const &b );
typedef ``['see-below]`` static_gcd_type;
template < static_gcd_type Value1, static_gcd_type Value2 >
struct static_gcd;
template < static_gcd_type Value1, static_gcd_type Value2 >
struct static_lcm;
}
}
[endsect]
[section GCD Function Object]
[*Header: ] [@../../../../boost/math/common_factor_rt.hpp <boost/math/common_factor_rt.hpp>]
template < typename IntegerType >
class boost::math::gcd_evaluator
{
public:
// Types
typedef IntegerType result_type;
typedef IntegerType first_argument_type;
typedef IntegerType second_argument_type;
// Function object interface
result_type operator ()( first_argument_type const &a,
second_argument_type const &b ) const;
};
The boost::math::gcd_evaluator class template defines a function object
class to return the greatest common divisor of two integers.
The template is parameterized by a single type, called IntegerType here.
This type should be a numeric type that represents integers.
The result of the function object is always nonnegative, even if either of
the operator arguments is negative.
This function object class template is used in the corresponding version of
the GCD function template. If a numeric type wants to customize evaluations
of its greatest common divisors, then the type should specialize on the
gcd_evaluator class template.
[endsect]
[section LCM Function Object]
[*Header: ] [@../../../../boost/math/common_factor_rt.hpp <boost/math/common_factor_rt.hpp>]
template < typename IntegerType >
class boost::math::lcm_evaluator
{
public:
// Types
typedef IntegerType result_type;
typedef IntegerType first_argument_type;
typedef IntegerType second_argument_type;
// Function object interface
result_type operator ()( first_argument_type const &a,
second_argument_type const &b ) const;
};
The boost::math::lcm_evaluator class template defines a function object
class to return the least common multiple of two integers. The template
is parameterized by a single type, called IntegerType here. This type
should be a numeric type that represents integers. The result of the
function object is always nonnegative, even if either of the operator
arguments is negative. If the least common multiple is beyond the range
of the integer type, the results are undefined.
This function object class template is used in the corresponding version
of the LCM function template. If a numeric type wants to customize
evaluations of its least common multiples, then the type should
specialize on the lcm_evaluator class template.
[endsect]
[section:run_time Run-time GCD & LCM Determination]
[*Header: ] [@../../../../boost/math/common_factor_rt.hpp <boost/math/common_factor_rt.hpp>]
template < typename IntegerType >
IntegerType boost::math::gcd( IntegerType const &a, IntegerType const &b );
template < typename IntegerType >
IntegerType boost::math::lcm( IntegerType const &a, IntegerType const &b );
The boost::math::gcd function template returns the greatest common
(nonnegative) divisor of the two integers passed to it.
The boost::math::lcm function template returns the least common
(nonnegative) multiple of the two integers passed to it.
The function templates are parameterized on the function arguments'
IntegerType, which is also the return type. Internally, these function
templates use an object of the corresponding version of the
gcd_evaluator and lcm_evaluator class templates, respectively.
[endsect]
[section:compile_time Compile time GCD and LCM determination]
[*Header: ] [@../../../../boost/math/common_factor_ct.hpp <boost/math/common_factor_ct.hpp>]
typedef ``['unspecified]`` static_gcd_type;
template < static_gcd_type Value1, static_gcd_type Value2 >
struct boost::math::static_gcd : public mpl::integral_c<static_gcd_type, implementation_defined>
{
};
template < static_gcd_type Value1, static_gcd_type Value2 >
struct boost::math::static_lcm : public mpl::integral_c<static_gcd_type, implementation_defined>
{
};
The type `static_gcd_type` is the widest unsigned-integer-type that is supported
for use in integral-constant-expressions by the compiler. Usually this
the same type as `boost::uintmax_t`, but may fall back to being `unsigned long`
for some older compilers.
The boost::math::static_gcd and boost::math::static_lcm class templates
take two value-based template parameters of the ['static_gcd_type] type
and inherit from the type `boost::mpl::integral_c`.
Inherited from the base class, they have a member /value/
that is the greatest common factor or least
common multiple, respectively, of the template arguments.
A compile-time error will occur if the least common multiple
is beyond the range of `static_gcd_type`.
[h3 Example]
#include <boost/math/common_factor.hpp>
#include <algorithm>
#include <iterator>
#include <iostream>
int main()
{
using std::cout;
using std::endl;
cout << "The GCD and LCM of 6 and 15 are "
<< boost::math::gcd(6, 15) << " and "
<< boost::math::lcm(6, 15) << ", respectively."
<< endl;
cout << "The GCD and LCM of 8 and 9 are "
<< boost::math::static_gcd<8, 9>::value
<< " and "
<< boost::math::static_lcm<8, 9>::value
<< ", respectively." << endl;
int a[] = { 4, 5, 6 }, b[] = { 7, 8, 9 }, c[3];
std::transform( a, a + 3, b, c, boost::math::gcd_evaluator<int>() );
std::copy( c, c + 3, std::ostream_iterator<int>(cout, " ") );
}
[endsect]
[section:gcd_header Header <boost/math/common_factor.hpp>]
This header simply includes the headers
[@../../../../boost/math/common_factor_ct.hpp <boost/math/common_factor_ct.hpp>]
and [@../../../../boost/math/common_factor_rt.hpp <boost/math/common_factor_rt.hpp>].
Note this is a legacy header: it used to contain the actual implementation,
but the compile-time and run-time facilities
were moved to separate headers (since they were independent of each other).
[endsect]
[section:demo Demonstration Program]
The program [@../../../../libs/math/test/common_factor_test.cpp common_factor_test.cpp] is a demonstration of the results from
instantiating various examples of the run-time GCD and LCM function
templates and the compile-time GCD and LCM class templates.
(The run-time GCD and LCM class templates are tested indirectly through
the run-time function templates.)
[endsect]
[section Rationale]
The greatest common divisor and least common multiple functions are
greatly used in some numeric contexts, including some of the other
Boost libraries. Centralizing these functions to one header improves
code factoring and eases maintainence.
[endsect]
[section:gcd_history History]
* 13 May 2013 Moved into main Boost.Math Quickbook documentation.
* 17 Dec 2005: Converted documentation to Quickbook Format.
* 2 Jul 2002: Compile-time and run-time items separated to new headers.
* 7 Nov 2001: Initial version
[endsect]
[section:gcd_credits Credits]
The author of the Boost compilation of GCD and LCM computations is
Daryle Walker. The code was prompted by existing code hiding in the
implementations of Paul Moore's rational library and Steve Cleary's
pool library. The code had updates by Helmut Zeisel.
[endsect]
[endmathpart]
[/
Copyright 2005, 2013 Daryle Walker.
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).
]

28
doc/integer.idx Normal file
View File

@ -0,0 +1,28 @@
# integer.idx for Boost.integer/Spreadinteger Quickbook, Doxygen and Auto-index.
# Note needs more customization to be useful in practice! TODO?
#!debug "\<\w*\>"
# integer Header files.
!scan-path "../include/boost/" ".*\.hpp" false
!scan-path "../include/boost/integer" ".*\.hpp" false
# All example source files, assuming no sub-folders.
# integer example files.
#!scan-path "../example" ".*\.cpp" true
# Allow alternative spellings colour | color, and plurals etc.
color \<\w*(colour|color)\w*\>
example \<example\w*\>
font \<font\w*\>
greek
ioflags
origin
outlier
maximum \<max\w*\>
minimum \<min\w*\>
precision
range \<\w*\range\w*\>
title
Unicode \<unicode\w*\>

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,62 @@
// boost integer_common_factor_example.cpp
// Copyright Paul A. Bristow 2015
// Use, modification and distribution are subject to 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)
// Caution: this file contains Quickbook markup as well as code
// and comments, don't change any of the special comment markups!
// #include <boost/integer/common_factor.hpp> // Includes both ct and rt but now deprecated.
//[integer_common_factor_example_0]
#include <boost/integer/common_factor_ct.hpp> // For compile-time GCD & LCM Determination.
#include <boost/integer/common_factor_rt.hpp> // For run-time GCD & LCM Determination.
//] [/integer_common_factor_example_0]
#include <iostream>
#include <algorithm>
#include <iterator>
#include <limits> // for numeric_limits min and max.
int main()
{
//[integer_common_factor_example_1
std::cout << "The run-time GCD and LCM of 6 and 15 are "
<< boost::integer::gcd(6, 15) << " and "
<< boost::integer::lcm(6, 15) << ", respectively."
<< std::endl;
// The GCD and LCM of 6 and 15 are 3 and 30, respectively.
//] [/integer_common_factor_example_1]
//[integer_common_factor_example_2
std::cout << "The compile-time GCD and LCM of 8 and 9 are "
<< boost::integer::static_gcd<8, 9>::value
<< " and "
<< boost::integer::static_lcm<8, 9>::value
<< ", respectively." << std::endl;
// The GCD and LCM of 8 and 9 are 1 and 72, respectively.
//] [/integer_common_factor_example_2]
//[integer_common_factor_example_3
int a[] = { 4, 5, 6 }, b[] = { 7, 8, 9 }, c[3];
std::transform(a, a + 3, b, c, boost::integer::gcd_evaluator<int>());
std::copy(c, c + 3, std::ostream_iterator<int>(std::cout, " "));
//] [/integer_common_factor_example_3]
} // int main
/*
//[integer_common_factor_example_output_1
The run-time GCD and LCM of 6 and 15 are 3 and 30, respectively.
The compile-time GCD and LCM of 8 and 9 are 1 and 72, respectively.
//] [/integer_common_factor_example_output_1]
//[integer_common_factor_example_output_2
1 1 3
//] [/integer_common_factor_example_output_2]
*/

View File

@ -0,0 +1,41 @@
// boost integer_log2_example.cpp
// Copyright Paul A. Bristow 2015
// Use, modification and distribution are subject to 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)
// Caution: this file contains Quickbook markup as well as code
// and comments, don't change any of the special comment markups!
//[integer_log2_example_0
#include <boost/integer/static_log2.hpp> // For boost::static_log2
//] [/integer_log2s_example_0]
#include <iostream>
#include <limits> // for numeric_limits min and max.
//[integer_log2_example_
//] [/integer_log2s_example_]
int main()
{
//[integer_log2_example_1
int n = boost::static_log2<256>::value;
//] [/integer_log2_example_1]
std::cout << n << std::endl; // 8
//[integer_log2_example_2
int n2 = boost::static_log2<65536>::value;
//] [/integer_log2_example_2]
std::cout << n2 << std::endl; // 16
} // int main
/*
//[integer_log2_example_output
8
//] [/integer_log2_example_output]
*/

View File

@ -0,0 +1,69 @@
// boost integer_mask_example.cpp
// Copyright Paul A. Bristow 2015
// Use, modification and distribution are subject to 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)
// Caution: this file contains Quickbook markup as well as code
// and comments, don't change any of the special comment markups!
//[integer_mask_example_0
#include <boost/integer/integer_mask.hpp> // For boost::high_bit_mask_t etc
//] [/integer_masks_example_0]
#include <iostream>
#include <limits> // for numeric_limits min and max.
//[integer_mask_example_
//] [/integer_masks_example_]
int main()
{
//[integer_mask_example_1
typedef boost::high_bit_mask_t<29> mask1_type;
typedef boost::low_bits_mask_t<15> mask2_type;
//] [/integer_masks_example_1]
//[integer_mask_example_2
// high_bit_mask_t
std::cout << std::hex << mask1_type::bit_position << std::endl; // 1d
std::cout << std::hex << mask1_type::high_bit << std::endl; // 20000000
std::cout << std::hex << mask1_type::high_bit_fast << std::endl; // 20000000
// low_bits_mask_t
std::cout << std::hex << mask2_type::bit_count << std::endl; // F
std::cout << std::hex << mask2_type::sig_bits << std::endl; // 7fff
std::cout << std::hex << mask2_type::sig_bits_fast << std::endl; // 7fff
//] [/integer_masks_example_2]
//[integer_mask_example_3
mask1_type::least my_var1;
mask2_type::fast my_var2;
//] [/integer_masks_example_3]
//[integer_mask_example_4
my_var1 |= mask1_type::high_bit;
std::cout << std::hex << my_var1 << std::endl; // eccccccc
//] [/integer_masks_example_4]
//[integer_mask_example_5
my_var2 &= mask2_type::sig_bits;
std::cout << std::hex << my_var2 << std::endl; // 4ccc
//] [/integer_masks_example_5]
} // int main
/*
//[integer_mask_example_output
1d
20000000
20000000
f
7fff
7fff
eccccccc
4ccc
//] [/integer_mask_example_output]
*/

View File

@ -0,0 +1,73 @@
// boost integer_static_minmax_example.cpp
// Copyright Paul A. Bristow 2015
// Use, modification and distribution are subject to 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)
// Caution: this file contains Quickbook markup as well as code
// and comments, don't change any of the special comment markups!
#include <boost/integer/static_min_max.hpp> // For boost::static_signed_min
#include <iostream>
#include <limits> // for numeric_limits min and max.
//[integer_min_max_example_0
template < unsigned long AddendSize1, unsigned long AddendSize2 >
class adder
{
public:
static unsigned long const addend1_size = AddendSize1;
static unsigned long const addend2_size = AddendSize2;
static unsigned long const sum_size = boost::static_unsigned_max<AddendSize1, AddendSize2>::value + 1;
typedef int addend1_type[addend1_size];
typedef int addend2_type[addend2_size];
typedef int sum_type[sum_size];
void operator ()(addend1_type const &a1, addend2_type const &a2, sum_type &s) const;
}; // class adder
//] [/integer_min_max_example_0]
int main()
{
//[integer_min_max_example_1
std::cout << "boost::static_signed_min< 9, 14>::value = "<< boost::static_signed_min< 9, 14>::value << std::endl;
std::cout << "boost::static_signed_max< 9, 14>::value = "<< boost::static_signed_max< 9, 14>::value << std::endl;
//] [/integer_min_max_example_1]
//[integer_min_max_example_2
int const a1[] = { 0, 4, 3 }; // 340
int const a2[] = { 9, 8 }; // 89
int s[4];
adder<3, 2> obj;
// adder<sizeof(a1), sizeof(a2)> obj;
std::cout << "obj.addend1_size = " << obj.addend1_size << std::endl; // 3
std::cout << "obj.addend2_size = " << obj.addend2_size << std::endl; // 2
std::cout << "obj.sum_size = " << obj.sum_size << std::endl; // 4
//] [/integer_min_max_example_2]
// obj(a1, a2, s); // 's' should be 429 or { 9, 2, 4, 0 }
//void adder<3,2>::operator()(int const (&)[3],int const (&)[2],int (&)[4])const
std::cout << s[0] << s[1] << s[2] << s[3]<< std::endl; // 's' should be 429 or { 9, 2, 4, 0 }
} // int main
/*
//[integer_min_max_example_output
boost::static_signed_min< 9, 14>::value = 9
boost::static_signed_max< 9, 14>::value = 14
obj.addend1_size = 3
obj.addend2_size = 2
obj.sum_size = 4
//] [/integer_min_max_example_output]
*/

View File

@ -0,0 +1,55 @@
// boost integer_traits_example.cpp
// Copyright Paul A. Bristow 2015
// Use, modification and distribution are subject to 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)
// Caution: this file contains Quickbook markup as well as code
// and comments, don't change any of the special comment markups!
#include <boost/integer_traits.hpp>
#include <iostream>
#include <limits> // for numeric_limits min and max.
//[integer_traits_example_1
template<int N>
void f()
{ // Entirely contrived templated function.
std::cout << N << std::endl; // Do something pointless.
}
//] [/integer_traits_example_1]
int main()
{
std::cout << "Type int, min = "
<< (std::numeric_limits<int>::min)()
<< ", const_min = " << boost::integer_traits<int>::const_min
<< std::endl; // Type int, min = -2147483648, const_min = -2147483648
//[integer_traits_example_2
f<2>();
//] [/integer_traits_example_2]
//[integer_traits_example_3
int i = 2;
// f<i>(); // A local variable cannot be used as a non-type argument.
//] [/integer_traits_example_3]
//[integer_traits_example_4
const int ci = 2;
f<ci>(); // But a const variable can.
//] [/integer_traits_example_4]
//[integer_traits_example_5
// f<std::numeric_limits<int>::min()>(); // Function call must have a constant value in a constant expression.
//] [/integer_traits_example_5]
//[integer_traits_example_6
f<boost::integer_traits<int>::const_min>();
//] [/integer_traits_example_6]
} // int main

View File

@ -0,0 +1,251 @@
// boost integer_type_selection.cpp
// Copyright Paul A. Bristow 2015
// Use, modification and distribution are subject to 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)
// Caution: this file contains Quickbook markup as well as code
// and comments, don't change any of the special comment markups!
#include <boost/integer.hpp>
#include <iostream>
#include <limits> // for numeric_limits min and max.
// Note use of extra brackets to avoid potential clash for any min and max macros:
// (std::numeric_limits<T>::max)() (std::numeric_limits<T>::min)()
//! Show info about type
template <const int Bits, typename T>
void show_type()
{
std::cout << typeid(T).name()
<< " is a signed int using at least " << Bits << " bits uses "
<< std::numeric_limits<boost::int_t<Bits>::least>::digits
<< " bits, needing at most "
<< std::numeric_limits<boost::int_t<Bits>::least>::digits10
<< " decimal digits, " " and representing a maximum decimal value of "
<< static_cast<int>((std::numeric_limits<boost::int_t<Bits>::least>::max)())
<< std::endl;
} // template <const int Bits, typename T> void show_type()
template <typename T>
void show_consts()
{ //
std::cout << "Type " << typeid(T).name();
if (std::numeric_limits<T>::is_specialized)
{
std::cout << " is specialized for std::numeric_limits,";
if (std::numeric_limits<T>::is_integer)
{
std::cout << " and is integer type." << std::endl;
// If a constant expression for `min` and `max` are required, see
// Boost integer_traits.
std::cout << "const_max = " << boost::integer_traits<T>::const_max << std::endl;
std::cout << "const_min = " << boost::integer_traits<T>::const_min << std::endl;
}
}
} // void show_consts()
template <typename T>
void show_type()
{
std::cout << "Type " << typeid(T).name(); // name of resultant type.
if (std::numeric_limits<T>::is_specialized)
{
if (std::numeric_limits<T>::is_integer)
{
std::cout << " is an unsigned int with at least 13 bits using "
<< std::numeric_limits<T>::digits + (std::numeric_limits<T>::is_signed ? 1 : 0) << " bits,\n"
// See http://en.cppreference.com/w/cpp/types/numeric_limits/digits for details on the meaning of digits10.
// The value of std::numeric_limits<T>::digits is the number of digits in base-radix that can be represented
// by the type T without change.
// For integer types, this is the number of bits *not counting the sign bit*, so add one for the total.
// (For floating-point types, this is the number of digits in the mantissa).
"guaranteeing at most " << std::numeric_limits<T>::digits10 << " decimal digits precision,\n"
"and using at most " << std::numeric_limits<T>::digits10 + 1
<< " decimal digits,\n"
// See http://en.cppreference.com/w/cpp/types/numeric_limits/digits10 for details on the meaning of digits10.
// The value of std::numeric_limits<T>::digits is the number of digits in base-radix that can be represented
// by the type T without change.
// For integer types, the maximum number of digits that may be required is one more than digits10.
"a maximum decimal value of " << static_cast<long>((std::numeric_limits<T>::max)())
<< " and a minimum decimal value of " << static_cast<long>((std::numeric_limits<T>::min)()) << "."
<< std::endl;
}
else
{
std::cout << "is floating-point." << std::endl;
}
}
else
{ // is NOT specialized.
std::cout << " is NOT specialized for std::numeric_limits!" << std::endl;
}
} // template <typename T> void show_type()
template <typename T>
void show_type_limits()
{ //
std::cout <<"Type " << typeid(T).name();
if (std::numeric_limits<T>::is_specialized)
{
std::cout << " is specialized for std::numeric_limits." << std::endl;
if (std::numeric_limits<T>::is_integer)
{
std::cout << "is integer." << std::endl;
std::cout << (std::numeric_limits<T>::is_bounded ? "is bounded." : "") << std::endl;
std::cout << (std::numeric_limits<T>::is_exact ? "is exact." : "") << std::endl;
std::cout << (std::numeric_limits<T>::is_modulo ? "is modulo." : "") << std::endl;
std::cout << "radix = " << std::numeric_limits<T>::radix << std::endl;
std::cout << (std::numeric_limits<T>::is_signed ? "is signed." : "is unsigned.") << std::endl;
std::cout << "max = " << static_cast<long>((std::numeric_limits<T>::max)()) << std::endl;
std::cout << "min = " << static_cast<long>((std::numeric_limits<T>::min)()) << std::endl;
// Can't list const_max or const_min here because compiler will protest that
// const_max' : is not a member of 'boost::integer_traits<T>' when T is double.
//std::cout << "const_max = " << boost::integer_traits<T>::const_max << std::endl;
//std::cout << "const_min = " << boost::integer_traits<T>::const_min << std::endl;
std::cout << "digits = " << std::numeric_limits<T>::digits << std::endl;
std::cout << "bits = " << std::numeric_limits<T>::digits +1 << std::endl;
// digits is the number of bits not counting the sign bit for integer types.
std::cout << "digits10 = " << std::numeric_limits<T>::digits10 << std::endl;
// max_digits10 is not defined for integer types.
//std::cout << "max_digits10 = " << std::numeric_limits<T>::max_digits10 << std::endl;
}
else
{ // Not integral, so might be floating-point, or ?
std::cout << "is NOT integral type." << std::endl;
if ((std::numeric_limits<T>::max_exponent != 0) && (std::numeric_limits<T>::max_exponent10 != 0))
{ // floating-point type including fundamental float, double and multiprecision like cpp_dec_float_50 and cpp_bin_float_50.
std::cout << "is floating-point type." << std::endl;
std::cout << "digits = " << std::numeric_limits<T>::digits << std::endl;
// digits is the number of bits in the significand (not counting the sign bit) for floating-point types.
std::cout << "digits10 = " << std::numeric_limits<T>::digits10 << std::endl;
// http://en.cppreference.com/w/cpp/types/numeric_limits/digits10
std::cout << "max_digits10 = " << std::numeric_limits<T>::max_digits10 << std::endl;
std::cout << "is_modulo = " << std::numeric_limits<T>::is_modulo << std::endl;
std::cout << "is_iec559 = " << std::numeric_limits<T>::is_iec559 << std::endl;
std::cout << (std::numeric_limits<T>::is_exact ? "is exact." : "") << std::endl;
std::cout << (std::numeric_limits<T>::is_signed ? "is signed." : "is unsigned.") << std::endl;
std::cout << "has_quiet_NaN = " << std::numeric_limits<T>::has_quiet_NaN << std::endl;
std::cout << "has_infinity = " << std::numeric_limits<T>::has_infinity << std::endl;
std::cout << "has_denorm_loss = " << std::numeric_limits<T>::has_denorm_loss << std::endl;
std::cout << "max_exponent = " << std::numeric_limits<T>::max_exponent << std::endl;
std::cout << "min_exponent = " << std::numeric_limits<T>::min_exponent << std::endl;
std::cout << "max_exponent10 = " << std::numeric_limits<T>::max_exponent10 << std::endl;
std::cout << "min_exponent10 = " << std::numeric_limits<T>::min_exponent10 << std::endl;
}
else
{ // Unknown type
std::cout << "Unknown type." << std::endl;
}
}
}
else
{ // Not specialized!
std::cout << " is NOT specialized for std::numeric_limits!" << std::endl;
}
} // show_type_limits
int main()
{
try
{
// Select, construct and assign an unsigned integer of at least 13 bits.
//[integer_type_selection_1
boost::uint_t<13>::least my_var13 = 42;
//] [/integer_type_selection_1]
//[integer_type_selection_2
show_type<boost::uint_t<13>::least>();
//] [/integer_type_selection_2]
// Select fastest signed integer of at least 7 bits.
//[integer_type_selection_3
typedef boost::int_t<7>::fast int_7;
//] [/integer_type_selection_3]
//[integer_type_selection_4
show_type<int_7>();
//] [/integer_type_selection_4]
// Choose an integer size that can hold at least a value of 1000
// Assignment of this value is guaranteed not to be truncated:
//[integer_value_type_selection_5
boost::int_max_value_t<1000>::least thousand = 1000;
//] [/integer_value_type_selection_5]
std::cout << thousand << std::endl;
//[integer_value_type_selection_6
std::cout << (std::numeric_limits<boost::int_max_value_t<1000>::least>::max)() << std::endl;
//] [/integer_value_type_selection_6]
//[integer_value_type_selection_7
thousand = 100000; // Probably too big!
// warning C4305 : warning C4305: '=' : truncation from 'int' to 'short'
// warning C4309: '=' : truncation of constant value
//] [/integer_value_type_selection_7]
//[integer_value_type_selection_8
boost::int_t<32>::exact my_exact32 = 1000000;
std::cout << "boost::int_t<32>::exact has "
<< std::numeric_limits<boost::int_t<32>::exact>::digits
<< " bits and 1 sign bit." << std::endl;
// boost::int_t<32>::exact has 31 bits and 1 sign bit.
//] [/integer_value_type_selection_8]
// If we ask for an unachievable number of bits or value
//boost::int_t<128>::exact exact128;
//error C2338 : No suitable signed integer type with the requested number of bits is available.
// see reference to class template instantiation 'boost::int_t<128>' being compiled
show_consts<boost::int_t<32>::exact>();
// const_max = 2147483647 const_min = -2147483648
show_type<16, boost::int_max_value_t<1000>::least>();
}
catch (std::exception ex)
{
std::cout << ex.what() << std::endl;
}
return 0;
} // int main
/*
//[integer_type_selection_output_1
Type short is an unsigned int with at least 13 bits using 16 bits,
guaranteeing at most 4 decimal digits precision,
and using at most 5 decimal digits,
//] [/integer_type_selection_output_1]
//[integer_type_selection_output_2
Type signed char is an unsigned int with at least 13 bits using 8 bits,
guaranteeing at most 2 decimal digits precision,
and using at most 3 decimal digits,
a maximum decimal value of 127 and a minimum decimal value of -128.
//] [/integer_type_selection_output_2]
*/

33
example/jamfile.v2 Normal file
View File

@ -0,0 +1,33 @@
# libs/integer/example Jamfile
# Copyright (c) Vladimir Batov 2009-2014
# Copyright Paul A. Bristow 2015
# Distributed under the Boost Software License, Version 1.0.
# See copy at http://www.boost.org/LICENSE_1_0.txt.
# bring in the rules for testing
import testing ;
project convert_examples
: requirements
<warnings>on
<toolset>gcc:<warnings>all
<toolset>msvc:<warnings>all
<toolset>gcc:<cxxflags>"-Wno-unused-local-typedefs -Wno-unused-variable -Wno-long-long"
<toolset>msvc:<cxxflags>"/wd4996 /wd4512 /wd4610 /wd4510 /wd4127 /wd4701 /wd4127 /wd4305 /wd4100 /wd4512 /wd4714"
<toolset>msvc:<asynch-exceptions>on
<toolset>msvc:<define>_CRT_SECURE_NO_DEPRECATE
<toolset>msvc:<define>_SCL_SECURE_NO_DEPRECATE
<toolset>msvc:<define>_SCL_SECURE_NO_WARNINGS
<toolset>msvc:<define>_CRT_SECURE_NO_WARNINGS
<include>../include
;
run integer_mask_example.cpp ;
run integer_traits_example.cpp ;
run integer_type_selection_example.cpp ;

View File

@ -6,6 +6,9 @@
// See http://www.boost.org/libs/integer for documentation.
//! \file
//! \brief Compile-time evaluation of specified (least and fast) integer types.
// Revision History
// 22 Sep 01 Added value-based integer templates. (Daryle Walker)
// 01 Apr 01 Modified to use new <boost/limits.hpp> header. (John Maddock)
@ -42,15 +45,16 @@ namespace boost
// fast integers from least integers
// int_fast_t<> works correctly for unsigned too, in spite of the name.
template< typename LeastInt >
struct int_fast_t
{
typedef LeastInt fast;
struct int_fast_t
{
typedef LeastInt fast;
typedef fast type;
}; // imps may specialize
//! \cond DETAIL
namespace detail{
// convert category to type
// convert category to type
template< int Category > struct int_least_helper {}; // default is empty
template< int Category > struct uint_least_helper {}; // default is empty
@ -104,18 +108,18 @@ namespace boost
template <> struct exact_unsigned_base_helper<sizeof(boost::ulong_long_type)* CHAR_BIT> { typedef boost::ulong_long_type exact; };
#endif
} // namespace detail
//! \endcond // DETAIL
// integer templates specifying number of bits ---------------------------//
// signed
template< int Bits > // bits (including sign) required
struct int_t : public detail::exact_signed_base_helper<Bits>
struct int_t : public boost::detail::exact_signed_base_helper<Bits>
{
BOOST_STATIC_ASSERT_MSG(Bits <= (int)(sizeof(boost::intmax_t) * CHAR_BIT),
"No suitable signed integer type with the requested number of bits is available.");
typedef typename detail::int_least_helper
typedef typename boost::detail::int_least_helper
<
#ifdef BOOST_HAS_LONG_LONG
(Bits <= (int)(sizeof(boost::long_long_type) * CHAR_BIT)) +
@ -132,13 +136,13 @@ namespace boost
// unsigned
template< int Bits > // bits required
struct uint_t : public detail::exact_unsigned_base_helper<Bits>
struct uint_t : public boost::detail::exact_unsigned_base_helper<Bits>
{
BOOST_STATIC_ASSERT_MSG(Bits <= (int)(sizeof(boost::uintmax_t) * CHAR_BIT),
"No suitable unsigned integer type with the requested number of bits is available.");
#if (defined(__BORLANDC__) || defined(__CODEGEAR__)) && defined(BOOST_NO_INTEGRAL_INT64_T)
// It's really not clear why this workaround should be needed... shrug I guess! JM
BOOST_STATIC_CONSTANT(int, s =
BOOST_STATIC_CONSTANT(int, s =
6 +
(Bits <= ::std::numeric_limits<unsigned long>::digits) +
(Bits <= ::std::numeric_limits<unsigned int>::digits) +
@ -146,8 +150,8 @@ namespace boost
(Bits <= ::std::numeric_limits<unsigned char>::digits));
typedef typename detail::int_least_helper< ::boost::uint_t<Bits>::s>::least least;
#else
typedef typename detail::uint_least_helper
<
typedef typename boost::detail::uint_least_helper
<
#ifdef BOOST_HAS_LONG_LONG
(Bits <= (int)(sizeof(boost::long_long_type) * CHAR_BIT)) +
#else
@ -171,9 +175,9 @@ namespace boost
#else
template< long MaxValue > // maximum value to require support
#endif
struct int_max_value_t
struct int_max_value_t
{
typedef typename detail::int_least_helper
typedef typename boost::detail::int_least_helper
<
#if !defined(BOOST_NO_INTEGRAL_INT64_T) && defined(BOOST_HAS_LONG_LONG)
(MaxValue <= ::boost::integer_traits<boost::long_long_type>::const_max) +
@ -193,9 +197,9 @@ namespace boost
#else
template< long MinValue > // minimum value to require support
#endif
struct int_min_value_t
struct int_min_value_t
{
typedef typename detail::int_least_helper
typedef typename boost::detail::int_least_helper
<
#if !defined(BOOST_NO_INTEGRAL_INT64_T) && defined(BOOST_HAS_LONG_LONG)
(MinValue >= ::boost::integer_traits<boost::long_long_type>::const_min) +
@ -216,12 +220,12 @@ namespace boost
#else
template< unsigned long MaxValue > // minimum value to require support
#endif
struct uint_value_t
struct uint_value_t
{
#if (defined(__BORLANDC__) || defined(__CODEGEAR__))
// It's really not clear why this workaround should be needed... shrug I guess! JM
#if defined(BOOST_NO_INTEGRAL_INT64_T)
BOOST_STATIC_CONSTANT(unsigned, which =
BOOST_STATIC_CONSTANT(unsigned, which =
1 +
(MaxValue <= ::boost::integer_traits<unsigned long>::const_max) +
(MaxValue <= ::boost::integer_traits<unsigned int>::const_max) +
@ -229,7 +233,7 @@ namespace boost
(MaxValue <= ::boost::integer_traits<unsigned char>::const_max));
typedef typename detail::int_least_helper< ::boost::uint_value_t<MaxValue>::which>::least least;
#else // BOOST_NO_INTEGRAL_INT64_T
BOOST_STATIC_CONSTANT(unsigned, which =
BOOST_STATIC_CONSTANT(unsigned, which =
1 +
(MaxValue <= ::boost::integer_traits<boost::ulong_long_type>::const_max) +
(MaxValue <= ::boost::integer_traits<unsigned long>::const_max) +
@ -239,8 +243,8 @@ namespace boost
typedef typename detail::uint_least_helper< ::boost::uint_value_t<MaxValue>::which>::least least;
#endif // BOOST_NO_INTEGRAL_INT64_T
#else
typedef typename detail::uint_least_helper
<
typedef typename boost::detail::uint_least_helper
<
#if !defined(BOOST_NO_INTEGRAL_INT64_T) && defined(BOOST_HAS_LONG_LONG)
(MaxValue <= ::boost::integer_traits<boost::ulong_long_type>::const_max) +
#else
@ -255,7 +259,6 @@ namespace boost
typedef typename int_fast_t<least>::type fast;
};
} // namespace boost
#endif // BOOST_INTEGER_HPP

View File

@ -0,0 +1,18 @@
// Boost common_factor.hpp header file -------------------------------------//
// (C) Copyright Daryle Walker 2001-2002.
// 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 Include both GCD and LCM.
#ifndef BOOST_INTEGER_COMMON_FACTOR_HPP
#define BOOST_INTEGER_COMMON_FACTOR_HPP
#include <boost/integer/common_factor_ct.hpp>
#include <boost/integer/common_factor_rt.hpp>
#endif // BOOST_INTEGER_COMMON_FACTOR_HPP

View File

@ -0,0 +1,106 @@
// Boost common_factor_ct.hpp header file ----------------------------------//
// (C) Copyright Daryle Walker and Stephen Cleary 2001-2002.
// 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 Evaluate Greatest Common Divisor and Least Common Multiple at compile-time.
#ifndef BOOST_INTEGER_COMMON_FACTOR_CT_HPP
#define BOOST_INTEGER_COMMON_FACTOR_CT_HPP
#include <boost/integer_fwd.hpp> // Include for declaration of static_gcd_type and static_gcd.
#include <boost/config.hpp> // for BOOST_STATIC_CONSTANT, etc.
#include <boost/cstdint.hpp> // for boost::uintmax_t
namespace boost
{
namespace integer
{
// Warning: From 1.58 this is in namespace boost::integer and no longer in namespace boost::math.
// Implementation details --------------------------------------------------//
//! \cond DETAIL
namespace detail
{
// Build GCD with Euclid's recursive algorithm
template < static_gcd_type Value1, static_gcd_type Value2 >
struct static_gcd_helper_t
{
private:
BOOST_STATIC_CONSTANT( static_gcd_type, new_value1 = Value2 );
BOOST_STATIC_CONSTANT( static_gcd_type, new_value2 = Value1 % Value2 );
#ifndef __BORLANDC__
#define BOOST_DETAIL_GCD_HELPER_VAL(Value) static_cast<static_gcd_type>(Value)
#else
typedef static_gcd_helper_t self_type;
#define BOOST_DETAIL_GCD_HELPER_VAL(Value) (self_type:: Value )
#endif
typedef static_gcd_helper_t< BOOST_DETAIL_GCD_HELPER_VAL(new_value1),
BOOST_DETAIL_GCD_HELPER_VAL(new_value2) > next_step_type;
#undef BOOST_DETAIL_GCD_HELPER_VAL
public:
BOOST_STATIC_CONSTANT( static_gcd_type, value = next_step_type::value );
};
// Non-recursive case
template < static_gcd_type Value1 >
struct static_gcd_helper_t< Value1, 0UL >
{
BOOST_STATIC_CONSTANT( static_gcd_type, value = Value1 );
};
// Build the LCM from the GCD
template < static_gcd_type Value1, static_gcd_type Value2 >
struct static_lcm_helper_t
{
typedef static_gcd_helper_t<Value1, Value2> gcd_type;
BOOST_STATIC_CONSTANT( static_gcd_type, value = Value1 / gcd_type::value
* Value2 );
};
// Special case for zero-GCD values
template < >
struct static_lcm_helper_t< 0UL, 0UL >
{
BOOST_STATIC_CONSTANT( static_gcd_type, value = 0UL );
};
} // namespace detail
//! \endcond // DETAIL
// Compile-time greatest common divisor evaluator class declaration --------//
template < static_gcd_type Value1, static_gcd_type Value2 > struct static_gcd
{
BOOST_STATIC_CONSTANT( static_gcd_type, value = (detail::static_gcd_helper_t<Value1, Value2>::value) );
}; // boost::integer::static_gcd
#if !defined(BOOST_NO_INCLASS_MEMBER_INITIALIZATION)
template< static_gcd_type Value1, static_gcd_type Value2 > static_gcd_type const static_gcd< Value1, Value2 >::value;
#endif
// Compile-time least common multiple evaluator class declaration ----------//
template < static_gcd_type Value1, static_gcd_type Value2 > struct static_lcm
{
BOOST_STATIC_CONSTANT( static_gcd_type, value = (detail::static_lcm_helper_t<Value1, Value2>::value) );
}; // boost::integer::static_lcm
#if !defined(BOOST_NO_INCLASS_MEMBER_INITIALIZATION)
template< static_gcd_type Value1, static_gcd_type Value2 > static_gcd_type const static_lcm< Value1, Value2 >::value;
#endif
} // namespace integer
} // namespace boost
#endif // BOOST_INTEGER_COMMON_FACTOR_CT_HPP

View File

@ -0,0 +1,488 @@
// Boost common_factor_rt.hpp header file ----------------------------------//
// (C) Copyright Daryle Walker and Paul Moore 2001-2002. Permission to copy,
// use, modify, sell and distribute this software is granted provided this
// copyright notice appears in all copies. This software is provided "as is"
// without express or implied warranty, and with no claim as to its suitability
// for any purpose.
// boostinspect:nolicense (don't complain about the lack of a Boost license)
// (Paul Moore hasn't been in contact for years, so there's no way to change the
// license.)
// See http://www.boost.org for updates, documentation, and revision history.
//! \file
//! \brief Evaluate Least Common Multiple at compile-time.
#ifndef BOOST_INTEGER_COMMON_FACTOR_RT_HPP
#define BOOST_INTEGER_COMMON_FACTOR_RT_HPP
#include <boost/config.hpp> // for BOOST_NESTED_TEMPLATE, etc.
#include <boost/limits.hpp> // for std::numeric_limits
#include <boost/detail/workaround.hpp>
#include <boost/integer_fwd.hpp> // Include for declarations of gcd_evaluator and lcm_evaluator.
#include <climits> // for CHAR_MIN
#ifdef BOOST_MSVC
#pragma warning(push)
#pragma warning(disable:4127 4244) // Conditional expression is constant
#endif
// Warning: this is from 1.58 in namespace boost::integer and no longer in namespace boost::math.
namespace boost
{
namespace integer
{
// Forward declarations copied from <boost/math/common_factor_rt.hpp> at 1.58 -----//
// Forward declarations for function templates -----------------------------//
template < typename IntegerType >
IntegerType gcd( IntegerType const &a, IntegerType const &b );
template < typename IntegerType >
IntegerType lcm( IntegerType const &a, IntegerType const &b );
// Greatest common divisor evaluator class declaration ---------------------//
template < typename IntegerType >
class gcd_evaluator;
template < typename IntegerType >
class lcm_evaluator;
template < typename IntegerType >
class gcd_evaluator
{
public:
// Types
typedef IntegerType result_type, first_argument_type, second_argument_type;
// Function object interface
result_type operator ()( first_argument_type const &a,
second_argument_type const &b ) const;
}; // boost::integer::gcd_evaluator
// Least common multiple evaluator class declaration -----------------------//
template < typename IntegerType >
class lcm_evaluator
{
public:
// Types
typedef IntegerType result_type, first_argument_type, second_argument_type;
// Function object interface
result_type operator ()( first_argument_type const &a,
second_argument_type const &b ) const;
}; // boost::integer::lcm_evaluator
// Implementation details --------------------------------------------------//
//! \cond DETAIL
namespace detail
{
// Greatest common divisor for rings (including unsigned integers)
template < typename RingType >
RingType
gcd_euclidean
(
RingType a,
RingType b
)
{
// Avoid repeated construction
#ifndef __BORLANDC__
RingType const zero = static_cast<RingType>( 0 );
#else
RingType zero = static_cast<RingType>( 0 );
#endif
// Reduce by GCD-remainder property [GCD(a,b) == GCD(b,a MOD b)]
while ( true )
{
if ( a == zero )
return b;
b %= a;
if ( b == zero )
return a;
a %= b;
}
}
// Greatest common divisor for (signed) integers
template < typename IntegerType >
inline
IntegerType
gcd_integer
(
IntegerType const & a,
IntegerType const & b
)
{
// Avoid repeated construction
IntegerType const zero = static_cast<IntegerType>( 0 );
IntegerType const result = gcd_euclidean( a, b );
return ( result < zero ) ? static_cast<IntegerType>(-result) : result;
}
// Greatest common divisor for unsigned binary integers
template < typename BuiltInUnsigned >
BuiltInUnsigned
gcd_binary
(
BuiltInUnsigned u,
BuiltInUnsigned v
)
{
if ( u && v )
{
// Shift out common factors of 2
unsigned shifts = 0;
while ( !(u & 1u) && !(v & 1u) )
{
++shifts;
u >>= 1;
v >>= 1;
}
// Start with the still-even one, if any
BuiltInUnsigned r[] = { u, v };
unsigned which = static_cast<bool>( u & 1u );
// Whittle down the values via their differences
do
{
#if BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x582))
while ( !(r[ which ] & 1u) )
{
r[ which ] = (r[which] >> 1);
}
#else
// Remove factors of two from the even one
while ( !(r[ which ] & 1u) )
{
r[ which ] >>= 1;
}
#endif
// Replace the larger of the two with their difference
if ( r[!which] > r[which] )
{
which ^= 1u;
}
r[ which ] -= r[ !which ];
}
while ( r[which] );
// Shift-in the common factor of 2 to the residues' GCD
return r[ !which ] << shifts;
}
else
{
// At least one input is zero, return the other
// (adding since zero is the additive identity)
// or zero if both are zero.
return u + v;
}
}
// Least common multiple for rings (including unsigned integers)
template < typename RingType >
inline
RingType
lcm_euclidean
(
RingType const & a,
RingType const & b
)
{
RingType const zero = static_cast<RingType>( 0 );
RingType const temp = gcd_euclidean( a, b );
return ( temp != zero ) ? ( a / temp * b ) : zero;
}
// Least common multiple for (signed) integers
template < typename IntegerType >
inline
IntegerType
lcm_integer
(
IntegerType const & a,
IntegerType const & b
)
{
// Avoid repeated construction
IntegerType const zero = static_cast<IntegerType>( 0 );
IntegerType const result = lcm_euclidean( a, b );
return ( result < zero ) ? static_cast<IntegerType>(-result) : result;
}
// Function objects to find the best way of computing GCD or LCM
#ifndef BOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS
template < typename T, bool IsSpecialized, bool IsSigned >
struct gcd_optimal_evaluator_helper_t
{
T operator ()( T const &a, T const &b )
{
return gcd_euclidean( a, b );
}
};
template < typename T >
struct gcd_optimal_evaluator_helper_t< T, true, true >
{
T operator ()( T const &a, T const &b )
{
return gcd_integer( a, b );
}
};
template < typename T >
struct gcd_optimal_evaluator
{
T operator ()( T const &a, T const &b )
{
typedef ::std::numeric_limits<T> limits_type;
typedef gcd_optimal_evaluator_helper_t<T,
limits_type::is_specialized, limits_type::is_signed> helper_type;
helper_type solver;
return solver( a, b );
}
};
#else // BOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS
template < typename T >
struct gcd_optimal_evaluator
{
T operator ()( T const &a, T const &b )
{
return gcd_integer( a, b );
}
};
#endif
// Specialize for the built-in integers
#define BOOST_PRIVATE_GCD_UF( Ut ) \
template < > struct gcd_optimal_evaluator<Ut> \
{ Ut operator ()( Ut a, Ut b ) const { return gcd_binary( a, b ); } }
BOOST_PRIVATE_GCD_UF( unsigned char );
BOOST_PRIVATE_GCD_UF( unsigned short );
BOOST_PRIVATE_GCD_UF( unsigned );
BOOST_PRIVATE_GCD_UF( unsigned long );
#ifdef BOOST_HAS_LONG_LONG
BOOST_PRIVATE_GCD_UF( boost::ulong_long_type );
#elif defined(BOOST_HAS_MS_INT64)
BOOST_PRIVATE_GCD_UF( unsigned __int64 );
#endif
#if CHAR_MIN == 0
BOOST_PRIVATE_GCD_UF( char ); // char is unsigned
#endif
#undef BOOST_PRIVATE_GCD_UF
#define BOOST_PRIVATE_GCD_SF( St, Ut ) \
template < > struct gcd_optimal_evaluator<St> \
{ St operator ()( St a, St b ) const { Ut const a_abs = \
static_cast<Ut>( a < 0 ? -a : +a ), b_abs = static_cast<Ut>( \
b < 0 ? -b : +b ); return static_cast<St>( \
gcd_optimal_evaluator<Ut>()(a_abs, b_abs) ); } }
BOOST_PRIVATE_GCD_SF( signed char, unsigned char );
BOOST_PRIVATE_GCD_SF( short, unsigned short );
BOOST_PRIVATE_GCD_SF( int, unsigned );
BOOST_PRIVATE_GCD_SF( long, unsigned long );
#if CHAR_MIN < 0
BOOST_PRIVATE_GCD_SF( char, unsigned char ); // char is signed
#endif
#ifdef BOOST_HAS_LONG_LONG
BOOST_PRIVATE_GCD_SF( boost::long_long_type, boost::ulong_long_type );
#elif defined(BOOST_HAS_MS_INT64)
BOOST_PRIVATE_GCD_SF( __int64, unsigned __int64 );
#endif
#undef BOOST_PRIVATE_GCD_SF
#ifndef BOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS
template < typename T, bool IsSpecialized, bool IsSigned >
struct lcm_optimal_evaluator_helper_t
{
T operator ()( T const &a, T const &b )
{
return lcm_euclidean( a, b );
}
};
template < typename T >
struct lcm_optimal_evaluator_helper_t< T, true, true >
{
T operator ()( T const &a, T const &b )
{
return lcm_integer( a, b );
}
};
template < typename T >
struct lcm_optimal_evaluator
{
T operator ()( T const &a, T const &b )
{
typedef ::std::numeric_limits<T> limits_type;
typedef lcm_optimal_evaluator_helper_t<T,
limits_type::is_specialized, limits_type::is_signed> helper_type;
helper_type solver;
return solver( a, b );
}
};
#else // BOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS
template < typename T >
struct lcm_optimal_evaluator
{
T operator ()( T const &a, T const &b )
{
return lcm_integer( a, b );
}
};
#endif
// Functions to find the GCD or LCM in the best way
template < typename T >
inline
T
gcd_optimal
(
T const & a,
T const & b
)
{
gcd_optimal_evaluator<T> solver;
return solver( a, b );
}
template < typename T >
inline
T
lcm_optimal
(
T const & a,
T const & b
)
{
lcm_optimal_evaluator<T> solver;
return solver( a, b );
}
} // namespace detail
//! \endcond // DETAIL
// Greatest common divisor operator() evaluator member function definition ------------//
//! Evaluation operator() for Greatest Common Divisor (GCD) of two arguments.
//! \tparam IntegerType must be an integral constant http://en.cppreference.com/w/cpp/types/integral_constant
//! \param a first_argument_type
//! \param b second_argument_type
template < typename IntegerType >
inline
typename gcd_evaluator<IntegerType>::result_type
gcd_evaluator<IntegerType>::operator ()
(
first_argument_type const & a,
second_argument_type const & b
) const
{
return detail::gcd_optimal( a, b );
}
// Least common multiple evaluator member function definition --------------//
//! Evaluates Least Common Multiplier of two arguments.
//! \tparam IntegerType must be an integral constant http://en.cppreference.com/w/cpp/types/integral_constant
//! \tparam IntegerType must be an integral constant http://en.cppreference.com/w/cpp/types/integral_constant
//! \param a first_argument_type
//! \param b second_argument_type
template < typename IntegerType >
inline
typename lcm_evaluator<IntegerType>::result_type
lcm_evaluator<IntegerType>::operator ()
(
first_argument_type const & a,
second_argument_type const & b
) const
{
return detail::lcm_optimal( a, b );
}
// Greatest common divisor and least common multiple function definitions --//
//! Function for Greatest Common Divisor (GCD) of two arguments.
//! \tparam IntegerType must be an integral constant http://en.cppreference.com/w/cpp/types/integral_constant
//! \param a first_argument_type
//! \param b second_argument_type
template < typename IntegerType >
inline
IntegerType
gcd
(
IntegerType const & a,
IntegerType const & b
)
{
gcd_evaluator<IntegerType> solver;
return solver( a, b );
}
//! Function for Least Common Multiplier (LCM) of two arguments.
//! \tparam IntegerType must be an integral constant http://en.cppreference.com/w/cpp/types/integral_constant
//! \param a first_argument_type
//! \param b second_argument_type
template < typename IntegerType >
inline
IntegerType
lcm
(
IntegerType const & a,
IntegerType const & b
)
{
lcm_evaluator<IntegerType> solver;
return solver( a, b );
}
} // namespace integer
} // namespace boost
#ifdef BOOST_MSVC
#pragma warning(pop)
#endif
#endif // BOOST_INTEGER_COMMON_FACTOR_RT_HPP

View File

@ -1,15 +1,18 @@
// -----------------------------------------------------------
// integer_log2.hpp
//
// Gives the integer part of the logarithm, in base 2, of a
// given number. Behavior is undefined if the argument is <= 0.
//
// Copyright (c) 2003-2004, 2008 Gennaro Prota
//
// 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)
//
//! \file
//! \brief Function integer_log2 gives the integer part of the logarithm, in base 2, of a
//! given number (at run-time). Behavior is undefined if the argument is <= 0.
//
//! \note For analogous compile-time computation of log2 see static_log2.
//! \sa http::www.boost.org/doc/integer
// -----------------------------------------------------------
#ifndef BOOST_INTEGER_INTEGER_LOG2_HPP
@ -24,6 +27,8 @@
namespace boost {
//! \cond DETAIL
namespace detail {
template <typename T>
@ -46,7 +51,6 @@ namespace boost {
}
// helper to find the maximum power of two
// less than p (more involved than necessary,
// to avoid PTS)
@ -82,9 +86,8 @@ namespace boost {
#endif
};
} // detail
//! \endcond // DETAIL
// ---------
// integer_log2
@ -104,9 +107,6 @@ namespace boost {
}
}
#endif // include guard
#endif // BOOST_INTEGER_INTEGER_LOG2_HPP include guard

View File

@ -5,7 +5,10 @@
// 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.
// See http://www.boost.org for updates, documentation, and revision history.
//! \file
//! \brief Compile-time integer mask evaluation.
#ifndef BOOST_INTEGER_INTEGER_MASK_HPP
#define BOOST_INTEGER_INTEGER_MASK_HPP
@ -35,7 +38,6 @@
namespace boost
{
// Specified single-bit mask class declaration -----------------------------//
// (Lowest bit starts counting at 0.)

View File

@ -10,6 +10,11 @@
//
// ---------------------------------------------------
// See http://www.boost.org/libs/integer for documentation.
//! \file
//! \brief Compile-time calculation of integer part of the logarithm, in base 2, of a given number.
//! \note For a run-time calculation of log2, see function integer_log2 at integer_log2.hpp.
// ------------------------------------------------------------------------- //
@ -20,6 +25,8 @@
namespace boost {
//! \cond DETAIL
namespace detail {
namespace static_log2_impl {
@ -99,8 +106,7 @@ namespace boost {
}
} // detail
//! \endcond // DETAIL
// --------------------------------------
// static_log2<x>
@ -120,7 +126,7 @@ namespace boost {
template <>
struct static_log2<0> { };
}
} // namespace boost

View File

@ -5,7 +5,9 @@
// 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.
// See http://www.boost.org for updates, documentation, and revision history.
//! \file
//! \brief Compile-time evaluation of extrema: minimum or maximum of two values, signed or unsigned.
#ifndef BOOST_INTEGER_STATIC_MIN_MAX_HPP
#define BOOST_INTEGER_STATIC_MIN_MAX_HPP

View File

@ -5,6 +5,8 @@
// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
// See http://www.boost.org/libs/integer for documentation.
//! \file
//! \brief Include all Boost.Integer files.
#ifndef BOOST_INTEGER_FWD_HPP
#define BOOST_INTEGER_FWD_HPP
@ -16,7 +18,6 @@
#include <boost/limits.hpp> // for std::numeric_limits
#include <boost/cstdint.hpp> // For intmax_t
namespace boost
{
@ -158,6 +159,32 @@ template <static_min_max_unsigned_type Value1, static_min_max_unsigned_type Valu
template <static_min_max_unsigned_type Value1, static_min_max_unsigned_type Value2>
struct static_unsigned_max;
namespace integer
{
// From <boost/integer/common_factor_ct.hpp>
#ifdef BOOST_NO_INTEGRAL_INT64_T
typedef unsigned long static_gcd_type;
#else
typedef boost::uintmax_t static_gcd_type;
#endif
template < static_gcd_type Value1, static_gcd_type Value2 >
struct static_gcd;
template < static_gcd_type Value1, static_gcd_type Value2 >
struct static_lcm;
// From <boost/integer/common_factor_rt.hpp>
template < typename IntegerType >
class gcd_evaluator;
template < typename IntegerType >
class lcm_evaluator;
] // namespace integer
} // namespace boost

View File

@ -8,10 +8,15 @@
* $Id$
*
* Idea by Beman Dawes, Ed Brey, Steve Cleary, and Nathan Myers
*/
// See http://www.boost.org/libs/integer for documentation.
//! \file
//! \brief integer traits
//! \sa http://www.boost.org/libs/integer
#ifndef BOOST_INTEGER_TRAITS_HPP
#define BOOST_INTEGER_TRAITS_HPP
@ -47,6 +52,7 @@ public:
BOOST_STATIC_CONSTANT(bool, is_integral = false);
};
//! \cond DETAIL
namespace detail {
template<class T, T min_val, T max_val>
class integer_traits_base
@ -70,6 +76,7 @@ const T integer_traits_base<T, min_val, max_val>::const_max;
#endif
} // namespace detail
//! \endcond // DETAIL
template<>
class integer_traits<bool>

12
meta/libraries.json Normal file
View File

@ -0,0 +1,12 @@
{
"key": "integer",
"name": "Integer",
"description": "The organization of boost integer headers and classes is designed to take advantage of <stdint.h> types from the 1999 C standard without resorting to undefined behavior in terms of the 1998 C++ standard. The header <boost/cstdint.hpp> makes the standard integer types safely available in namespace boost without placing any names in namespace std.",
"category": [
"Math"
],
"authors": "",
"maintainers": [
"Daryle Walker <darylew -at- hotmail.com>"
]
}

View File

@ -25,4 +25,5 @@ test-suite integer
[ compile-fail fail_uint_exact.cpp ]
[ compile-fail fail_uint_fast.cpp ]
[ compile-fail fail_uint_least.cpp ]
[ run common_factor_test.cpp ]
;

473
test/common_factor_test.cpp Normal file
View File

@ -0,0 +1,473 @@
// Boost GCD & LCM common_factor.hpp test program --------------------------//
// (C) Copyright Daryle Walker 2001, 2006.
// 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 most recent version including documentation.
// Revision History
// 01 Dec 2006 Various fixes for old compilers (Joaquin M Lopez Munoz)
// 10 Nov 2006 Make long long and __int64 mutually exclusive (Daryle Walker)
// 04 Nov 2006 Use more built-in numeric types, binary-GCD (Daryle Walker)
// 03 Nov 2006 Use custom numeric types (Daryle Walker)
// 02 Nov 2006 Change to Boost.Test's unit test system (Daryle Walker)
// 07 Nov 2001 Initial version (Daryle Walker)
#define BOOST_TEST_MAIN "Boost.Math GCD & LCM unit tests"
#include <boost/integer/common_factor.hpp>
#include <boost/config.hpp> // for BOOST_MSVC, etc.
#include <boost/detail/workaround.hpp>
#include <boost/operators.hpp>
#include <boost/core/lightweight_test.hpp>
#include <istream> // for std::basic_istream
#include <limits> // for std::numeric_limits
#include <ostream> // for std::basic_ostream
namespace {
// TODO: add polynominal/non-real type; especially after any switch to the
// binary-GCD algorithm for built-in types
// Custom integer class (template)
template < typename IntType, int ID = 0 >
class my_wrapped_integer
: private ::boost::shiftable1<my_wrapped_integer<IntType, ID>,
::boost::operators<my_wrapped_integer<IntType, ID> > >
{
// Helper type-aliases
typedef my_wrapped_integer self_type;
typedef IntType self_type::* bool_type;
// Member data
IntType v_;
public:
// Template parameters
typedef IntType int_type;
BOOST_STATIC_CONSTANT(int,id = ID);
// Lifetime management (use automatic destructor and copy constructor)
my_wrapped_integer( int_type const &v = int_type() ) : v_( v ) {}
// Accessors
int_type value() const { return this->v_; }
// Operators (use automatic copy assignment)
operator bool_type() const { return this->v_ ? &self_type::v_ : 0; }
self_type & operator ++() { ++this->v_; return *this; }
self_type & operator --() { --this->v_; return *this; }
self_type operator ~() const { return self_type( ~this->v_ ); }
self_type operator !() const { return self_type( !this->v_ ); }
self_type operator +() const { return self_type( +this->v_ ); }
self_type operator -() const { return self_type( -this->v_ ); }
bool operator <( self_type const &r ) const { return this->v_ < r.v_; }
bool operator ==( self_type const &r ) const { return this->v_ == r.v_; }
self_type &operator *=(self_type const &r) {this->v_ *= r.v_; return *this;}
self_type &operator /=(self_type const &r) {this->v_ /= r.v_; return *this;}
self_type &operator %=(self_type const &r) {this->v_ %= r.v_; return *this;}
self_type &operator +=(self_type const &r) {this->v_ += r.v_; return *this;}
self_type &operator -=(self_type const &r) {this->v_ -= r.v_; return *this;}
self_type &operator<<=(self_type const &r){this->v_ <<= r.v_; return *this;}
self_type &operator>>=(self_type const &r){this->v_ >>= r.v_; return *this;}
self_type &operator &=(self_type const &r) {this->v_ &= r.v_; return *this;}
self_type &operator |=(self_type const &r) {this->v_ |= r.v_; return *this;}
self_type &operator ^=(self_type const &r) {this->v_ ^= r.v_; return *this;}
// Input & output
friend std::istream & operator >>( std::istream &i, self_type &x )
{ return i >> x.v_; }
friend std::ostream & operator <<( std::ostream &o, self_type const &x )
{ return o << x.v_; }
}; // my_wrapped_integer
template < typename IntType, int ID >
my_wrapped_integer<IntType, ID> abs( my_wrapped_integer<IntType, ID> const &x )
{ return ( x < my_wrapped_integer<IntType, ID>(0) ) ? -x : +x; }
typedef my_wrapped_integer<int> MyInt1;
typedef my_wrapped_integer<unsigned> MyUnsigned1;
typedef my_wrapped_integer<int, 1> MyInt2;
typedef my_wrapped_integer<unsigned, 1> MyUnsigned2;
// Without these explicit instantiations, MSVC++ 6.5/7.0 does not find
// some friend operators in certain contexts.
MyInt1 dummy1;
MyUnsigned1 dummy2;
MyInt2 dummy3;
MyUnsigned2 dummy4;
} // namespace
#define BOOST_NO_MACRO_EXPAND /**/
// Specialize numeric_limits for _some_ of our types
namespace std
{
template < >
class numeric_limits< MyInt1 >
{
typedef MyInt1::int_type int_type;
typedef numeric_limits<int_type> limits_type;
public:
BOOST_STATIC_CONSTANT(bool, is_specialized = limits_type::is_specialized);
static MyInt1 min BOOST_NO_MACRO_EXPAND() throw() { return (limits_type::min)(); }
static MyInt1 max BOOST_NO_MACRO_EXPAND() throw() { return (limits_type::max)(); }
BOOST_STATIC_CONSTANT(int, digits = limits_type::digits);
BOOST_STATIC_CONSTANT(int, digits10 = limits_type::digits10);
#ifndef BOOST_NO_CXX11_NUMERIC_LIMITS
BOOST_STATIC_CONSTANT(int, max_digits10 = limits_type::max_digits10);
#endif
BOOST_STATIC_CONSTANT(bool, is_signed = limits_type::is_signed);
BOOST_STATIC_CONSTANT(bool, is_integer = limits_type::is_integer);
BOOST_STATIC_CONSTANT(bool, is_exact = limits_type::is_exact);
BOOST_STATIC_CONSTANT(int, radix = limits_type::radix);
static MyInt1 epsilon() throw() { return limits_type::epsilon(); }
static MyInt1 round_error() throw() { return limits_type::round_error(); }
BOOST_STATIC_CONSTANT(int, min_exponent = limits_type::min_exponent);
BOOST_STATIC_CONSTANT(int, min_exponent10 = limits_type::min_exponent10);
BOOST_STATIC_CONSTANT(int, max_exponent = limits_type::max_exponent);
BOOST_STATIC_CONSTANT(int, max_exponent10 = limits_type::max_exponent10);
BOOST_STATIC_CONSTANT(bool, has_infinity = limits_type::has_infinity);
BOOST_STATIC_CONSTANT(bool, has_quiet_NaN = limits_type::has_quiet_NaN);
BOOST_STATIC_CONSTANT(bool, has_signaling_NaN = limits_type::has_signaling_NaN);
BOOST_STATIC_CONSTANT(float_denorm_style, has_denorm = limits_type::has_denorm);
BOOST_STATIC_CONSTANT(bool, has_denorm_loss = limits_type::has_denorm_loss);
static MyInt1 infinity() throw() { return limits_type::infinity(); }
static MyInt1 quiet_NaN() throw() { return limits_type::quiet_NaN(); }
static MyInt1 signaling_NaN() throw() {return limits_type::signaling_NaN();}
static MyInt1 denorm_min() throw() { return limits_type::denorm_min(); }
BOOST_STATIC_CONSTANT(bool, is_iec559 = limits_type::is_iec559);
BOOST_STATIC_CONSTANT(bool, is_bounded = limits_type::is_bounded);
BOOST_STATIC_CONSTANT(bool, is_modulo = limits_type::is_modulo);
BOOST_STATIC_CONSTANT(bool, traps = limits_type::traps);
BOOST_STATIC_CONSTANT(bool, tinyness_before = limits_type::tinyness_before);
BOOST_STATIC_CONSTANT(float_round_style, round_style = limits_type::round_style);
}; // std::numeric_limits<MyInt1>
template < >
class numeric_limits< MyUnsigned1 >
{
typedef MyUnsigned1::int_type int_type;
typedef numeric_limits<int_type> limits_type;
public:
BOOST_STATIC_CONSTANT(bool, is_specialized = limits_type::is_specialized);
static MyUnsigned1 min BOOST_NO_MACRO_EXPAND() throw() { return (limits_type::min)(); }
static MyUnsigned1 max BOOST_NO_MACRO_EXPAND() throw() { return (limits_type::max)(); }
BOOST_STATIC_CONSTANT(int, digits = limits_type::digits);
BOOST_STATIC_CONSTANT(int, digits10 = limits_type::digits10);
#ifndef BOOST_NO_CXX11_NUMERIC_LIMITS
BOOST_STATIC_CONSTANT(int, max_digits10 = limits_type::max_digits10);
#endif
BOOST_STATIC_CONSTANT(bool, is_signed = limits_type::is_signed);
BOOST_STATIC_CONSTANT(bool, is_integer = limits_type::is_integer);
BOOST_STATIC_CONSTANT(bool, is_exact = limits_type::is_exact);
BOOST_STATIC_CONSTANT(int, radix = limits_type::radix);
static MyUnsigned1 epsilon() throw() { return limits_type::epsilon(); }
static MyUnsigned1 round_error() throw(){return limits_type::round_error();}
BOOST_STATIC_CONSTANT(int, min_exponent = limits_type::min_exponent);
BOOST_STATIC_CONSTANT(int, min_exponent10 = limits_type::min_exponent10);
BOOST_STATIC_CONSTANT(int, max_exponent = limits_type::max_exponent);
BOOST_STATIC_CONSTANT(int, max_exponent10 = limits_type::max_exponent10);
BOOST_STATIC_CONSTANT(bool, has_infinity = limits_type::has_infinity);
BOOST_STATIC_CONSTANT(bool, has_quiet_NaN = limits_type::has_quiet_NaN);
BOOST_STATIC_CONSTANT(bool, has_signaling_NaN = limits_type::has_signaling_NaN);
BOOST_STATIC_CONSTANT(float_denorm_style, has_denorm = limits_type::has_denorm);
BOOST_STATIC_CONSTANT(bool, has_denorm_loss = limits_type::has_denorm_loss);
static MyUnsigned1 infinity() throw() { return limits_type::infinity(); }
static MyUnsigned1 quiet_NaN() throw() { return limits_type::quiet_NaN(); }
static MyUnsigned1 signaling_NaN() throw()
{ return limits_type::signaling_NaN(); }
static MyUnsigned1 denorm_min() throw(){ return limits_type::denorm_min(); }
BOOST_STATIC_CONSTANT(bool, is_iec559 = limits_type::is_iec559);
BOOST_STATIC_CONSTANT(bool, is_bounded = limits_type::is_bounded);
BOOST_STATIC_CONSTANT(bool, is_modulo = limits_type::is_modulo);
BOOST_STATIC_CONSTANT(bool, traps = limits_type::traps);
BOOST_STATIC_CONSTANT(bool, tinyness_before = limits_type::tinyness_before);
BOOST_STATIC_CONSTANT(float_round_style, round_style = limits_type::round_style);
}; // std::numeric_limits<MyUnsigned1>
#if BOOST_WORKAROUND(BOOST_MSVC,<1300)
// MSVC 6.0 lacks operator<< for __int64, see
// http://support.microsoft.com/default.aspx?scid=kb;en-us;168440
inline ostream& operator<<(ostream& os, __int64 i)
{
char buf[20];
sprintf(buf,"%I64d", i);
os << buf;
return os;
}
inline ostream& operator<<(ostream& os, unsigned __int64 i)
{
char buf[20];
sprintf(buf,"%I64u", i);
os << buf;
return os;
}
#endif
} // namespace std
// GCD tests
// GCD on signed integer types
template< class T > void gcd_int_test() // signed_test_types
{
using boost::integer::gcd;
// Originally from Boost.Rational tests
BOOST_TEST_EQ( gcd<T>( 1, -1), static_cast<T>( 1) );
BOOST_TEST_EQ( gcd<T>( -1, 1), static_cast<T>( 1) );
BOOST_TEST_EQ( gcd<T>( 1, 1), static_cast<T>( 1) );
BOOST_TEST_EQ( gcd<T>( -1, -1), static_cast<T>( 1) );
BOOST_TEST_EQ( gcd<T>( 0, 0), static_cast<T>( 0) );
BOOST_TEST_EQ( gcd<T>( 7, 0), static_cast<T>( 7) );
BOOST_TEST_EQ( gcd<T>( 0, 9), static_cast<T>( 9) );
BOOST_TEST_EQ( gcd<T>( -7, 0), static_cast<T>( 7) );
BOOST_TEST_EQ( gcd<T>( 0, -9), static_cast<T>( 9) );
BOOST_TEST_EQ( gcd<T>( 42, 30), static_cast<T>( 6) );
BOOST_TEST_EQ( gcd<T>( 6, -9), static_cast<T>( 3) );
BOOST_TEST_EQ( gcd<T>(-10, -10), static_cast<T>(10) );
BOOST_TEST_EQ( gcd<T>(-25, -10), static_cast<T>( 5) );
BOOST_TEST_EQ( gcd<T>( 3, 7), static_cast<T>( 1) );
BOOST_TEST_EQ( gcd<T>( 8, 9), static_cast<T>( 1) );
BOOST_TEST_EQ( gcd<T>( 7, 49), static_cast<T>( 7) );
}
// GCD on unmarked signed integer type
void gcd_unmarked_int_test()
{
using boost::integer::gcd;
// The regular signed-integer GCD function performs the unsigned version,
// then does an absolute-value on the result. Signed types that are not
// marked as such (due to no std::numeric_limits specialization) may be off
// by a sign.
BOOST_TEST_EQ( abs(gcd<MyInt2>( 1, -1 )), MyInt2( 1) );
BOOST_TEST_EQ( abs(gcd<MyInt2>( -1, 1 )), MyInt2( 1) );
BOOST_TEST_EQ( abs(gcd<MyInt2>( 1, 1 )), MyInt2( 1) );
BOOST_TEST_EQ( abs(gcd<MyInt2>( -1, -1 )), MyInt2( 1) );
BOOST_TEST_EQ( abs(gcd<MyInt2>( 0, 0 )), MyInt2( 0) );
BOOST_TEST_EQ( abs(gcd<MyInt2>( 7, 0 )), MyInt2( 7) );
BOOST_TEST_EQ( abs(gcd<MyInt2>( 0, 9 )), MyInt2( 9) );
BOOST_TEST_EQ( abs(gcd<MyInt2>( -7, 0 )), MyInt2( 7) );
BOOST_TEST_EQ( abs(gcd<MyInt2>( 0, -9 )), MyInt2( 9) );
BOOST_TEST_EQ( abs(gcd<MyInt2>( 42, 30 )), MyInt2( 6) );
BOOST_TEST_EQ( abs(gcd<MyInt2>( 6, -9 )), MyInt2( 3) );
BOOST_TEST_EQ( abs(gcd<MyInt2>( -10, -10 )), MyInt2(10) );
BOOST_TEST_EQ( abs(gcd<MyInt2>( -25, -10 )), MyInt2( 5) );
BOOST_TEST_EQ( abs(gcd<MyInt2>( 3, 7 )), MyInt2( 1) );
BOOST_TEST_EQ( abs(gcd<MyInt2>( 8, 9 )), MyInt2( 1) );
BOOST_TEST_EQ( abs(gcd<MyInt2>( 7, 49 )), MyInt2( 7) );
}
// GCD on unsigned integer types
template< class T > void gcd_unsigned_test() // unsigned_test_types
{
using boost::integer::gcd;
// Note that unmarked types (i.e. have no std::numeric_limits
// specialization) are treated like non/unsigned types
BOOST_TEST_EQ( gcd<T>( 1u, 1u), static_cast<T>( 1u) );
BOOST_TEST_EQ( gcd<T>( 0u, 0u), static_cast<T>( 0u) );
BOOST_TEST_EQ( gcd<T>( 7u, 0u), static_cast<T>( 7u) );
BOOST_TEST_EQ( gcd<T>( 0u, 9u), static_cast<T>( 9u) );
BOOST_TEST_EQ( gcd<T>(42u, 30u), static_cast<T>( 6u) );
BOOST_TEST_EQ( gcd<T>( 3u, 7u), static_cast<T>( 1u) );
BOOST_TEST_EQ( gcd<T>( 8u, 9u), static_cast<T>( 1u) );
BOOST_TEST_EQ( gcd<T>( 7u, 49u), static_cast<T>( 7u) );
}
// GCD at compile-time
void gcd_static_test()
{
using boost::integer::static_gcd;
BOOST_TEST_EQ( (static_gcd< 1, 1>::value), 1 );
BOOST_TEST_EQ( (static_gcd< 0, 0>::value), 0 );
BOOST_TEST_EQ( (static_gcd< 7, 0>::value), 7 );
BOOST_TEST_EQ( (static_gcd< 0, 9>::value), 9 );
BOOST_TEST_EQ( (static_gcd<42, 30>::value), 6 );
BOOST_TEST_EQ( (static_gcd< 3, 7>::value), 1 );
BOOST_TEST_EQ( (static_gcd< 8, 9>::value), 1 );
BOOST_TEST_EQ( (static_gcd< 7, 49>::value), 7 );
}
// TODO: non-built-in signed and unsigned integer tests, with and without
// numeric_limits specialization; polynominal tests; note any changes if
// built-ins switch to binary-GCD algorithm
// LCM tests
// LCM on signed integer types
template< class T > void lcm_int_test() // signed_test_types
{
using boost::integer::lcm;
// Originally from Boost.Rational tests
BOOST_TEST_EQ( lcm<T>( 1, -1), static_cast<T>( 1) );
BOOST_TEST_EQ( lcm<T>( -1, 1), static_cast<T>( 1) );
BOOST_TEST_EQ( lcm<T>( 1, 1), static_cast<T>( 1) );
BOOST_TEST_EQ( lcm<T>( -1, -1), static_cast<T>( 1) );
BOOST_TEST_EQ( lcm<T>( 0, 0), static_cast<T>( 0) );
BOOST_TEST_EQ( lcm<T>( 6, 0), static_cast<T>( 0) );
BOOST_TEST_EQ( lcm<T>( 0, 7), static_cast<T>( 0) );
BOOST_TEST_EQ( lcm<T>( -5, 0), static_cast<T>( 0) );
BOOST_TEST_EQ( lcm<T>( 0, -4), static_cast<T>( 0) );
BOOST_TEST_EQ( lcm<T>( 18, 30), static_cast<T>(90) );
BOOST_TEST_EQ( lcm<T>( -6, 9), static_cast<T>(18) );
BOOST_TEST_EQ( lcm<T>(-10, -10), static_cast<T>(10) );
BOOST_TEST_EQ( lcm<T>( 25, -10), static_cast<T>(50) );
BOOST_TEST_EQ( lcm<T>( 3, 7), static_cast<T>(21) );
BOOST_TEST_EQ( lcm<T>( 8, 9), static_cast<T>(72) );
BOOST_TEST_EQ( lcm<T>( 7, 49), static_cast<T>(49) );
}
// LCM on unmarked signed integer type
void lcm_unmarked_int_test()
{
using boost::integer::lcm;
// The regular signed-integer LCM function performs the unsigned version,
// then does an absolute-value on the result. Signed types that are not
// marked as such (due to no std::numeric_limits specialization) may be off
// by a sign.
BOOST_TEST_EQ( abs(lcm<MyInt2>( 1, -1 )), MyInt2( 1) );
BOOST_TEST_EQ( abs(lcm<MyInt2>( -1, 1 )), MyInt2( 1) );
BOOST_TEST_EQ( abs(lcm<MyInt2>( 1, 1 )), MyInt2( 1) );
BOOST_TEST_EQ( abs(lcm<MyInt2>( -1, -1 )), MyInt2( 1) );
BOOST_TEST_EQ( abs(lcm<MyInt2>( 0, 0 )), MyInt2( 0) );
BOOST_TEST_EQ( abs(lcm<MyInt2>( 6, 0 )), MyInt2( 0) );
BOOST_TEST_EQ( abs(lcm<MyInt2>( 0, 7 )), MyInt2( 0) );
BOOST_TEST_EQ( abs(lcm<MyInt2>( -5, 0 )), MyInt2( 0) );
BOOST_TEST_EQ( abs(lcm<MyInt2>( 0, -4 )), MyInt2( 0) );
BOOST_TEST_EQ( abs(lcm<MyInt2>( 18, 30 )), MyInt2(90) );
BOOST_TEST_EQ( abs(lcm<MyInt2>( -6, 9 )), MyInt2(18) );
BOOST_TEST_EQ( abs(lcm<MyInt2>( -10, -10 )), MyInt2(10) );
BOOST_TEST_EQ( abs(lcm<MyInt2>( 25, -10 )), MyInt2(50) );
BOOST_TEST_EQ( abs(lcm<MyInt2>( 3, 7 )), MyInt2(21) );
BOOST_TEST_EQ( abs(lcm<MyInt2>( 8, 9 )), MyInt2(72) );
BOOST_TEST_EQ( abs(lcm<MyInt2>( 7, 49 )), MyInt2(49) );
}
// LCM on unsigned integer types
template< class T > void lcm_unsigned_test() // unsigned_test_types
{
using boost::integer::lcm;
// Note that unmarked types (i.e. have no std::numeric_limits
// specialization) are treated like non/unsigned types
BOOST_TEST_EQ( lcm<T>( 1u, 1u), static_cast<T>( 1u) );
BOOST_TEST_EQ( lcm<T>( 0u, 0u), static_cast<T>( 0u) );
BOOST_TEST_EQ( lcm<T>( 6u, 0u), static_cast<T>( 0u) );
BOOST_TEST_EQ( lcm<T>( 0u, 7u), static_cast<T>( 0u) );
BOOST_TEST_EQ( lcm<T>(18u, 30u), static_cast<T>(90u) );
BOOST_TEST_EQ( lcm<T>( 3u, 7u), static_cast<T>(21u) );
BOOST_TEST_EQ( lcm<T>( 8u, 9u), static_cast<T>(72u) );
BOOST_TEST_EQ( lcm<T>( 7u, 49u), static_cast<T>(49u) );
}
// LCM at compile-time
void lcm_static_test()
{
using boost::integer::static_lcm;
BOOST_TEST_EQ( (static_lcm< 1, 1>::value), 1 );
BOOST_TEST_EQ( (static_lcm< 0, 0>::value), 0 );
BOOST_TEST_EQ( (static_lcm< 6, 0>::value), 0 );
BOOST_TEST_EQ( (static_lcm< 0, 7>::value), 0 );
BOOST_TEST_EQ( (static_lcm<18, 30>::value), 90 );
BOOST_TEST_EQ( (static_lcm< 3, 7>::value), 21 );
BOOST_TEST_EQ( (static_lcm< 8, 9>::value), 72 );
BOOST_TEST_EQ( (static_lcm< 7, 49>::value), 49 );
}
// TODO: see GCD to-do
// main
// Various types to test with each GCD/LCM
#define TEST_SIGNED_( test ) \
test<signed char>(); \
test<short>(); \
test<int>(); \
test<long>(); \
test<MyInt1>();
#ifdef BOOST_HAS_LONG_LONG
# define TEST_SIGNED( test ) \
TEST_SIGNED_( test ) \
test<boost::long_long_type>();
#elif defined(BOOST_HAS_MS_INT64)
# define TEST_SIGNED( test ) \
TEST_SIGNED_( test ) \
test<__int64>();
#endif
#define TEST_UNSIGNED_( test ) \
test<unsigned char>(); \
test<unsigned short>(); \
test<unsigned>(); \
test<unsigned long>(); \
test<MyUnsigned1>(); \
test<MyUnsigned2>();
#ifdef BOOST_HAS_LONG_LONG
# define TEST_UNSIGNED( test ) \
TEST_UNSIGNED_( test ) \
test<boost::ulong_long_type>();
#elif defined(BOOST_HAS_MS_INT64)
# define TEST_UNSIGNED( test ) \
TEST_UNSIGNED_( test ) \
test<unsigned __int64>();
#endif
int main()
{
TEST_SIGNED( gcd_int_test )
gcd_unmarked_int_test();
TEST_UNSIGNED( gcd_unsigned_test )
gcd_static_test();
TEST_SIGNED( lcm_int_test )
lcm_unmarked_int_test();
TEST_UNSIGNED( lcm_unsigned_test )
lcm_static_test();
return boost::report_errors();
}

View File

@ -69,7 +69,7 @@ void do_test_exact(boost::mpl::true_ const&)
template <int Bits>
void do_test_exact(boost::mpl::false_ const&)
{
// Nothing to do, type does not have an ::extact member.
// Nothing to do, type does not have an ::exact member.
}
template <int Bits>