The integer_t class template. More...
Public Types | |
typedef base_divide_t < integer_t<...> > | divide_t |
Public Member Functions | |
Constructors & Destructors | |
integer_t () | |
Creates a new integer with an initial value of zero. | |
integer_t (const integer_t<...> &b, bool force_thread_safety=false) | |
Creates a copy of an existing integer. | |
integer_t (BOOST_XINT_RV_REF(type) b) | |
template<typename charT > | |
integer_t (const charT *str, std::size_t base=10) | |
template<typename charT > | |
integer_t (const charT *str, const charT *&endptr, std::size_t base=10) | |
Parse an integer from a string representation. | |
template<typename charT , typename traitsT , typename allocT > | |
integer_t (const std::basic_string< charT, traitsT, allocT > &str, std::size_t base=10) | |
Create an integer from a string representation. | |
integer_t (const xint::binary_t b, bitsize_t bits=0) | |
Converts a portable binary representation of a number into an integer. | |
template<other > | |
integer_t (const integer_t< other > &other, bool force_thread_safety=false) | |
template<typename Type > | |
integer_t (const Type n, typename boost::enable_if< boost::is_integral< Type > >::type *=0) | |
Mathematical and Bitwise Operators | |
integer_t<...> & | operator= (BOOST_XINT_COPY_ASSIGN_REF(type) c) |
integer_t<...> & | operator= (BOOST_XINT_RV_REF(type) c) |
template<typename Type > | |
integer_t<...> & | operator= (const Type n) |
operator safe_bool_type () const | |
bool | operator! () const |
integer_t<...> | operator- () const |
Return the additive inverse of an integer. | |
integer_t<...> & | operator+ () |
const integer_t<...> & | operator+ () const |
integer_t<...> | operator~ () const |
The Bitwise Ones Complement (i.e. bitwise NOT) operator. | |
integer_t<...> & | operator+= (const integer_t<...> b) |
integer_t<...> & | operator-= (const integer_t<...> b) |
integer_t<...> & | operator*= (const integer_t<...> b) |
integer_t<...> & | operator/= (const integer_t<...> b) |
integer_t<...> & | operator%= (const integer_t<...> b) |
integer_t<...> & | operator++ () |
integer_t<...> & | operator-- () |
integer_t<...> | operator++ (int) |
integer_t<...> | operator-- (int) |
integer_t<...> & | operator&= (const integer_t<...> n) |
integer_t<...> & | operator|= (const integer_t<...> n) |
integer_t<...> & | operator^= (const integer_t<...> n) |
integer_t<...> | operator<< (bitsize_t shift) const |
Left-shift an integer by a specified number of bits. | |
integer_t<...> | operator>> (bitsize_t shift) const |
Right-shift an integer by a specified number of bits. | |
integer_t<...> & | operator<<= (bitsize_t shift) |
integer_t<...> & | operator>>= (bitsize_t shift) |
Miscellaneous Functions | |
bool | is_odd () const |
Tests the lowest bit of *this to determine oddness. | |
bool | is_even () const |
Tests the lowest bit of *this to determine evenness. | |
int | sign (bool signed_zero=false) const |
Tests the sign of *this . | |
std::size_t | hex_digits () const |
Tells you roughly how large *this is. | |
bool | is_nan () const |
Tests whether the object is a Not-a-Number. | |
Static Public Member Functions | |
Static Member Functions | |
These are functions that return an integer_t, but don't take one as a parameter. To use them as free functions, you would have to specify the return type anyway, so they're static member functions of the type instead. | |
static integer_t<...> | pow2 (std::size_t exponent) |
Calculate the value of 2e | |
static integer_t<...> | factorial (std::size_t n) |
Calculate the value of n! | |
static integer_t<...> | nan () |
Returns a Not-a-Number value, for types that support it. | |
template<class Type > | |
static integer_t<...> | random_by_size (Type &gen, bitsize_t size_in_bits, bool high_bit_on=false, bool low_bit_on=false, bool can_be_negative=false) |
Generates a random integer with specific attributes. | |
template<class Type > | |
static integer_t<...> | random_prime (Type &gen, bitsize_t size_in_bits, callback_t callback=no_callback) |
Generate a randomly-generated prime number of a particular bit-size. |
The integer_t class template.
This class implements the standard aribitrary-length integer type.
There are only a few member functions, primarily operators. Most of the functionality is implemented using standalone functions.
You can specify several template parameters for the type. Most of these parameters must be specified via helper classes defined in the boost::xint::options namespace. See the list and descriptions there.
Here are some examples of how to specify an integer type:
namespace xopts = boost::xint::options; typedef boost::xint::integer_t<xopts::fixedlength<512>, xopts::secure, xopts::negative_modulus> secure_uint512; typedef boost::xint::integer_t<my_allocator<boost::xint::detail::digit_t>, xopts::copy_on_write> my_integer;
Note that the basic integer and nothrow_integer types are already typedef'd for you.
Definition at line 50 of file integer.hpp.
typedef base_divide_t<integer_t<...> > boost::xint::integer_t<>::divide_t |
Definition at line 147 of file integer.hpp.
boost::xint::integer_t<>::integer_t | ( | ) |
Creates a new integer with an initial value of zero.
Definition at line 224 of file integer.hpp.
boost::xint::integer_t<>::integer_t | ( | const integer_t<...> & | b, | |
bool | force_thread_safety = false | |||
) |
Creates a copy of an existing integer.
[in] | b | An existing integer. |
[in] | force_thread_safety | If the threadsafe template parameter is false , you can use this to make a thread-safe copy anyway. See this page for a full treatment of the matter. |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
Definition at line 239 of file integer.hpp.
boost::xint::integer_t<>::integer_t | ( | BOOST_XINT_RV_REF(type) | b | ) | [inline] |
Definition at line 61 of file integer.hpp.
boost::xint::integer_t<>::integer_t | ( | const charT * | str, | |
std::size_t | base = 10 | |||
) | [explicit] |
Create an integer from a string representation.
[in] | str | A string representation of a number. |
[in] | base | The base that the string representation of the number is in. This can be any number between 2 and 36 (inclusive). It can also be the constant xint::autobase, in which case the function will follow the standard C/C++ rules for interpreting a numeric constant: any number with a zero as the first digit is assumed to be base-8; any number with a leading zero-x or zero-X (such as 0x1f) is base-16, and anything else is base-10. |
exceptions::invalid_base | if the base parameter is not between 2 and 36 (inclusive) or the constant xint::autobase. | |
exceptions::invalid_digit | if the string contains any digit that cannot be part of a number in the specified base, or if there are no valid digits. |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
Definition at line 261 of file integer.hpp.
boost::xint::integer_t<>::integer_t | ( | const charT * | str, | |
const charT *& | endptr, | |||
std::size_t | base = 10 | |||
) |
Parse an integer from a string representation.
[in] | str | A string representation of a number. |
[out] | endptr | When the function returns, this will contain a pointer to the first character after the parsed integer. |
[in] | base | The base that the string representation of the number is in. This can be any number between 2 and 36 (inclusive). It can also be the constant xint::autobase, in which case the function will follow the standard C/C++ rules for interpreting a numeric constant: any number with a zero as the first digit is assumed to be base-8; any number with a leading zero-x or zero-X (such as 0x1f) is base-16, and anything else is base-10. |
exceptions::invalid_base | if the base parameter is not between 2 and 36 (inclusive) or the constant xint::autobase. | |
exceptions::invalid_digit | if there are no valid digits before the first invalid character. |
strtol
function from the standard library. Like strtol
, it first discards any leading whitespace characters, then takes as many characters as are valid for the given base
and interprets them as an integer. Then a pointer to the first character following the integer is stored in the object pointed by endptr
.This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
Definition at line 310 of file integer.hpp.
boost::xint::integer_t<>::integer_t | ( | const std::basic_string< charT, traitsT, allocT > & | str, | |
std::size_t | base = 10 | |||
) | [explicit] |
Create an integer from a string representation.
[in] | str | A string representation of a number. |
[in] | base | The base that the string representation of the number is in. This can be any number between 2 and 36 (inclusive). It can also be the constant xint::autobase, in which case the function will follow the standard C/C++ rules for interpreting a numeric constant: any number with a zero as the first digit is assumed to be base-8; any number with a leading zero-x or zero-X (such as 0x1f) is base-16, and anything else is base-10. |
exceptions::invalid_base | if the base parameter is not between 2 and 36 (inclusive) or the constant xint::autobase. | |
exceptions::invalid_digit | if the string contains any digit that cannot be part of a number in the specified base, or if there are no valid digits. |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
Definition at line 361 of file integer.hpp.
boost::xint::integer_t<>::integer_t | ( | const xint::binary_t | b, | |
bitsize_t | bits = 0 | |||
) | [explicit] |
Converts a portable binary representation of a number into an integer.
[in] | b | An xint::binary_t containing the bytes to convert, lowest byte first. |
[in] | bits | The number of bits packed into each character (for portability with systems that might not have characters with the same number of bits), or zero (the default) to assume the number of bits in the current system's unsigned character type. |
exceptions::invalid_argument | if bits is greater than the number of bits in an unsigned character. |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
Definition at line 402 of file integer.hpp.
boost::xint::integer_t<>::integer_t | ( | const integer_t< other > & | b, | |
bool | force_thread_safety = false | |||
) | [explicit] |
Constructs a new integer object from an existing integer object.
Converts any existing integer_t object to this type.
[in] | b | An existing integer. |
[in] | force_thread_safety | If the threadsafe template parameter is false , you can use this to make a thread-safe copy anyway. See this page for a full treatment of the matter. |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
Definition at line 429 of file integer.hpp.
boost::xint::integer_t<>::integer_t | ( | const Type | n, | |
typename boost::enable_if< boost::is_integral< Type > >::type * | = 0 | |||
) |
Constructs a new integer object with the value of the built-in integer type passed to it.
[in] | n | The value for the new object. |
Type | The type of n . |
You can ignore the enable_if
parameter, it's there only to tell the compiler what types the function applies to.
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
Definition at line 469 of file integer.hpp.
integer_t<...> boost::xint::integer_t<>::factorial | ( | std::size_t | n | ) | [static] |
Calculate the value of n!
[in] | n | The value to operate on. |
n
factorial, defined as 1*2*3*...*n
n
. This function, when used on an unlimited-size integer and with a large parameter, is the easiest way to exhaust the system's memory. Definition at line 1074 of file integer.hpp.
size_t boost::xint::integer_t<>::hex_digits | ( | ) | const |
Tells you roughly how large *this
is.
this
. The nothrow version returns zero instead of throwing. Definition at line 1012 of file integer.hpp.
bool boost::xint::integer_t<>::is_even | ( | ) | const |
Tests the lowest bit of *this
to determine evenness.
true
if *this
is even, otherwise false
. The nothrow version returns false instead of throwing. Definition at line 954 of file integer.hpp.
bool boost::xint::integer_t<>::is_nan | ( | ) | const |
Tests whether the object is a Not-a-Number.
true
if *this
is a Not-a-Number value, otherwise false
.false
on any type where the nothrow
template parameter is false
. bool boost::xint::integer_t<>::is_odd | ( | ) | const |
Tests the lowest bit of *this
to determine oddness.
true
if *this
is odd, otherwise false
. The nothrow version returns false instead of throwing. Definition at line 937 of file integer.hpp.
integer_t<...> boost::xint::integer_t<>::nan | ( | ) | [static] |
Returns a Not-a-Number value, for types that support it.
nothrow
template parameter.boost::xint::integer_t<>::operator safe_bool_type | ( | ) | const [inline] |
Definition at line 96 of file integer.hpp.
bool boost::xint::integer_t<>::operator! | ( | ) | const [inline] |
Definition at line 98 of file integer.hpp.
integer_t<...> & boost::xint::integer_t<>::operator%= | ( | const integer_t<...> | b | ) |
Definition at line 651 of file integer.hpp.
integer_t<...> & boost::xint::integer_t<>::operator&= | ( | const integer_t<...> | n | ) |
Definition at line 771 of file integer.hpp.
integer_t<...> & boost::xint::integer_t<>::operator*= | ( | const integer_t<...> | b | ) |
Definition at line 607 of file integer.hpp.
const integer_t<...>& boost::xint::integer_t<>::operator+ | ( | ) | const [inline] |
Definition at line 101 of file integer.hpp.
integer_t<...>& boost::xint::integer_t<>::operator+ | ( | ) | [inline] |
Definition at line 100 of file integer.hpp.
integer_t<...> boost::xint::integer_t<>::operator++ | ( | int | ) |
Postincrement operator
This is not recommended. It must make a copy before incrementing the *this
object, making it noticeably less efficient than the preincrement operator.
Definition at line 725 of file integer.hpp.
integer_t<...> & boost::xint::integer_t<>::operator++ | ( | ) |
integer_t<...> & boost::xint::integer_t<>::operator+= | ( | const integer_t<...> | b | ) |
Definition at line 561 of file integer.hpp.
integer_t<...> boost::xint::integer_t<>::operator- | ( | ) | const |
Return the additive inverse of an integer.
*this
with the sign reversed. Definition at line 538 of file integer.hpp.
integer_t<...> & boost::xint::integer_t<>::operator-- | ( | ) |
integer_t<...> boost::xint::integer_t<>::operator-- | ( | int | ) |
Postdecrement operator
This is not recommended. It must make a copy before decrementing the *this
object, making it noticeably less efficient than the predecrement operator.
Definition at line 750 of file integer.hpp.
integer_t<...> & boost::xint::integer_t<>::operator-= | ( | const integer_t<...> | b | ) |
Definition at line 583 of file integer.hpp.
integer_t<...> & boost::xint::integer_t<>::operator/= | ( | const integer_t<...> | b | ) |
Definition at line 629 of file integer.hpp.
integer_t<...> boost::xint::integer_t<>::operator<< | ( | bitsize_t | shift | ) | const |
Left-shift an integer by a specified number of bits.
[in] | shift | The number of bits to left-shift the number. |
Definition at line 845 of file integer.hpp.
integer_t<...> & boost::xint::integer_t<>::operator<<= | ( | bitsize_t | shift | ) |
Definition at line 892 of file integer.hpp.
integer_t<...>& boost::xint::integer_t<>::operator= | ( | BOOST_XINT_RV_REF(type) | c | ) | [inline] |
Definition at line 91 of file integer.hpp.
integer_t<...> & boost::xint::integer_t<>::operator= | ( | BOOST_XINT_COPY_ASSIGN_REF(type) | c | ) |
Definition at line 491 of file integer.hpp.
integer_t<...>& boost::xint::integer_t<>::operator= | ( | const Type | n | ) | [inline] |
Definition at line 93 of file integer.hpp.
integer_t<...> boost::xint::integer_t<>::operator>> | ( | bitsize_t | shift | ) | const |
Right-shift an integer by a specified number of bits.
[in] | shift | The number of bits to right-shift the number. |
Definition at line 872 of file integer.hpp.
integer_t<...> & boost::xint::integer_t<>::operator>>= | ( | bitsize_t | shift | ) |
Definition at line 912 of file integer.hpp.
integer_t<...> & boost::xint::integer_t<>::operator^= | ( | const integer_t<...> | n | ) |
Definition at line 815 of file integer.hpp.
integer_t<...> & boost::xint::integer_t<>::operator|= | ( | const integer_t<...> | n | ) |
Definition at line 793 of file integer.hpp.
integer_t<...> boost::xint::integer_t<>::operator~ | ( | ) | const |
The Bitwise Ones Complement (i.e. bitwise NOT) operator.
n
is a built-in signed type and m
is a signed integer_t with the same value, then ~n != ~m
. integer_t<...> boost::xint::integer_t<>::pow2 | ( | std::size_t | exponent | ) | [static] |
Calculate the value of 2e
[in] | exponent | The exponent to operate on. |
exponent
.pow
function to get the same result. Definition at line 1039 of file integer.hpp.
integer_t<...> boost::xint::integer_t<>::random_by_size | ( | Type & | gen, | |
bitsize_t | size_in_bits, | |||
bool | high_bit_on = false , |
|||
bool | low_bit_on = false , |
|||
bool | can_be_negative = false | |||
) | [static] |
Generates a random integer with specific attributes.
[in] | gen | The random number generator to use. This can be boost::xint::default_random_generator, boost::xint::strong_random_generator, or any other class that supplies the member functions required by the Boost.Random random number generators. |
[in] | size_in_bits | The maximum number of bits that you want the returned number to have. |
[in] | high_bit_on | If true , the returned number will have exactly the requested size. If false , the upper bits may be zero, resulting in a number that is slightly smaller than requested. |
[in] | low_bit_on | If true , the returned number will always be odd. If false , it has an equal chance of being odd or even. |
[in] | can_be_negative | If true , the returned value has an equal chance of being positive or negative. If false , it will always be positive. Note that on unsigned types, this parameter is ignored; the result will always be positive regardless of it. |
Definition at line 1134 of file integer.hpp.
integer_t<...> boost::xint::integer_t<>::random_prime | ( | Type & | gen, | |
bitsize_t | size_in_bits, | |||
callback_t | callback = no_callback | |||
) | [static] |
Generate a randomly-generated prime number of a particular bit-size.
[in] | gen | The random number generator to use. This can be boost::xint::default_random_generator, boost::xint::strong_random_generator, or any other class that supplies the member functions required by the Boost.Random random number generators. |
[in] | size_in_bits | The number of bits that you want the returned value to have. |
[in] | callback | An optional function that will be called regularly during the operation. If it returns false , the function will immediately return. |
exceptions::invalid_argument | if size_in_bits is less than two. |
Definition at line 1181 of file integer.hpp.
int boost::xint::integer_t<>::sign | ( | bool | signed_zero = false |
) | const |
Tests the sign of *this
.
[in] | signed_zero | If false (the default value), returns zero if *this is zero. If true , returns 1 or -1 on a zero *this as well. Primarily used to identify a negative zero. |
*this
is negative, 0 if it's zero, or 1 if it's greater than zero. The nothrow version returns zero instead of throwing. Definition at line 991 of file integer.hpp.
© Copyright Chad Nelson, 2010-2011. 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)