DGtal  1.3.beta
IntegerConverter.h
1 
17 #pragma once
18 
31 #if defined(IntegerConverter_RECURSES)
32 #error Recursive header files inclusion detected in IntegerConverter.h
33 #else // defined(IntegerConverter_RECURSES)
34 
35 #define IntegerConverter_RECURSES
36 
37 #if !defined IntegerConverter_h
38 
39 #define IntegerConverter_h
40 
42 // Inclusions
43 #include <iostream>
44 #include <string>
45 #include <vector>
46 #include <array>
47 #include "DGtal/base/Common.h"
48 #include "DGtal/kernel/CInteger.h"
49 #include "DGtal/kernel/NumberTraits.h"
50 #include "DGtal/kernel/PointVector.h"
51 
52 namespace DGtal
53 {
54 #ifdef WITH_BIGINTEGER
55  namespace detail {
57 
60  static void mpz_set_sll(mpz_t n, long long sll)
61  {
62  mpz_set_si(n, (int)(sll >> 32)); /* n = (int)sll >> 32 */
63  mpz_mul_2exp(n, n, 32 ); /* n <<= 32 */
64  mpz_add_ui(n, n, (unsigned int)sll); /* n += (unsigned int)sll */
65  }
66 
69  static void mpz_set_ull(mpz_t n, unsigned long long ull)
70  {
71  mpz_set_ui(n, (unsigned int)(ull >> 32)); /* n = (unsigned int)(ull >> 32) */
72  mpz_mul_2exp(n, n, 32); /* n <<= 32 */
73  mpz_add_ui(n, n, (unsigned int)ull); /* n += (unsigned int)ull */
74  }
75 
79  static unsigned long long mpz_get_ull(mpz_t n)
80  {
81  unsigned int lo, hi;
82  mpz_t tmp;
83  mpz_init( tmp );
84  mpz_mod_2exp( tmp, n, 64 ); /* tmp = (lower 64 bits of n) */
85  lo = mpz_get_ui( tmp ); /* lo = tmp & 0xffffffff */
86  mpz_div_2exp( tmp, tmp, 32 ); /* tmp >>= 32 */
87  hi = mpz_get_ui( tmp ); /* hi = tmp & 0xffffffff */
88  mpz_clear( tmp );
89  return (((unsigned long long)hi) << 32) + lo;
90  }
91 
95  static long long mpz_get_sll(mpz_t n)
96  {
97  return (long long)mpz_get_ull(n); /* just use unsigned version */
98  }
99  }
100 #endif
101 
103 
115  template < DGtal::Dimension dim,
116  typename TInteger >
119  typedef TInteger Integer;
120 
123  static Integer cast( Integer i )
124  {
125  return i;
126  }
127 
132  static
135  {
136  return p;
137  }
138  };
139 
149  template < DGtal::Dimension dim >
152 
156  {
157  return i;
158  }
159 
164  static
167  {
168  return p;
169  }
170 
174  {
176  if ( DGtal::int64_t( r ) != i )
177  trace.warning() << "Bad integer conversion: " << i << " -> " << r
178  << std::endl;
179  return r;
180  }
181 
186  static
189  {
191  for ( DGtal::Dimension i = 0; i < dim; i++ )
192  q[ i ] = cast( p[ i ] );
193  return q;
194  }
195 
196 #ifdef WITH_BIGINTEGER
200  {
201  DGtal::int32_t r = i.get_si();
202  if ( DGtal::BigInteger( r ) != i )
203  trace.warning() << "Bad integer conversion: " << i << " -> " << r
204  << std::endl;
205  return r;
206  }
207 
212  static
213  PointVector< dim, DGtal::int32_t >
215  {
217  for ( DGtal::Dimension i = 0; i < dim; i++ )
218  q[ i ] = cast( p[ i ] );
219  return q;
220  }
221 
222 #endif
223  };
224 
225 
235  template < DGtal::Dimension dim >
238 
242  {
243  return i;
244  }
245 
250  static
253  {
255  for ( DGtal::Dimension i = 0; i < dim; i++ )
256  q[ i ] = cast( p[ i ] );
257  return q;
258  }
259 
263  {
264  return i;
265  }
266 
271  static
274  {
275  return p;
276  }
277 
278 #ifdef WITH_BIGINTEGER
282  {
283  DGtal::int64_t r = detail::mpz_get_sll( i.get_mpz_t() );
284  DGtal::BigInteger tmp;
285  detail::mpz_set_sll( tmp.get_mpz_t(), r );
286  if ( tmp != i )
287  trace.warning() << "Bad integer conversion: " << i << " -> " << r
288  << std::endl;
289  return r;
290  }
291 
296  static
297  PointVector< dim, DGtal::int64_t >
299  {
301  for ( DGtal::Dimension i = 0; i < dim; i++ )
302  q[ i ] = cast( p[ i ] );
303  return q;
304  }
305 
306 #endif
307  };
308 
309 
310 #ifdef WITH_BIGINTEGER
311  template < DGtal::Dimension dim >
323 
327  {
328  return DGtal::BigInteger( i );
329  }
330 
335  static
338  {
340  for ( DGtal::Dimension i = 0; i < dim; i++ )
341  q[ i ] = cast( p[ i ] );
342  return q;
343  }
344 
348  {
349  DGtal::BigInteger tmp;
350  detail::mpz_set_sll( tmp.get_mpz_t(), i );
351  return tmp;
352  }
353 
358  static
361  {
363  for ( DGtal::Dimension i = 0; i < dim; i++ )
364  q[ i ] = cast( p[ i ] );
365  return q;
366  }
367 
371  {
372  return i;
373  }
374 
379  static
382  {
383  return p;
384  }
385 
386  };
387 #endif
388 
389 
390 } // namespace DGtal {
391 
392 #endif // !defined IntegerConverter_h
393 
394 #undef IntegerConverter_RECURSES
395 #endif // else defined(IntegerConverter_RECURSES)
DGtal::BigInteger
mpz_class BigInteger
Multi-precision integer with GMP implementation.
Definition: BasicTypes.h:79
DGtal::IntegerConverter< dim, DGtal::BigInteger >::cast
static DGtal::BigInteger cast(DGtal::int64_t i)
Definition: IntegerConverter.h:347
DGtal::IntegerConverter< dim, DGtal::BigInteger >::cast
static PointVector< dim, DGtal::BigInteger > cast(PointVector< dim, DGtal::int32_t > p)
Definition: IntegerConverter.h:337
DGtal::IntegerConverter< dim, DGtal::int64_t >::cast
static PointVector< dim, DGtal::int64_t > cast(PointVector< dim, DGtal::BigInteger > p)
Definition: IntegerConverter.h:298
DGtal::concepts::CInteger
Aim: Concept checking for Integer Numbers. More precisely, this concept is a refinement of both CEucl...
Definition: CInteger.h:87
DGtal::IntegerConverter< dim, DGtal::BigInteger >::Integer
DGtal::BigInteger Integer
Definition: IntegerConverter.h:322
DGtal::IntegerConverter< dim, DGtal::int64_t >::cast
static PointVector< dim, DGtal::int64_t > cast(PointVector< dim, DGtal::int64_t > p)
Definition: IntegerConverter.h:273
DGtal::int32_t
boost::int32_t int32_t
signed 32-bit integer.
Definition: BasicTypes.h:72
DGtal::trace
Trace trace
Definition: Common.h:154
DGtal::Dimension
DGtal::uint32_t Dimension
Definition: Common.h:137
DGtal::detail::mpz_get_sll
static long long mpz_get_sll(mpz_t n)
Definition: IntegerConverter.h:95
dim
unsigned int dim(const Vector &z)
Definition: viewDualSurface.cpp:174
DGtal::IntegerConverter< dim, DGtal::int64_t >::cast
static DGtal::int64_t cast(DGtal::int32_t i)
Definition: IntegerConverter.h:241
DGtal::IntegerConverter< dim, DGtal::int32_t >::cast
static DGtal::int32_t cast(DGtal::int64_t i)
Definition: IntegerConverter.h:173
DGtal::IntegerConverter< dim, DGtal::int64_t >::Integer
DGtal::int64_t Integer
Definition: IntegerConverter.h:237
DGtal::detail::mpz_set_sll
static void mpz_set_sll(mpz_t n, long long sll)
----------— GMP SPECIALIZED SERVICES -------------------------—
Definition: IntegerConverter.h:60
DGtal::IntegerConverter< dim, DGtal::int32_t >::Integer
DGtal::int32_t Integer
Definition: IntegerConverter.h:151
DGtal::IntegerConverter< dim, DGtal::BigInteger >::cast
static PointVector< dim, DGtal::BigInteger > cast(PointVector< dim, DGtal::int64_t > p)
Definition: IntegerConverter.h:360
DGtal::IntegerConverter< dim, DGtal::int32_t >::cast
static PointVector< dim, DGtal::int32_t > cast(PointVector< dim, DGtal::int32_t > p)
Definition: IntegerConverter.h:166
DGtal
DGtal is the top-level namespace which contains all DGtal functions and types.
DGtal::IntegerConverter< dim, DGtal::BigInteger >::cast
static DGtal::BigInteger cast(DGtal::BigInteger i)
Definition: IntegerConverter.h:370
DGtal::IntegerConverter::cast
static Integer cast(Integer i)
Definition: IntegerConverter.h:123
DGtal::IntegerConverter::cast
static PointVector< dim, Integer > cast(PointVector< dim, Integer > p)
Definition: IntegerConverter.h:134
DGtal::IntegerConverter< dim, DGtal::int32_t >::cast
static PointVector< dim, DGtal::int32_t > cast(PointVector< dim, DGtal::BigInteger > p)
Definition: IntegerConverter.h:214
DGtal::detail::mpz_set_ull
static void mpz_set_ull(mpz_t n, unsigned long long ull)
Definition: IntegerConverter.h:69
DGtal::IntegerConverter< dim, DGtal::int64_t >::cast
static DGtal::int64_t cast(DGtal::int64_t i)
Definition: IntegerConverter.h:262
DGtal::IntegerConverter< dim, DGtal::int64_t >::cast
static PointVector< dim, DGtal::int64_t > cast(PointVector< dim, DGtal::int32_t > p)
Definition: IntegerConverter.h:252
DGtal::IntegerConverter< dim, DGtal::BigInteger >::cast
static DGtal::BigInteger cast(DGtal::int32_t i)
Definition: IntegerConverter.h:326
DGtal::PointVector< dim, Integer >
DGtal::IntegerConverter< dim, DGtal::int32_t >::cast
static DGtal::int32_t cast(DGtal::int32_t i)
Definition: IntegerConverter.h:155
DGtal::IntegerConverter::BOOST_CONCEPT_ASSERT
BOOST_CONCEPT_ASSERT((concepts::CInteger< TInteger >))
DGtal::IntegerConverter
----------— INTEGER/POINT CONVERSION SERVICES -----------------—
Definition: IntegerConverter.h:117
DGtal::int64_t
boost::int64_t int64_t
signed 94-bit integer.
Definition: BasicTypes.h:74
DGtal::detail::mpz_get_ull
static unsigned long long mpz_get_ull(mpz_t n)
Definition: IntegerConverter.h:79
DGtal::IntegerConverter< dim, DGtal::BigInteger >::cast
static PointVector< dim, DGtal::BigInteger > cast(PointVector< dim, DGtal::BigInteger > p)
Definition: IntegerConverter.h:381
DGtal::Trace::warning
std::ostream & warning()
DGtal::IntegerConverter< dim, DGtal::int32_t >::cast
static PointVector< dim, DGtal::int32_t > cast(PointVector< dim, DGtal::int64_t > p)
Definition: IntegerConverter.h:188
DGtal::IntegerConverter::Integer
TInteger Integer
Definition: IntegerConverter.h:119