21 #ifndef FST_COMPACT_FST_H_ 22 #define FST_COMPACT_FST_H_ 24 #include <sys/types.h> 53 #include <string_view> 241 template <
class Element,
class Un
signed>
249 template <
class Arc,
class ArcCompactor>
252 template <
class Iterator,
class ArcCompactor>
254 const ArcCompactor &arc_compactor);
258 template <
class ArcCompactor>
261 const ArcCompactor &arc_compactor);
270 Unsigned
States(ssize_t i)
const {
return states_[i]; }
274 const Element &
Compacts(
size_t i)
const {
return compacts_[i]; }
282 ssize_t
Start()
const {
return start_; }
284 bool Error()
const {
return error_; }
287 static const std::string &Type();
290 std::shared_ptr<MappedFile> states_region_;
291 std::shared_ptr<MappedFile> compacts_region_;
293 Unsigned *states_ =
nullptr;
295 Element *compacts_ =
nullptr;
297 size_t ncompacts_ = 0;
303 template <
class Element,
class Un
signed>
304 template <
class Arc,
class ArcCompactor>
306 const Fst<Arc> &
fst,
const ArcCompactor &arc_compactor) {
307 using StateId =
typename Arc::StateId;
308 using Weight =
typename Arc::Weight;
309 start_ = fst.
Start();
314 const auto s = siter.Value();
316 if (fst.
Final(s) != Weight::Zero()) ++nfinals;
318 if (arc_compactor.Size() == -1) {
320 sizeof(states_[0]) * (nstates_ + 1),
alignof(decltype(states_[0]))));
321 states_ =
static_cast<Unsigned *
>(states_region_->mutable_data());
322 ncompacts_ = narcs_ + nfinals;
324 sizeof(compacts_[0]) * ncompacts_,
alignof(decltype(compacts_[0]))));
325 compacts_ =
static_cast<Element *
>(compacts_region_->mutable_data());
326 states_[nstates_] = ncompacts_;
329 ncompacts_ = nstates_ * arc_compactor.Size();
330 if ((narcs_ + nfinals) != ncompacts_) {
331 FSTERROR() <<
"CompactArcStore: ArcCompactor incompatible with FST";
336 sizeof(compacts_[0]) * ncompacts_,
alignof(decltype(compacts_[0]))));
337 compacts_ =
static_cast<Element *
>(compacts_region_->mutable_data());
341 for (
size_t s = 0; s < nstates_; ++s) {
343 if (arc_compactor.Size() == -1) states_[s] = pos;
344 if (fst.
Final(s) != Weight::Zero()) {
345 compacts_[pos++] = arc_compactor.Compact(
349 compacts_[pos++] = arc_compactor.Compact(s, aiter.Value());
351 if ((arc_compactor.Size() != -1) && (pos != fpos + arc_compactor.Size())) {
352 FSTERROR() <<
"CompactArcStore: ArcCompactor incompatible with FST";
357 if (pos != ncompacts_) {
358 FSTERROR() <<
"CompactArcStore: ArcCompactor incompatible with FST";
364 template <
class Element,
class Un
signed>
365 template <
class Iterator,
class ArcCompactor>
367 const Iterator begin,
const Iterator end,
368 const ArcCompactor &arc_compactor) {
369 using Arc =
typename ArcCompactor::Arc;
370 using Weight =
typename Arc::Weight;
371 if (arc_compactor.Size() != -1) {
372 ncompacts_ = std::distance(begin, end);
373 if (arc_compactor.Size() == 1) {
376 if (ncompacts_ == 0) {
380 arc_compactor.Expand(ncompacts_ - 1, *(begin + (ncompacts_ - 1)));
381 if (arc.ilabel !=
kNoLabel) ++ncompacts_;
384 if (ncompacts_ % arc_compactor.Size()) {
385 FSTERROR() <<
"CompactArcStore: Size of input container incompatible" 386 <<
" with arc compactor";
390 if (ncompacts_ == 0)
return;
392 nstates_ = ncompacts_ / arc_compactor.Size();
394 sizeof(compacts_[0]) * ncompacts_,
alignof(decltype(compacts_[0]))));
395 compacts_ =
static_cast<Element *
>(compacts_region_->mutable_data());
398 for (; it != end; ++it, ++i) {
400 if (arc_compactor.Expand(i, *it).ilabel !=
kNoLabel) ++narcs_;
402 if (i < ncompacts_) {
403 compacts_[i] = arc_compactor.Compact(
407 if (std::distance(begin, end) == 0)
return;
410 for (
size_t i = 0; it != end; ++it, ++i) {
411 const auto arc = arc_compactor.Expand(i, *it);
417 if (arc.weight != Weight::Zero()) ++ncompacts_;
422 sizeof(compacts_[0]) * ncompacts_,
alignof(decltype(compacts_[0]))));
423 compacts_ =
static_cast<Element *
>(compacts_region_->mutable_data());
425 sizeof(states_[0]) * (nstates_ + 1),
alignof(decltype(states_[0]))));
426 states_ =
static_cast<Unsigned *
>(states_region_->mutable_data());
427 states_[nstates_] = ncompacts_;
430 for (it = begin; it != end; ++it) {
431 const auto arc = arc_compactor.Expand(i, *it);
433 compacts_[i++] = *it;
436 if (arc.weight != Weight::Zero()) compacts_[i++] = *it;
439 if ((s != nstates_) || (i != ncompacts_)) {
440 FSTERROR() <<
"CompactArcStore: Ill-formed input container";
447 template <
class Element,
class Un
signed>
448 template <
class ArcCompactor>
451 const ArcCompactor &arc_compactor) {
452 auto data = std::make_unique<CompactArcStore>();
453 data->start_ = hdr.
Start();
456 if (arc_compactor.Size() == -1) {
458 LOG(ERROR) <<
"CompactArcStore::Read: Alignment failed: " << opts.
source;
461 auto b = (data->nstates_ + 1) *
sizeof(Unsigned);
464 if (!strm || !data->states_region_) {
465 LOG(ERROR) <<
"CompactArcStore::Read: Read failed: " << opts.
source;
469 static_cast<Unsigned *
>(data->states_region_->mutable_data());
471 data->states_ =
nullptr;
473 data->ncompacts_ = arc_compactor.Size() == -1
474 ? data->states_[data->nstates_]
475 : data->nstates_ * arc_compactor.Size();
477 LOG(ERROR) <<
"CompactArcStore::Read: Alignment failed: " << opts.
source;
480 size_t b = data->ncompacts_ *
sizeof(Element);
481 data->compacts_region_.reset(
483 if (!strm || !data->compacts_region_) {
484 LOG(ERROR) <<
"CompactArcStore::Read: Read failed: " << opts.
source;
488 static_cast<Element *
>(data->compacts_region_->mutable_data());
489 return data.release();
492 template <
class Element,
class Un
signed>
497 LOG(ERROR) <<
"CompactArcStore::Write: Alignment failed: " << opts.
source;
500 strm.write(reinterpret_cast<const char *>(states_),
501 (nstates_ + 1) *
sizeof(Unsigned));
504 LOG(ERROR) <<
"CompactArcStore::Write: Alignment failed: " << opts.
source;
507 strm.write(reinterpret_cast<const char *>(compacts_),
508 ncompacts_ *
sizeof(Element));
511 LOG(ERROR) <<
"CompactArcStore::Write: Write failed: " << opts.
source;
517 template <
class Element,
class Un
signed>
519 static const std::string *
const type =
new std::string(
"compact");
523 template <
class C,
class U,
class S>
529 template <
class AC,
class U,
537 using Arc =
typename AC::Arc;
549 fst, std::make_shared<
ArcCompactor>(std::move(arc_compactor))) {}
552 std::shared_ptr<ArcCompactor> arc_compactor)
553 : arc_compactor_(std::move(arc_compactor)),
554 compact_store_(std::make_shared<S>(fst, *arc_compactor_)) {}
557 std::shared_ptr<CompactArcCompactor> compactor)
558 : arc_compactor_(compactor->arc_compactor_),
559 compact_store_(compactor->compact_store_ == nullptr
560 ? std::make_shared<S>(fst, *arc_compactor_)
561 : compactor->compact_store_) {}
565 std::shared_ptr<CompactStore> compact_store)
566 : arc_compactor_(std::move(arc_compactor)),
567 compact_store_(std::move(compact_store)) {}
591 template <
class Iterator>
593 std::shared_ptr<ArcCompactor> arc_compactor)
594 : arc_compactor_(std::move(arc_compactor)),
595 compact_store_(std::make_shared<S>(b, e, *arc_compactor_)) {}
597 template <
class Iterator>
606 compactor.GetArcCompactor() == nullptr
608 : std::make_shared<
ArcCompactor>(*compactor.GetArcCompactor())),
609 compact_store_(compactor.GetCompactStore() == nullptr
612 *compactor.GetCompactStore())) {}
614 template <
class OtherC>
618 compactor.GetArcCompactor() == nullptr
620 : std::make_shared<
ArcCompactor>(*compactor.GetArcCompactor())),
621 compact_store_(compactor.GetCompactStore() == nullptr
624 *compactor.GetCompactStore())) {}
628 size_t NumArcs()
const {
return compact_store_->NumArcs(); }
637 std::shared_ptr<ArcCompactor> arc_compactor(ArcCompactor::Read(strm));
638 if (arc_compactor ==
nullptr)
return nullptr;
639 std::shared_ptr<S> compact_store(S::Read(strm, opts, hdr, *arc_compactor));
640 if (compact_store ==
nullptr)
return nullptr;
645 return arc_compactor_->Write(strm) && compact_store_->Write(strm, opts);
652 return arc_compactor_->Properties() | props;
656 return arc_compactor_->Compatible(fst);
659 bool Error()
const {
return compact_store_->Error(); }
663 static const std::string &
Type() {
664 static const std::string *
const type = [] {
665 std::string type =
"compact";
666 if (
sizeof(U) !=
sizeof(uint32_t)) type += std::to_string(8 *
sizeof(U));
668 type += ArcCompactor::Type();
669 if (CompactStore::Type() !=
"compact") {
671 type += CompactStore::Type();
673 return new std::string(type);
689 return arc_compactor_->Expand(s, compact_store_->Compacts(i), flags);
693 std::pair<Unsigned, Unsigned> CompactsRange(
StateId s)
const {
694 std::pair<size_t, size_t> range;
695 if (HasFixedOutdegree()) {
696 range.first = s * arc_compactor_->Size();
697 range.second = arc_compactor_->Size();
699 range.first = compact_store_->States(s);
700 range.second = compact_store_->States(s + 1) - range.first;
706 std::shared_ptr<ArcCompactor> arc_compactor_;
707 std::shared_ptr<CompactStore> compact_store_;
712 template <
class ArcCompactor,
class U,
class S>
715 using Arc =
typename ArcCompactor::Arc;
723 : compactor_(compactor),
725 range_(compactor->CompactsRange(s)),
727 range_.second != 0 &&
737 compactor_ = compactor;
739 range_ = compactor->CompactsRange(s);
740 if (range_.second != 0 &&
754 if (!has_final_)
return Weight::Zero();
755 return compactor_->ComputeArc(s_, range_.first - 1,
kArcWeightValue).weight;
758 size_t NumArcs()
const {
return range_.second; }
761 return compactor_->ComputeArc(s_, range_.first + i, flags);
767 std::pair<U, U> range_ = {0, 0};
768 bool has_final_ =
false;
772 template <
class ArcCompactor,
class U>
776 using Arc =
typename ArcCompactor::Arc;
785 : arc_compactor_(compactor->GetArcCompactor()), s_(s) {
799 if (!has_final_)
return Weight::Zero();
800 return arc_compactor_->Expand(s_, *(compacts_ - 1),
kArcWeightValue).weight;
806 return arc_compactor_->Expand(s_, compacts_[i], flags);
814 offset = store->States(s_);
815 num_arcs_ = store->States(s_ + 1) - offset;
817 offset = s_ * arc_compactor_->Size();
818 num_arcs_ = arc_compactor_->Size();
821 compacts_ = &(store->Compacts(offset));
832 const ArcCompactor *arc_compactor_ =
nullptr;
833 const typename ArcCompactor::Element *compacts_ =
837 bool has_final_ =
false;
840 template <
class F,
class G>
841 void Cast(
const F &, G *);
843 template <
class CompactArcFST,
class FST>
846 const typename CompactArcFST::Compactor::ArcCompactor &arc_compactor,
854 template <
class Arc,
class C,
class CacheStore = DefaultCacheStore<Arc>>
856 :
public CacheBaseImpl<typename CacheStore::State, CacheStore> {
870 using ImplBase::HasArcs;
871 using ImplBase::HasFinal;
872 using ImplBase::HasStart;
873 using ImplBase::PushArc;
874 using ImplBase::SetArcs;
875 using ImplBase::SetFinal;
876 using ImplBase::SetStart;
880 compactor_(std::make_shared<
Compactor>()) {
892 compactor_(std::make_shared<
Compactor>(fst, std::move(compactor))) {
897 uint64_t copy_properties =
903 if ((copy_properties &
kError) || !compactor_->IsCompatible(fst)) {
904 FSTERROR() <<
"CompactFstImpl: Input Fst incompatible with compactor";
905 SetProperties(kError, kError);
908 SetProperties(compactor_->Properties(copy_properties) | kStaticProperties);
913 :
ImplBase(opts), compactor_(std::move(compactor)) {
915 SetProperties(kStaticProperties | compactor_->Properties(0));
923 compactor_(impl.compactor_ == nullptr
925 : std::make_shared<
Compactor>(*impl.compactor_)) {
933 template <
class OtherCacheStore>
937 compactor_(impl.compactor_ == nullptr
939 : std::make_shared<
Compactor>(*impl.compactor_)) {
947 if (!HasStart()) SetStart(compactor_->Start());
948 return ImplBase::Start();
953 compactor_->SetState(s, &state_);
954 return state_.Final();
958 if (Properties(
kError))
return 0;
959 return compactor_->NumStates();
964 compactor_->SetState(s, &state_);
965 return state_.NumArcs();
971 return CountEpsilons(s,
false);
977 return CountEpsilons(s,
true);
981 compactor_->SetState(s, &state_);
984 const size_t num_arcs = state_.NumArcs();
985 for (
size_t i = 0; i < num_arcs; ++i) {
986 const auto &arc = state_.GetArc(i, flags);
987 const auto label = output_epsilons ? arc.olabel : arc.ilabel;
990 }
else if (label > 0) {
998 auto impl = std::make_unique<CompactFstImpl>();
1000 if (!impl->ReadHeader(strm, opts, kMinFileVersion, &hdr)) {
1004 if (hdr.Version() == kAlignedFileVersion) {
1008 std::shared_ptr<Compactor>(Compactor::Read(strm, opts, hdr));
1009 if (!impl->compactor_) {
1012 return impl.release();
1021 const auto file_version = opts.
align ? kAlignedFileVersion : kFileVersion;
1022 WriteHeader(strm, opts, file_version, &hdr);
1023 return compactor_->Write(strm, opts);
1028 data->
base =
nullptr;
1029 data->
nstates = compactor_->NumStates();
1033 if (!HasArcs(s))
Expand(s);
1034 ImplBase::InitArcIterator(s, data);
1038 compactor_->SetState(s, &state_);
1039 const size_t num_arcs = state_.NumArcs();
1040 for (
size_t i = 0; i < num_arcs; ++i)
1043 if (!HasFinal(s)) SetFinal(s, state_.Final());
1053 compactor_ = std::move(compactor);
1060 template <
class OtherArc,
class OtherCompactor,
class OtherCacheStore>
1072 template <
class CompactArcFST,
class FST>
1075 const typename CompactArcFST::Compactor::ArcCompactor &arc_compactor,
1079 static constexpr
int kFileVersion = 2;
1081 static constexpr
int kAlignedFileVersion = 1;
1083 static constexpr
int kMinFileVersion = 1;
1085 std::shared_ptr<Compactor> compactor_;
1086 typename Compactor::State state_;
1092 template <
class Compactor,
class Arc>
1097 template <
class Compactor,
class Arc>
1107 template <
class A,
class C,
class CacheStore>
1111 template <
class F,
class G>
1112 void friend Cast(
const F &, G *);
1136 std::make_shared<
Impl>(fst, std::move(compactor), opts)) {}
1152 std::make_shared<
Impl>(std::move(compactor), opts)) {}
1165 auto *impl = Impl::Read(strm, opts);
1166 return impl ?
new CompactFst(std::shared_ptr<Impl>(impl)) :
nullptr;
1173 return impl ?
new CompactFst(std::shared_ptr<Impl>(impl)) :
nullptr;
1177 return GetImpl()->Write(strm, opts);
1180 bool Write(
const std::string &source)
const override {
1185 GetImpl()->InitStateIterator(data);
1189 GetMutableImpl()->InitArcIterator(s, data);
1199 GetMutableImpl()->SetCompactor(std::move(compactor));
1206 explicit CompactFst(std::shared_ptr<Impl> impl)
1214 template <
class CompactArcFST,
class FST>
1217 const typename CompactArcFST::Compactor::ArcCompactor &arc_compactor,
1219 using Arc =
typename CompactArcFST::Arc;
1220 using Compactor =
typename CompactArcFST::Compactor;
1221 using ArcCompactor =
typename Compactor::ArcCompactor;
1222 using CompactStore =
typename Compactor::CompactStore;
1223 using Element =
typename ArcCompactor::Element;
1224 using Impl =
typename CompactArcFST::Impl;
1225 using Unsigned =
typename Compactor::Unsigned;
1226 using Weight =
typename Arc::Weight;
1227 const auto file_version =
1228 opts.
align ? Impl::kAlignedFileVersion : Impl::kFileVersion;
1229 size_t num_arcs = -1;
1230 size_t num_states = -1;
1231 auto first_pass_arc_compactor = arc_compactor;
1235 if (
const Compactor *
const compactor =
1236 internal::GetCompactor<Compactor>(fst)) {
1237 num_arcs = compactor->NumArcs();
1238 num_states = compactor->NumStates();
1239 first_pass_arc_compactor = *compactor->GetArcCompactor();
1248 const auto s = siter.Value();
1250 if (fst.Final(s) != Weight::Zero()) {
1251 first_pass_arc_compactor.Compact(
1256 first_pass_arc_compactor.Compact(s, aiter.Value());
1264 std::string type =
"compact";
1265 if (
sizeof(Unsigned) !=
sizeof(uint32_t)) {
1266 type += std::to_string(CHAR_BIT *
sizeof(Unsigned));
1269 type += ArcCompactor::Type();
1270 if (CompactStore::Type() !=
"compact") {
1272 type += CompactStore::Type();
1275 if ((copy_properties &
kError) || !arc_compactor.Compatible(fst)) {
1276 FSTERROR() <<
"Fst incompatible with compactor";
1279 uint64_t properties = copy_properties | Impl::kStaticProperties;
1282 first_pass_arc_compactor.Write(strm);
1283 if (first_pass_arc_compactor.Size() == -1) {
1285 LOG(ERROR) <<
"WriteCompactArcFst: Alignment failed: " << opts.
source;
1288 Unsigned compacts = 0;
1290 const auto s = siter.Value();
1291 strm.write(reinterpret_cast<const char *>(&compacts),
sizeof(compacts));
1292 if (fst.Final(s) != Weight::Zero()) {
1295 compacts += fst.NumArcs(s);
1297 strm.write(reinterpret_cast<const char *>(&compacts),
sizeof(compacts));
1300 LOG(ERROR) <<
"Could not align file during write after writing states";
1302 const auto &second_pass_arc_compactor = arc_compactor;
1305 const auto s = siter.Value();
1306 if (fst.Final(s) != Weight::Zero()) {
1307 element = second_pass_arc_compactor.Compact(
1309 strm.write(reinterpret_cast<const char *>(&element),
sizeof(element));
1312 element = second_pass_arc_compactor.Compact(s, aiter.Value());
1313 strm.write(reinterpret_cast<const char *>(&element),
sizeof(element));
1318 LOG(ERROR) <<
"WriteCompactArcFst: Write failed: " << opts.
source;
1326 template <
class Arc,
class Compactor,
class CacheStore>
1332 : nstates_(fst.NumStates()), s_(0) {}
1334 bool Done()
const {
return s_ >= nstates_; }
1349 template <
class Arc,
class Compactor,
class CacheStore>
1353 using State =
typename Compactor::State;
1356 : state_(fst.GetMutableImpl()->MutableCompactor(), s),
1361 bool Done()
const {
return pos_ >= num_arcs_; }
1364 arc_ = state_.GetArc(pos_, flags_);
1374 void Seek(
size_t pos) { pos_ = pos; }
1376 uint8_t
Flags()
const {
return flags_; }
1410 constexpr ssize_t
Size()
const {
return 1; }
1415 const auto props = Properties();
1420 static const std::string *
const type =
new std::string(
"string");
1424 bool Write(std::ostream &strm)
const {
return true; }
1443 return std::make_pair(arc.ilabel, arc.weight);
1448 return Arc(p.first, p.first, p.second,
1452 constexpr ssize_t
Size()
const {
return 1; }
1457 const auto props = Properties();
1462 static const std::string *
const type =
new std::string(
"weighted_string");
1466 bool Write(std::ostream &strm)
const {
return true; }
1485 return std::make_pair(arc.ilabel, arc.nextstate);
1490 return Arc(p.first, p.first, Weight::One(), p.second);
1493 constexpr ssize_t
Size()
const {
return -1; }
1498 const auto props = Properties();
1503 static const std::string *
const type =
1504 new std::string(
"unweighted_acceptor");
1508 bool Write(std::ostream &strm)
const {
return true; }
1527 return std::make_pair(std::make_pair(arc.ilabel, arc.weight),
1533 return Arc(p.first.first, p.first.first, p.first.second, p.second);
1536 constexpr ssize_t
Size()
const {
return -1; }
1541 const auto props = Properties();
1546 static const std::string *
const type =
new std::string(
"acceptor");
1550 bool Write(std::ostream &strm)
const {
return true; }
1569 return std::make_pair(std::make_pair(arc.ilabel, arc.olabel),
1575 return Arc(p.first.first, p.first.second, Weight::One(), p.second);
1578 constexpr ssize_t
Size()
const {
return -1; }
1583 const auto props = Properties();
1588 static const std::string *
const type =
new std::string(
"unweighted");
1592 bool Write(std::ostream &strm)
const {
return true; }
1599 template <
class Arc,
class Unsigned >
1602 template <
class Arc,
class Unsigned >
1606 template <
class Arc,
class Unsigned >
1609 template <
class Arc,
class Unsigned >
1613 template <
class Arc,
class Unsigned >
1630 template <
class Arc,
class Un
signed = u
int32_t,
class LabelIterator>
1632 const LabelIterator begin,
const LabelIterator end) {
1638 template <
class LabelIterator>
1640 const LabelIterator end) {
1641 return MakeCompactStringFst<StdArc>(begin, end);
1646 #endif // FST_COMPACT_FST_H_
static Impl * Read(std::istream &strm, const FstReadOptions &opts)
ssize_t NumOutputEpsilons(const ExpandedFst< Arc > &fst, typename Arc::StateId s)
CompactFstImpl(const Fst< Arc > &fst, std::shared_ptr< Compactor > compactor, const CompactFstOptions &opts)
constexpr uint64_t Properties() const
CompactArcCompactor(const Fst< Arc > &fst, std::shared_ptr< ArcCompactor > arc_compactor)
Compactor * MutableCompactor()
bool AlignInput(std::istream &strm, size_t align=MappedFile::kArchAlignment)
void InitArcIterator(StateId s, ArcIteratorData< Arc > *data) const override
const Element & Compacts(size_t i) const
constexpr uint64_t kMutable
StdCompactStringFst MakeStdCompactStringFst(const LabelIterator begin, const LabelIterator end)
constexpr ssize_t Size() const
constexpr uint64_t kWeightedCycles
constexpr uint64_t Properties() const
std::pair< std::pair< Label, Label >, StateId > Element
constexpr uint8_t kArcValueFlags
CompactFst(const CompactFst &fst, bool safe=false)
static const std::string & Type()
typename Arc::Weight Weight
static UnweightedCompactor * Read(std::istream &strm)
Arc GetArc(size_t i, uint8_t flags) const
CompactStore * MutableCompactStore()
void Cast(const F &, G *)
bool IsCompatible(const Fst< Arc > &fst) const
bool Compatible(const Fst< Arc > &fst) const
virtual uint64_t Properties(uint64_t mask, bool test) const =0
const Compactor * GetCompactor() const
typename Arc::StateId StateId
typename Arc::Label Label
Arc Expand(StateId s, const Element &p, uint8_t flags=kArcValueFlags) const
static const std::string & Type()
Arc GetArc(size_t i, uint8_t flags) const
CompactFst(const Fst< Arc > &fst, const CompactFstOptions &opts=CompactFstOptions())
std::shared_ptr< CompactStore > SharedCompactStore()
CompactFstOptions(const CacheOptions &opts)
virtual size_t NumArcs(StateId) const =0
Element Compact(StateId s, const Arc &arc) const
CompactFstImpl(std::shared_ptr< Compactor > compactor, const CompactFstOptions &opts)
void Set(const Compactor *compactor, StateId s)
Arc Expand(StateId s, const Element &p, uint8_t flags=kArcValueFlags) const
bool Write(std::ostream &strm) const
Element Compact(StateId s, const Arc &arc) const
bool Write(std::ostream &strm) const
CompactArcCompactor(const CompactArcCompactor &compactor)
CompactStringFst< Arc, Unsigned > MakeCompactStringFst(const LabelIterator begin, const LabelIterator end)
Arc::Weight Final(const ExpandedFst< Arc > &fst, typename Arc::StateId s)
CompactArcFst< Arc, StringCompactor< Arc >, Unsigned > CompactStringFst
const SymbolTable * OutputSymbols() const
bool Write(std::ostream &strm) const
constexpr uint64_t Properties() const
StateId GetStateId() const
typename Arc::Label Label
constexpr uint64_t kError
void InitArcIterator(StateId s, ArcIteratorData< Arc > *data)
CompactFst(std::shared_ptr< Compactor > compactor, const CompactFstOptions &opts=CompactFstOptions())
static MappedFile * Map(std::istream &istrm, bool memorymap, const std::string &source, size_t size)
typename Arc::StateId StateId
constexpr uint64_t kCompiledStringProperties
virtual Weight Final(StateId) const =0
StateIterator(const CompactFst< Arc, Compactor, CacheStore > &fst)
CompactFst(const Fst< Arc > &fst, Compactor &&compactor, const CompactFstOptions &opts=CompactFstOptions())
typename Arc::StateId StateId
constexpr uint64_t kUnweightedCycles
CompactArcCompactor(const Fst< Arc > &fst, ArcCompactor &&arc_compactor=ArcCompactor())
bool AlignOutput(std::ostream &strm, size_t align=MappedFile::kArchAlignment)
CompactArcStore()=default
static AcceptorCompactor * Read(std::istream &strm)
static WeightedStringCompactor * Read(std::istream &strm)
constexpr uint8_t kArcILabelValue
ssize_t NumArcs(const ExpandedFst< Arc > &fst, typename Arc::StateId s)
CompactFstImpl(const CompactFstImpl &impl)
Arc Expand(StateId s, const Element &p, uint8_t flags=kArcValueFlags) const
std::shared_ptr< ArcCompactor > SharedArcCompactor()
std::unique_ptr< T > WrapUnique(T *ptr)
static CompactFst * Read(std::istream &strm, const FstReadOptions &opts)
MatcherBase< Arc > * InitMatcher(MatchType match_type) const override
typename ArcCompactor::Arc Arc
typename Arc::Weight Weight
CompactFstImpl(const CompactFstImpl< OtherArc, OtherCompactor, OtherCacheStore > &impl)
ArcCompactor * MutableArcCompactor()
typename Arc::Weight Weight
typename ArcCompactor::Arc Arc
void SetState(StateId s, State *state) const
static MappedFile * Allocate(size_t size, size_t align=kArchAlignment)
std::pair< Label, StateId > Element
CompactArcCompactor(std::shared_ptr< ArcCompactor > arc_compactor, std::shared_ptr< CompactStore > compact_store)
static const std::string & Type()
size_t NumOutputEpsilons(StateId s)
constexpr uint64_t Properties() const
static void WriteFstHeader(const Fst< Arc > &fst, std::ostream &strm, const FstWriteOptions &opts, int version, std::string_view type, uint64_t properties, FstHeader *hdr)
static UnweightedAcceptorCompactor * Read(std::istream &istrm)
Unsigned States(ssize_t i) const
CompactFst(const Fst< Arc > &fst, std::shared_ptr< Compactor > compactor, const CompactFstOptions &opts=CompactFstOptions())
ssize_t NumInputEpsilons(const ExpandedFst< Arc > &fst, typename Arc::StateId s)
std::pair< Label, Weight > Element
virtual uint64_t Properties() const
typename Arc::Label Label
bool Compatible(const Fst< Arc > &fst) const
size_t NumCompacts() const
typename Arc::Weight Weight
constexpr uint64_t kOLabelSorted
constexpr uint64_t kCopyProperties
void SetFlags(uint8_t flags, uint8_t mask)
CompactArcCompactor(const Iterator b, const Iterator e, std::shared_ptr< ArcCompactor > arc_compactor)
CompactArcCompactor(const CompactArcCompactor< OtherC, U, S > &compactor)
Arc Expand(StateId s, const Element &p, uint8_t flags=kArcValueFlags) const
uint64_t CheckProperties(const Fst< Arc > &fst, uint64_t check_mask, uint64_t test_mask)
bool Compatible(const Fst< Arc > &fst) const
constexpr ssize_t Size() const
Arc Expand(StateId s, const Element &p, uint8_t flags=kArcValueFlags) const
constexpr uint8_t kArcOLabelValue
virtual StateId Start() const =0
typename Arc::StateId StateId
constexpr ssize_t Size() const
static StringCompactor * Read(std::istream &strm)
std::unique_ptr< StateIteratorBase< Arc > > base
ArcIterator(const CompactFst< Arc, Compactor, CacheStore > &fst, StateId s)
StateId NumStates() const
typename Arc::Weight Weight
typename Arc::StateId StateId
typename Arc::Weight Weight
typename Arc::StateId StateId
bool WriteCompactArcFst(const FST &fst, const typename CompactArcFST::Compactor::ArcCompactor &arc_compactor, std::ostream &strm, const FstWriteOptions &opts)
typename Arc::Label Label
size_t NumInputEpsilons(StateId s)
bool Write(std::ostream &strm, const FstWriteOptions &opts) const
constexpr uint64_t kNullProperties
Element Compact(StateId s, const Arc &arc) const
typename Arc::Weight Weight
const std::string & Type() const
CompactArcCompactor(const Fst< Arc > &fst, std::shared_ptr< CompactArcCompactor > compactor)
CompactFstImpl(const CompactFstImpl< Arc, Compactor, OtherCacheStore > &impl)
bool HasFixedOutdegree() const
std::pair< std::pair< Label, Weight >, StateId > Element
bool WriteFile(const std::string &source) const
constexpr uint8_t kArcWeightValue
constexpr uint64_t kILabelSorted
CompactFst * Copy(bool safe=false) const override
static CompactFstImpl * Read(std::istream &strm, const FstReadOptions &opts)
constexpr uint64_t kUnweighted
typename Arc::Weight Weight
virtual const SymbolTable * InputSymbols() const =0
const SymbolTable * InputSymbols() const
void SetCompactor(std::shared_ptr< Compactor > compactor)
bool Write(std::ostream &strm, const FstWriteOptions &opts) const override
Element Compact(StateId s, const Arc &arc) const
typename Arc::Weight Weight
CompactArcCompactor(const Iterator b, const Iterator e)
const ArcCompactor * GetArcCompactor() const
typename Arc::StateId StateId
static CompactArcStore * Read(std::istream &strm, const FstReadOptions &opts, const FstHeader &hdr, const ArcCompactor &arc_compactor)
void SetCompactor(std::shared_ptr< Compactor > compactor)
typename Arc::StateId StateId
std::shared_ptr< Compactor > SharedCompactor()
typename AC::Element Element
typename Arc::StateId StateId
const Arc & Value() const
constexpr uint64_t kString
uint64_t Properties(uint64_t props) const
typename Arc::StateId StateId
size_t NumArcs(StateId s)
void Expand(const Fst< Arc > &ifst, const std::vector< std::pair< typename Arc::Label, typename Arc::Label >> &parens, const std::vector< typename Arc::Label > &assignments, MutableFst< Arc > *ofst, const MPdtExpandOptions &opts)
const Compactor * GetCompactor() const
void InitStateIterator(StateIteratorData< Arc > *data) const
bool Write(std::ostream &strm, const FstWriteOptions &opts) const
static const std::string & Type()
bool Write(std::ostream &strm) const
typename Arc::StateId StateId
CompactArcState(const Compactor *compactor, StateId s)
Element Compact(StateId s, const Arc &arc) const
constexpr ssize_t Size() const
constexpr uint64_t kExpanded
void Set(const Compactor *compactor, StateId s)
static CompactArcCompactor * Read(std::istream &strm, const FstReadOptions &opts, const FstHeader &hdr)
bool Compatible(const Fst< Arc > &fst) const
Arc ComputeArc(StateId s, Unsigned i, uint8_t flags) const
void InitStateIterator(StateIteratorData< Arc > *data) const override
constexpr ssize_t Size() const
StateId GetStateId() const
StateId NumStates() const
CompactArcState(const Compactor *compactor, StateId s)
typename Arc::Label Label
bool Write(std::ostream &strm, const FstWriteOptions &opts) const
const CompactStore * GetCompactStore() const
bool Write(std::ostream &strm) const
const Compactor * GetCompactor(const Fst< Arc > &fst)
static const std::string & Type()
static const std::string & Type()
static CompactFst * Read(std::string_view source)
size_t CountEpsilons(StateId s, bool output_epsilons)
constexpr uint64_t Properties() const
bool Compatible(const Fst< Arc > &fst) const
constexpr uint64_t kAcceptor
typename Compactor::State State
typename Arc::StateId StateId
static const std::string & Type()
bool Write(const std::string &source) const override
virtual const SymbolTable * OutputSymbols() const =0