Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue #13(std::variant) #21

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ matrix:
- wget --quiet -O - ${BOOST_URL} | tar --strip-components=1 -xz -C /tmp/boost || exit 1
- cd /tmp/boost/tools/build && ./bootstrap.sh && ./b2 install --prefix=/tmp/b2
- export PATH=/tmp/b2/bin:${PATH}
- cd /tmp/boost && b2 toolset=gcc cxxflags="-std=c++14 ${CXXFLAGS}"
- cd /tmp/boost && b2 toolset=gcc cxxflags="-std=c++17 ${CXXFLAGS}"
- os: osx
osx_image: xcode9.4
env:
Expand Down
68 changes: 41 additions & 27 deletions include/real/real.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <algorithm>
#include <initializer_list>
#include <utility>
#include <variant>

#include <real/real_exception.hpp>
#include <real/real_helpers.hpp>
Expand Down Expand Up @@ -59,12 +60,14 @@ namespace boost {

KIND _kind;

std::variant<real_explicit, real_algorithm> diff_number;
w3rew marked this conversation as resolved.
Show resolved Hide resolved
/*
// Explicit number
real_explicit _explicit_number;

// Algorithmic number
real_algorithm _algorithmic_number;

*/
// Composed number
OPERATION _operation;
real* _lhs_ptr = nullptr;
Expand Down Expand Up @@ -103,11 +106,16 @@ namespace boost {
// Internal number to iterate
real const* _real_ptr = nullptr;

std::variant<boost::real::real_explicit::const_precision_iterator,
boost::real::real_algorithm::const_precision_iterator> diff_it;

/*
w3rew marked this conversation as resolved.
Show resolved Hide resolved
// Explicit number iterator
boost::real::real_explicit::const_precision_iterator _explicit_it;

// Algorithmic number iterator
boost::real::real_algorithm::const_precision_iterator _algorithmic_it;
*/

// If the number is a composition, the const_precision_iterator uses the operand iterators
const_precision_iterator* _lhs_it_ptr = nullptr;
Expand Down Expand Up @@ -303,13 +311,15 @@ namespace boost {
switch (this->_real_ptr->_kind) {

case KIND::EXPLICIT:
this->_explicit_it = this->_real_ptr->_explicit_number.cbegin();
this->approximation_interval = this->_explicit_it.approximation_interval;
this->diff_it = std::get<real_explicit>(
w3rew marked this conversation as resolved.
Show resolved Hide resolved
this->_real_ptr->diff_number).cbegin();
this->approximation_interval = std::get<0>(this -> diff_it).approximation_interval;
w3rew marked this conversation as resolved.
Show resolved Hide resolved
break;

case KIND::ALGORITHM:
this->_algorithmic_it = this->_real_ptr->_algorithmic_number.cbegin();
this->approximation_interval = this->_algorithmic_it.approximation_interval;
this->diff_it = std::get<real_algorithm>
(this->_real_ptr->diff_number).cbegin();
this->approximation_interval = std::get<1>(this->diff_it).approximation_interval;
break;

case KIND::OPERATION:
Expand Down Expand Up @@ -339,20 +349,25 @@ namespace boost {

case KIND::EXPLICIT:
if (cend) {
this->_explicit_it = this->_real_ptr->_explicit_number.cend();
this->diff_it = std::get<real_explicit>
(this->_real_ptr->diff_number).cend();
} else {
this->_explicit_it = this->_real_ptr->_explicit_number.cbegin();
this->diff_it=
std::get<real_explicit>
(this->_real_ptr->diff_number).cbegin();
}
this->approximation_interval = this->_explicit_it.approximation_interval;
this->approximation_interval = std::get<0>(this->diff_it).approximation_interval;
break;

case KIND::ALGORITHM:
if (cend) {
this->_algorithmic_it = this->_real_ptr->_algorithmic_number.cend();
this->diff_it = std::get<real_algorithm>(
this->_real_ptr->diff_number).cend();
} else {
this->_algorithmic_it = this->_real_ptr->_algorithmic_number.cbegin();
this->diff_it = std::get<real_algorithm>
(this->_real_ptr->diff_number).cbegin();
}
this->approximation_interval = this->_algorithmic_it.approximation_interval;
this->approximation_interval = std::get<1>(this->diff_it).approximation_interval;
break;

case KIND::OPERATION:
Expand All @@ -372,13 +387,13 @@ namespace boost {
switch (this->_real_ptr->_kind) {

case KIND::EXPLICIT:
++this->_explicit_it;
this->approximation_interval = this->_explicit_it.approximation_interval;
++(std::get<0>(this->diff_it));
this->approximation_interval = std::get<0>(this->diff_it).approximation_interval;
break;

case KIND::ALGORITHM:
++this->_algorithmic_it;
this->approximation_interval = this->_algorithmic_it.approximation_interval;
++(std::get<1>(this->diff_it));
this->approximation_interval = std::get<1>(this->diff_it).approximation_interval;
break;

case KIND::OPERATION:
Expand Down Expand Up @@ -440,8 +455,7 @@ namespace boost {
*/
real(const real& other) :
_kind(other._kind),
_explicit_number(other._explicit_number),
_algorithmic_number(other._algorithmic_number),
diff_number(other.diff_number),
_operation(other._operation) { this->copy_operands(other); };

/**
Expand All @@ -454,7 +468,7 @@ namespace boost {
* @throws boost::real::invalid_string_number exception
*/
real(const std::string& number)
: _kind(KIND::EXPLICIT), _explicit_number(number) {}
: _kind(KIND::EXPLICIT), diff_number(std::in_place_type<real_explicit>, number) {}

/**
* @brief *Initializer list constructor:* Creates a boost::real::real_explicit instance
Expand All @@ -464,7 +478,7 @@ namespace boost {
* @param digits - a initializer_list<int> that represents the number digits.
*/
real(std::initializer_list<int> digits)
: _kind(KIND::EXPLICIT), _explicit_number(digits, digits.size()) {}
: _kind(KIND::EXPLICIT), diff_number(std::in_place_type<real_explicit>, digits, digits.size()) {}


/**
Expand All @@ -478,7 +492,7 @@ namespace boost {
* the number is positive, otherwise is negative.
*/
real(std::initializer_list<int> digits, bool positive)
: _kind(KIND::EXPLICIT), _explicit_number(digits, digits.size(), positive) {}
: _kind(KIND::EXPLICIT), diff_number(std::in_place_type<real_explicit>, digits, digits.size(), positive) {}

/**
* @brief *Initializer list constructor with exponent:* Creates a boost::real::real
Expand All @@ -490,7 +504,7 @@ namespace boost {
* @param exponent - an integer representing the number exponent.
*/
real(std::initializer_list<int> digits, int exponent)
: _kind(KIND::EXPLICIT), _explicit_number(digits, exponent) {};
: _kind(KIND::EXPLICIT), diff_number(std::in_place_type<real_explicit>, digits, exponent) {};

/**
* @brief *Initializer list constructor with exponent and sign:* Creates a boost::real::real instance
Expand All @@ -504,7 +518,7 @@ namespace boost {
* the number is positive, otherwise is negative.
*/
real(std::initializer_list<int> digits, int exponent, bool positive)
: _kind(KIND::EXPLICIT), _explicit_number(digits, exponent, positive) {};
: _kind(KIND::EXPLICIT), diff_number(std::in_place_type<real_explicit>, digits, exponent, positive) {};

/**
* @brief *Lambda function constructor with exponent:* Creates a boost::real::real
Expand All @@ -517,7 +531,7 @@ namespace boost {
* @param exponent - an integer representing the number exponent.
*/
real(int (*get_nth_digit)(unsigned int), int exponent)
: _kind(KIND::ALGORITHM), _algorithmic_number(get_nth_digit, exponent) {}
: _kind(KIND::ALGORITHM), diff_number(std::in_place_type<real_algorithm>, get_nth_digit, exponent) {}

/**
* @brief *Lambda function constructor with exponent and sign:* Creates a boost::real::real instance
Expand All @@ -536,7 +550,7 @@ namespace boost {
int exponent,
bool positive)
: _kind(KIND::ALGORITHM),
_algorithmic_number(get_nth_digit, exponent, positive) {}
diff_number(std::in_place_type<real_algorithm>, get_nth_digit, exponent, positive) {}

/**
* @brief *Default destructor:* If the number is an operator, the destructor destroys its operands.
Expand Down Expand Up @@ -616,11 +630,11 @@ namespace boost {
switch (this->_kind) {

case KIND::EXPLICIT:
result = this->_explicit_number[n];
result = std::get<real_explicit>(this -> diff_number)[n];
break;

case KIND::ALGORITHM:
result = this->_algorithmic_number[n];
result = std::get<real_algorithm>(this->diff_number)[n];
break;

case KIND::OPERATION:
Expand Down Expand Up @@ -729,7 +743,7 @@ namespace boost {
*/
real& operator=(const real& other) {
this->_kind = other._kind;
this->_explicit_number = other._explicit_number;
this->diff_number = other.diff_number;
this->_operation = other._operation;
this->copy_operands(other);
return *this;
Expand Down