Blame view
tools/openfst-1.6.7/include/fst/script/arciterator-class.h
5.67 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 |
// See www.openfst.org for extensive documentation on this weighted // finite-state transducer library. #ifndef FST_SCRIPT_ARCITERATOR_CLASS_H_ #define FST_SCRIPT_ARCITERATOR_CLASS_H_ #include <memory> #include <utility> #include <fst/fstlib.h> #include <fst/script/fst-class.h> // Scripting API support for ArcIterator. // // A call to Value() causes the underlying arc to be used to construct the // associated ArcClass. namespace fst { namespace script { // Non-mutable arc iterators. // Virtual interface implemented by each concrete ArcIteratorImpl<F>. class ArcIteratorImplBase { public: virtual bool Done() const = 0; virtual uint32 Flags() const = 0; virtual void Next() = 0; virtual size_t Position() const = 0; virtual void Reset() = 0; virtual void Seek(size_t a) = 0; virtual void SetFlags(uint32 flags, uint32 mask) = 0; virtual ArcClass Value() const = 0; virtual ~ArcIteratorImplBase() {} }; // Templated implementation. template <class Arc> class ArcIteratorClassImpl : public ArcIteratorImplBase { public: explicit ArcIteratorClassImpl(const Fst<Arc> &fst, int64 s) : aiter_(fst, s) {} bool Done() const final { return aiter_.Done(); } uint32 Flags() const final { return aiter_.Flags(); } void Next() final { aiter_.Next(); } size_t Position() const final { return aiter_.Position(); } void Reset() final { aiter_.Reset(); } void Seek(size_t a) final { aiter_.Seek(a); } void SetFlags(uint32 flags, uint32 mask) final { aiter_.SetFlags(flags, mask); } // This is returned by value because it has not yet been constructed, and // is likely to participate in return-value optimization. ArcClass Value() const final { return ArcClass(aiter_.Value()); } ~ArcIteratorClassImpl() final {} private: ArcIterator<Fst<Arc>> aiter_; }; class ArcIteratorClass; using InitArcIteratorClassArgs = std::tuple<const FstClass &, int64, ArcIteratorClass *>; // Untemplated user-facing class holding a templated pimpl. class ArcIteratorClass { public: ArcIteratorClass(const FstClass &fst, int64 s); template <class Arc> ArcIteratorClass(const Fst<Arc> &fst, int64 s) : impl_(new ArcIteratorClassImpl<Arc>(fst, s)) {} bool Done() const { return impl_->Done(); } uint32 Flags() const { return impl_->Flags(); } void Next() { impl_->Next(); } size_t Position() const { return impl_->Position(); } void Reset() { impl_->Reset(); } void Seek(size_t a) { impl_->Seek(a); } void SetFlags(uint32 flags, uint32 mask) { impl_->SetFlags(flags, mask); } ArcClass Value() const { return impl_->Value(); } template <class Arc> friend void InitArcIteratorClass(InitArcIteratorClassArgs *args); private: std::unique_ptr<ArcIteratorImplBase> impl_; }; template <class Arc> void InitArcIteratorClass(InitArcIteratorClassArgs *args) { const Fst<Arc> &fst = *(std::get<0>(*args).GetFst<Arc>()); std::get<2>(*args)->impl_.reset( new ArcIteratorClassImpl<Arc>(fst, std::get<1>(*args))); } // Mutable arc iterators. // Virtual interface implemented by each concrete MutableArcIteratorImpl<F>. class MutableArcIteratorImplBase : public ArcIteratorImplBase { public: virtual void SetValue(const ArcClass &) = 0; ~MutableArcIteratorImplBase() override {} }; // Templated implementation. template <class Arc> class MutableArcIteratorClassImpl : public MutableArcIteratorImplBase { public: explicit MutableArcIteratorClassImpl(MutableFst<Arc> *fst, int64 s) : aiter_(fst, s) {} bool Done() const final { return aiter_.Done(); } uint32 Flags() const final { return aiter_.Flags(); } void Next() final { aiter_.Next(); } size_t Position() const final { return aiter_.Position(); } void Reset() final { aiter_.Reset(); } void Seek(size_t a) final { aiter_.Seek(a); } void SetFlags(uint32 flags, uint32 mask) final { aiter_.SetFlags(flags, mask); } void SetValue(const Arc &arc) { aiter_.SetValue(arc); } void SetValue(const ArcClass &ac) final { aiter_.SetValue(ac.GetArc<Arc>()); } // This is returned by value because it has not yet been constructed, and // is likely to participate in return-value optimization. ArcClass Value() const final { return ArcClass(aiter_.Value()); } ~MutableArcIteratorClassImpl() override {} private: MutableArcIterator<MutableFst<Arc>> aiter_; }; class MutableArcIteratorClass; using InitMutableArcIteratorClassArgs = std::tuple<MutableFstClass *, int64, MutableArcIteratorClass *>; // Untemplated user-facing class holding a templated pimpl. class MutableArcIteratorClass { public: MutableArcIteratorClass(MutableFstClass *fst, int64 s); template <class Arc> MutableArcIteratorClass(MutableFst<Arc> *fst, int64 s) : impl_(new MutableArcIteratorClassImpl<Arc>(fst, s)) {} bool Done() const { return impl_->Done(); } uint32 Flags() const { return impl_->Flags(); } void Next() { impl_->Next(); } size_t Position() const { return impl_->Position(); } void Reset() { impl_->Reset(); } void Seek(size_t a) { impl_->Seek(a); } void SetFlags(uint32 flags, uint32 mask) { impl_->SetFlags(flags, mask); } void SetValue(const ArcClass &ac) { impl_->SetValue(ac); } ArcClass Value() const { return impl_->Value(); } template <class Arc> friend void InitMutableArcIteratorClass( InitMutableArcIteratorClassArgs *args); private: std::unique_ptr<MutableArcIteratorImplBase> impl_; }; template <class Arc> void InitMutableArcIteratorClass(InitMutableArcIteratorClassArgs *args) { MutableFst<Arc> *fst = std::get<0>(*args)->GetMutableFst<Arc>(); std::get<2>(*args)->impl_.reset( new MutableArcIteratorClassImpl<Arc>(fst, std::get<1>(*args))); } } // namespace script } // namespace fst #endif // FST_SCRIPT_ARCITERATOR_CLASS_H_ |