/usr/include/fst/queue.h is in libfst-dev 1.5.3+r3-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 | // See www.openfst.org for extensive documentation on this weighted
// finite-state transducer library.
//
// Functions and classes for various FST state queues with a unified interface.
#ifndef FST_LIB_QUEUE_H_
#define FST_LIB_QUEUE_H_
#include <deque>
#include <vector>
#include <fst/arcfilter.h>
#include <fst/connect.h>
#include <fst/heap.h>
#include <fst/topsort.h>
namespace fst {
// template <class S>
// class Queue {
// public:
// typedef typename S StateId;
//
// // Ctr: may need args (e.g., Fst, comparator) for some queues
// Queue(...);
// // Returns the head of the queue
// StateId Head() const;
// // Inserts a state
// void Enqueue(StateId s);
// // Removes the head of the queue
// void Dequeue();
// // Updates ordering of state s when weight changes, if necessary
// void Update(StateId s);
// // Does the queue contain no elements?
// bool Empty() const;
// // Remove all states from queue
// void Clear();
// };
// State queue types.
enum QueueType {
TRIVIAL_QUEUE = 0, // Single state queue
FIFO_QUEUE = 1, // First-in, first-out queue
LIFO_QUEUE = 2, // Last-in, first-out queue
SHORTEST_FIRST_QUEUE = 3, // Shortest-first queue
TOP_ORDER_QUEUE = 4, // Topologically-ordered queue
STATE_ORDER_QUEUE = 5, // State-ID ordered queue
SCC_QUEUE = 6, // Component graph top-ordered meta-queue
AUTO_QUEUE = 7, // Auto-selected queue
OTHER_QUEUE = 8
};
// QueueBase, templated on the StateId, is the base class shared by the
// queues considered by AutoQueue.
template <class S>
class QueueBase {
public:
typedef S StateId;
QueueBase(QueueType type) : queue_type_(type), error_(false) {}
virtual ~QueueBase() {}
StateId Head() const { return Head_(); }
void Enqueue(StateId s) { Enqueue_(s); }
void Dequeue() { Dequeue_(); }
void Update(StateId s) { Update_(s); }
bool Empty() const { return Empty_(); }
void Clear() { Clear_(); }
QueueType Type() { return queue_type_; }
bool Error() const { return error_; }
void SetError(bool error) { error_ = error; }
private:
// This allows base-class virtual access to non-virtual derived-
// class members of the same name. It makes the derived class more
// efficient to use but unsafe to further derive.
virtual StateId Head_() const = 0;
virtual void Enqueue_(StateId s) = 0;
virtual void Dequeue_() = 0;
virtual void Update_(StateId s) = 0;
virtual bool Empty_() const = 0;
virtual void Clear_() = 0;
QueueType queue_type_;
bool error_;
};
// Trivial queue discipline, templated on the StateId. You may enqueue
// at most one state at a time. It is used for strongly connected components
// with only one state and no self loops.
template <class S>
class TrivialQueue : public QueueBase<S> {
public:
typedef S StateId;
TrivialQueue() : QueueBase<S>(TRIVIAL_QUEUE), front_(kNoStateId) {}
StateId Head() const { return front_; }
void Enqueue(StateId s) { front_ = s; }
void Dequeue() { front_ = kNoStateId; }
void Update(StateId s) {}
bool Empty() const { return front_ == kNoStateId; }
void Clear() { front_ = kNoStateId; }
private:
// This allows base-class virtual access to non-virtual derived-
// class members of the same name. It makes the derived class more
// efficient to use but unsafe to further derive.
StateId Head_() const override { return Head(); }
void Enqueue_(StateId s) override { Enqueue(s); }
void Dequeue_() override { Dequeue(); }
void Update_(StateId s) override { Update(s); }
bool Empty_() const override { return Empty(); }
void Clear_() override { return Clear(); }
StateId front_;
};
// First-in, first-out queue discipline, templated on the StateId.
template <class S>
class FifoQueue : public QueueBase<S>, public std::deque<S> {
public:
using std::deque<S>::back;
using std::deque<S>::push_front;
using std::deque<S>::pop_back;
using std::deque<S>::empty;
using std::deque<S>::clear;
typedef S StateId;
FifoQueue() : QueueBase<S>(FIFO_QUEUE) {}
StateId Head() const { return back(); }
void Enqueue(StateId s) { push_front(s); }
void Dequeue() { pop_back(); }
void Update(StateId s) {}
bool Empty() const { return empty(); }
void Clear() { clear(); }
private:
// This allows base-class virtual access to non-virtual derived-
// class members of the same name. It makes the derived class more
// efficient to use but unsafe to further derive.
StateId Head_() const override { return Head(); }
void Enqueue_(StateId s) override { Enqueue(s); }
void Dequeue_() override { Dequeue(); }
void Update_(StateId s) override { Update(s); }
bool Empty_() const override { return Empty(); }
void Clear_() override { return Clear(); }
};
// Last-in, first-out queue discipline, templated on the StateId.
template <class S>
class LifoQueue : public QueueBase<S>, public std::deque<S> {
public:
using std::deque<S>::front;
using std::deque<S>::push_front;
using std::deque<S>::pop_front;
using std::deque<S>::empty;
using std::deque<S>::clear;
typedef S StateId;
LifoQueue() : QueueBase<S>(LIFO_QUEUE) {}
StateId Head() const { return front(); }
void Enqueue(StateId s) { push_front(s); }
void Dequeue() { pop_front(); }
void Update(StateId s) {}
bool Empty() const { return empty(); }
void Clear() { clear(); }
private:
// This allows base-class virtual access to non-virtual derived-
// class members of the same name. It makes the derived class more
// efficient to use but unsafe to further derive.
StateId Head_() const override { return Head(); }
void Enqueue_(StateId s) override { Enqueue(s); }
void Dequeue_() override { Dequeue(); }
void Update_(StateId s) override { Update(s); }
bool Empty_() const override { return Empty(); }
void Clear_() override { return Clear(); }
};
// Shortest-first queue discipline, templated on the StateId and
// comparison function object. Comparison function object COMP is
// used to compare two StateIds. If a (single) state's order changes,
// it can be reordered in the queue with a call to Update().
// If 'update == false', call to Update() does not reorder the queue.
template <typename S, typename C, bool update = true>
class ShortestFirstQueue : public QueueBase<S> {
public:
typedef S StateId;
typedef C Compare;
ShortestFirstQueue(C comp)
: QueueBase<S>(SHORTEST_FIRST_QUEUE), heap_(comp) {}
StateId Head() const { return heap_.Top(); }
void Enqueue(StateId s) {
if (update) {
for (StateId i = key_.size(); i <= s; ++i) key_.push_back(kNoKey);
key_[s] = heap_.Insert(s);
} else {
heap_.Insert(s);
}
}
void Dequeue() {
if (update)
key_[heap_.Pop()] = kNoKey;
else
heap_.Pop();
}
void Update(StateId s) {
if (!update) return;
if (s >= key_.size() || key_[s] == kNoKey) {
Enqueue(s);
} else {
heap_.Update(key_[s], s);
}
}
bool Empty() const { return heap_.Empty(); }
void Clear() {
heap_.Clear();
if (update) key_.clear();
}
private:
enum { kNoKey = Heap<S, C>::kNoKey };
Heap<S, C> heap_;
std::vector<ssize_t> key_;
// This allows base-class virtual access to non-virtual derived-
// class members of the same name. It makes the derived class more
// efficient to use but unsafe to further derive.
StateId Head_() const override { return Head(); }
void Enqueue_(StateId s) override { Enqueue(s); }
void Dequeue_() override { Dequeue(); }
void Update_(StateId s) override { Update(s); }
bool Empty_() const override { return Empty(); }
void Clear_() override { return Clear(); }
};
// Given a vector that maps from states to weights and a Less
// comparison function object between weights, this class defines a
// comparison function object between states.
template <typename S, typename L>
class StateWeightCompare {
public:
typedef L Less;
typedef typename L::Weight Weight;
typedef S StateId;
StateWeightCompare(const std::vector<Weight> &weights, const L &less)
: weights_(weights), less_(less) {}
bool operator()(const S x, const S y) const {
return less_(weights_[x], weights_[y]);
}
private:
const std::vector<Weight> &weights_;
L less_;
};
// Shortest-first queue discipline, templated on the StateId and Weight, is
// specialized to use the weight's natural order for the comparison function.
template <typename S, typename W>
class NaturalShortestFirstQueue
: public ShortestFirstQueue<S, StateWeightCompare<S, NaturalLess<W>> > {
public:
typedef StateWeightCompare<S, NaturalLess<W>> C;
explicit NaturalShortestFirstQueue(const std::vector<W> &distance)
: ShortestFirstQueue<S, C>(C(distance, less_)) {}
private:
NaturalLess<W> less_;
};
// Topological-order queue discipline, templated on the StateId.
// States are ordered in the queue topologically. The FST must be acyclic.
template <class S>
class TopOrderQueue : public QueueBase<S> {
public:
typedef S StateId;
// This constructor computes the top. order. It accepts an arc filter
// to limit the transitions considered in that computation (e.g., only
// the epsilon graph).
template <class Arc, class ArcFilter>
TopOrderQueue(const Fst<Arc> &fst, ArcFilter filter)
: QueueBase<S>(TOP_ORDER_QUEUE),
front_(0),
back_(kNoStateId),
order_(0),
state_(0) {
bool acyclic;
TopOrderVisitor<Arc> top_order_visitor(&order_, &acyclic);
DfsVisit(fst, &top_order_visitor, filter);
if (!acyclic) {
FSTERROR() << "TopOrderQueue: Fst is not acyclic";
QueueBase<S>::SetError(true);
}
state_.resize(order_.size(), kNoStateId);
}
// This constructor is passed the top. order, useful when we know it
// beforehand.
explicit TopOrderQueue(const std::vector<StateId> &order)
: QueueBase<S>(TOP_ORDER_QUEUE),
front_(0),
back_(kNoStateId),
order_(order),
state_(order.size(), kNoStateId) {}
StateId Head() const { return state_[front_]; }
void Enqueue(StateId s) {
if (front_ > back_)
front_ = back_ = order_[s];
else if (order_[s] > back_)
back_ = order_[s];
else if (order_[s] < front_)
front_ = order_[s];
state_[order_[s]] = s;
}
void Dequeue() {
state_[front_] = kNoStateId;
while ((front_ <= back_) && (state_[front_] == kNoStateId)) ++front_;
}
void Update(StateId s) {}
bool Empty() const { return front_ > back_; }
void Clear() {
for (StateId i = front_; i <= back_; ++i) state_[i] = kNoStateId;
back_ = kNoStateId;
front_ = 0;
}
private:
StateId front_;
StateId back_;
std::vector<StateId> order_;
std::vector<StateId> state_;
// This allows base-class virtual access to non-virtual derived-
// class members of the same name. It makes the derived class more
// efficient to use but unsafe to further derive.
StateId Head_() const override { return Head(); }
void Enqueue_(StateId s) override { Enqueue(s); }
void Dequeue_() override { Dequeue(); }
void Update_(StateId s) override { Update(s); }
bool Empty_() const override { return Empty(); }
void Clear_() override { return Clear(); }
};
// State order queue discipline, templated on the StateId.
// States are ordered in the queue by state Id.
template <class S>
class StateOrderQueue : public QueueBase<S> {
public:
typedef S StateId;
StateOrderQueue()
: QueueBase<S>(STATE_ORDER_QUEUE), front_(0), back_(kNoStateId) {}
StateId Head() const { return front_; }
void Enqueue(StateId s) {
if (front_ > back_)
front_ = back_ = s;
else if (s > back_)
back_ = s;
else if (s < front_)
front_ = s;
while (enqueued_.size() <= s) enqueued_.push_back(false);
enqueued_[s] = true;
}
void Dequeue() {
enqueued_[front_] = false;
while ((front_ <= back_) && (enqueued_[front_] == false)) ++front_;
}
void Update(StateId s) {}
bool Empty() const { return front_ > back_; }
void Clear() {
for (StateId i = front_; i <= back_; ++i) enqueued_[i] = false;
front_ = 0;
back_ = kNoStateId;
}
private:
StateId front_;
StateId back_;
std::vector<bool> enqueued_;
// This allows base-class virtual access to non-virtual derived-
// class members of the same name. It makes the derived class more
// efficient to use but unsafe to further derive.
StateId Head_() const override { return Head(); }
void Enqueue_(StateId s) override { Enqueue(s); }
void Dequeue_() override { Dequeue(); }
void Update_(StateId s) override { Update(s); }
bool Empty_() const override { return Empty(); }
void Clear_() override { return Clear(); }
};
// SCC topological-order meta-queue discipline, templated on the StateId S
// and a queue Q, which is used inside each SCC. It visits the SCC's
// of an FST in topological order. Its constructor is passed the queues to
// to use within an SCC.
template <class S, class Q>
class SccQueue : public QueueBase<S> {
public:
typedef S StateId;
typedef Q Queue;
// Constructor takes a vector specifying the SCC number per state
// and a vector giving the queue to use per SCC number.
SccQueue(const std::vector<StateId> &scc, std::vector<Queue *> *queue)
: QueueBase<S>(SCC_QUEUE),
queue_(queue),
scc_(scc),
front_(0),
back_(kNoStateId) {}
StateId Head() const {
while ((front_ <= back_) &&
(((*queue_)[front_] && (*queue_)[front_]->Empty()) ||
(((*queue_)[front_] == 0) &&
((front_ >= trivial_queue_.size()) ||
(trivial_queue_[front_] == kNoStateId)))))
++front_;
if ((*queue_)[front_])
return (*queue_)[front_]->Head();
else
return trivial_queue_[front_];
}
void Enqueue(StateId s) {
if (front_ > back_)
front_ = back_ = scc_[s];
else if (scc_[s] > back_)
back_ = scc_[s];
else if (scc_[s] < front_)
front_ = scc_[s];
if ((*queue_)[scc_[s]]) {
(*queue_)[scc_[s]]->Enqueue(s);
} else {
while (trivial_queue_.size() <= scc_[s])
trivial_queue_.push_back(kNoStateId);
trivial_queue_[scc_[s]] = s;
}
}
void Dequeue() {
if ((*queue_)[front_])
(*queue_)[front_]->Dequeue();
else if (front_ < trivial_queue_.size())
trivial_queue_[front_] = kNoStateId;
}
void Update(StateId s) {
if ((*queue_)[scc_[s]]) (*queue_)[scc_[s]]->Update(s);
}
bool Empty() const {
if (front_ < back_) // Queue scc # back_ not empty unless back_==front_
return false;
else if (front_ > back_)
return true;
else if ((*queue_)[front_])
return (*queue_)[front_]->Empty();
else
return (front_ >= trivial_queue_.size()) ||
(trivial_queue_[front_] == kNoStateId);
}
void Clear() {
for (StateId i = front_; i <= back_; ++i)
if ((*queue_)[i])
(*queue_)[i]->Clear();
else if (i < trivial_queue_.size())
trivial_queue_[i] = kNoStateId;
front_ = 0;
back_ = kNoStateId;
}
private:
std::vector<Queue *> *queue_;
const std::vector<StateId> &scc_;
mutable StateId front_;
StateId back_;
std::vector<StateId> trivial_queue_;
// This allows base-class virtual access to non-virtual derived-
// class members of the same name. It makes the derived class more
// efficient to use but unsafe to further derive.
StateId Head_() const override { return Head(); }
void Enqueue_(StateId s) override { Enqueue(s); }
void Dequeue_() override { Dequeue(); }
void Update_(StateId s) override { Update(s); }
bool Empty_() const override { return Empty(); }
void Clear_() override { return Clear(); }
DISALLOW_COPY_AND_ASSIGN(SccQueue);
};
// Automatic queue discipline, templated on the StateId. It selects a
// queue discipline for a given FST based on its properties.
template <class S>
class AutoQueue : public QueueBase<S> {
public:
typedef S StateId;
// This constructor takes a state distance vector that, if non-null and if
// the Weight type has the path property, will entertain the
// shortest-first queue using the natural order w.r.t to the distance.
template <class Arc, class ArcFilter>
AutoQueue(const Fst<Arc> &fst,
const std::vector<typename Arc::Weight> *distance, ArcFilter filter)
: QueueBase<S>(AUTO_QUEUE) {
typedef typename Arc::Weight Weight;
typedef StateWeightCompare<StateId, NaturalLess<Weight>> Compare;
// First check if the FST is known to have these properties.
uint64 props =
fst.Properties(kAcyclic | kCyclic | kTopSorted | kUnweighted, false);
if ((props & kTopSorted) || fst.Start() == kNoStateId) {
queue_ = new StateOrderQueue<StateId>();
VLOG(2) << "AutoQueue: using state-order discipline";
} else if (props & kAcyclic) {
queue_ = new TopOrderQueue<StateId>(fst, filter);
VLOG(2) << "AutoQueue: using top-order discipline";
} else if ((props & kUnweighted) && (Weight::Properties() & kIdempotent)) {
queue_ = new LifoQueue<StateId>();
VLOG(2) << "AutoQueue: using LIFO discipline";
} else {
uint64 properties;
// Decompose into strongly-connected components.
SccVisitor<Arc> scc_visitor(&scc_, 0, 0, &properties);
DfsVisit(fst, &scc_visitor, filter);
StateId nscc = *std::max_element(scc_.begin(), scc_.end()) + 1;
std::vector<QueueType> queue_types(nscc);
NaturalLess<Weight> *less = 0;
Compare *comp = 0;
if (distance && (Weight::Properties() & kPath)) {
less = new NaturalLess<Weight>;
comp = new Compare(*distance, *less);
}
// Find the queue type to use per SCC.
bool unweighted;
bool all_trivial;
SccQueueType(fst, scc_, &queue_types, filter, less, &all_trivial,
&unweighted);
// If unweighted and semiring is idempotent, use lifo queue.
if (unweighted) {
queue_ = new LifoQueue<StateId>();
VLOG(2) << "AutoQueue: using LIFO discipline";
delete comp;
delete less;
return;
}
// If all the scc are trivial, FST is acyclic and the scc# gives
// the topological order.
if (all_trivial) {
queue_ = new TopOrderQueue<StateId>(scc_);
VLOG(2) << "AutoQueue: using top-order discipline";
delete comp;
delete less;
return;
}
VLOG(2) << "AutoQueue: using SCC meta-discipline";
queues_.resize(nscc);
for (StateId i = 0; i < nscc; ++i) {
switch (queue_types[i]) {
case TRIVIAL_QUEUE:
queues_[i] = 0;
VLOG(3) << "AutoQueue: SCC #" << i << ": using trivial discipline";
break;
case SHORTEST_FIRST_QUEUE:
queues_[i] = new ShortestFirstQueue<StateId, Compare, false>(*comp);
VLOG(3) << "AutoQueue: SCC #" << i
<< ": using shortest-first discipline";
break;
case LIFO_QUEUE:
queues_[i] = new LifoQueue<StateId>();
VLOG(3) << "AutoQueue: SCC #" << i << ": using LIFO disciplle";
break;
case FIFO_QUEUE:
default:
queues_[i] = new FifoQueue<StateId>();
VLOG(3) << "AutoQueue: SCC #" << i << ": using FIFO disciplle";
break;
}
}
queue_ = new SccQueue<StateId, QueueBase<StateId>>(scc_, &queues_);
delete comp;
delete less;
}
}
~AutoQueue() override {
for (StateId i = 0; i < queues_.size(); ++i) delete queues_[i];
delete queue_;
}
StateId Head() const { return queue_->Head(); }
void Enqueue(StateId s) { queue_->Enqueue(s); }
void Dequeue() { queue_->Dequeue(); }
void Update(StateId s) { queue_->Update(s); }
bool Empty() const { return queue_->Empty(); }
void Clear() { queue_->Clear(); }
private:
QueueBase<StateId> *queue_;
std::vector<QueueBase<StateId> *> queues_;
std::vector<StateId> scc_;
template <class Arc, class ArcFilter, class Less>
static void SccQueueType(const Fst<Arc> &fst, const std::vector<StateId> &scc,
std::vector<QueueType> *queue_types,
ArcFilter filter, Less *less, bool *all_trivial,
bool *unweighted);
// This allows base-class virtual access to non-virtual derived-
// class members of the same name. It makes the derived class more
// efficient to use but unsafe to further derive.
StateId Head_() const override { return Head(); }
void Enqueue_(StateId s) override { Enqueue(s); }
void Dequeue_() override { Dequeue(); }
void Update_(StateId s) override { Update(s); }
bool Empty_() const override { return Empty(); }
void Clear_() override { return Clear(); }
DISALLOW_COPY_AND_ASSIGN(AutoQueue);
};
// Examines the states in an Fst's strongly connected components and
// determines which type of queue to use per SCC. Stores result in
// vector QUEUE_TYPES, which is assumed to have length equal to the
// number of SCCs. An arc filter is used to limit the transitions
// considered (e.g., only the epsilon graph). ALL_TRIVIAL is set
// to true if every queue is the trivial queue. UNWEIGHTED is set to
// true if the semiring is idempotent and all the arc weights are equal to
// Zero() or One().
template <class StateId>
template <class A, class ArcFilter, class Less>
void AutoQueue<StateId>::SccQueueType(const Fst<A> &fst,
const std::vector<StateId> &scc,
std::vector<QueueType> *queue_type,
ArcFilter filter, Less *less,
bool *all_trivial, bool *unweighted) {
typedef A Arc;
typedef typename A::StateId StateId;
typedef typename A::Weight Weight;
*all_trivial = true;
*unweighted = true;
for (StateId i = 0; i < queue_type->size(); ++i)
(*queue_type)[i] = TRIVIAL_QUEUE;
for (StateIterator<Fst<Arc>> sit(fst); !sit.Done(); sit.Next()) {
StateId state = sit.Value();
for (ArcIterator<Fst<Arc>> ait(fst, state); !ait.Done(); ait.Next()) {
const Arc &arc = ait.Value();
if (!filter(arc)) continue;
if (scc[state] == scc[arc.nextstate]) {
QueueType &type = (*queue_type)[scc[state]];
if (!less || ((*less)(arc.weight, Weight::One())))
type = FIFO_QUEUE;
else if ((type == TRIVIAL_QUEUE) || (type == LIFO_QUEUE)) {
if (!(Weight::Properties() & kIdempotent) ||
(arc.weight != Weight::Zero() && arc.weight != Weight::One()))
type = SHORTEST_FIRST_QUEUE;
else
type = LIFO_QUEUE;
}
if (type != TRIVIAL_QUEUE) *all_trivial = false;
}
if (!(Weight::Properties() & kIdempotent) ||
(arc.weight != Weight::Zero() && arc.weight != Weight::One()))
*unweighted = false;
}
}
}
// An A* estimate is a function object that maps from a state ID to a
// an estimate of the shortest distance to the final states.
// The trivial A* estimate is always One().
template <typename S, typename W>
struct TrivialAStarEstimate {
W operator()(S s) const { return W::One(); }
};
// Given a vector that maps from states to weights representing the
// shortest distance from the initial state, a Less comparison
// function object between weights, and an estimate E of the
// shortest distance to the final states, this class defines a
// comparison function object between states.
template <typename S, typename L, typename E>
class AStarWeightCompare {
public:
typedef L Less;
typedef typename L::Weight Weight;
typedef S StateId;
AStarWeightCompare(const std::vector<Weight> &weights, const L &less,
const E &estimate)
: weights_(weights), less_(less), estimate_(estimate) {}
bool operator()(const S x, const S y) const {
Weight wx = Times(weights_[x], estimate_(x));
Weight wy = Times(weights_[y], estimate_(y));
return less_(wx, wy);
}
private:
const std::vector<Weight> &weights_;
L less_;
const E &estimate_;
};
// A* queue discipline, templated on the StateId, Weight and an
// estimate E of the shortest distance to the final states, is specialized
// to use the weight's natural order for the comparison function.
template <typename S, typename W, typename E>
class NaturalAStarQueue
: public ShortestFirstQueue<S, AStarWeightCompare<S, NaturalLess<W>, E>> {
public:
typedef AStarWeightCompare<S, NaturalLess<W>, E> C;
NaturalAStarQueue(const std::vector<W> &distance, const E &estimate)
: ShortestFirstQueue<S, C>(C(distance, less_, estimate)) {}
private:
NaturalLess<W> less_;
};
// A state equivalence class is a function object that
// maps from a state ID to an equivalence class (state) ID.
// The trivial equivalence class maps a state to itself.
template <typename S>
struct TrivialStateEquivClass {
S operator()(S s) const { return s; }
};
// Distance-based pruning queue discipline: Enqueues a state 's'
// only when its shortest distance (so far), as specified by
// 'distance', is less than (as specified by 'comp') the shortest
// distance Times() the 'threshold' to any state in the same
// equivalence class, as specified by the function object
// 'class_func'. The underlying queue discipline is specified by
// 'queue'. The ownership of 'queue' is given to this class.
template <typename Q, typename L, typename C>
class PruneQueue : public QueueBase<typename Q::StateId> {
public:
typedef typename Q::StateId StateId;
typedef typename L::Weight Weight;
PruneQueue(const std::vector<Weight> &distance, Q *queue, L comp,
const C &class_func, Weight threshold)
: QueueBase<StateId>(OTHER_QUEUE),
distance_(distance),
queue_(queue),
less_(comp),
class_func_(class_func),
threshold_(threshold) {}
~PruneQueue() override { delete queue_; }
StateId Head() const { return queue_->Head(); }
void Enqueue(StateId s) {
StateId c = class_func_(s);
if (c >= class_distance_.size())
class_distance_.resize(c + 1, Weight::Zero());
if (less_(distance_[s], class_distance_[c]))
class_distance_[c] = distance_[s];
// Enqueue only if below threshold limit
Weight limit = Times(class_distance_[c], threshold_);
if (less_(distance_[s], limit)) queue_->Enqueue(s);
}
void Dequeue() { queue_->Dequeue(); }
void Update(StateId s) {
StateId c = class_func_(s);
if (less_(distance_[s], class_distance_[c]))
class_distance_[c] = distance_[s];
queue_->Update(s);
}
bool Empty() const { return queue_->Empty(); }
void Clear() { queue_->Clear(); }
private:
// This allows base-class virtual access to non-virtual derived-
// class members of the same name. It makes the derived class more
// efficient to use but unsafe to further derive.
StateId Head_() const override { return Head(); }
void Enqueue_(StateId s) override { Enqueue(s); }
void Dequeue_() override { Dequeue(); }
void Update_(StateId s) override { Update(s); }
bool Empty_() const override { return Empty(); }
void Clear_() override { return Clear(); }
const std::vector<Weight> &distance_; // shortest distance to state
Q *queue_;
L less_;
const C &class_func_; // eqv. class function object
Weight threshold_; // pruning weight threshold
std::vector<Weight> class_distance_; // shortest distance to class
DISALLOW_COPY_AND_ASSIGN(PruneQueue);
};
// Pruning queue discipline (see above) using the weight's natural
// order for the comparison function. The ownership of 'queue' is
// given to this class.
template <typename Q, typename W, typename C>
class NaturalPruneQueue : public PruneQueue<Q, NaturalLess<W>, C> {
public:
typedef typename Q::StateId StateId;
typedef W Weight;
NaturalPruneQueue(const std::vector<W> &distance, Q *queue,
const C &class_func_, Weight threshold)
: PruneQueue<Q, NaturalLess<W>, C>(distance, queue, NaturalLess<W>(),
class_func_, threshold) {}
};
// Filter-based pruning queue discipline: Enqueues a state 's' only
// if allowed by the filter, specified by the function object 'state_filter'.
// The underlying queue discipline is specified by 'queue'. The ownership
// of 'queue' is given to this class.
template <typename Q, typename F>
class FilterQueue : public QueueBase<typename Q::StateId> {
public:
typedef typename Q::StateId StateId;
FilterQueue(Q *queue, const F &state_filter)
: QueueBase<StateId>(OTHER_QUEUE),
queue_(queue),
state_filter_(state_filter) {}
~FilterQueue() override { delete queue_; }
StateId Head() const { return queue_->Head(); }
// Enqueues only if allowed by state filter.
void Enqueue(StateId s) {
if (state_filter_(s)) {
queue_->Enqueue(s);
}
}
void Dequeue() { queue_->Dequeue(); }
void Update(StateId s) {}
bool Empty() const { return queue_->Empty(); }
void Clear() { queue_->Clear(); }
private:
// This allows base-class virtual access to non-virtual derived-
// class members of the same name. It makes the derived class more
// efficient to use but unsafe to further derive.
StateId Head_() const override { return Head(); }
void Enqueue_(StateId s) override { Enqueue(s); }
void Dequeue_() override { Dequeue(); }
void Update_(StateId s) override { Update(s); }
bool Empty_() const override { return Empty(); }
void Clear_() override { return Clear(); }
Q *queue_;
const F &state_filter_; // Filter to prune states
DISALLOW_COPY_AND_ASSIGN(FilterQueue);
};
} // namespace fst
#endif
|