43 #ifndef FST_EXPANDER_CACHE_H_ 44 #define FST_EXPANDER_CACHE_H_ 54 #include <unordered_map> 55 #include <unordered_map> 69 final_weight_ = Weight::Zero();
81 size_t NumArcs()
const {
return arcs_.size(); }
83 const Arc &
GetArc(
size_t n)
const {
return arcs_[n]; }
85 const Arc *
Arcs()
const {
return arcs_.empty() ?
nullptr : &arcs_[0]; }
92 if (arc.ilabel == 0) ++niepsilons_;
93 if (arc.olabel == 0) ++noepsilons_;
98 if (arc.ilabel == 0) ++niepsilons_;
99 if (arc.olabel == 0) ++noepsilons_;
100 arcs_.push_back(std::move(arc));
106 Weight final_weight_ = Weight::Zero();
107 size_t niepsilons_ = 0;
108 size_t noepsilons_ = 0;
109 std::vector<Arc> arcs_;
137 : state_(new
State(*copy.state_)) {}
139 template <
class Expander>
141 if (state_id == state_id_)
return state_.get();
142 if (state_->ref_count_ > 0) cache_[state_id_] = std::move(state_);
143 state_id_ = state_id;
144 if (cache_.empty()) {
146 expander.Expand(state_id_, state_.get());
149 if (
auto i = cache_.find(state_id_); i != cache_.end()) {
150 state_ = std::move(i->second);
152 if (state_ ==
nullptr) {
153 state_ = std::make_unique<State>();
154 expander.Expand(state_id_, state_.get());
161 std::unordered_map<StateId, std::unique_ptr<State>>
cache_;
175 for (
const auto &[
id, state] : copy.cache_) {
176 cache_[id] = std::make_unique<State>(*state);
183 template <
class Expander>
185 auto [it, inserted] = cache_.emplace(state_id,
nullptr);
187 it->second = std::make_unique<State>();
188 expander.Expand(state_id, it->second.get());
190 return it->second.get();
194 std::unordered_map<StateId, std::unique_ptr<State>> cache_;
210 vec_.resize(copy.vec_.size());
211 for (
StateId i = 0; i < copy.vec_.size(); ++i) {
212 const auto *state = copy.vec_[i];
213 if (state !=
nullptr) {
214 states_.emplace_back(*state);
215 vec_[i] = &states_.back();
221 template <
class Expander>
223 if (state_id >= vec_.size()) vec_.resize(state_id + 1);
224 auto **slot = &vec_[state_id];
225 if (*slot ==
nullptr) {
226 states_.emplace_back();
227 *slot = &states_.back();
228 expander.Expand(state_id, *slot);
234 std::deque<State> states_;
235 std::vector<State *> vec_;
238 template <
class Expander>
242 #endif // FST_EXPANDER_CACHE_H_ int * MutableRefCount() const
const Arc & GetArc(size_t n) const
typename Arc::StateId StateId
void SetFinal(Weight weight)
VectorExpanderCache(const VectorExpanderCache ©)
typename Arc::StateId StateId
typename Arc::StateId StateId
HashExpanderCache & operator=(const HashExpanderCache ©)
State * FindOrExpand(Expander &expander, StateId state_id)
typename Arc::Weight Weight
VectorExpanderCache & operator=(const VectorExpanderCache ©)
NoGcKeepOneExpanderCache(const NoGcKeepOneExpanderCache ©)
typename Arc::StateId StateId
State * FindOrExpand(Expander &expander, StateId state_id)
std::unordered_map< StateId, std::unique_ptr< State > > cache_
size_t NumOutputEpsilons() const
size_t NumInputEpsilons() const
void AddArc(const Arc &arc)
State * FindOrExpand(Expander &expander, StateId state_id)
NoGcKeepOneExpanderCache()
void ReserveArcs(size_t n)
std::unique_ptr< State > state_
HashExpanderCache(const HashExpanderCache ©)