DGtal  0.6.devel
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
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)
34 
35 #define NumberTraits_RECURSES
36 
37 #if !defined NumberTraits_h
38 
39 #define NumberTraits_h
40 
42 // Inclusions
43 #include <iostream>
44 #include <limits>
45 #include <cfloat>
46 #include <boost/integer_traits.hpp>
47 #include <boost/call_traits.hpp>
48 #include "DGtal/base/Common.h"
49 
50 
52 
53 namespace DGtal
54 {
55  enum BoundEnum {BOUNDED = 0, UNBOUNDED = 1, BOUND_UNKNOWN = 2};
56  enum SignEnum {SIGNED = 0, UNSIGNED = 1, SIGN_UNKNOWN = 2};
57 
58 
60 // template class NumberTraits
70  template <typename T>
71  struct NumberTraits
72  {
73  // ----------------------- Associated types ------------------------------
78  typedef T SignedVersion;
79  typedef T UnsignedVersion;
80  typedef T ReturnType;
81 
82  //Defines a type that represents the "best" way to pass
83  // a parameter of type T to a function.
84  typedef typename boost::call_traits<T>::param_type ParamType;
85 
86 
91 #if ( defined(WIN32))
92  static const T ZERO;
93 #else
94  static const T ZERO = T(0);
95 #endif
96 
100 #if (defined (WIN32))
101  static const T ONE;
102 #else
103  static const T ONE = T(1);
104 #endif
105 
109  static ReturnType zero();
110 
114  static ReturnType one();
115 
120  static ReturnType min();
121 
126  static ReturnType max();
127 
132  static unsigned int digits();
133 
137  static BoundEnum isBounded();
141  static SignEnum isSigned();
142 
147  static DGtal::int64_t castToInt64_t(const T & aT)
148  {
149  return static_cast<DGtal::int64_t>(aT);
150  }
155  static double castToDouble(const T & aT)
156  {
157  return static_cast<double>(aT);
158  }
163  static bool even( ParamType aT )
164  {
165  return ( aT & ONE ) == ZERO;
166  }
171  static bool odd( ParamType aT )
172  {
173  return ( aT & ONE ) != ZERO;
174  }
175  }; // end of class NumberTraits
176 
177 
181  template <>
183  {
191  typedef boost::call_traits<uint16_t>::param_type ParamType;
192  static const uint16_t ZERO; // = 0;
193  static const uint16_t ONE; // = 1;
194  static ReturnType zero()
195  {
196  return 0;
197  }
198  static ReturnType one()
199  {
200  return 1;
201  }
202  static ReturnType min()
203  {
204  return boost::integer_traits<uint16_t>::const_min;
205  }
206  static ReturnType max()
207  {
208  return boost::integer_traits<uint16_t>::const_max;
209  }
210  static unsigned int digits()
211  {
212  return boost::integer_traits<uint16_t>::digits;
213  }
215  {
216  return BOUNDED;
217  }
219  {
220  return UNSIGNED;
221  }
223  {
224  return static_cast<DGtal::int64_t>(aT);
225  }
230  static double castToDouble(const uint16_t & aT)
231  {
232  return static_cast<double>(aT);
233  }
238  static bool even( ParamType aT )
239  {
240  return ( aT & 1 ) == 0;
241  }
246  static bool odd( ParamType aT )
247  {
248  return ( aT & 1 ) != 0;
249  }
250 
251  }; // end of class NumberTraits<uint16_t>.
252 
256  template <>
258  {
261  typedef TagTrue IsSigned;
266  typedef boost::call_traits<int16_t>::param_type ParamType;
267  static const int16_t ZERO; // = 0;
268  static const int16_t ONE; // = 1;
269  static ReturnType zero()
270  {
271  return 0;
272  }
273  static ReturnType one()
274  {
275  return 1;
276  }
277  static ReturnType min()
278  {
279  return boost::integer_traits<int16_t>::const_min;
280  }
281  static ReturnType max()
282  {
283  return boost::integer_traits<int16_t>::const_max;
284  }
285  static unsigned int digits()
286  {
287  return boost::integer_traits<int16_t>::digits;
288  }
290  {
291  return BOUNDED;
292  }
294  {
295  return SIGNED;
296  }
298  {
299  return static_cast<DGtal::int64_t>(aT);
300  }
305  static double castToDouble(const int16_t & aT)
306  {
307  return static_cast<double>(aT);
308  }
313  static bool even( ParamType aT )
314  {
315  return ( aT & 1 ) == 0;
316  }
321  static bool odd( ParamType aT )
322  {
323  return ( aT & 1 ) != 0;
324  }
325  }; // end of class NumberTraits<int16_t>.
326 
327 
331  template <>
333  {
341  typedef boost::call_traits<uint8_t>::param_type ParamType;
342  static const uint8_t ZERO; // = 0;
343  static const uint8_t ONE; // = 1;
344  static ReturnType zero()
345  {
346  return 0;
347  }
348  static ReturnType one()
349  {
350  return 1;
351  }
352  static ReturnType min()
353  {
354  return boost::integer_traits<uint8_t>::const_min;
355  }
356  static ReturnType max()
357  {
358  return boost::integer_traits<uint8_t>::const_max;
359  }
360  static unsigned int digits()
361  {
362  return boost::integer_traits<uint8_t>::digits;
363  }
365  {
366  return BOUNDED;
367  }
369  {
370  return UNSIGNED;
371  }
373  {
374  return static_cast<DGtal::int64_t>(aT);
375  }
380  static double castToDouble(const uint8_t & aT)
381  {
382  return static_cast<double>(aT);
383  }
388  static bool even( ParamType aT )
389  {
390  return ( aT & 1 ) == 0;
391  }
396  static bool odd( ParamType aT )
397  {
398  return ( aT & 1 ) != 0;
399  }
400 
401  }; // end of class NumberTraits<uint8_t>.
402 
406  template <>
408  {
411  typedef TagTrue IsSigned;
416  typedef boost::call_traits<int8_t>::param_type ParamType;
417  static const int8_t ZERO; // = 0;
418  static const int8_t ONE; // = 1;
419  static ReturnType zero()
420  {
421  return 0;
422  }
423  static ReturnType one()
424  {
425  return 1;
426  }
427  static ReturnType min()
428  {
429  return boost::integer_traits<int8_t>::const_min;
430  }
431  static ReturnType max()
432  {
433  return boost::integer_traits<int8_t>::const_max;
434  }
435  static unsigned int digits()
436  {
437  return boost::integer_traits<int8_t>::digits;
438  }
440  {
441  return BOUNDED;
442  }
444  {
445  return SIGNED;
446  }
448  {
449  return static_cast<DGtal::int64_t>(aT);
450  }
455  static double castToDouble(const int8_t & aT)
456  {
457  return static_cast<double>(aT);
458  }
463  static bool even( ParamType aT )
464  {
465  return ( aT & 1 ) == 0;
466  }
471  static bool odd( ParamType aT )
472  {
473  return ( aT & 1 ) != 0;
474  }
475  }; // end of class NumberTraits<int16_t>.
476 
480  template <>
482  {
490  typedef boost::call_traits<uint32_t>::param_type ParamType;
491  static const uint32_t ZERO; // = 0;
492  static const uint32_t ONE; // = 1;
493  static ReturnType zero()
494  {
495  return 0;
496  }
497  static ReturnType one()
498  {
499  return 1;
500  }
501  static ReturnType min()
502  {
503  return boost::integer_traits<uint32_t>::const_min;
504  }
505  static ReturnType max()
506  {
507  return boost::integer_traits<uint32_t>::const_max;
508  }
509  static unsigned int digits()
510  {
511  return boost::integer_traits<uint32_t>::digits;
512  }
514  {
515  return BOUNDED;
516  }
518  {
519  return UNSIGNED;
520  }
522  {
523  return static_cast<DGtal::int64_t>(aT);
524  }
525  static double castToDouble(const uint32_t & aT)
526  {
527  return static_cast<double>(aT);
528  }
533  static bool even( ParamType aT )
534  {
535  return ( aT & 1 ) == 0;
536  }
541  static bool odd( ParamType aT )
542  {
543  return ( aT & 1 ) != 0;
544  }
545  }; // end of class NumberTraits<uint32_t>.
546 
550  template <>
552  {
555  typedef TagTrue IsSigned;
560  typedef boost::call_traits<int32_t>::param_type ParamType;
561  static const int32_t ZERO;// = 0;
562  static const int32_t ONE;// = 1;
563  static ReturnType zero()
564  {
565  return 0;
566  }
567  static ReturnType one()
568  {
569  return 1;
570  }
571  static ReturnType min()
572  {
573  return boost::integer_traits<int32_t>::const_min;
574  }
575  static ReturnType max()
576  {
577  return boost::integer_traits<int32_t>::const_max;
578  }
579  static unsigned int digits()
580  {
581  return boost::integer_traits<int32_t>::digits;
582  }
584  {
585  return BOUNDED;
586  }
588  {
589  return SIGNED;
590  }
592  {
593  return static_cast<DGtal::int64_t>(aT);
594  }
599  static double castToDouble(const int32_t & aT)
600  {
601  return static_cast<double>(aT);
602  }
607  static bool even( ParamType aT )
608  {
609  return ( aT & 1 ) == 0;
610  }
615  static bool odd( ParamType aT )
616  {
617  return ( aT & 1 ) != 0;
618  }
619  }; // end of class NumberTraits<int32_t>.
620 
624  template <>
626  {
634  typedef boost::call_traits<uint64_t>::param_type ParamType;
635  static const uint64_t ZERO; // = 0;
636  static const uint64_t ONE; // = 1;
637  static ReturnType zero()
638  {
639  return 0;
640  }
641  static ReturnType one()
642  {
643  return 1;
644  }
645  static ReturnType min()
646  {
647  return boost::integer_traits<uint64_t>::const_min;
648  }
649  static ReturnType max()
650  {
651  return boost::integer_traits<uint64_t>::const_max;
652  }
653  static unsigned int digits()
654  {
655  return boost::integer_traits<uint64_t>::digits;
656  }
658  {
659  return BOUNDED;
660  }
662  {
663  return UNSIGNED;
664  }
666  {
667  return static_cast<DGtal::int64_t>(aT);
668  }
673  static double castToDouble(const uint64_t & aT)
674  {
675  return static_cast<double>(aT);
676  }
681  static bool even( ParamType aT )
682  {
683  return ( aT & 1 ) == 0;
684  }
689  static bool odd( ParamType aT )
690  {
691  return ( aT & 1 ) != 0;
692  }
693  }; // end of class NumberTraits<uint64_t>.
694 
698  template <>
700  {
703  typedef TagTrue IsSigned;
708  typedef boost::call_traits<int64_t>::param_type ParamType;
709  static const int64_t ZERO; // = 0;
710  static const int64_t ONE; // = 1;
711  static ReturnType zero()
712  {
713  return 0;
714  }
715  static ReturnType one()
716  {
717  return 1;
718  }
719  static ReturnType min()
720  {
721  return boost::integer_traits<int64_t>::const_min;
722  }
723  static ReturnType max()
724  {
725  return boost::integer_traits<int64_t>::const_max;
726  }
727  static unsigned int digits()
728  {
729  return boost::integer_traits<int64_t>::digits;
730  }
732  {
733  return BOUNDED;
734  }
736  {
737  return SIGNED;
738  }
740  {
741  return aT;
742  }
743  static double castToDouble(const int64_t & aT)
744  {
745  return static_cast<double>(aT);
746  }
751  static bool even( ParamType aT )
752  {
753  return ( aT & 1 ) == 0;
754  }
759  static bool odd( ParamType aT )
760  {
761  return ( aT & 1 ) != 0;
762  }
763  }; // end of class NumberTraits<int64_t>.
764 
768  template <>
769  struct NumberTraits<float>
770  {
773  typedef TagTrue IsSigned;
775  typedef float SignedVersion;
776  typedef float UnsignedVersion;
777  typedef float ReturnType;
778  typedef boost::call_traits<float>::param_type ParamType;
779  static const float ZERO;
780  static const float ONE;
781  static ReturnType zero()
782  {
783  return 0.0f;
784  }
785  static ReturnType one()
786  {
787  return 1.0f;
788  }
789  static ReturnType min()
790  {
791  return FLT_MIN;
792  }
793  static ReturnType max()
794  {
795  return FLT_MAX;
796  }
797  static unsigned int digits()
798  {
799  return FLT_DIG;
800  }
802  {
803  return BOUNDED;
804  }
806  {
807  return SIGNED;
808  }
809  static DGtal::int64_t castToInt64_t(const float & aT)
810  {
811  return static_cast<int64_t>( aT );
812  }
813  static double castToDouble(const float & aT)
814  {
815  return static_cast<double>(aT);
816  }
817  }; // end of class NumberTraits<float>.
818 
819 
823  template <>
824  struct NumberTraits<double>
825  {
828  typedef TagTrue IsSigned;
830  typedef double SignedVersion;
831  typedef double UnsignedVersion;
832  typedef double ReturnType;
833  typedef boost::call_traits<double>::param_type ParamType;
834  static const double ZERO;
835  static const double ONE;
836  static ReturnType zero()
837  {
838  return 0.0;
839  }
840  static ReturnType one()
841  {
842  return 1.0;
843  }
844  static ReturnType min()
845  {
846  return DBL_MIN;
847  }
848  static ReturnType max()
849  {
850  return DBL_MAX;
851  }
852  static unsigned int digits()
853  {
854  return DBL_DIG;
855  }
857  {
858  return BOUNDED;
859  }
861  {
862  return SIGNED;
863  }
864  static DGtal::int64_t castToInt64_t(const double & aT)
865  {
866  return static_cast<int64_t>( aT );
867  }
868  static double castToDouble(const double & aT)
869  {
870  return static_cast<double>(aT);
871  }
872  }; // end of class NumberTraits<double>.
873 
874 
878  template <>
879  struct NumberTraits<long double>
880  {
883  typedef TagTrue IsSigned;
885  typedef long double SignedVersion;
886  typedef long double UnsignedVersion;
887  typedef long double ReturnType;
888  typedef boost::call_traits<long double>::param_type ParamType;
889  static const long double ZERO;
890  static const long double ONE;
891  static ReturnType zero()
892  {
893  return 0.0;
894  }
895  static ReturnType one()
896  {
897  return 1.0;
898  }
899  static ReturnType min()
900  {
901  return LDBL_MIN;
902  }
903  static ReturnType max()
904  {
905  return LDBL_MAX;
906  }
907  static unsigned int digits()
908  {
909  return LDBL_DIG;
910  }
912  {
913  return BOUNDED;
914  }
916  {
917  return SIGNED;
918  }
919  static DGtal::int64_t castToInt64_t(const long double & aT)
920  {
921  return static_cast<int64_t>( aT );
922  }
923  static double castToDouble(const long double & aT)
924  {
925  return static_cast<double>(aT);
926  }
927  }; // end of class NumberTraits<long double>.
928 
929 
930 
931 #ifdef WITH_BIGINTEGER
932 
938  template <>
939  struct NumberTraits<DGtal::BigInteger>
940  {
941  typedef TagFalse IsBounded;
942  typedef TagTrue IsUnsigned;
943  typedef TagTrue IsSigned;
944  typedef TagTrue IsSpecialized;
945  typedef DGtal::BigInteger SignedVersion;
946  typedef DGtal::BigInteger UnsignedVersion;
947  typedef DGtal::BigInteger ReturnType;
948  typedef DGtal::BigInteger ParamType;
949  static const DGtal::BigInteger ZERO;
950  static const DGtal::BigInteger ONE;
951  static ReturnType zero()
952  {
953  return ZERO;
954  }
955  static ReturnType one()
956  {
957  return ONE;
958  }
959  static ReturnType min()
960  {
961  ASSERT2(false, "UnBounded interger type does not support min() function");
962  return ZERO;
963  }
964  static ReturnType max()
965  {
966  ASSERT2(false, "UnBounded interger type does not support max() function");
967  return ZERO;
968  }
969 
970  static unsigned int digits()
971  {
972  ASSERT2(false, "UnBounded interger type does not support digits() function");
973  return 0;
974  }
975  static BoundEnum isBounded()
976  {
977  return BOUNDED;
978  }
979  static SignEnum isSigned()
980  {
981  return SIGNED;
982  }
983  static DGtal::int64_t castToInt64_t(const DGtal::BigInteger & aT)
984  {
985  return aT.get_si();
986  }
987  static double castToDouble(const DGtal::BigInteger & aT)
988  {
989  return aT.get_d();
990  }
995  static bool even( ParamType aT )
996  {
997  return mpz_even_p( aT.get_mpz_t() );
998  }
1003  static bool odd( ParamType aT )
1004  {
1005  return mpz_odd_p( aT.get_mpz_t() );
1006  }
1007  }; // end of class NumberTraits<DGtal::BigInteger>.
1008 #endif
1009 
1010 
1012 
1013  template<class A, class B>
1015  {
1017  };
1018 
1019  template<>
1021  {
1023  };
1024 
1025 } // namespace DGtal
1026 
1027 
1029 // Includes inline functions.
1030 #include "DGtal/kernel/NumberTraits.ih"
1031 
1032 // //
1034 
1035 #endif // !defined NumberTraits_h
1036 
1037 #undef NumberTraits_RECURSES
1038 #endif // else defined(NumberTraits_RECURSES)