libstdc++
|
00001 // <experimental/numeric> -*- C++ -*- 00002 00003 // Copyright (C) 2015-2016 Free Software Foundation, Inc. 00004 // 00005 // This file is part of the GNU ISO C++ Library. This library is free 00006 // software; you can redistribute it and/or modify it under the 00007 // terms of the GNU General Public License as published by the 00008 // Free Software Foundation; either version 3, or (at your option) 00009 // any later version. 00010 00011 // This library is distributed in the hope that it will be useful, 00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00014 // GNU General Public License for more details. 00015 00016 // Under Section 7 of GPL version 3, you are granted additional 00017 // permissions described in the GCC Runtime Library Exception, version 00018 // 3.1, as published by the Free Software Foundation. 00019 00020 // You should have received a copy of the GNU General Public License and 00021 // a copy of the GCC Runtime Library Exception along with this program; 00022 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see 00023 // <http://www.gnu.org/licenses/>. 00024 00025 /** @file experimental/numeric 00026 * This is a TS C++ Library header. 00027 */ 00028 00029 // 00030 // N4336 Working Draft, C++ Extensions for Library Fundamentals, Version 2 00031 // 00032 00033 #ifndef _GLIBCXX_EXPERIMENTAL_NUMERIC 00034 #define _GLIBCXX_EXPERIMENTAL_NUMERIC 1 00035 00036 #pragma GCC system_header 00037 00038 #if __cplusplus <= 201103L 00039 # include <bits/c++14_warning.h> 00040 #else 00041 00042 #include <numeric> 00043 #include <experimental/type_traits> 00044 00045 namespace std _GLIBCXX_VISIBILITY(default) 00046 { 00047 namespace experimental 00048 { 00049 inline namespace fundamentals_v2 00050 { 00051 _GLIBCXX_BEGIN_NAMESPACE_VERSION 00052 00053 #define __cpp_lib_experimental_gcd_lcm 201411 00054 00055 // std::abs is not constexpr and doesn't support unsigned integers. 00056 template<typename _Tp> 00057 constexpr 00058 enable_if_t<__and_<is_integral<_Tp>, is_signed<_Tp>>::value, _Tp> 00059 __abs(_Tp __val) 00060 { return __val < 0 ? -__val : __val; } 00061 00062 template<typename _Tp> 00063 constexpr 00064 enable_if_t<__and_<is_integral<_Tp>, is_unsigned<_Tp>>::value, _Tp> 00065 __abs(_Tp __val) 00066 { return __val; } 00067 00068 // Greatest common divisor 00069 template<typename _Mn, typename _Nn> 00070 constexpr common_type_t<_Mn, _Nn> 00071 gcd(_Mn __m, _Nn __n) 00072 { 00073 static_assert(is_integral<_Mn>::value, "arguments to gcd are integers"); 00074 static_assert(is_integral<_Nn>::value, "arguments to gcd are integers"); 00075 00076 return __m == 0 ? fundamentals_v2::__abs(__n) 00077 : __n == 0 ? fundamentals_v2::__abs(__m) 00078 : fundamentals_v2::gcd(__n, __m % __n); 00079 } 00080 00081 // Least common multiple 00082 template<typename _Mn, typename _Nn> 00083 constexpr common_type_t<_Mn, _Nn> 00084 lcm(_Mn __m, _Nn __n) 00085 { 00086 static_assert(is_integral<_Mn>::value, "arguments to lcm are integers"); 00087 static_assert(is_integral<_Nn>::value, "arguments to lcm are integers"); 00088 00089 return (__m != 0 && __n != 0) 00090 ? (fundamentals_v2::__abs(__m) / fundamentals_v2::gcd(__m, __n)) 00091 * fundamentals_v2::__abs(__n) 00092 : 0; 00093 } 00094 00095 _GLIBCXX_END_NAMESPACE_VERSION 00096 } // namespace fundamentals_v2 00097 } // namespace experimental 00098 } // namespace std 00099 00100 #endif // __cplusplus <= 201103L 00101 00102 #endif // _GLIBCXX_EXPERIMENTAL_NUMERIC