18 #ifndef FST_EXTENSIONS_LINEAR_TRIE_H_ 19 #define FST_EXTENSIONS_LINEAR_TRIE_H_ 27 #include <unordered_map> 34 template <
class L,
class H>
36 template <
class L,
class H>
49 return parent == that.
parent && label == that.
label;
52 std::istream &
Read(std::istream &strm) {
58 std::ostream &
Write(std::ostream &strm)
const {
65 template <
class L,
class H>
68 return static_cast<size_t>(pl.
parent * 7853 + H()(pl.
label));
74 template <
class L,
class H>
79 typedef std::unordered_map<L, int, H>
NextMap;
106 return ptr_ == that.ptr_ && cur_node_ == that.cur_node_ &&
107 cur_edge_ == that.cur_edge_;
110 return !(*
this == that);
115 : ptr_(ptr), cur_node_(cur_node) {
119 void SetProperCurEdge() {
120 if (cur_node_ < ptr_->NumNodes())
121 cur_edge_ = ptr_->nodes_[cur_node_]->begin();
123 cur_edge_ = ptr_->nodes_[0]->begin();
128 stub_.second = cur_edge_->second;
133 typename NextMap::const_iterator cur_edge_;
148 int Find(
int parent,
const L &label)
const;
151 std::istream &
Read(std::istream &strm);
152 std::ostream &
Write(std::ostream &strm)
const;
159 std::vector<std::unique_ptr<NextMap>> nodes_;
162 template <
class L,
class H>
164 nodes_.push_back(std::make_unique<NextMap>());
167 template <
class L,
class H>
169 nodes_.reserve(that.nodes_.size());
170 for (
const auto &node : that.nodes_) {
171 nodes_.push_back(std::make_unique<NextMap>(*node));
176 template <
class L,
class H>
178 nodes_.swap(that.nodes_);
181 template <
class L,
class H>
189 template <
class L,
class H>
192 if (NumNodes() != that.
NumNodes())
return false;
193 for (
int i = 0; i < NumNodes(); ++i)
194 if (ChildrenOf(i) != that.
ChildrenOf(i))
return false;
198 template <
class L,
class H>
201 return !(*
this == that);
204 template <
class L,
class H>
206 int ret = Find(parent, label);
207 if (ret == kNoTrieNodeId) {
209 (*nodes_[
parent])[label] = ret;
210 nodes_.push_back(std::make_unique<NextMap>());
215 template <
class L,
class H>
217 typename NextMap::const_iterator it = nodes_[
parent]->find(label);
218 return it == nodes_[
parent]->end() ? kNoTrieNodeId : it->second;
221 template <
class L,
class H>
225 if (!
ReadType(strm, &num_nodes))
return strm;
226 for (
size_t i = 1; i < num_nodes; ++i) {
227 new_trie.nodes_.push_back(std::make_unique<NextMap>());
229 for (
size_t i = 0; i < num_nodes; ++i) {
230 ReadType(strm, new_trie.nodes_[i].get());
232 if (strm) swap(new_trie);
236 template <
class L,
class H>
239 for (
size_t i = 0; i < NumNodes(); ++i)
WriteType(strm, *nodes_[i]);
243 template <
class L,
class H>
247 if (cur_edge_ == ptr_->nodes_[cur_node_]->end()) {
249 while (cur_node_ < ptr_->NumNodes() && ptr_->nodes_[cur_node_]->empty())
256 template <
class L,
class H>
266 template <
class L,
class H>
287 return next_ == that.next_;
290 return !(*
this == that);
294 size_t NumNodes()
const {
return next_.size() + 1; }
296 int Find(
int parent,
const L &label)
const;
298 std::istream &
Read(std::istream &strm) {
return ReadType(strm, &next_); }
299 std::ostream &
Write(std::ostream &strm)
const {
303 const_iterator
begin()
const {
return next_.begin(); }
304 const_iterator
end()
const {
return next_.end(); }
310 template <
class L,
class H>
313 : next_(that.begin(), that.end()) {}
315 template <
class L,
class H>
317 int ret =
Find(parent, label);
318 if (ret == kNoTrieNodeId) {
325 template <
class L,
class H>
327 typename NextMap::const_iterator it =
329 return it == next_.end() ? kNoTrieNodeId : it->second;
375 template <
class L,
class V,
class T>
378 template <
class LL,
class VV,
class TT>
392 : topology_(that.topology_), values_(that.values_) {}
396 topology_.swap(that.topology_);
397 values_.swap(that.values_);
400 int Root()
const {
return topology_.Root(); }
401 size_t NumNodes()
const {
return topology_.NumNodes(); }
407 int ret = topology_.Insert(parent, label);
414 int Find(
int parent,
const L &label)
const {
415 return topology_.Find(parent, label);
422 const V &
operator[](
int node_id)
const {
return values_[node_id]; }
426 return topology_ == that.topology_ && values_ == that.values_;
431 std::istream &
Read(std::istream &strm) {
436 std::ostream &
Write(std::ostream &strm)
const {
444 std::vector<V> values_;
449 #endif // FST_EXTENSIONS_LINEAR_TRIE_H_
int Insert(int parent, const L &label)
const_iterator begin() const
V & operator[](int node_id)
MutableTrie(const MutableTrie< L, V, S > &that)
bool operator!=(const NestedTrieTopology &that) const
const V & operator[](int node_id) const
std::istream & Read(std::istream &strm)
NestedTrieTopology & operator=(const NestedTrieTopology &that)
const_iterator & operator++()
NextMap::const_iterator const_iterator
FlatTrieTopology(const FlatTrieTopology &that)
bool operator!=(const const_iterator &that) const
const_iterator begin() const
std::ostream & WriteType(std::ostream &strm, const T t)
std::ostream & Write(std::ostream &strm) const
bool operator==(const NestedTrieTopology &that) const
const value_type & reference
bool operator!=(const ErrorWeight &, const ErrorWeight &)
size_t operator()(const ParentLabel< L > &pl) const
std::ptrdiff_t difference_type
constexpr int kNoTrieNodeId
std::forward_iterator_tag iterator_category
std::ostream & Write(std::ostream &strm) const
std::ostream & Write(std::ostream &strm) const
std::istream & Read(std::istream &strm)
const value_type * pointer
void swap(NestedTrieTopology &that)
const_iterator end() const
std::ostream & Write(std::ostream &strm) const
int Find(int parent, const L &label) const
std::istream & Read(std::istream &strm)
int Insert(int parent, const L &label)
const NextMap & ChildrenOf(int parent) const
bool operator==(const const_iterator &that) const
const_iterator end() const
std::unordered_map< L, int, H > NextMap
std::pair< ParentLabel< L >, int > value_type
bool operator!=(const FlatTrieTopology &that) const
std::istream & ReadType(std::istream &strm, T *t)
const T & TrieTopology() const
int Find(int parent, const L &label) const
std::istream & Read(std::istream &strm)
int Insert(int parent, const L &label)
void swap(FlatTrieTopology &that)
bool operator==(const ParentLabel &that) const
bool operator==(const FlatTrieTopology &that) const
void swap(MutableTrie &that)
bool operator==(const MutableTrie &that) const
bool operator!=(const MutableTrie &that) const
int Find(int parent, const L &label) const