FST  openfst-1.8.3
OpenFst Library
collection.h
Go to the documentation of this file.
1 // Copyright 2005-2024 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 <sys/types.h>
24 
25 #include <cstddef>
26 #include <functional>
27 #include <vector>
28 
29 #include <fst/log.h>
30 #include <fst/bi-table.h>
31 
32 namespace fst {
33 
34 // Stores a collection of non-empty, ordered (multi-)sets with elements of type
35 // T. A default constructor, operator==, and an STL-style hash functor must be
36 // defined on the elements. Provides signed integer ID (of type I) for each
37 // unique set. The IDs are allocated starting from 0 in order.
38 template <class I, class T>
39 class Collection {
40  public:
41  struct Node { // Trie node.
42  I node_id; // Root is kNoNodeId;
44 
45  Node() : node_id(kNoNodeId), element(T()) {}
46 
47  Node(I i, const T &t) : node_id(i), element(t) {}
48 
49  bool operator==(const Node &n) const {
50  return n.node_id == node_id && n.element == element;
51  }
52  };
53 
54  struct NodeHash {
55  size_t operator()(const Node &n) const {
56  static constexpr auto kPrime = 7853;
57  return n.node_id + hash_(n.element) * kPrime;
58  }
59  };
60 
62 
63  class SetIterator {
64  public:
65  SetIterator(I id, Node node, NodeTable *node_table)
66  : id_(id), node_(node), node_table_(node_table) {}
67 
68  bool Done() const { return id_ == kNoNodeId; }
69 
70  const T &Element() const { return node_.element; }
71 
72  void Next() {
73  id_ = node_.node_id;
74  if (id_ != kNoNodeId) node_ = node_table_->FindEntry(id_);
75  }
76 
77  private:
78  I id_; // Iterator set node ID.
79  Node node_; // Iterator set node.
80  NodeTable *node_table_;
81  };
82 
83  Collection() = default;
84 
85  // Looks up integer ID from ordered multi-se, and if it doesn't exist and
86  // insert is true, then adds it. Otherwise returns -1.
87  I FindId(const std::vector<T> &set, bool insert = true) {
88  I node_id = kNoNodeId;
89  for (ssize_t i = set.size() - 1; i >= 0; --i) {
90  Node node(node_id, set[i]);
91  node_id = node_table_.FindId(node, insert);
92  if (node_id == -1) break;
93  }
94  return node_id;
95  }
96 
97  // Finds ordered (multi-)set given integer ID. Returns set iterator to
98  // traverse result.
99  SetIterator FindSet(I id) {
100  if (id < 0 || id >= node_table_.Size()) {
101  return SetIterator(kNoNodeId, Node(kNoNodeId, T()), &node_table_);
102  } else {
103  return SetIterator(id, node_table_.FindEntry(id), &node_table_);
104  }
105  }
106 
107  I Size() const { return node_table_.Size(); }
108 
109  private:
110  static constexpr I kNoNodeId = -1;
111  static const std::hash<T> hash_;
112 
113  NodeTable node_table_;
114 };
115 
116 template <class I, class T>
117 const std::hash<T> Collection<I, T>::hash_ = {};
118 
119 } // namespace fst
120 
121 #endif // FST_EXTENSIONS_PDT_COLLECTION_H_
I FindId(const std::vector< T > &set, bool insert=true)
Definition: collection.h:87
size_t operator()(const Node &n) const
Definition: collection.h:55
SetIterator(I id, Node node, NodeTable *node_table)
Definition: collection.h:65
SetIterator FindSet(I id)
Definition: collection.h:99
I Size() const
Definition: collection.h:107
Node(I i, const T &t)
Definition: collection.h:47
const T & Element() const
Definition: collection.h:70
bool operator==(const Node &n) const
Definition: collection.h:49
Collection()=default