forked from google/language-resources
-
Notifications
You must be signed in to change notification settings - Fork 0
/
value-weight-singleton.h
230 lines (191 loc) · 6.71 KB
/
value-weight-singleton.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
// festus/value-weight-singleton.h
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http:https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// Copyright 2016 Google, Inc.
// Author: [email protected] (Martin Jansche)
//
// \file
// Façade for OpenFst weights around a singleton semiring object.
#ifndef FESTUS_VALUE_WEIGHT_SINGLETON_H__
#define FESTUS_VALUE_WEIGHT_SINGLETON_H__
#include <cstddef>
#include <functional>
#include <istream>
#include <ostream>
#include <type_traits>
#include <utility>
#include <fst/compat.h>
#include <fst/weight.h>
namespace festus {
template <class S>
struct DefaultInstance {
// Owned pointer, will never be deleted.
static const S *const kInstance;
static constexpr const S &Instance() { return *kInstance; }
};
template <class S>
const S *const DefaultInstance<S>::kInstance = new S();
template <class S>
struct DefaultStaticInstance {
static constexpr S kStaticInstance = {};
static constexpr const S &Instance() { return kStaticInstance; }
};
template <class S>
constexpr S DefaultStaticInstance<S>::kStaticInstance;
// OpenFst weight façade for semirings whose elements are passed by value.
//
// This version works with a semiring class S with const member functions and
// uses a singleton instance of S, which must never be deleted. The singleton
// semiring object should be thread safe.
template <class S, class Singleton = DefaultInstance<S>>
class ValueWeightSingleton {
public:
typedef S SemiringType;
typedef typename S::ValueType ValueType;
typedef ValueWeightSingleton ReverseWeight;
ValueWeightSingleton() = default;
~ValueWeightSingleton() = default;
ValueWeightSingleton(ValueWeightSingleton &&) = default;
ValueWeightSingleton(const ValueWeightSingleton &) = default;
ValueWeightSingleton &operator=(ValueWeightSingleton &&) = default;
ValueWeightSingleton &operator=(const ValueWeightSingleton &) = default;
// Deprecated implicit constructor. Use From() instead.
constexpr ValueWeightSingleton(ValueType value) : value_(value) {}
template <typename T>
static constexpr ValueWeightSingleton From(T &&arg) {
return ValueWeightSingleton(Semiring().From(std::forward<T>(arg)));
}
ValueType Value() const { return value_; }
static constexpr const S &Semiring() { return Singleton::Instance(); }
static constexpr ValueWeightSingleton NoWeight() {
return ValueWeightSingleton(Semiring().NoWeight());
}
static constexpr ValueWeightSingleton Zero() {
return ValueWeightSingleton(Semiring().Zero());
}
static constexpr ValueWeightSingleton One() {
return ValueWeightSingleton(Semiring().One());
}
friend inline ValueWeightSingleton Plus(
ValueWeightSingleton lhs,
ValueWeightSingleton rhs) {
lhs.value_ = Semiring().OpPlus(lhs.value_, rhs.value_);
return lhs;
}
friend inline ValueWeightSingleton Minus(
ValueWeightSingleton lhs,
ValueWeightSingleton rhs) {
lhs.value_ = Semiring().OpMinus(lhs.value_, rhs.value_);
return lhs;
}
friend inline ValueWeightSingleton Times(
ValueWeightSingleton lhs,
ValueWeightSingleton rhs) {
lhs.value_ = Semiring().OpTimes(lhs.value_, rhs.value_);
return lhs;
}
friend inline ValueWeightSingleton Divide(
ValueWeightSingleton lhs,
ValueWeightSingleton rhs,
fst::DivideType typ = fst::DIVIDE_ANY) {
if (Semiring().Commutative()) {
return DivideCommutative(lhs, rhs);
} else {
return DivideNoncommutative(lhs, rhs, typ);
}
}
friend inline ValueWeightSingleton Star(
ValueWeightSingleton w) {
w.value_ = Semiring().OpStar(w.value_);
return w;
}
ValueWeightSingleton Reverse() const {
return ValueWeightSingleton(Semiring().Reverse(value_));
}
ValueWeightSingleton Quantize(float delta = fst::kDelta) const {
return ValueWeightSingleton(Semiring().Quantize(value_, delta));
}
constexpr bool Member() const { return Semiring().Member(value_); }
friend inline bool operator==(
ValueWeightSingleton lhs,
ValueWeightSingleton rhs) {
return Semiring().EqualTo(lhs.value_, rhs.value_);
}
friend inline bool operator!=(
ValueWeightSingleton lhs,
ValueWeightSingleton rhs) {
return !Semiring().EqualTo(lhs.value_, rhs.value_);
}
friend inline bool ApproxEqual(
ValueWeightSingleton lhs,
ValueWeightSingleton rhs,
float delta = fst::kDelta) {
return Semiring().ApproxEqualTo(lhs.value_, rhs.value_, delta);
}
std::size_t Hash() const { return std::hash<ValueType>()(value_); }
friend std::ostream &operator<<(
std::ostream &strm,
ValueWeightSingleton w) {
return Semiring().Print(strm, w.value_);
}
std::istream &Read(std::istream &strm) {
static_assert(std::is_pod<ValueType>::value, "value type should be POD");
return strm.read(reinterpret_cast<char *>(&value_), sizeof(value_));
}
std::ostream &Write(std::ostream &strm) const {
return strm.write(reinterpret_cast<const char *>(&value_), sizeof(value_));
}
static const string &Type() {
static const string type = Semiring().Name();
return type;
}
static constexpr uint64 Properties() {
return fst::kSemiring
| (Semiring().Commutative() ? fst::kCommutative : 0)
| (Semiring().Idempotent() ? fst::kIdempotent : 0);
}
private:
static ValueWeightSingleton DivideCommutative(
ValueWeightSingleton lhs,
ValueWeightSingleton rhs) {
lhs.value_ = Semiring().OpDivide(lhs.value_, rhs.value_);
return lhs;
}
static ValueWeightSingleton DivideNoncommutative(
ValueWeightSingleton lhs,
ValueWeightSingleton rhs,
fst::DivideType typ) {
switch (typ) {
case fst::DIVIDE_LEFT: {
auto recip = Semiring().Reciprocal(rhs.value_);
lhs.value_ = Semiring().OpTimes(recip, lhs.value_);
break;
}
case fst::DIVIDE_RIGHT: {
auto recip = Semiring().Reciprocal(rhs.value_);
lhs.value_ = Semiring().OpTimes(lhs.value_, recip);
break;
}
case fst::DIVIDE_ANY:
FSTERROR() << "Only explicit left or right division is defined "
<< "for the noncommutative " << Type() << " semiring";
lhs.value_ = Semiring().NoWeight();
break;
}
return lhs;
}
ValueType value_;
};
} // namespace festus
#endif // FESTUS_VALUE_WEIGHT_SINGLETON_H__