-
Notifications
You must be signed in to change notification settings - Fork 2
/
Integer.cc
77 lines (69 loc) · 2.24 KB
/
Integer.cc
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
/* Copyright (c) 1997-2006
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.
*/
#ident "$Project: polymake $$Id: Integer.cc 7315 2006-04-02 21:37:53Z gawrilow $"
#include "Integer.h"
// fragments borrowed from read_int() of GNU libio
size_t Integer::strsize(const std::ios::fmtflags flags) const
{
size_t s=1+(mpz_sgn(rep)<0); // terminating '\0' and possible sign
int base;
switch (flags & (std::ios::basefield | std::ios::showbase)) {
case int(std::ios::hex) | int(std::ios::showbase):
s+=2;
case std::ios::hex:
base=16;
break;
case int(std::ios::oct) | int(std::ios::showbase):
s+=1;
case std::ios::oct:
base=8;
break;
default:
base=10;
}
return s+mpz_sizeinbase(rep, base);
}
void Integer::putstr(std::ios::fmtflags flags, char* buf) const
{
int base;
switch (flags & (std::ios::basefield | std::ios::showbase)) {
case int(std::ios::hex) | int(std::ios::showbase):
mpz_get_str(buf+2, 16, rep);
if (mpz_sgn(rep)<0) *buf++='-';
*buf++='0';
*buf='x';
return;
case int(std::ios::oct) | int(std::ios::showbase):
mpz_get_str(buf+1, 8, rep);
if (mpz_sgn(rep)<0) *buf++='-';
*buf='0';
return;
case std::ios::hex:
base=16;
break;
case std::ios::oct:
base=8;
break;
default:
base=10;
}
mpz_get_str(buf, base, rep);
}
std::string Integer::to_string(int base) const
{
std::string s(mpz_sizeinbase(rep,base)+2, '\0');
mpz_get_str(const_cast<char*>(s.data()), base, rep); // a nasty cast, I know...
s.resize(s.find('\0'));
return s;
}
char Integer::little_buffer::little[256];