-
Notifications
You must be signed in to change notification settings - Fork 2
/
Integer.h
804 lines (664 loc) · 20.6 KB
/
Integer.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
/* Copyright (c) 1997-2007
Ewgenij Gawrilow, Michael Joswig (Technische Universitaet Berlin, Germany)
http://www.math.tu-berlin.de/polymake, mailto:[email protected]
This program is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the
Free Software Foundation; either version 2, or (at your option) any
later version: http://www.gnu.org/licenses/gpl.txt.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
*/
#ifndef _POLYMAKE_GMP_INTEGER_H
#define _POLYMAKE_GMP_INTEGER_H "$Project: polymake $$Id: Integer.h 7556 2007-01-12 17:36:36Z gawrilow $"
#include <iostream>
#include <string>
#include <stdexcept>
#include <cstdlib>
#include <cctype>
#include <limits>
#include <gmp.h>
class Integer; class Rational;
/** Exception type
A constructor of Integer or Rational from const char* throws an exception
of this type in case of a syntax error.
*/
class gmp_error : public std::domain_error {
public:
gmp_error(const std::string& what_arg) : std::domain_error(what_arg) { }
};
class temp_Integer : public MP_INT {
protected:
/// never instantiate this class: it is a pure masquerade
temp_Integer();
~temp_Integer();
};
// forward declarations of some friends
namespace std {
Integer abs(const Integer&);
Rational abs(const Rational&);
}
/** Arbitrary precision integer number.
It is a wrapper around GMP (GNU Multiple Precision Library) type \\mpz_t\\.
Developed and tested with GMP versions 3.1.1 and 4.0
See the GMP Home Pages at `http://www.swox.com/gmp/'.
@index main
*/
class Integer {
private:
/// GMP's representation
mpz_t rep;
public:
#if defined(__GMP_PLUSPLUS__)
// convert to the GMP's own C++ wrapper
mpz_class gmp() const
{
return mpz_class(rep);
}
// construct an Integer from the GMP's own C++ wrapper
Integer(const mpz_class& i)
{
mpz_init_set(rep,i.get_mpz_t());
}
#endif
/// Initialize to 0.
Integer() { mpz_init(rep); }
Integer(const Integer& i)
{
mpz_init_set(rep, i.rep);
}
Integer(long i)
{
mpz_init_set_si(rep, i);
}
Integer(int i)
{
mpz_init_set_si(rep, i);
}
Integer(double d)
{
mpz_init_set_d(rep, d);
}
/// Recognizes automatically number base 10, 8, or 16.
explicit Integer(const char* s)
{
mpz_init(rep);
try {
set(s);
}
catch (const gmp_error&) {
mpz_clear(rep);
throw;
}
}
explicit Integer(mpz_srcptr src)
{
mpz_init_set(rep, src);
}
/// take over the GMP structure without copying
explicit Integer(temp_Integer& tmp)
{
rep[0]=tmp;
}
/// Performs division with rounding via truncation.
inline Integer(const Rational& r);
/// Performs division with rounding via truncation.
inline Integer& operator= (const Rational& r);
template <class Arg>
Integer(void (*f)(mpz_ptr,Arg), Arg a)
{
mpz_init(rep);
f(rep,a);
}
template <class Arg1, class Arg2>
Integer(void (*f)(mpz_ptr,Arg1,Arg2), Arg1 a, Arg2 b)
{
mpz_init(rep);
f(rep,a,b);
}
template <class Arg1, class Arg2>
Integer(Arg2 (*f)(mpz_ptr,Arg1,Arg2), Arg1 a, Arg2 b)
{
mpz_init(rep);
f(rep,a,b);
}
static
mpz_srcptr _tmp_negate(mpz_ptr temp, mpz_srcptr src)
{
*temp=*src; mpz_neg(temp,temp); return temp;
}
~Integer() { mpz_clear(rep); }
Integer& operator= (const Integer& b)
{
mpz_set(rep, b.rep);
return *this;
}
Integer& operator= (long b)
{
mpz_set_si(rep, b);
return *this;
}
Integer& operator= (int b) { return operator=(long(b)); }
Integer& operator= (double d)
{
mpz_set_d(rep, d);
return *this;
}
/// Recognizes automatically number base 10, 8, or 16.
Integer& set(const char *s)
{
if (mpz_set_str(rep, s, 0) < 0)
throw gmp_error("Integer: syntax error in string");
return *this;
}
/// for the seldom case of unwrapped GMP objects coexisting with us
Integer& set(mpz_srcptr src)
{
mpz_set(rep, src);
return *this;
}
operator double() const { return mpz_get_d(rep); }
operator long() const
{
if (!mpz_fits_slong_p(rep))
throw gmp_error("Integer: value too big");
return mpz_get_si(rep);
}
operator int() const
{
if (!mpz_fits_sint_p(rep))
throw gmp_error("Integer: value too big");
return mpz_get_si(rep);
}
/// Converts integer to string.
std::string to_string(int base=10) const;
void swap(Integer& b) { mpz_swap(rep, b.rep); }
/** Accelerated combination of copy constructor and destructor.
Aimed to be used in container classes only! */
friend void relocate(Integer* from, Integer* to)
{
to->rep[0] = from->rep[0];
}
Integer& operator++()
{
mpz_add_ui(rep, rep, 1);
return *this;
}
Integer& operator--()
{
mpz_sub_ui(rep, rep, 1);
return *this;
}
/// In-place negation.
Integer& negate()
{
mpz_neg(rep, rep);
return *this;
}
Integer& operator+= (const Integer& b)
{
mpz_add(rep, rep, b.rep);
return *this;
}
Integer& operator+= (long b)
{
if (b>=0) mpz_add_ui(rep, rep, b);
else mpz_sub_ui(rep, rep, -b);
return *this;
}
Integer& operator-= (const Integer& b)
{
mpz_sub(rep, rep, b.rep);
return *this;
}
Integer& operator-= (long b)
{
if (b>=0) mpz_sub_ui(rep, rep, b);
else mpz_add_ui(rep, rep, -b);
return *this;
}
Integer& operator*= (const Integer& b)
{
mpz_mul(rep, rep, b.rep);
return *this;
}
Integer& operator*= (long b)
{
mpz_mul_si(rep, rep, b);
return *this;
}
/// @group Division with rounding via truncation.
Integer& operator/= (const Integer& b)
{
mpz_tdiv_q(rep, rep, b.rep);
return *this;
}
Integer& operator/= (long b)
{
if (b>=0) { // the case b==0 is handled within GMP and converted to ZERO DIV exception
mpz_tdiv_q_ui(rep, rep, b);
} else {
mpz_tdiv_q_ui(rep, rep, -b);
negate();
}
return *this;
}
Integer& operator%= (const Integer& b)
{
mpz_tdiv_r(rep, rep, b.rep);
return *this;
}
/// @endgroup
Integer& operator%= (long b)
{
mpz_tdiv_r_ui(rep, rep, b>=0 ? b : -b);
return *this;
}
/// Multiplies with 2<sup>k</sup>.
Integer& operator<<= (unsigned long k)
{
mpz_mul_2exp(rep, rep, k);
return *this;
}
/// Divides thru 2<sup>k</sup>, rounds via truncation.
Integer& operator>>= (unsigned long k)
{
mpz_tdiv_q_2exp(rep, rep, k);
return *this;
}
friend Integer operator+ (const Integer& a, const Integer& b)
{
return Integer(mpz_add, a.rep, b.rep);
}
friend Integer operator+ (const Integer& a, long b)
{
return Integer(b>=0 ? mpz_add_ui : mpz_sub_ui, a.rep, (unsigned long)(b>=0 ? b : -b));
}
friend Integer operator- (const Integer& a, const Integer& b)
{
return Integer(mpz_sub, a.rep, b.rep);
}
friend Integer operator- (const Integer& a, long b)
{
return Integer(b>=0 ? mpz_sub_ui : mpz_add_ui, a.rep, (unsigned long)(b>=0 ? b : -b));
}
friend Integer operator- (long a, const Integer& b)
{
mpz_t minus_b;
return Integer(a>=0 ? mpz_add_ui : mpz_sub_ui,
_tmp_negate(minus_b,b.rep), (unsigned long)(a>=0 ? a : -a));
}
friend Integer operator- (const Integer& a)
{
return Integer(mpz_neg, a.rep);
}
friend Integer operator* (const Integer& a, const Integer& b)
{
return Integer(mpz_mul, a.rep, b.rep);
}
friend Integer operator* (const Integer& a, long b)
{
return Integer(mpz_mul_si, a.rep, b);
}
/// @group Division with rounding via truncation.
friend Integer operator/ (const Integer& a, const Integer& b)
{
return Integer(mpz_tdiv_q, a.rep, b.rep);
}
friend Integer operator/ (const Integer& a, long b)
{
if (b>=0)
return Integer(mpz_tdiv_q_ui, a.rep, (unsigned long)b);
mpz_t minus_a;
return Integer(mpz_tdiv_q_ui, _tmp_negate(minus_a,a.rep), (unsigned long)(-b));
}
friend int operator/ (int a, const Integer& b)
{
return mpz_fits_sint_p(b.rep) ? a/mpz_get_si(b.rep) : 0;
}
friend long operator/ (long a, const Integer& b)
{
return mpz_fits_slong_p(b.rep) ? a/mpz_get_si(b.rep) : 0;
}
friend Integer operator% (const Integer& a, const Integer& b)
{
return Integer(mpz_tdiv_r, a.rep, b.rep);
}
friend long operator% (const Integer& a, long b)
{
long r=mpz_tdiv_ui(a.rep, b);
return mpz_sgn(a.rep)>=0 ? r : -r;
}
friend Integer operator% (int a, const Integer& b)
{
return mpz_fits_sint_p(b.rep) ? Integer(a%mpz_get_si(b.rep)) : b;
}
friend Integer operator% (long a, const Integer& b)
{
return mpz_fits_slong_p(b.rep) ? Integer(a%mpz_get_si(b.rep)) : b;
}
/// Multiplies with 2<sup>k</sup>.
friend Integer operator<< (const Integer& a, unsigned long k)
{
return Integer(mpz_mul_2exp, a.rep, k);
}
/// Divides through 2<sup>k</sup>, truncates to 0.
friend Integer operator>> (const Integer& a, unsigned long k)
{
return Integer(mpz_tdiv_q_2exp, a.rep, k);
}
/// Compares with 0.
bool operator!() const { return !mpz_sgn(rep); }
/// Compares with 0.
operator bool() const { return mpz_sgn(rep); }
/// Comparison. The magnitude of the return value is arbitrary, only its sign is relevant.
int compare(const Integer& b) const
{
return mpz_cmp(rep, b.rep);
}
int compare(long b) const
{
return mpz_cmp_si(rep, b);
}
int compare(double b) const
{
return mpz_cmp_d(rep, b);
}
friend bool operator== (const Integer& a, long b)
{
return mpz_fits_slong_p(a.rep) && mpz_get_si(a.rep)==b;
}
friend bool operator< (const Integer& a, long b)
{
return mpz_fits_slong_p(a.rep) ? mpz_get_si(a.rep)<b : mpz_sgn(a.rep)<0;
}
friend bool operator> (const Integer& a, long b)
{
return mpz_fits_slong_p(a.rep) ? mpz_get_si(a.rep)>b : mpz_sgn(a.rep)>0;
}
friend bool abs_equal(const Integer& a, const Integer& b)
{
return !mpz_cmpabs(a.rep, b.rep);
}
friend bool abs_equal(const Integer& a, long b)
{
return !mpz_cmpabs_ui(a.rep, std::abs(b));
}
static Integer fac(unsigned long k)
{
return Integer(mpz_fac_ui, k);
}
static Integer pow(const Integer& a, unsigned long k)
{
return Integer(mpz_pow_ui, a.rep, k);
}
static Integer pow(unsigned long a, unsigned long k)
{
return Integer(mpz_ui_pow_ui, a, k);
}
friend Integer sqrt(const Integer& a)
{
return Integer(mpz_sqrt, a.rep);
}
friend Integer std::abs(const Integer& a);
friend Integer gcd(const Integer& a, const Integer& b)
{
return Integer(mpz_gcd, a.rep, b.rep);
}
friend Integer gcd(const Integer& a, long b)
{
return Integer(mpz_gcd_ui, a.rep, (unsigned long)b);
}
friend Integer gcd(long a, const Integer& b)
{
return Integer(mpz_gcd_ui, b.rep, (unsigned long)a);
}
friend Integer lcm(const Integer& a, const Integer& b)
{
return Integer(mpz_lcm, a.rep, b.rep);
}
#if __GNU_MP_VERSION >=4
friend Integer lcm(const Integer& a, long b)
{
return Integer(mpz_lcm_ui, a.rep, (unsigned long)b);
}
friend Integer lcm(long a, const Integer& b)
{
return Integer(mpz_lcm_ui, b.rep, (unsigned long)a);
}
#endif
/// Extended gcd algorithm: $g=a*p+b*q$.
friend void gcd_ext(const Integer& a, const Integer& b, Integer& g, Integer& p, Integer& q)
{
mpz_gcdext(g.rep, p.rep, q.rep, a.rep, b.rep);
}
friend Integer div_exact(const Integer& a, const Integer& b)
{
return Integer(mpz_divexact, a.rep, b.rep);
}
static Integer binom(const Integer& n, unsigned long k)
{
return Integer(mpz_bin_ui, n.rep, k);
}
static Integer binom(unsigned long n, unsigned long k)
{
return Integer(mpz_bin_uiui, n, k);
}
/// @param allow_sign whether leading whitespaces and sign are expected
template <typename Traits>
void read(std::basic_istream<char, Traits>& is, bool allow_sign=true);
/// Calculates the size of the buffer needed to store an ASCII representation of an \\Integer\\.
size_t strsize(std::ios::fmtflags flags) const;
/** Produces a printable representation of an Integer.
@param buf buffer of size not less than the return value of strsize().
*/
void putstr(std::ios::fmtflags flags, char* buf) const;
friend class Rational;
mpz_srcptr get_rep() const { return rep; }
struct div_t;
class little_buffer {
private:
static char little[256];
char *buf;
public:
explicit little_buffer(int n)
: buf(n<256 ? little : new char[n]) { }
~little_buffer() { if (buf!=little) delete buf; }
operator char* () const { return buf; }
};
};
/// Analogous to ::div_t
struct Integer::div_t {
Integer quot, rem;
div_t(const Integer& n, const Integer& d)
{
mpz_tdiv_qr(quot.rep, rem.rep, n.rep, d.rep);
}
};
inline Integer operator+ (const Integer& a) { return a; }
inline const Integer operator++ (Integer& a, int) { Integer copy(a); ++a; return copy; }
inline const Integer operator-- (Integer& a, int) { Integer copy(a); --a; return copy; }
inline Integer operator+ (long a, const Integer& b) { return b+a; }
inline Integer operator+ (const Integer& a, int b) { return a+long(b); }
inline Integer operator+ (int a, const Integer& b) { return b+long(a); }
inline Integer operator- (const Integer& a, int b) { return a-long(b); }
inline Integer operator- (int a, const Integer& b) { return long(a)-b; }
inline Integer operator* (long a, const Integer& b) { return b*a; }
inline Integer operator* (const Integer& a, int b) { return a*long(b); }
inline Integer operator* (int a, const Integer& b) { return b*long(a); }
inline Integer operator/ (const Integer& a, int b) { return a/long(b); }
inline Integer operator% (const Integer& a, int b) { return a%long(b); }
inline Integer operator<< (const Integer& a, unsigned int k)
{
return a << static_cast<unsigned long>(k);
}
inline Integer operator>> (const Integer& a, unsigned int k)
{
return a >> static_cast<unsigned long>(k);
}
inline Integer operator<< (const Integer& a, long k)
{
if (k<0) return a >> static_cast<unsigned long>(-k);
return a << static_cast<unsigned long>(k);
}
inline Integer operator>> (const Integer& a, long k)
{
if (k<0) return a << static_cast<unsigned long>(-k);
return a >> static_cast<unsigned long>(k);
}
inline Integer operator<< (const Integer& a, int k) { return a << long(k); }
inline Integer operator>> (const Integer& a, int k) { return a >> long(k); }
inline bool operator== (const Integer& a, const Integer& b) { return a.compare(b)==0; }
inline bool operator!= (const Integer& a, const Integer& b) { return a.compare(b)!=0; }
inline bool operator< (const Integer& a, const Integer& b) { return a.compare(b)<0; }
inline bool operator> (const Integer& a, const Integer& b) { return a.compare(b)>0; }
inline bool operator<= (const Integer& a, const Integer& b) { return a.compare(b)<=0; }
inline bool operator>= (const Integer& a, const Integer& b) { return a.compare(b)>=0; }
inline bool operator== (const temp_Integer& a, const temp_Integer& b) { return mpz_cmp(&a,&b)==0; }
inline bool operator!= (const temp_Integer& a, const temp_Integer& b) { return mpz_cmp(&a,&b)!=0; }
inline bool operator< (const temp_Integer& a, const temp_Integer& b) { return mpz_cmp(&a,&b)<0; }
inline bool operator> (const temp_Integer& a, const temp_Integer& b) { return mpz_cmp(&a,&b)>0; }
inline bool operator<= (const temp_Integer& a, const temp_Integer& b) { return mpz_cmp(&a,&b)<=0; }
inline bool operator>= (const temp_Integer& a, const temp_Integer& b) { return mpz_cmp(&a,&b)>=0; }
inline bool operator== (long a, const Integer& b) { return b==a; }
inline bool operator== (const Integer& a, int b) { return a==long(b); }
inline bool operator== (int a, const Integer& b) { return b==long(a); }
inline bool abs_equal(long a, const Integer& b) { return abs_equal(b,a); }
inline bool abs_equal(const Integer& a, int b) { return abs_equal(a,long(b)); }
inline bool abs_equal(int a, const Integer& b) { return abs_equal(b,long(a)); }
inline bool operator!= (const Integer& a, long b) { return !(a==b); }
inline bool operator!= (const Integer& a, int b) { return !(a==long(b)); }
inline bool operator!= (long a, const Integer& b) { return !(b==a); }
inline bool operator!= (int a, const Integer& b) { return !(b==long(a)); }
inline bool operator< (const Integer& a, int b) { return a<long(b); }
inline bool operator< (long a, const Integer& b) { return b>a; }
inline bool operator< (int a, const Integer& b) { return b>long(a); }
inline bool operator> (const Integer& a, int b) { return a>long(b); }
inline bool operator> (long a, const Integer& b) { return b<a; }
inline bool operator> (int a, const Integer& b) { return b<long(a); }
inline bool operator<= (const Integer& a, long b) { return !(a>b); }
inline bool operator<= (const Integer& a, int b) { return !(a>long(b)); }
inline bool operator<= (long a, const Integer& b) { return !(b<a); }
inline bool operator<= (int a, const Integer& b) { return !(b<long(a)); }
inline bool operator>= (const Integer& a, long b) { return !(a>b); }
inline bool operator>= (const Integer& a, int b) { return !(a>long(b)); }
inline bool operator>= (long a, const Integer& b) { return !(b>a); }
inline bool operator>= (int a, const Integer& b) { return !(b>long(a)); }
template <typename Traits>
void Integer::read(std::basic_istream<char, Traits>& is, bool allow_sign)
{
std::ios::iostate exc=is.exceptions();
is.exceptions(std::ios::goodbit);
std::string text;
char c=0;
if (allow_sign) {
is >> c; // skip leading whitespaces, consume the sign or the first digit
switch (c) {
case '-':
text += c;
/* NOBREAK */
case '+':
is >> c; // there may be some spaces after the sign, too
}
} else {
is.get(c);
}
if (is.eof()) {
is.setstate(std::ios::failbit);
} else {
bool valid=false;
int base=0;
switch (is.flags() & std::ios::basefield) {
case std::ios::hex:
base=16; break;
case std::ios::oct:
base=8; break;
case std::ios::dec:
base=10; break;
default: // the base is to be guessed from the prefix
if (c == '0') {
is.get(c);
if (c == 'x' || c == 'X') {
base=16;
is.get(c);
} else {
text += '0';
valid=true;
base=8;
}
} else {
base=10;
}
}
// gather all feasible characters
while (!is.eof()) {
if (isdigit(c)
? (base==8 && c > '7')
: !isalpha(c) || base != 16 || (isupper(c) ? c > 'F' : c > 'f')) {
is.unget();
break;
}
text += c;
valid=true;
is.get(c);
}
if (valid) {
mpz_set_str(rep, text.c_str(), base);
is.clear(is.rdstate() & std::ios::eofbit);
} else {
is.setstate(std::ios::failbit);
}
}
is.exceptions(exc);
}
template <typename Traits>
std::basic_ostream<char, Traits>&
operator<< (std::basic_ostream<char, Traits>& os, const Integer& a)
{
int s=a.strsize(os.flags());
Integer::little_buffer buf(s);
a.putstr(os.flags(), buf);
return os << static_cast<char*>(buf);
}
template <typename Traits> inline
std::basic_istream<char, Traits>&
operator>> (std::basic_istream<char, Traits>& is, Integer& a)
{
a.read(is);
return is;
}
namespace std {
inline void swap(Integer& i1, Integer& i2) { i1.swap(i2); }
inline Integer abs(const Integer& a)
{
return Integer(mpz_abs, a.rep);
}
inline Integer::div_t div(const Integer& n, const Integer& d)
{
return Integer::div_t(n,d);
}
template <>
struct numeric_limits<Integer> : numeric_limits<long> {
static double min() throw() { return -numeric_limits<double>::infinity(); }
static double max() throw() { return +numeric_limits<double>::infinity(); }
static const int digits=INT_MAX;
static const int digits10=INT_MAX;
static const bool is_bounded=false;
};
} // end namespace std
namespace std {
template <> struct hash<Integer> : hash<MP_INT> {
size_t _do(mpz_srcptr a) const {
size_t result=0;
for (int i=0, n=mpz_size(a); i<n; ++i)
(result <<= 1) ^= mpz_getlimbn(a, i);
return result;
}
size_t operator() (const Integer& a) const { return _do(a.get_rep()); }
};
} // end namespace std_ext
#endif // _POLYMAKE_GMP_INTEGER_H
// Local Variables:
// mode:C++
// End: