DGtal 1.4.0
Loading...
Searching...
No Matches
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
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 }
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
399#ifdef WITH_BIGINTEGER
406 template <typename Enable>
408 {
414
418
422 typedef typename boost::call_traits<BigInteger>::param_type ParamType;
423
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
474 {
475 return UNBOUNDED;
476 }
477
482 static inline
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>
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.
@ UNBOUNDED
@ BOUND_UNKNOWN
SignEnum
Sign type of a number.
@ SIGN_UNKNOWN
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
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.
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