FST  openfst-1.7.2
OpenFst Library
arciterator-class.h
Go to the documentation of this file.
1 // See www.openfst.org for extensive documentation on this weighted
2 // finite-state transducer library.
3 
4 #ifndef FST_SCRIPT_ARCITERATOR_CLASS_H_
5 #define FST_SCRIPT_ARCITERATOR_CLASS_H_
6 
7 #include <memory>
8 #include <utility>
9 
10 #include <fst/fstlib.h>
11 #include <fst/script/fst-class.h>
12 
13 // Scripting API support for ArcIterator.
14 //
15 // A call to Value() causes the underlying arc to be used to construct the
16 // associated ArcClass.
17 
18 namespace fst {
19 namespace script {
20 
21 // Non-mutable arc iterators.
22 
23 // Virtual interface implemented by each concrete ArcIteratorImpl<F>.
25  public:
26  virtual bool Done() const = 0;
27  virtual uint32 Flags() const = 0;
28  virtual void Next() = 0;
29  virtual size_t Position() const = 0;
30  virtual void Reset() = 0;
31  virtual void Seek(size_t a) = 0;
32  virtual void SetFlags(uint32 flags, uint32 mask) = 0;
33  virtual ArcClass Value() const = 0;
34  virtual ~ArcIteratorImplBase() {}
35 };
36 
37 // Templated implementation.
38 template <class Arc>
40  public:
41  explicit ArcIteratorClassImpl(const Fst<Arc> &fst, int64 s)
42  : aiter_(fst, s) {}
43 
44  bool Done() const final { return aiter_.Done(); }
45 
46  uint32 Flags() const final { return aiter_.Flags(); }
47 
48  void Next() final { aiter_.Next(); }
49 
50  size_t Position() const final { return aiter_.Position(); }
51 
52  void Reset() final { aiter_.Reset(); }
53 
54  void Seek(size_t a) final { aiter_.Seek(a); }
55 
56  void SetFlags(uint32 flags, uint32 mask) final {
57  aiter_.SetFlags(flags, mask);
58  }
59 
60  // This is returned by value because it has not yet been constructed, and
61  // is likely to participate in return-value optimization.
62  ArcClass Value() const final { return ArcClass(aiter_.Value()); }
63 
65 
66  private:
67  ArcIterator<Fst<Arc>> aiter_;
68 };
69 
70 class ArcIteratorClass;
71 
73  std::tuple<const FstClass &, int64, ArcIteratorClass *>;
74 
75 // Untemplated user-facing class holding a templated pimpl.
77  public:
79 
80  template <class Arc>
82  : impl_(new ArcIteratorClassImpl<Arc>(fst, s)) {}
83 
84  bool Done() const { return impl_->Done(); }
85 
86  uint32 Flags() const { return impl_->Flags(); }
87 
88  void Next() { impl_->Next(); }
89 
90  size_t Position() const { return impl_->Position(); }
91 
92  void Reset() { impl_->Reset(); }
93 
94  void Seek(size_t a) { impl_->Seek(a); }
95 
96  void SetFlags(uint32 flags, uint32 mask) { impl_->SetFlags(flags, mask); }
97 
98  ArcClass Value() const { return impl_->Value(); }
99 
100  template <class Arc>
102 
103  private:
104  std::unique_ptr<ArcIteratorImplBase> impl_;
105 };
106 
107 template <class Arc>
109  const Fst<Arc> &fst = *(std::get<0>(*args).GetFst<Arc>());
110  std::get<2>(*args)->impl_.reset(
111  new ArcIteratorClassImpl<Arc>(fst, std::get<1>(*args)));
112 }
113 
114 // Mutable arc iterators.
115 
116 // Virtual interface implemented by each concrete MutableArcIteratorImpl<F>.
118  public:
119  virtual void SetValue(const ArcClass &) = 0;
120 
122 };
123 
124 // Templated implementation.
125 template <class Arc>
127  : public MutableArcIteratorImplBase {
128  public:
130  : aiter_(fst, s) {}
131 
132  bool Done() const final { return aiter_.Done(); }
133 
134  uint32 Flags() const final { return aiter_.Flags(); }
135 
136  void Next() final { aiter_.Next(); }
137 
138  size_t Position() const final { return aiter_.Position(); }
139 
140  void Reset() final { aiter_.Reset(); }
141 
142  void Seek(size_t a) final { aiter_.Seek(a); }
143 
144  void SetFlags(uint32 flags, uint32 mask) final {
145  aiter_.SetFlags(flags, mask);
146  }
147 
148  void SetValue(const Arc &arc) { aiter_.SetValue(arc); }
149 
150  void SetValue(const ArcClass &ac) final { aiter_.SetValue(ac.GetArc<Arc>()); }
151 
152  // This is returned by value because it has not yet been constructed, and
153  // is likely to participate in return-value optimization.
154  ArcClass Value() const final { return ArcClass(aiter_.Value()); }
155 
157 
158  private:
160 };
161 
163 
165  std::tuple<MutableFstClass *, int64, MutableArcIteratorClass *>;
166 
167 // Untemplated user-facing class holding a templated pimpl.
169  public:
171 
172  template <class Arc>
174  : impl_(new MutableArcIteratorClassImpl<Arc>(fst, s)) {}
175 
176  bool Done() const { return impl_->Done(); }
177 
178  uint32 Flags() const { return impl_->Flags(); }
179 
180  void Next() { impl_->Next(); }
181 
182  size_t Position() const { return impl_->Position(); }
183 
184  void Reset() { impl_->Reset(); }
185 
186  void Seek(size_t a) { impl_->Seek(a); }
187 
188  void SetFlags(uint32 flags, uint32 mask) { impl_->SetFlags(flags, mask); }
189 
190  void SetValue(const ArcClass &ac) { impl_->SetValue(ac); }
191 
192  ArcClass Value() const { return impl_->Value(); }
193 
194  template <class Arc>
195  friend void InitMutableArcIteratorClass(
197 
198  private:
199  std::unique_ptr<MutableArcIteratorImplBase> impl_;
200 };
201 
202 template <class Arc>
204  MutableFst<Arc> *fst = std::get<0>(*args)->GetMutableFst<Arc>();
205  std::get<2>(*args)->impl_.reset(
206  new MutableArcIteratorClassImpl<Arc>(fst, std::get<1>(*args)));
207 }
208 
209 } // namespace script
210 } // namespace fst
211 
212 #endif // FST_SCRIPT_ARCITERATOR_CLASS_H_
void SetFlags(uint32 flags, uint32 mask) final
MutableArcIteratorClass(MutableFst< Arc > *fst, int64 s)
MutableArcIteratorClassImpl(MutableFst< Arc > *fst, int64 s)
ArcIteratorClassImpl(const Fst< Arc > &fst, int64 s)
int64_t int64
Definition: types.h:27
std::tuple< MutableFstClass *, int64, MutableArcIteratorClass * > InitMutableArcIteratorClassArgs
void SetFlags(uint32 flags, uint32 mask)
virtual bool Done() const =0
virtual void SetFlags(uint32 flags, uint32 mask)=0
virtual uint32 Flags() const =0
uint32_t uint32
Definition: types.h:31
void SetValue(const ArcClass &ac) final
virtual size_t Position() const =0
virtual ArcClass Value() const =0
void InitArcIteratorClass(InitArcIteratorClassArgs *args)
void SetFlags(uint32 flags, uint32 mask)
void SetFlags(uint32 flags, uint32 mask) final
virtual void Seek(size_t a)=0
std::tuple< const FstClass &, int64, ArcIteratorClass * > InitArcIteratorClassArgs
ArcIteratorClass(const Fst< Arc > &fst, int64 s)
void InitMutableArcIteratorClass(InitMutableArcIteratorClassArgs *args)