forked from laurentnoe/iedera
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcost.hh
127 lines (103 loc) · 3.35 KB
/
cost.hh
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
#ifndef __COST_HH__
#define __COST_HH__
/** @defgroup cost cost class template
* @brief costs defined over the templated C elements (int, long int, long long int, or other ...),
* and in the @f$ (\oplus = min, \otimes = plus) @f$ tropical semi-ring
*
*
* @see matrix, automaton
*/
// @{
//STL
#include <functional>
#include <algorithm>
#include <vector>
//STD
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <string>
//STR
using namespace std;
/**
* @class cost
* @brief costs defined in the @f$ (\oplus = min, \otimes = plus) @f$
* tropical semi-ring
*
* This class defines common operations in the @f$ (\oplus = min, \otimes = plus) @f$ tropical semi-ring to get
* compatibility issues with the classical @f$ (\oplus = +, \otimes = \times) @f$ semi-ring used for probabilistic
* computations. As such, any algorithm programmed with @f$ (\oplus, \otimes) @f$
* (C++ operators @f$+@f$ and @f$\times@f$) can be used in both cases : either on costs, otherwise on more
* traditional floating point values...
*
* @see matrix, automaton
*/
template<typename C> class cost {
public:
/// Build a cost
cost(const C c): _c(c) {};
/// Erase a cost
~cost() {};
// @{
/// Operator + (min) for two costs
template<typename U> friend cost<U> operator+ (const cost<U> l,const cost<U> r);
/// Operator @f$ \times @f$ (add) for two costs
template<typename U> friend cost<U> operator* (const cost<U> l,const cost<U> r);
/// Operator @f$ != @f$ for two costs
template<typename U> friend bool operator!= (const cost<U> l,const cost<U> r);
/// Operator @f$ == @f$ for two costs
template<typename U> friend bool operator== (const cost<U> l,const cost<U> r);
/// Operator @f$ < @f$ for two costs
template<typename U> friend bool operator< (const cost<U> l,const cost<U> r);
/// Operator @f$ > @f$ for two costs
template<typename U> friend bool operator> (const cost<U> l,const cost<U> r);
// @}
// @{
/// Print a cost
template<typename U> friend ostream& operator<< (ostream& os, const cost<U>& c);
/// Load a cost
template<typename U> friend istream& operator>> (istream& is, cost<U>& c);
// @}
protected:
/// memorized cost
C _c;
};
/// Operator + (min) for two costs
template<typename C> inline cost<C> operator+ (const cost<C> l, const cost<C> r) {
cost<C> x(MIN(l._c,r._c));
return x;
}
/// Operator @f$ \times @f$ (add) for two costs
template<typename C> inline cost<C> operator* (const cost<C> l, const cost<C> r) {
cost<C> x(l._c + r._c);
return x;
}
/// Operator @f$ == @f$ for two costs
template<typename C> inline bool operator== (const cost<C> l, const cost<C> r) {
return l._c == r._c;
}
/// Operator @f$ != @f$ for two costs
template<typename C> inline bool operator!= (const cost<C> l, const cost<C> r) {
return l._c != r._c;
}
/// Operator @f$ < @f$ for two costs
template<typename C> inline bool operator< (const cost<C> l, const cost<C> r) {
return l._c < r._c;
}
/// Operator @f$ > @f$ for two costs
template<typename C> inline bool operator> (const cost<C> l, const cost<C> r) {
return l._c > r._c;
}
/// Print a cost
template<typename C> ostream& operator<< (ostream& os, const cost<C>& c) {
os << c._c;
return os;
}
/// Load a cost
template<typename C> istream& operator>> (istream& is, cost<C>& c) {
is >> c._c;
return is;
}
// @}
#endif