FST  openfst-1.8.2.post1
OpenFst Library
collection.h
Go to the documentation of this file.
1 // Copyright 2005-2020 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the 'License');
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an 'AS IS' BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 //
15 // See www.openfst.org for extensive documentation on this weighted
16 // finite-state transducer library.
17 //
18 // Class to store a collection of ordered (multi-)sets with elements of type T.
19 
20 #ifndef FST_EXTENSIONS_PDT_COLLECTION_H_
21 #define FST_EXTENSIONS_PDT_COLLECTION_H_
22 
23 #include <functional>
24 #include <vector>
25 
26 #include <fst/log.h>
27 #include <fst/bi-table.h>
28 
29 namespace fst {
30 
31 // Stores a collection of non-empty, ordered (multi-)sets with elements of type
32 // T. A default constructor, operator==, and an STL-style hash functor must be
33 // defined on the elements. Provides signed integer ID (of type I) for each
34 // unique set. The IDs are allocated starting from 0 in order.
35 template <class I, class T>
36 class Collection {
37  public:
38  struct Node { // Trie node.
39  I node_id; // Root is kNoNodeId;
41 
42  Node() : node_id(kNoNodeId), element(T()) {}
43 
44  Node(I i, const T &t) : node_id(i), element(t) {}
45 
46  bool operator==(const Node &n) const {
47  return n.node_id == node_id && n.element == element;
48  }
49  };
50 
51  struct NodeHash {
52  size_t operator()(const Node &n) const {
53  static constexpr auto kPrime = 7853;
54  return n.node_id + hash_(n.element) * kPrime;
55  }
56  };
57 
59 
60  class SetIterator {
61  public:
62  SetIterator(I id, Node node, NodeTable *node_table)
63  : id_(id), node_(node), node_table_(node_table) {}
64 
65  bool Done() const { return id_ == kNoNodeId; }
66 
67  const T &Element() const { return node_.element; }
68 
69  void Next() {
70  id_ = node_.node_id;
71  if (id_ != kNoNodeId) node_ = node_table_->FindEntry(id_);
72  }
73 
74  private:
75  I id_; // Iterator set node ID.
76  Node node_; // Iterator set node.
77  NodeTable *node_table_;
78  };
79 
81 
82  // Looks up integer ID from ordered multi-se, and if it doesn't exist and
83  // insert is true, then adds it. Otherwise returns -1.
84  I FindId(const std::vector<T> &set, bool insert = true) {
85  I node_id = kNoNodeId;
86  for (ssize_t i = set.size() - 1; i >= 0; --i) {
87  Node node(node_id, set[i]);
88  node_id = node_table_.FindId(node, insert);
89  if (node_id == -1) break;
90  }
91  return node_id;
92  }
93 
94  // Finds ordered (multi-)set given integer ID. Returns set iterator to
95  // traverse result.
96  SetIterator FindSet(I id) {
97  if (id < 0 || id >= node_table_.Size()) {
98  return SetIterator(kNoNodeId, Node(kNoNodeId, T()), &node_table_);
99  } else {
100  return SetIterator(id, node_table_.FindEntry(id), &node_table_);
101  }
102  }
103 
104  I Size() const { return node_table_.Size(); }
105 
106  private:
107  static constexpr I kNoNodeId = -1;
108  static const std::hash<T> hash_;
109 
110  NodeTable node_table_;
111 };
112 
113 template <class I, class T>
114 const std::hash<T> Collection<I, T>::hash_ = {};
115 
116 } // namespace fst
117 
118 #endif // FST_EXTENSIONS_PDT_COLLECTION_H_
I FindId(const std::vector< T > &set, bool insert=true)
Definition: collection.h:84
size_t operator()(const Node &n) const
Definition: collection.h:52
SetIterator(I id, Node node, NodeTable *node_table)
Definition: collection.h:62
SetIterator FindSet(I id)
Definition: collection.h:96
I Size() const
Definition: collection.h:104
Node(I i, const T &t)
Definition: collection.h:44
const T & Element() const
Definition: collection.h:67
bool operator==(const Node &n) const
Definition: collection.h:46