DGtal  1.4.beta
NumberTraits.h
1 
17 #pragma once
18 
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 
51 namespace DGtal
52 {
54  enum BoundEnum {BOUNDED = 0, UNBOUNDED = 1, BOUND_UNKNOWN = 2};
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 ------------------------------
86  typedef TagUnknown IsSigned;
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 
139  static SignEnum isSigned();
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  }
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>
200  struct BoolToTag
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
279  BoundEnum isBounded() noexcept
280  {
281  return NL::is_bounded ? BOUNDED : UNBOUNDED;
282  }
283 
288  static inline constexpr
289  SignEnum isSigned() noexcept
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 
399 #ifdef WITH_BIGINTEGER
406  template <typename Enable>
408  {
409  typedef TagTrue IsIntegral;
410  typedef TagFalse IsBounded;
411  typedef TagTrue IsUnsigned;
412  typedef TagTrue IsSigned;
414 
418 
422  typedef typename boost::call_traits<BigInteger>::param_type ParamType;
423 
425  static const DGtal::BigInteger ZERO;
426 
428  static const DGtal::BigInteger ONE;
429 
431  static inline
432  ReturnType zero() noexcept
433  {
434  return ZERO;
435  }
436 
438  static inline
439  ReturnType one() noexcept
440  {
441  return ONE;
442  }
443 
445  static inline
446  ReturnType min() noexcept
447  {
448  FATAL_ERROR_MSG(false, "UnBounded interger type does not support min() function");
449  return ZERO;
450  }
451 
453  static inline
454  ReturnType max() noexcept
455  {
456  FATAL_ERROR_MSG(false, "UnBounded interger type does not support max() function");
457  return ZERO;
458  }
459 
461  static inline
462  unsigned int digits() noexcept
463  {
464  FATAL_ERROR_MSG(false, "UnBounded interger type does not support digits() function");
465  return 0;
466  }
467 
472  static inline
473  BoundEnum isBounded() noexcept
474  {
475  return UNBOUNDED;
476  }
477 
482  static inline
483  SignEnum isSigned() noexcept
484  {
485  return SIGNED;
486  }
487 
492  static inline
494  {
495  return aT.get_si();
496  }
497 
502  static inline
504  {
505  return aT.get_ui();
506  }
507 
508 
513  static inline
514  double castToDouble(const DGtal::BigInteger & aT) noexcept
515  {
516  return aT.get_d();
517  }
518 
524  static inline
525  bool even( ParamType aT ) noexcept
526  {
527  return mpz_even_p( aT.get_mpz_t() );
528  }
529 
535  static inline
536  bool odd( ParamType aT ) noexcept
537  {
538  return mpz_odd_p( aT.get_mpz_t() );
539  }
540  }; // end of class NumberTraits<DGtal::BigInteger>.
541 
542  // Definition of the static attributes in order to allow ODR-usage.
543  template <typename Enable> const DGtal::BigInteger NumberTraitsImpl<DGtal::BigInteger, Enable>::ZERO = 0;
544  template <typename Enable> const DGtal::BigInteger NumberTraitsImpl<DGtal::BigInteger, Enable>::ONE = 1;
545 #endif
546 
561  template <typename T>
563  : NumberTraitsImpl<typename std::decay<T>::type>
564  {
565  };
566 
568 
569  template<class A, class B>
571  {
573  };
574 
575  template<>
577  {
579  };
580 
581 } // namespace DGtal
582 
583 #endif // !defined NumberTraits_h
584 
585 #undef NumberTraits_RECURSES
586 #endif // else defined(NumberTraits_RECURSES)
DGtal is the top-level namespace which contains all DGtal functions and types.
boost::int64_t int64_t
signed 94-bit integer.
Definition: BasicTypes.h:74
BoundEnum
Bounding type of a number.
Definition: NumberTraits.h:54
@ UNBOUNDED
Definition: NumberTraits.h:54
@ BOUNDED
Definition: NumberTraits.h:54
@ BOUND_UNKNOWN
Definition: NumberTraits.h:54
SignEnum
Sign type of a number.
Definition: NumberTraits.h:57
@ SIGN_UNKNOWN
Definition: NumberTraits.h:57
@ UNSIGNED
Definition: NumberTraits.h:57
@ SIGNED
Definition: NumberTraits.h:57
boost::uint64_t uint64_t
unsigned 64-bit integer.
Definition: BasicTypes.h:65
boost::int32_t int32_t
signed 32-bit integer.
Definition: BasicTypes.h:72
mpz_class BigInteger
Multi-precision integer with GMP implementation.
Definition: BasicTypes.h:79
static bool odd(ParamType aT) noexcept
Check the parity of a number.
Definition: NumberTraits.h:536
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.
Definition: NumberTraits.h:422
static ReturnType max() noexcept
Return the maximum possible value (trigger an error since BitInteger is unbounded).
Definition: NumberTraits.h:454
TagFalse IsBounded
A BigInteger is not bounded.
Definition: NumberTraits.h:410
static unsigned int digits() noexcept
Return the number of significant binary digits (trigger an error since BitInteger is unbounded).
Definition: NumberTraits.h:462
TagTrue IsSpecialized
Is that a number type with specific traits.
Definition: NumberTraits.h:413
static bool even(ParamType aT) noexcept
Check the parity of a number.
Definition: NumberTraits.h:525
TagTrue IsUnsigned
A BigInteger can be signed and unsigned.
Definition: NumberTraits.h:411
TagTrue IsSigned
a BigInteger can be signed and unsigned.
Definition: NumberTraits.h:412
static ReturnType min() noexcept
Return the minimum possible value (trigger an error since BitInteger is unbounded).
Definition: NumberTraits.h:446
static SignEnum isSigned() noexcept
Return the sign type of the number.
Definition: NumberTraits.h:483
TagTrue IsIntegral
A BigInteger is of integral type.
Definition: NumberTraits.h:409
DGtal::BigInteger UnsignedVersion
Alias to the unsigned version of a BigInteger (aka a BigInteger).
Definition: NumberTraits.h:416
static ReturnType zero() noexcept
Return the zero of this integer.
Definition: NumberTraits.h:432
DGtal::BigInteger ReturnType
Alias to the type that should be used as return type.
Definition: NumberTraits.h:417
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).
Definition: NumberTraits.h:493
static double castToDouble(const DGtal::BigInteger &aT) noexcept
Cast method to double (for I/O or board export uses only).
Definition: NumberTraits.h:514
static const DGtal::BigInteger ONE
Constant One.
Definition: NumberTraits.h:428
static BoundEnum isBounded() noexcept
Return the bounding type of the number.
Definition: NumberTraits.h:473
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).
Definition: NumberTraits.h:503
static const DGtal::BigInteger ZERO
Constant Zero.
Definition: NumberTraits.h:425
static ReturnType one() noexcept
Return the one of this integer.
Definition: NumberTraits.h:439
DGtal::BigInteger SignedVersion
Alias to the signed version of a BigInteger (aka a BigInteger).
Definition: NumberTraits.h:415
T UnsignedVersion
Alias to the unsigned version of a floating-point type (aka itself).
Definition: NumberTraits.h:396
T SignedVersion
Alias to the signed version of a floating-point type (aka itself).
Definition: NumberTraits.h:395
typename std::make_signed< T >::type SignedVersion
Alias to the signed version of the number type.
Definition: NumberTraits.h:363
static constexpr bool even(typename NTIF::ParamType aT) noexcept
Check the parity of a number.
Definition: NumberTraits.h:372
static constexpr bool odd(typename NTIF::ParamType aT) noexcept
Check the parity of a number.
Definition: NumberTraits.h:383
typename details::NumberTraitsImplFundamental< T > NTIF
Internal type alias to avoid repetitions.
Definition: NumberTraits.h:360
typename std::make_unsigned< T >::type UnsignedVersion
Alias to the unsigned version of the number type.
Definition: NumberTraits.h:364
Aim: The traits class for all models of Cinteger (implementation)
Definition: NumberTraits.h:82
T UnsignedVersion
Alias to the unsigned version of the number type.
Definition: NumberTraits.h:90
static DGtal::int64_t castToInt64_t(const T &aT)
Cast method to DGtal::int64_t (for I/O or board export uses only).
Definition: NumberTraits.h:145
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.
Definition: NumberTraits.h:103
TagUnknown IsBounded
Is the number bounded.
Definition: NumberTraits.h:84
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.
Definition: NumberTraits.h:173
static const T ZERO
Constant Zero.
Definition: NumberTraits.h:100
static ReturnType one()
Return the one of this integer.
T SignedVersion
Alias to the signed version of the number type.
Definition: NumberTraits.h:89
TagUnknown IsUnsigned
Is the number unsigned.
Definition: NumberTraits.h:85
static BoundEnum isBounded()
Return the bounding type of the number.
TagUnknown IsSigned
Is the number signed.
Definition: NumberTraits.h:86
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.
Definition: NumberTraits.h:96
TagFalse IsSpecialized
Is that a number type with specific traits.
Definition: NumberTraits.h:88
TagUnknown IsIntegral
Is the number of integral type.
Definition: NumberTraits.h:87
static bool odd(ParamType aT)
Check the parity of a number.
Definition: NumberTraits.h:183
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).
Definition: NumberTraits.h:155
T ReturnType
Alias to the type that should be used as return type.
Definition: NumberTraits.h:91
static double castToDouble(const T &aT)
Cast method to double (for I/O or board export uses only).
Definition: NumberTraits.h:164
Aim: The traits class for all models of Cinteger.
Definition: NumberTraits.h:564
Convert a boolean to the corresponding DGtal tag (TagTrue or TagFalse).
Definition: NumberTraits.h:201
NumberTraits common part for fundamental integer and floating-point types.
Definition: NumberTraits.h:214
static constexpr ReturnType zero() noexcept
Return the zero of this integer.
Definition: NumberTraits.h:241
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).
Definition: NumberTraits.h:309
typename BoolToTag< NL::is_signed >::type IsSigned
Is the number signed.
Definition: NumberTraits.h:222
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.
Definition: NumberTraits.h:231
static constexpr T ZERO
Constant Zero.
Definition: NumberTraits.h:234
static constexpr T ONE
Constant One.
Definition: NumberTraits.h:237
static constexpr ReturnType min() noexcept
Return the minimum possible value for this type of number.
Definition: NumberTraits.h:255
static constexpr BoundEnum isBounded() noexcept
Return the bounding type of the number.
Definition: NumberTraits.h:279
static constexpr double castToDouble(const T &aT) noexcept
Cast method to double (for I/O or board export uses only).
Definition: NumberTraits.h:319
static constexpr ReturnType max() noexcept
Return the maximum possible value for this type of number.
Definition: NumberTraits.h:262
static constexpr SignEnum isSigned() noexcept
Return the sign type of the number.
Definition: NumberTraits.h:289
std::numeric_limits< T > NL
Type alias to std::numeric_limits.
Definition: NumberTraits.h:216
typename BoolToTag<!NL::is_signed >::type IsUnsigned
Is the number unsigned.
Definition: NumberTraits.h:221
static constexpr ReturnType one() noexcept
Return the one of this integer.
Definition: NumberTraits.h:248
static constexpr unsigned int digits() noexcept
Return the number of significant binary digits for this type of number.
Definition: NumberTraits.h:269
T ReturnType
Alias to the type that should be used as return type.
Definition: NumberTraits.h:226
static constexpr bool even(ParamType aT) noexcept
Check the parity of a number.
Definition: NumberTraits.h:330
static constexpr bool odd(ParamType aT) noexcept
Check the parity of a number.
Definition: NumberTraits.h:341
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).
Definition: NumberTraits.h:299
typename BoolToTag< NL::is_integer >::type IsIntegral
Is the number of integral type.
Definition: NumberTraits.h:223
typename BoolToTag< NL::is_bounded >::type IsBounded
Is the number bounded.
Definition: NumberTraits.h:220
Warning_promote_trait_not_specialized_for_this_case promote_t
Definition: NumberTraits.h:572
int max(int a, int b)