/usr/include/fst/sparse-tuple-weight.h is in libfst-dev 1.6.3-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 | // See www.openfst.org for extensive documentation on this weighted
// finite-state transducer library.
//
// Sparse version of tuple-weight, based on tuple-weight.h.
// Internally stores sparse key, value pairs in linked list. The default value
// element is the assumed value of unset keys. Internal singleton
// implementation that stores first key, value pair as a initialized member
// variable to avoid unnecessary allocation on heap. Use
// SparseTupleWeightIterator to iterate through the key,value pairs. Note:
// this does NOT iterate through the default value.
//
// Sparse tuple weight set operation definitions.
#ifndef FST_LIB_SPARSE_TUPLE_WEIGHT_H_
#define FST_LIB_SPARSE_TUPLE_WEIGHT_H_
#include <list>
#include <stack>
#include <string>
#include <unordered_map>
#include <utility>
#include <fst/weight.h>
namespace fst {
template <class W, class K>
class SparseTupleWeightIterator;
// Arbitrary dimension tuple weight, stored as a sorted linked-list.
// W is any weight class, and K is the key value type. kNoKey (-1) is reserved
// for internal use.
template <class W, class K = int>
class SparseTupleWeight {
public:
using ReverseWeight = SparseTupleWeight<typename W::ReverseWeight, K>;
using Pair = std::pair<K, W>;
constexpr static K kNoKey = -1;
SparseTupleWeight() { Init(); }
template <class Iterator>
SparseTupleWeight(Iterator begin, Iterator end) {
Init();
// Assumes input iterator is sorted.
for (auto it = begin; it != end; ++it) Push(*it);
}
SparseTupleWeight(const K &key, const W &weight) {
Init();
Push(key, weight);
}
explicit SparseTupleWeight(const W &weight) { Init(weight); }
SparseTupleWeight(const SparseTupleWeight &weight) {
Init(weight.DefaultValue());
SetDefaultValue(weight.DefaultValue());
for (SparseTupleWeightIterator<W, K> it(weight); !it.Done(); it.Next()) {
Push(it.Value());
}
}
static const SparseTupleWeight &Zero() {
static const SparseTupleWeight zero(W::Zero());
return zero;
}
static const SparseTupleWeight &One() {
static const SparseTupleWeight one(W::One());
return one;
}
static const SparseTupleWeight &NoWeight() {
static const SparseTupleWeight no_weight(W::NoWeight());
return no_weight;
}
std::istream &Read(std::istream &strm) {
ReadType(strm, &default_);
ReadType(strm, &first_);
return ReadType(strm, &rest_);
}
std::ostream &Write(std::ostream &strm) const {
WriteType(strm, default_);
WriteType(strm, first_);
return WriteType(strm, rest_);
}
SparseTupleWeight &operator=(const SparseTupleWeight &weight) {
if (this == &weight) return *this; // Checks for identity.
Init(weight.DefaultValue());
for (SparseTupleWeightIterator<W, K> it(weight); !it.Done(); it.Next()) {
Push(it.Value());
}
return *this;
}
bool Member() const {
if (!DefaultValue().Member()) return false;
for (SparseTupleWeightIterator<W, K> it(*this); !it.Done(); it.Next()) {
if (!it.Value().second.Member()) return false;
}
return true;
}
// Assumes H() function exists for the hash of the key value.
size_t Hash() const {
size_t h = 0;
static const std::hash<K> H;
for (SparseTupleWeightIterator<W, K> it(*this); !it.Done(); it.Next()) {
h = 5 * h + H(it.Value().first);
h = 13 * h + it.Value().second.Hash();
}
return h;
}
SparseTupleWeight Quantize(float delta = kDelta) const {
SparseTupleWeight weight;
for (SparseTupleWeightIterator<W, K> it(*this); !it.Done(); it.Next()) {
weight.Push(it.Value().first, it.Value().second.Quantize(delta));
}
return weight;
}
ReverseWeight Reverse() const {
SparseTupleWeight weight;
for (SparseTupleWeightIterator<W, K> it(*this); !it.Done(); it.Next()) {
weight.Push(it.Value().first, it.Value().second.Reverse());
}
return ReverseWeight(weight);
}
void Init(const W &default_value = W::Zero()) {
first_.first = kNoKey;
// Initialized to the reserved key value.
default_ = default_value;
rest_.clear();
}
size_t Size() const {
if (first_.first == kNoKey) {
return 0;
} else {
return rest_.size() + 1;
}
}
inline void Push(const K &key, const W &weight,
bool default_value_check = true) {
Push(std::make_pair(key, weight), default_value_check);
}
inline void Push(const Pair &pair, bool default_value_check = true) {
if (default_value_check && pair.second == default_) return;
if (first_.first == kNoKey) {
first_ = pair;
} else {
rest_.push_back(pair);
}
}
void SetDefaultValue(const W &value) { default_ = value; }
const W &DefaultValue() const { return default_; }
private:
// Assumed default value of uninitialized keys, by default W::Zero().
W default_;
// Key values pairs are first stored in first_, then fill rest_ this way we
// can avoid dynamic allocation in the common case where the weight is a
// single key/value pair.
Pair first_;
std::list<Pair> rest_;
friend class SparseTupleWeightIterator<W, K>;
};
template <class W, class K>
class SparseTupleWeightIterator {
public:
using Pair = typename SparseTupleWeight<W, K>::Pair;
using const_iterator = typename std::list<Pair>::const_iterator;
using iterator = typename std::list<Pair>::iterator;
explicit SparseTupleWeightIterator(const SparseTupleWeight<W, K> &weight)
: first_(weight.first_),
rest_(weight.rest_),
init_(true),
iter_(rest_.begin()) {}
bool Done() const {
if (init_) {
return first_.first == SparseTupleWeight<W, K>::kNoKey;
} else {
return iter_ == rest_.end();
}
}
const Pair &Value() const { return init_ ? first_ : *iter_; }
void Next() {
if (init_) {
init_ = false;
} else {
++iter_;
}
}
void Reset() {
init_ = true;
iter_ = rest_.begin();
}
private:
const Pair &first_;
const std::list<Pair> &rest_;
bool init_; // In the initialized state?
const_iterator iter_;
};
template <class W, class K, class M>
inline void SparseTupleWeightMap(SparseTupleWeight<W, K> *result,
const SparseTupleWeight<W, K> &w1,
const SparseTupleWeight<W, K> &w2,
const M &operator_mapper) {
SparseTupleWeightIterator<W, K> w1_it(w1);
SparseTupleWeightIterator<W, K> w2_it(w2);
const auto &v1_def = w1.DefaultValue();
const auto &v2_def = w2.DefaultValue();
result->SetDefaultValue(operator_mapper.Map(0, v1_def, v2_def));
while (!w1_it.Done() || !w2_it.Done()) {
const auto &k1 = (w1_it.Done()) ? w2_it.Value().first : w1_it.Value().first;
const auto &k2 = (w2_it.Done()) ? w1_it.Value().first : w2_it.Value().first;
const auto &v1 = (w1_it.Done()) ? v1_def : w1_it.Value().second;
const auto &v2 = (w2_it.Done()) ? v2_def : w2_it.Value().second;
if (k1 == k2) {
result->Push(k1, operator_mapper.Map(k1, v1, v2));
if (!w1_it.Done()) w1_it.Next();
if (!w2_it.Done()) w2_it.Next();
} else if (k1 < k2) {
result->Push(k1, operator_mapper.Map(k1, v1, v2_def));
w1_it.Next();
} else {
result->Push(k2, operator_mapper.Map(k2, v1_def, v2));
w2_it.Next();
}
}
}
template <class W, class K>
inline bool operator==(const SparseTupleWeight<W, K> &w1,
const SparseTupleWeight<W, K> &w2) {
const auto &v1_def = w1.DefaultValue();
const auto &v2_def = w2.DefaultValue();
if (v1_def != v2_def) return false;
SparseTupleWeightIterator<W, K> w1_it(w1);
SparseTupleWeightIterator<W, K> w2_it(w2);
while (!w1_it.Done() || !w2_it.Done()) {
const auto &k1 = (w1_it.Done()) ? w2_it.Value().first : w1_it.Value().first;
const auto &k2 = (w2_it.Done()) ? w1_it.Value().first : w2_it.Value().first;
const auto &v1 = (w1_it.Done()) ? v1_def : w1_it.Value().second;
const auto &v2 = (w2_it.Done()) ? v2_def : w2_it.Value().second;
if (k1 == k2) {
if (v1 != v2) return false;
if (!w1_it.Done()) w1_it.Next();
if (!w2_it.Done()) w2_it.Next();
} else if (k1 < k2) {
if (v1 != v2_def) return false;
w1_it.Next();
} else {
if (v1_def != v2) return false;
w2_it.Next();
}
}
return true;
}
template <class W, class K>
inline bool operator!=(const SparseTupleWeight<W, K> &w1,
const SparseTupleWeight<W, K> &w2) {
return !(w1 == w2);
}
template <class W, class K>
inline std::ostream &operator<<(std::ostream &strm,
const SparseTupleWeight<W, K> &weight) {
CompositeWeightWriter writer(strm);
writer.WriteBegin();
writer.WriteElement(weight.DefaultValue());
for (SparseTupleWeightIterator<W, K> it(weight); !it.Done(); it.Next()) {
writer.WriteElement(it.Value().first);
writer.WriteElement(it.Value().second);
}
writer.WriteEnd();
return strm;
}
template <class W, class K>
inline std::istream &operator>>(std::istream &strm,
SparseTupleWeight<W, K> &weight) {
CompositeWeightReader reader(strm);
reader.ReadBegin();
W def;
bool more = reader.ReadElement(&def);
weight.Init(def);
while (more) {
K key;
reader.ReadElement(&key);
W v;
more = reader.ReadElement(&v);
weight.Push(key, v);
}
reader.ReadEnd();
return strm;
}
} // namespace fst
#endif // FST_LIB_SPARSE_TUPLE_WEIGHT_H_
|