2
0
mirror of https://github.com/boostorg/math.git synced 2026-01-19 04:22:09 +00:00

Merge branch 'polynomial_constructor' into develop

This commit is contained in:
jzmaddock
2018-12-01 11:21:27 +00:00
4 changed files with 74 additions and 3 deletions

View File

@@ -24,7 +24,11 @@
template <class I>
polynomial(I first, I last);
template <class U>
explicit polynomial(const U& point);
explicit polynomial(const U& point,
typename boost::enable_if<boost::is_convertible<U, T> >::type* = 0);
template <class Range>
explicit polynomial(const Range& r,
typename boost::enable_if<boost::math::tools::detail::is_const_iterable<Range> >::type* = 0); // C++14
polynomial(std::initializer_list<T> l); // C++11
polynomial(std::vector<T>&& p);

View File

@@ -0,0 +1,40 @@
// (C) Copyright John Maddock 2018.
// 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)
#ifndef BOOST_MATH_TOOLS_IS_CONST_ITERABLE_HPP
#define BOOST_MATH_TOOLS_IS_CONST_ITERABLE_HPP
#if !defined(BOOST_NO_CXX14_VARIABLE_TEMPLATES) && !defined(BOOST_NO_CXX11_DECLTYPE) && !defined(BOOST_NO_CXX11_SFINAE_EXPR)
#define BOOST_MATH_HAS_IS_CONST_ITERABLE
#include <boost/type_traits/is_detected.hpp>
#include <utility>
namespace boost {
namespace math {
namespace tools {
namespace detail {
template<class T>
using begin_t = decltype(std::declval<const T&>().begin());
template<class T>
using end_t = decltype(std::declval<const T&>().end());
template<class T>
using const_iterator_t = typename T::const_iterator;
template <class T>
struct is_const_iterable
: public boost::integral_constant<bool,
boost::is_detected<begin_t, T>::value
&& boost::is_detected<end_t, T>::value
&& boost::is_detected<const_iterator_t, T>::value
> {};
} } } }
#endif
#endif // BOOST_MATH_TOOLS_IS_CONST_ITERABLE_HPP

View File

@@ -23,6 +23,8 @@
#include <boost/math/policies/error_handling.hpp>
#include <boost/math/special_functions/binomial.hpp>
#include <boost/core/enable_if.hpp>
#include <boost/type_traits/is_convertible.hpp>
#include <boost/math/tools/detail/is_const_iterable.hpp>
#include <vector>
#include <ostream>
@@ -309,7 +311,7 @@ public:
#endif
template <class U>
explicit polynomial(const U& point)
explicit polynomial(const U& point, typename boost::enable_if<boost::is_convertible<U, T> >::type* = 0)
{
if (point != U(0))
m_data.push_back(point);
@@ -334,7 +336,13 @@ public:
m_data[i] = boost::math::tools::real_cast<T>(p[i]);
}
}
#ifdef BOOST_MATH_HAS_IS_CONST_ITERABLE
template <class Range>
explicit polynomial(const Range& r, typename boost::enable_if<boost::math::tools::detail::is_const_iterable<Range> >::type* = 0)
: polynomial(r.begin(), r.end())
{
}
#endif
#if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST) && !BOOST_WORKAROUND(BOOST_GCC_VERSION, < 40500)
polynomial(std::initializer_list<T> l) : polynomial(std::begin(l), std::end(l))
{

View File

@@ -79,6 +79,25 @@ BOOST_AUTO_TEST_CASE( test_construction )
BOOST_CHECK_EQUAL(a, b);
}
#ifdef BOOST_MATH_HAS_IS_CONST_ITERABLE
#include <list>
#include <array>
BOOST_AUTO_TEST_CASE(test_range_construction)
{
std::list<double> l{ 1, 2, 3, 4 };
std::array<double, 4> a{ 3, 4, 5, 6 };
polynomial<double> p1{ 1, 2, 3, 4 };
polynomial<double> p2{ 3, 4, 5, 6 };
polynomial<double> p3(l);
polynomial<double> p4(a);
BOOST_CHECK_EQUAL(p1, p3);
BOOST_CHECK_EQUAL(p2, p4);
}
#endif
#if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST) && !BOOST_WORKAROUND(BOOST_GCC_VERSION, < 40500)
BOOST_AUTO_TEST_CASE( test_initializer_list_construction )