File failed to load: https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.3/config/TeX-MML-AM_CHTML/MathJax.js
DGtal 2.0.0
NumberTraits.h
1
16
17#pragma once
18
30
31#if defined(NumberTraits_RECURSES)
32#error Recursive header files inclusion detected in NumberTraits.h
33#else // defined(NumberTraits_RECURSES)
35#define NumberTraits_RECURSES
36
37#if !defined NumberTraits_h
39#define NumberTraits_h
40
42// Inclusions
43#include <type_traits>
44#include <limits>
45
46#include <boost/call_traits.hpp>
47
48#include "DGtal/base/Common.h"
50
51namespace DGtal
52{
55
57 enum SignEnum {SIGNED = 0, UNSIGNED = 1, SIGN_UNKNOWN = 2};
58
59
61// template class NumberTraits and NumberTraitsImpl
62
80 template <typename T, typename Enable = void>
82 {
83 // ----------------------- Associated types ------------------------------
89 typedef T SignedVersion;
90 typedef T UnsignedVersion;
91 typedef T ReturnType;
92
96 typedef typename boost::call_traits<T>::param_type ParamType;
97
98
100 static const T ZERO = T(0);
101
103 static const T ONE = T(1);
104
106 static ReturnType zero();
107
109 static ReturnType one();
110
115 static ReturnType min();
116
121 static ReturnType max();
122
127 static unsigned int digits();
128
134
140
145 static DGtal::int64_t castToInt64_t(const T & aT)
146 {
147 return static_cast<DGtal::int64_t>(aT);
148 }
149
154 static inline constexpr
155 DGtal::uint64_t castToUInt64_t(const T & aT) noexcept
156 {
157 return static_cast<DGtal::uint64_t>(aT);
158 }
159
164 static double castToDouble(const T & aT)
165 {
166 return static_cast<double>(aT);
167 }
168
173 static bool even( ParamType aT )
174 {
175 return ( aT & ONE ) == ZERO;
176 }
177
183 static bool odd( ParamType aT )
184 {
185 return ( aT & ONE ) != ZERO;
186 }
187
188 }; // end of class NumberTraitsImpl
189
190 // Definition of the static attributes in order to allow ODR-usage.
191 template <typename T, typename Enable> const T NumberTraitsImpl<T, Enable>::ZERO;
192 template <typename T, typename Enable> const T NumberTraitsImpl<T, Enable>::ONE;
193
194 // Some custom structs to factorize the code.
195 namespace details
196 {
197
199 template <bool Value>
201 {
203 };
204
205 template <>
206 struct BoolToTag<false>
207 {
209 };
210
212 template <typename T>
214 {
215 private:
216 using NL = std::numeric_limits<T>;
217
218 public:
219 // ----------------------- Associated types ------------------------------
225
226 using ReturnType = T;
227
231 using ParamType = typename boost::call_traits<T>::param_type;
232
234 static constexpr T ZERO = T(0);
235
237 static constexpr T ONE = T(1);
238
240 static inline constexpr
241 ReturnType zero() noexcept
242 {
243 return ZERO;
244 }
245
247 static inline constexpr
248 ReturnType one() noexcept
249 {
250 return ONE;
251 }
252
254 static inline constexpr
255 ReturnType min() noexcept
256 {
257 return NL::min();
258 }
259
261 static inline constexpr
262 ReturnType max() noexcept
263 {
264 return NL::max();
265 }
266
268 static inline constexpr
269 unsigned int digits() noexcept
270 {
271 return static_cast<unsigned int>(NL::digits);
272 }
273
278 static inline constexpr
280 {
281 return NL::is_bounded ? BOUNDED : UNBOUNDED;
282 }
283
288 static inline constexpr
290 {
291 return NL::is_signed ? SIGNED : UNSIGNED;
292 }
293
298 static inline constexpr
299 DGtal::int64_t castToInt64_t(const T & aT) noexcept
300 {
301 return static_cast<DGtal::int64_t>(aT);
302 }
303
308 static inline constexpr
309 DGtal::uint64_t castToUInt64_t(const T & aT) noexcept
310 {
311 return static_cast<DGtal::uint64_t>(aT);
312 }
313
318 static inline constexpr
319 double castToDouble(const T & aT) noexcept
320 {
321 return static_cast<double>(aT);
322 }
323
329 static inline constexpr
330 bool even( ParamType aT ) noexcept
331 {
332 return ( aT & ONE ) == ZERO;
333 }
334
340 static inline constexpr
341 bool odd( ParamType aT ) noexcept
342 {
343 return ( aT & ONE ) != ZERO;
344 }
345
346 };
347
348 // Definition of the static attributes in order to allow ODR-usage.
349 template <typename T> constexpr T NumberTraitsImplFundamental<T>::ZERO;
350 template <typename T> constexpr T NumberTraitsImplFundamental<T>::ONE;
351
352 } // namespace details
353
355 template <typename T>
356 struct NumberTraitsImpl<T, typename std::enable_if<std::is_integral<T>::value>::type>
358 {
359 private:
361
362 public:
363 using SignedVersion = typename std::make_signed<T>::type;
364 using UnsignedVersion = typename std::make_unsigned<T>::type;
365
371 static inline constexpr
372 bool even( typename NTIF::ParamType aT ) noexcept
373 {
374 return ( aT & NTIF::ONE ) == NTIF::ZERO;
375 }
376
382 static inline constexpr
383 bool odd( typename NTIF::ParamType aT ) noexcept
384 {
385 return ( aT & NTIF::ONE ) != NTIF::ZERO;
386 }
387
388 }; // end of class NumberTraitsImpl
389
391 template <typename T>
392 struct NumberTraitsImpl<T, typename std::enable_if<std::is_floating_point<T>::value>::type>
394 {
395 using SignedVersion = T;
396 using UnsignedVersion = T;
397 }; // end of class NumberTraitsImpl
398
405 template <typename Enable>
407 {
413
417
421 typedef typename boost::call_traits<BigInteger>::param_type ParamType;
422
424 static const DGtal::BigInteger ZERO ;
425
427 static const DGtal::BigInteger ONE ;
428
430 static inline
431 ReturnType zero() noexcept
432 {
433 return ZERO;
434 }
435
437 static inline
438 ReturnType one() noexcept
439 {
440 return ONE;
441 }
442
444 static inline
445 ReturnType min() noexcept
446 {
447 FATAL_ERROR_MSG(false, "UnBounded interger type does not support min() function");
448 return zero();
449 }
450
452 static inline
453 ReturnType max() noexcept
454 {
455 FATAL_ERROR_MSG(false, "UnBounded interger type does not support max() function");
456 return zero();
457 }
458
460 static inline
461 unsigned int digits() noexcept
462 {
463 FATAL_ERROR_MSG(false, "UnBounded interger type does not support digits() function");
464 return 0;
465 }
466
471 static inline
473 {
474 return UNBOUNDED;
475 }
476
481 static inline
483 {
484 return SIGNED;
485 }
486
491 static inline
493 {
494 return static_cast<DGtal::int64_t>(aT);
495 }
496
501 static inline
503 {
504 return static_cast<DGtal::uint64_t>(aT);
505 }
506
507
512 static inline
513 double castToDouble(const DGtal::BigInteger & aT) noexcept
514 {
515 return static_cast<double>(aT);
516 }
517
523 static inline
524 bool even( ParamType aT ) noexcept
525 {
526 return (boost::multiprecision::integer_modulus(aT, 2) == 0);
527 }
528
534 static inline
535 bool odd( ParamType aT ) noexcept
536 {
537 return (boost::multiprecision::integer_modulus(aT, 2) == 1);
538 }
539 }; // end of class NumberTraits<DGtal::BigInteger>.
540
541 // Definition of the static attributes in order to allow ODR-usage.
544
559 template <typename T>
561 : NumberTraitsImpl<typename std::decay<T>::type>
562 {
563 };
564
566
567 template<class A, class B>
572
573 template<>
575 {
577 };
578
579} // namespace DGtal
580
581#endif // !defined NumberTraits_h
582
583#undef NumberTraits_RECURSES
584#endif // else defined(NumberTraits_RECURSES)
DGtal is the top-level namespace which contains all DGtal functions and types.
std::int32_t int32_t
signed 32-bit integer.
Definition BasicTypes.h:71
std::uint64_t uint64_t
unsigned 64-bit integer.
Definition BasicTypes.h:64
std::int64_t int64_t
signed 94-bit integer.
Definition BasicTypes.h:73
BoundEnum
Bounding type of a number.
@ UNBOUNDED
@ BOUND_UNKNOWN
SignEnum
Sign type of a number.
@ SIGN_UNKNOWN
boost::multiprecision::number< boost::multiprecision::cpp_int_backend<>, boost::multiprecision::et_off > BigInteger
Definition BasicTypes.h:75
STL namespace.
static bool odd(ParamType aT) noexcept
Check the parity of a number.
boost::call_traits< BigInteger >::param_type ParamType
Defines a type that represents the "best" way to pass a parameter of type T to a function.
static ReturnType max() noexcept
Return the maximum possible value (trigger an error since BitInteger is unbounded).
TagFalse IsBounded
A BigInteger is not bounded.
static unsigned int digits() noexcept
Return the number of significant binary digits (trigger an error since BitInteger is unbounded).
TagTrue IsSpecialized
Is that a number type with specific traits.
static bool even(ParamType aT) noexcept
Check the parity of a number.
TagTrue IsUnsigned
A BigInteger can be signed and unsigned.
TagTrue IsSigned
a BigInteger can be signed and unsigned.
static ReturnType min() noexcept
Return the minimum possible value (trigger an error since BitInteger is unbounded).
static SignEnum isSigned() noexcept
Return the sign type of the number.
TagTrue IsIntegral
A BigInteger is of integral type.
DGtal::BigInteger UnsignedVersion
Alias to the unsigned version of a BigInteger (aka a BigInteger).
static ReturnType zero() noexcept
Return the zero of this integer.
DGtal::BigInteger ReturnType
Alias to the type that should be used as return type.
static DGtal::int64_t castToInt64_t(const DGtal::BigInteger &aT) noexcept
Cast method to DGtal::int64_t (for I/O or board export uses only).
static double castToDouble(const DGtal::BigInteger &aT) noexcept
Cast method to double (for I/O or board export uses only).
static const DGtal::BigInteger ONE
Constant One.
static BoundEnum isBounded() noexcept
Return the bounding type of the number.
static DGtal::uint64_t castToUInt64_t(const DGtal::BigInteger &aT) noexcept
Cast method to DGtal::uint64_t (for I/O or board export uses only).
static const DGtal::BigInteger ZERO
Constant Zero.
static ReturnType one() noexcept
Return the one of this integer.
DGtal::BigInteger SignedVersion
Alias to the signed version of a BigInteger (aka a BigInteger).
T UnsignedVersion
Alias to the unsigned version of a floating-point type (aka itself).
T SignedVersion
Alias to the signed version of a floating-point type (aka itself).
typename std::make_signed< T >::type SignedVersion
Alias to the signed version of the number type.
static constexpr bool even(typename NTIF::ParamType aT) noexcept
Check the parity of a number.
static constexpr bool odd(typename NTIF::ParamType aT) noexcept
Check the parity of a number.
typename details::NumberTraitsImplFundamental< T > NTIF
Internal type alias to avoid repetitions.
typename std::make_unsigned< T >::type UnsignedVersion
Alias to the unsigned version of the number type.
Aim: The traits class for all models of Cinteger (implementation)
T UnsignedVersion
Alias to the unsigned version of the number type.
static DGtal::int64_t castToInt64_t(const T &aT)
Cast method to DGtal::int64_t (for I/O or board export uses only).
static ReturnType min()
Return the minimum possible value for this type of integer or ONE if not bounded or unknown.
static const T ONE
Constant One.
TagUnknown IsBounded
Is the number bounded.
static ReturnType max()
Return the maximum possible value for this type of integer or ZERO if not bounded or unknown.
static bool even(ParamType aT)
Check the parity of a number.
static const T ZERO
Constant Zero.
static ReturnType one()
Return the one of this integer.
T SignedVersion
Alias to the signed version of the number type.
TagUnknown IsUnsigned
Is the number unsigned.
static BoundEnum isBounded()
Return the bounding type of the number.
TagUnknown IsSigned
Is the number signed.
static unsigned int digits()
Return the number of significant binary digits for this integer type, or 0 if unbounded or unknown.
static SignEnum isSigned()
Return the sign type of the number.
static ReturnType zero()
Return the zero of this integer.
boost::call_traits< T >::param_type ParamType
Defines a type that represents the "best" way to pass a parameter of type T to a function.
TagFalse IsSpecialized
Is that a number type with specific traits.
TagUnknown IsIntegral
Is the number of integral type.
static bool odd(ParamType aT)
Check the parity of a number.
static constexpr DGtal::uint64_t castToUInt64_t(const T &aT) noexcept
Cast method to DGtal::uint64_t (for I/O or board export uses only).
T ReturnType
Alias to the type that should be used as return type.
static double castToDouble(const T &aT)
Cast method to double (for I/O or board export uses only).
Aim: The traits class for all models of Cinteger.
Convert a boolean to the corresponding DGtal tag (TagTrue or TagFalse).
NumberTraits common part for fundamental integer and floating-point types.
static constexpr ReturnType zero() noexcept
Return the zero of this integer.
static constexpr DGtal::uint64_t castToUInt64_t(const T &aT) noexcept
Cast method to DGtal::uint64_t (for I/O or board export uses only).
typename BoolToTag< NL::is_signed >::type IsSigned
Is the number signed.
typename boost::call_traits< T >::param_type ParamType
Defines a type that represents the "best" way to pass a parameter of type T to a function.
static constexpr T ZERO
Constant Zero.
static constexpr T ONE
Constant One.
static constexpr ReturnType min() noexcept
Return the minimum possible value for this type of number.
TagTrue IsSpecialized
Is that a number type with specific traits.
static constexpr BoundEnum isBounded() noexcept
Return the bounding type of the number.
static constexpr double castToDouble(const T &aT) noexcept
Cast method to double (for I/O or board export uses only).
static constexpr ReturnType max() noexcept
Return the maximum possible value for this type of number.
static constexpr SignEnum isSigned() noexcept
Return the sign type of the number.
std::numeric_limits< T > NL
Type alias to std::numeric_limits.
typename BoolToTag<!NL::is_signed >::type IsUnsigned
Is the number unsigned.
static constexpr ReturnType one() noexcept
Return the one of this integer.
static constexpr unsigned int digits() noexcept
Return the number of significant binary digits for this type of number.
T ReturnType
Alias to the type that should be used as return type.
static constexpr bool even(ParamType aT) noexcept
Check the parity of a number.
static constexpr bool odd(ParamType aT) noexcept
Check the parity of a number.
static constexpr DGtal::int64_t castToInt64_t(const T &aT) noexcept
Cast method to DGtal::int64_t (for I/O or board export uses only).
typename BoolToTag< NL::is_integer >::type IsIntegral
Is the number of integral type.
typename BoolToTag< NL::is_bounded >::type IsBounded
Is the number bounded.
Warning_promote_trait_not_specialized_for_this_case promote_t