Blame view
tools/openfst-1.6.7/src/include/fst/sparse-tuple-weight.h
12.2 KB
8dcb6dfcb first commit |
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 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 |
// See www.openfst.org for extensive documentation on this weighted // finite-state transducer library. // // Sparse version of tuple-weight, based on tuple-weight.h. // Internally stores sparse key, value pairs in linked list. The default value // element is the assumed value of unset keys. Internal singleton // implementation that stores first key, value pair as a initialized member // variable to avoid unnecessary allocation on heap. Use // SparseTupleWeightIterator to iterate through the key,value pairs. Note: // this does NOT iterate through the default value. // // Sparse tuple weight set operation definitions. #ifndef FST_SPARSE_TUPLE_WEIGHT_H_ #define FST_SPARSE_TUPLE_WEIGHT_H_ #include <algorithm> #include <list> #include <stack> #include <string> #include <unordered_map> #include <utility> #include <fst/weight.h> namespace fst { template <class W, class K> class SparseTupleWeightIterator; // Arbitrary dimension tuple weight, stored as a sorted linked-list. // W is any weight class, and K is the key value type. kNoKey (-1) is reserved // for internal use. template <class W, class K = int> class SparseTupleWeight { public: using ReverseWeight = SparseTupleWeight<typename W::ReverseWeight, K>; using Iterator = SparseTupleWeightIterator<W, K>; using Pair = std::pair<K, W>; using Weight = W; using Index = K; constexpr static K kNoKey = -1; SparseTupleWeight() { Init(); } template <class Iterator> SparseTupleWeight(Iterator begin, Iterator end) { Init(); // Assumes input iterator is sorted. for (auto it = begin; it != end; ++it) PushBack(*it); } // Initialize component `key` to `weight`, with `default_weight` for all // other components. SparseTupleWeight(const K &key, const W &weight, const W &default_weight) : default_(default_weight), first_(weight == default_weight ? kNoKey : key, weight) {} explicit SparseTupleWeight(const W &weight) { Init(weight); } SparseTupleWeight(const SparseTupleWeight &weight) { Init(weight.DefaultValue()); SetDefaultValue(weight.DefaultValue()); for (Iterator it(weight); !it.Done(); it.Next()) { PushBack(it.Value()); } } SparseTupleWeight(SparseTupleWeight &&weight) // Don't move the default, so weight.default_ is still valid. : default_(weight.default_), first_(std::move(weight.first_)), rest_(std::move(weight.rest_)) { // move leaves the source in a valid but unspecified state. // Make sure the source weight is empty. weight.first_ = Pair(kNoKey, W::NoWeight()); weight.rest_.clear(); } static const SparseTupleWeight &Zero() { static const SparseTupleWeight zero(W::Zero()); return zero; } static const SparseTupleWeight &One() { static const SparseTupleWeight one(W::One()); return one; } static const SparseTupleWeight &NoWeight() { static const SparseTupleWeight no_weight(W::NoWeight()); return no_weight; } std::istream &Read(std::istream &strm) { ReadType(strm, &default_); ReadType(strm, &first_); return ReadType(strm, &rest_); } std::ostream &Write(std::ostream &strm) const { WriteType(strm, default_); WriteType(strm, first_); return WriteType(strm, rest_); } SparseTupleWeight &operator=(const SparseTupleWeight &weight) { if (this == &weight) return *this; // Checks for identity. Init(weight.DefaultValue()); for (Iterator it(weight); !it.Done(); it.Next()) { PushBack(it.Value()); } return *this; } SparseTupleWeight &operator=(SparseTupleWeight &&weight) { if (this == &weight) return *this; // Checks for identity. default_ = weight.default_; std::swap(first_, weight.first_); std::swap(rest_, weight.rest_); return *this; } bool Member() const { if (!DefaultValue().Member()) return false; for (Iterator it(*this); !it.Done(); it.Next()) { if (!it.Value().second.Member()) return false; } return true; } // Assumes H() function exists for the hash of the key value. size_t Hash() const { size_t h = 0; static const std::hash<K> H; for (Iterator it(*this); !it.Done(); it.Next()) { h = 5 * h + H(it.Value().first); h = 13 * h + it.Value().second.Hash(); } return h; } SparseTupleWeight Quantize(float delta = kDelta) const { SparseTupleWeight weight; for (Iterator it(*this); !it.Done(); it.Next()) { weight.PushBack(it.Value().first, it.Value().second.Quantize(delta)); } return weight; } ReverseWeight Reverse() const { SparseTupleWeight weight; for (Iterator it(*this); !it.Done(); it.Next()) { weight.PushBack(it.Value().first, it.Value().second.Reverse()); } return ReverseWeight(weight); } void Init(const W &default_value = W::Zero()) { first_ = Pair(kNoKey, W::NoWeight()); // Initialized to the reserved key value. default_ = default_value; rest_.clear(); } size_t Size() const { if (first_.first == kNoKey) { return 0; } else { return rest_.size() + 1; } } inline void PushBack(const K &key, const W &weight, bool default_value_check = true) { PushBack(std::make_pair(key, weight), default_value_check); } inline void PushBack(const Pair &pair, bool default_value_check = true) { if (default_value_check && pair.second == default_) return; if (first_.first == kNoKey) { first_ = pair; } else { rest_.push_back(pair); } } // Returns the `key`-th component, or the default value if not set. const W &Value(const K &key) const { // TODO(rybach): Consider binary search. Iterator iter(*this); for (; !iter.Done() && iter.Value().first < key; iter.Next()) continue; return !iter.Done() && iter.Value().first == key ? iter.Value().second : DefaultValue(); } void SetValue(const K &key, const W &w) { if (w == DefaultValue()) { ClearValue(key); } else { SetValueToNonDefault(key, w); } } void SetDefaultValue(const W &value) { default_ = value; } const W &DefaultValue() const { return default_; } private: void SetValueToNonDefault(const K &key, const W &w) { // Don't use SparseTupleWeightIterator, since that's const. if (first_.first == kNoKey) { first_ = Pair(key, w); } else if (key < first_.first) { rest_.push_front(first_); first_ = Pair(key, w); } else if (key == first_.first) { first_.second = w; } else { const auto i = std::find_if(rest_.begin(), rest_.end(), [key](const Pair &p) { return p.first >= key; }); if (i != rest_.end() && i->first == key) { i->second = w; } else { rest_.insert(i, Pair(key, w)); } } } // Removes the weight value for `key`, having the effect of setting // it to `DefaultValue()`. void ClearValue(const K &key) { if (key == first_.first) { if (!rest_.empty()) { first_ = rest_.front(); rest_.pop_front(); } else { first_.first = kNoKey; } } else if (key > first_.first) { const auto i = std::find_if(rest_.begin(), rest_.end(), [key](const Pair &p) { return p.first >= key; }); if (i != rest_.end() && i->first == key) { rest_.erase(i); } } } // Assumed default value of uninitialized keys, by default W::Zero(). W default_; // Key values pairs are first stored in first_, then fill rest_ this way we // can avoid dynamic allocation in the common case where the weight is a // single key/value pair. Pair first_; std::list<Pair> rest_; friend class SparseTupleWeightIterator<W, K>; }; // Declare storage for kNoKey since it is passed by reference. template <class W, class K> constexpr K SparseTupleWeight<W, K>::kNoKey; template <class W, class K> class SparseTupleWeightIterator { public: using Pair = typename SparseTupleWeight<W, K>::Pair; using const_iterator = typename std::list<Pair>::const_iterator; using iterator = typename std::list<Pair>::iterator; explicit SparseTupleWeightIterator(const SparseTupleWeight<W, K> &weight) : first_(weight.first_), rest_(weight.rest_), init_(true), iter_(rest_.begin()) {} bool Done() const { if (init_) { return first_.first == SparseTupleWeight<W, K>::kNoKey; } else { return iter_ == rest_.end(); } } const Pair &Value() const { return init_ ? first_ : *iter_; } void Next() { if (init_) { init_ = false; } else { ++iter_; } } void Reset() { init_ = true; iter_ = rest_.begin(); } private: const Pair &first_; const std::list<Pair> &rest_; bool init_; // In the initialized state? const_iterator iter_; }; // M must be callable as a function W(K, W, W). // K will be kNoKey when mapping the default value. template <class W, class K, class M> inline void SparseTupleWeightMap(SparseTupleWeight<W, K> *result, const SparseTupleWeight<W, K> &w1, const SparseTupleWeight<W, K> &w2, const M &operator_mapper) { SparseTupleWeightIterator<W, K> w1_it(w1); SparseTupleWeightIterator<W, K> w2_it(w2); const auto &v1_def = w1.DefaultValue(); const auto &v2_def = w2.DefaultValue(); result->SetDefaultValue( operator_mapper(SparseTupleWeight<W, K>::kNoKey, v1_def, v2_def)); while (!w1_it.Done() || !w2_it.Done()) { const auto &k1 = (w1_it.Done()) ? w2_it.Value().first : w1_it.Value().first; const auto &k2 = (w2_it.Done()) ? w1_it.Value().first : w2_it.Value().first; const auto &v1 = (w1_it.Done()) ? v1_def : w1_it.Value().second; const auto &v2 = (w2_it.Done()) ? v2_def : w2_it.Value().second; if (k1 == k2) { result->PushBack(k1, operator_mapper(k1, v1, v2)); if (!w1_it.Done()) w1_it.Next(); if (!w2_it.Done()) w2_it.Next(); } else if (k1 < k2) { result->PushBack(k1, operator_mapper(k1, v1, v2_def)); w1_it.Next(); } else { result->PushBack(k2, operator_mapper(k2, v1_def, v2)); w2_it.Next(); } } } template <class W, class K> inline bool operator==(const SparseTupleWeight<W, K> &w1, const SparseTupleWeight<W, K> &w2) { const auto &v1_def = w1.DefaultValue(); const auto &v2_def = w2.DefaultValue(); if (v1_def != v2_def) return false; SparseTupleWeightIterator<W, K> w1_it(w1); SparseTupleWeightIterator<W, K> w2_it(w2); while (!w1_it.Done() || !w2_it.Done()) { const auto &k1 = (w1_it.Done()) ? w2_it.Value().first : w1_it.Value().first; const auto &k2 = (w2_it.Done()) ? w1_it.Value().first : w2_it.Value().first; const auto &v1 = (w1_it.Done()) ? v1_def : w1_it.Value().second; const auto &v2 = (w2_it.Done()) ? v2_def : w2_it.Value().second; if (k1 == k2) { if (v1 != v2) return false; if (!w1_it.Done()) w1_it.Next(); if (!w2_it.Done()) w2_it.Next(); } else if (k1 < k2) { if (v1 != v2_def) return false; w1_it.Next(); } else { if (v1_def != v2) return false; w2_it.Next(); } } return true; } template <class W, class K> inline bool operator!=(const SparseTupleWeight<W, K> &w1, const SparseTupleWeight<W, K> &w2) { return !(w1 == w2); } template <class W, class K> inline std::ostream &operator<<(std::ostream &strm, const SparseTupleWeight<W, K> &weight) { CompositeWeightWriter writer(strm); writer.WriteBegin(); writer.WriteElement(weight.DefaultValue()); for (SparseTupleWeightIterator<W, K> it(weight); !it.Done(); it.Next()) { writer.WriteElement(it.Value().first); writer.WriteElement(it.Value().second); } writer.WriteEnd(); return strm; } template <class W, class K> inline std::istream &operator>>(std::istream &strm, SparseTupleWeight<W, K> &weight) { CompositeWeightReader reader(strm); reader.ReadBegin(); W def; bool more = reader.ReadElement(&def); weight.Init(def); while (more) { K key; reader.ReadElement(&key); W v; more = reader.ReadElement(&v); weight.PushBack(key, v); } reader.ReadEnd(); return strm; } } // namespace fst #endif // FST_SPARSE_TUPLE_WEIGHT_H_ |