-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommon.h
72 lines (58 loc) · 1.55 KB
/
common.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
#ifndef COMMON_H
#define COMMON_H
#include <iostream>
#include <unordered_map>
#include <utility>
#include <gmpxx.h>
enum Sign { Pos = 0, Neg = 1 };
inline Sign negate(Sign s) {
if (s == Neg)
return Pos;
return Neg;
}
std::ostream& operator<<(std::ostream& out, const Sign& s) {
if (s == Neg)
out << "-";
else
out << "+";
return out;
}
typedef unsigned int VarId;
typedef std::pair<VarId, Sign> SignedVarId;
inline SignedVarId negate(SignedVarId s_varid) {
return SignedVarId(s_varid.first, negate(s_varid.second));
}
std::ostream& operator<<(std::ostream& out, const SignedVarId& pair) {
out << pair.first << pair.second;
return out;
}
namespace std {
template<>
struct hash<SignedVarId> {
std::size_t operator()(SignedVarId const &var) const {
return var.second << var.first;
}
};
}
/*
* This is necessary for Boost's Bellman-Ford to work correctly --- closed_plus
* is used for addition. The problem is that with mpz_class that has no
* std::numeric_limits::max it assumes that zero is max value and no matter what
* is added to zero the result is zero.. This makes sense in case of fixed size
* integers but not for e.g. mpz_class..
*/
template <typename T>
struct custom_plus : public boost::closed_plus<T> { };
template <>
struct custom_plus<mpz_class> {
mpz_class operator()(const mpz_class& a, const mpz_class& b) const {
return a + b;
}
};
template <>
struct custom_plus<mpq_class> {
mpq_class operator()(const mpq_class& a, const mpq_class& b) const {
return a + b;
}
};
#endif /* COMMON_H */