28 #ifndef FST_SPARSE_TUPLE_WEIGHT_H_ 29 #define FST_SPARSE_TUPLE_WEIGHT_H_ 46 template <
class W,
class K>
52 template <
class W,
class K =
int>
58 using Pair = std::pair<K, W>;
68 template <
class Iterator>
72 for (
auto it = begin; it != end; ++it)
PushBack(*it);
78 : default_(default_weight),
79 first_(weight == default_weight ? kNoKey : key, weight) {}
93 : default_(weight.default_),
94 first_(std::move(weight.first_)),
95 rest_(std::move(weight.rest_)) {
98 weight.first_ =
Pair(kNoKey, W::NoWeight());
117 std::istream &
Read(std::istream &strm) {
123 std::ostream &
Write(std::ostream &strm)
const {
130 if (
this == &weight)
return *
this;
139 if (
this == &weight)
return *
this;
141 default_ = weight.default_;
142 first_ = std::move(weight.first_);
143 rest_ = std::move(weight.rest_);
146 weight.first_ =
Pair(kNoKey, W::NoWeight());
147 weight.rest_.clear();
154 if (!it.Value().second.Member())
return false;
162 static const std::hash<K> H;
164 h = 5 * h + H(it.Value().first);
165 h = 13 * h + it.Value().second.Hash();
173 weight.
PushBack(it.Value().first, it.Value().second.Quantize(delta));
181 weight.
PushBack(it.Value().first, it.Value().second.Reverse());
186 void Init(
const W &default_value = W::Zero()) {
187 first_ =
Pair(kNoKey, W::NoWeight());
189 default_ = default_value;
194 if (first_.first == kNoKey) {
197 return rest_.size() + 1;
201 inline void PushBack(
const K &key,
const W &weight,
202 bool default_value_check =
true) {
203 PushBack(std::make_pair(key, weight), default_value_check);
206 inline void PushBack(
const Pair &pair,
bool default_value_check =
true) {
207 if (default_value_check && pair.second == default_)
return;
208 if (first_.first == kNoKey) {
211 rest_.push_back(pair);
216 const W &
Value(
const K &key)
const {
219 for (; !iter.
Done() && iter.
Value().first < key; iter.
Next())
continue;
220 return !iter.
Done() && iter.
Value().first == key ? iter.
Value().second
228 SetValueToNonDefault(key, w);
237 void SetValueToNonDefault(
const K &key,
const W &w) {
239 if (first_.first == kNoKey) {
240 first_ =
Pair(key, w);
241 }
else if (key < first_.first) {
242 rest_.push_front(first_);
243 first_ =
Pair(key, w);
244 }
else if (key == first_.first) {
248 std::find_if(rest_.begin(), rest_.end(),
249 [key](
const Pair &p) {
return p.first >= key; });
250 if (i != rest_.end() && i->first == key) {
253 rest_.insert(i,
Pair(key, w));
260 void ClearValue(
const K &key) {
261 if (key == first_.first) {
262 if (!rest_.empty()) {
263 first_ = rest_.front();
268 }
else if (key > first_.first) {
270 std::find_if(rest_.begin(), rest_.end(),
271 [key](
const Pair &p) {
return p.first >= key; });
272 if (i != rest_.end() && i->first == key) {
285 std::list<Pair> rest_;
290 template <
class W,
class K>
295 using iterator =
typename std::list<Pair>::iterator;
298 : first_(weight.first_),
301 iter_(rest_.begin()) {}
307 return iter_ == rest_.end();
311 const Pair &
Value()
const {
return init_ ? first_ : *iter_; }
323 iter_ = rest_.begin();
328 const std::list<Pair> &rest_;
335 template <
class W,
class K,
class M>
339 const M &operator_mapper) {
346 while (!w1_it.
Done() || !w2_it.
Done()) {
347 const auto &k1 = (w1_it.
Done()) ? w2_it.
Value().first : w1_it.
Value().first;
348 const auto &k2 = (w2_it.
Done()) ? w1_it.
Value().first : w2_it.
Value().first;
349 const auto &v1 = (w1_it.
Done()) ? v1_def : w1_it.
Value().second;
350 const auto &v2 = (w2_it.
Done()) ? v2_def : w2_it.
Value().second;
352 result->
PushBack(k1, operator_mapper(k1, v1, v2));
355 }
else if (k1 < k2) {
356 result->
PushBack(k1, operator_mapper(k1, v1, v2_def));
359 result->
PushBack(k2, operator_mapper(k2, v1_def, v2));
365 template <
class W,
class K>
370 if (v1_def != v2_def)
return false;
373 while (!w1_it.
Done() || !w2_it.
Done()) {
374 const auto &k1 = (w1_it.
Done()) ? w2_it.
Value().first : w1_it.
Value().first;
375 const auto &k2 = (w2_it.
Done()) ? w1_it.
Value().first : w2_it.
Value().first;
376 const auto &v1 = (w1_it.
Done()) ? v1_def : w1_it.
Value().second;
377 const auto &v2 = (w2_it.
Done()) ? v2_def : w2_it.
Value().second;
379 if (v1 != v2)
return false;
382 }
else if (k1 < k2) {
383 if (v1 != v2_def)
return false;
386 if (v1_def != v2)
return false;
393 template <
class W,
class K>
399 template <
class W,
class K>
413 template <
class W,
class K>
434 #endif // FST_SPARSE_TUPLE_WEIGHT_H_ void PushBack(const Pair &pair, bool default_value_check=true)
void PushBack(const K &key, const W &weight, bool default_value_check=true)
~SparseTupleWeight() noexcept=default
SparseTupleWeight & operator=(SparseTupleWeight &&weight) noexcept
void Init(const W &default_value=W::Zero())
std::ostream & Write(std::ostream &strm) const
std::pair< K, A::Weight > Pair
void SparseTupleWeightMap(SparseTupleWeight< W, K > *result, const SparseTupleWeight< W, K > &w1, const SparseTupleWeight< W, K > &w2, const M &operator_mapper)
static const SparseTupleWeight & Zero()
static const SparseTupleWeight & One()
SparseTupleWeightIterator(const SparseTupleWeight< W, K > &weight)
static const SparseTupleWeight & NoWeight()
const Pair & Value() const
void SetValue(const K &key, const W &w)
std::ostream & WriteType(std::ostream &strm, const T t)
std::istream & operator>>(std::istream &strm, FloatWeightTpl< T > &w)
SparseTupleWeight(Iterator begin, Iterator end)
bool operator!=(const ErrorWeight &, const ErrorWeight &)
SparseTupleWeight(const K &key, const W &weight, const W &default_weight)
void SetDefaultValue(const W &value)
std::ostream & operator<<(std::ostream &strm, const ErrorWeight &)
static constexpr K kNoKey
SparseTupleWeight(const W &weight)
typename SparseTupleWeight< W, K >::Pair Pair
const W & DefaultValue() const
SparseTupleWeight & operator=(const SparseTupleWeight &weight)
SparseTupleWeight(const SparseTupleWeight &weight)
typename std::list< Pair >::const_iterator const_iterator
std::istream & Read(std::istream &strm)
bool ReadElement(T *comp, bool last=false)
const W & Value(const K &key) const
bool operator==(const ErrorWeight &, const ErrorWeight &)
SparseTupleWeight Quantize(float delta=kDelta) const
typename std::list< Pair >::iterator iterator
std::istream & ReadType(std::istream &strm, T *t)
void WriteElement(const T &comp)
SparseTupleWeight(SparseTupleWeight &&weight) noexcept
ReverseWeight Reverse() const