18 #ifndef FST_EXTENSIONS_LINEAR_TRIE_H_ 19 #define FST_EXTENSIONS_LINEAR_TRIE_H_ 31 #include <unordered_map> 37 template <
class L,
class H>
40 template <
class L,
class H>
53 return parent == that.
parent && label == that.
label;
56 std::istream &
Read(std::istream &strm) {
62 std::ostream &
Write(std::ostream &strm)
const {
69 template <
class L,
class H>
72 return static_cast<size_t>(pl.
parent * 7853 + H()(pl.
label));
78 template <
class L,
class H>
83 typedef std::unordered_map<L, int, H>
NextMap;
110 return ptr_ == that.ptr_ && cur_node_ == that.cur_node_ &&
111 cur_edge_ == that.cur_edge_;
114 return !(*
this == that);
119 : ptr_(ptr), cur_node_(cur_node) {
123 void SetProperCurEdge() {
124 if (cur_node_ < ptr_->NumNodes())
125 cur_edge_ = ptr_->nodes_[cur_node_]->begin();
127 cur_edge_ = ptr_->nodes_[0]->begin();
132 stub_.second = cur_edge_->second;
137 typename NextMap::const_iterator cur_edge_;
152 int Find(
int parent,
const L &label)
const;
155 std::istream &
Read(std::istream &strm);
156 std::ostream &
Write(std::ostream &strm)
const;
163 std::vector<std::unique_ptr<NextMap>> nodes_;
166 template <
class L,
class H>
168 nodes_.push_back(std::make_unique<NextMap>());
171 template <
class L,
class H>
173 nodes_.reserve(that.nodes_.size());
174 for (
const auto &node : that.nodes_) {
175 nodes_.push_back(std::make_unique<NextMap>(*node));
180 template <
class L,
class H>
182 nodes_.swap(that.nodes_);
185 template <
class L,
class H>
193 template <
class L,
class H>
196 if (NumNodes() != that.
NumNodes())
return false;
197 for (
int i = 0; i < NumNodes(); ++i)
198 if (ChildrenOf(i) != that.
ChildrenOf(i))
return false;
202 template <
class L,
class H>
205 return !(*
this == that);
208 template <
class L,
class H>
210 int ret = Find(parent, label);
211 if (ret == kNoTrieNodeId) {
213 (*nodes_[
parent])[label] = ret;
214 nodes_.push_back(std::make_unique<NextMap>());
219 template <
class L,
class H>
221 typename NextMap::const_iterator it = nodes_[
parent]->find(label);
222 return it == nodes_[
parent]->end() ? kNoTrieNodeId : it->second;
225 template <
class L,
class H>
229 if (!
ReadType(strm, &num_nodes))
return strm;
230 for (
size_t i = 1; i < num_nodes; ++i) {
231 new_trie.nodes_.push_back(std::make_unique<NextMap>());
233 for (
size_t i = 0; i < num_nodes; ++i) {
234 ReadType(strm, new_trie.nodes_[i].get());
236 if (strm) swap(new_trie);
240 template <
class L,
class H>
243 for (
size_t i = 0; i < NumNodes(); ++i)
WriteType(strm, *nodes_[i]);
247 template <
class L,
class H>
251 if (cur_edge_ == ptr_->nodes_[cur_node_]->end()) {
253 while (cur_node_ < ptr_->NumNodes() && ptr_->nodes_[cur_node_]->empty())
260 template <
class L,
class H>
270 template <
class L,
class H>
291 return next_ == that.next_;
294 return !(*
this == that);
298 size_t NumNodes()
const {
return next_.size() + 1; }
300 int Find(
int parent,
const L &label)
const;
302 std::istream &
Read(std::istream &strm) {
return ReadType(strm, &next_); }
303 std::ostream &
Write(std::ostream &strm)
const {
307 const_iterator
begin()
const {
return next_.begin(); }
308 const_iterator
end()
const {
return next_.end(); }
314 template <
class L,
class H>
317 : next_(that.begin(), that.end()) {}
319 template <
class L,
class H>
321 int ret =
Find(parent, label);
322 if (ret == kNoTrieNodeId) {
329 template <
class L,
class H>
331 typename NextMap::const_iterator it =
333 return it == next_.end() ? kNoTrieNodeId : it->second;
379 template <
class L,
class V,
class T>
382 template <
class LL,
class VV,
class TT>
396 : topology_(that.topology_), values_(that.values_) {}
400 topology_.swap(that.topology_);
401 values_.swap(that.values_);
404 int Root()
const {
return topology_.Root(); }
405 size_t NumNodes()
const {
return topology_.NumNodes(); }
411 int ret = topology_.Insert(parent, label);
418 int Find(
int parent,
const L &label)
const {
419 return topology_.Find(parent, label);
426 const V &
operator[](
int node_id)
const {
return values_[node_id]; }
430 return topology_ == that.topology_ && values_ == that.values_;
435 std::istream &
Read(std::istream &strm) {
440 std::ostream &
Write(std::ostream &strm)
const {
448 std::vector<V> values_;
453 #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)
FlatTrieTopology()=default
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