forked from mmurshed/biginteger
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBigIntegerComparator.h
63 lines (53 loc) · 1.26 KB
/
BigIntegerComparator.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
/**
* BigInteger Class
* Version 9.0
* S. M. Mahbub Murshed (murshed@gmail.com)
*/
#ifndef BIGINTEGER_COMPARATOR
#define BIGINTEGER_COMPARATOR
#include <vector>
using namespace std;
namespace BigMath
{
// All operations are unsigned
class BigIntegerComparator
{
public:
// Compares this with `with' irrespective of sign
// Returns
// 0 if equal
// +value if this > with
// -value if this < with
// Runtime O(n), Space O(1)
static Int CompareTo(
vector<DataT> const& a,
vector<DataT> const& b)
{
// Case with zero
bool aIsZero = BigIntegerUtil::IsZero(a);
bool bIsZero = BigIntegerUtil::IsZero(b);
if(aIsZero && bIsZero)
return 0;
else if(!aIsZero && bIsZero)
return 1;
else if(aIsZero && !bIsZero)
return -1;
// Different in size
Long diff = a.size();
diff -= b.size();
if(diff != 0)
return (Int)diff;
// Both ints have same number of digits
Int cmp = 0;
for(Int i = (Int)a.size() - 1; i >= 0; i--)
{
diff = a[i];
diff -= b[i];
if(diff != 0)
return (Int)diff;
}
return cmp;
}
};
}
#endif