DGtal  1.2.0
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)
35 #define IntegerConverter_RECURSES
36
37 #if !defined IntegerConverter_h
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
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
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
320  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)
std::ostream & warning()
static void mpz_set_sll(mpz_t n, long long sll)
----------— GMP SPECIALIZED SERVICES -------------------------—
static long long mpz_get_sll(mpz_t n)
static unsigned long long mpz_get_ull(mpz_t n)
static void mpz_set_ull(mpz_t n, unsigned long long ull)
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
DGtal::uint32_t Dimension
Definition: Common.h:137
Trace trace
Definition: Common.h:154
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 PointVector< dim, DGtal::BigInteger > cast(PointVector< dim, DGtal::BigInteger > p)
static DGtal::BigInteger cast(DGtal::int32_t i)
static PointVector< dim, DGtal::BigInteger > cast(PointVector< dim, DGtal::int32_t > p)
static PointVector< dim, DGtal::BigInteger > cast(PointVector< dim, DGtal::int64_t > p)
static DGtal::BigInteger cast(DGtal::BigInteger i)
static DGtal::BigInteger cast(DGtal::int64_t i)
static DGtal::int32_t cast(DGtal::int32_t i)
static PointVector< dim, DGtal::int32_t > cast(PointVector< dim, DGtal::int64_t > p)
static PointVector< dim, DGtal::int32_t > cast(PointVector< dim, DGtal::BigInteger > p)
static DGtal::int32_t cast(DGtal::BigInteger i)
static PointVector< dim, DGtal::int32_t > cast(PointVector< dim, DGtal::int32_t > p)
static DGtal::int32_t cast(DGtal::int64_t i)
static DGtal::int64_t cast(DGtal::int32_t i)
static PointVector< dim, DGtal::int64_t > cast(PointVector< dim, DGtal::int64_t > p)
static DGtal::int64_t cast(DGtal::int64_t i)
static PointVector< dim, DGtal::int64_t > cast(PointVector< dim, DGtal::BigInteger > p)
static PointVector< dim, DGtal::int64_t > cast(PointVector< dim, DGtal::int32_t > p)
static DGtal::int64_t cast(DGtal::BigInteger i)
----------— INTEGER/POINT CONVERSION SERVICES -----------------—
BOOST_CONCEPT_ASSERT((concepts::CInteger< TInteger >))
static PointVector< dim, Integer > cast(PointVector< dim, Integer > p)
static Integer cast(Integer i)
Aim: Concept checking for Integer Numbers. More precisely, this concept is a refinement of both CEucl...
Definition: CInteger.h:88
unsigned int dim(const Vector &z)