20 #ifndef KALDI_TREE_EVENT_MAP_H_ 21 #define KALDI_TREE_EVENT_MAP_H_ 58 typedef std::vector<std::pair<EventKeyType, EventValueType> >
EventType;
62 inline std::pair<EventKeyType, EventValueType>
MakeEventPair (EventKeyType k, EventValueType v) {
63 return std::pair<EventKeyType, EventValueType>(k, v);
66 void WriteEventType(std::ostream &os,
bool binary,
const EventType &vec);
67 void ReadEventType(std::istream &is,
bool binary, EventType *vec);
74 size_t operator () (
const EventType *ptr) {
return (*
this)(*ptr); }
78 size_t operator () (
const EventType *p1,
const EventType *p2) {
return (*p1 == *p2); }
88 static void Check(
const EventType &event);
89 static bool Lookup(
const EventType &event, EventKeyType key, EventValueType *ans);
92 virtual bool Map(
const EventType &event, EventAnswerType *ans)
const = 0;
99 virtual void MultiMap(
const EventType &event, std::vector<EventAnswerType> *ans)
const = 0;
104 virtual void GetChildren(std::vector<EventMap*> *out)
const = 0;
115 virtual EventMap *
Copy(
const std::vector<EventMap*> &new_leaves)
const = 0;
131 const unordered_set<EventKeyType> &keys_to_map,
132 const unordered_map<EventValueType,EventValueType> &value_map)
const = 0;
140 virtual EventMap *Prune()
const = 0;
144 std::vector<EventAnswerType> tmp; EventType empty_event;
145 MultiMap(empty_event, &tmp);
147 KALDI_WARN <<
"EventMap::MaxResult(), empty result";
148 return std::numeric_limits<EventAnswerType>::min();
150 else {
return * std::max_element(tmp.begin(), tmp.end()); }
154 virtual void Write(std::ostream &os,
bool binary) = 0;
159 static void Write(std::ostream &os,
bool binary,
EventMap *emap);
162 static EventMap *Read(std::istream &is,
bool binary);
168 virtual bool Map(
const EventType &event, EventAnswerType *ans)
const {
174 std::vector<EventAnswerType> *ans)
const {
175 ans->push_back(answer_);
178 virtual void GetChildren(std::vector<EventMap*> *out)
const { out->clear(); }
180 virtual EventMap *
Copy(
const std::vector<EventMap*> &new_leaves)
const {
181 if (answer_ < 0 || answer_ >= (EventAnswerType)new_leaves.size() ||
182 new_leaves[answer_] == NULL)
184 else return new_leaves[answer_]->
Copy();
188 const unordered_set<EventKeyType> &keys_to_map,
189 const unordered_map<EventValueType,EventValueType> &value_map)
const {
199 virtual void Write(std::ostream &os,
bool binary);
209 virtual bool Map(
const EventType &event, EventAnswerType *ans)
const {
210 EventValueType tmp; *ans = -1;
211 if (Lookup(event, key_, &tmp) && tmp >= 0
212 && tmp < (EventValueType)table_.size() && table_[tmp] != NULL) {
213 return table_[tmp]->Map(event, ans);
220 for (
size_t i = 0;
i<table_.size();
i++)
221 if (table_[
i] != NULL) out->push_back(table_[
i]);
224 virtual void MultiMap(
const EventType &event, std::vector<EventAnswerType> *ans)
const {
226 if (Lookup(event, key_, &tmp)) {
227 if (tmp >= 0 && tmp < (EventValueType)table_.size() && table_[tmp] != NULL)
228 return table_[tmp]->MultiMap(event, ans);
231 for (
size_t i = 0;
i < table_.size();
i++)
232 if (table_[
i] != NULL) table_[
i]->MultiMap(event, ans);
239 const unordered_set<EventKeyType> &keys_to_map,
240 const unordered_map<EventValueType,EventValueType> &value_map)
const;
243 explicit TableEventMap(EventKeyType key,
const std::vector<EventMap*> &table): key_(key), table_(table) {}
245 explicit TableEventMap(EventKeyType key,
const std::map<EventValueType, EventMap*> &map_in);
247 explicit TableEventMap(EventKeyType key,
const std::map<EventValueType, EventAnswerType> &map_in);
249 virtual void Write(std::ostream &os,
bool binary);
252 virtual EventMap *
Copy(
const std::vector<EventMap*> &new_leaves)
const {
253 std::vector<EventMap*> new_table_(table_.size(), NULL);
254 for (
size_t i = 0;
i<table_.size();
i++)
if (table_[
i]) new_table_[
i]=table_[
i]->
Copy(new_leaves);
272 virtual bool Map(
const EventType &event, EventAnswerType *ans)
const {
273 EventValueType value;
274 if (Lookup(event, key_, &value)) {
276 if (yes_set_.count(value)) {
277 return yes_->Map(event, ans);
279 return no_->Map(event, ans);
284 virtual void MultiMap(
const EventType &event, std::vector<EventAnswerType> *ans)
const {
286 if (Lookup(event, key_, &tmp)) {
287 if (std::binary_search(yes_set_.begin(), yes_set_.end(), tmp))
288 yes_->MultiMap(event, ans);
290 no_->MultiMap(event, ans);
292 yes_->MultiMap(event, ans);
293 no_->MultiMap(event, ans);
299 out->push_back(yes_);
303 virtual EventMap *
Copy(
const std::vector<EventMap*> &new_leaves)
const {
304 return new SplitEventMap(key_, yes_set_, yes_->Copy(new_leaves), no_->Copy(new_leaves));
307 virtual void Write(std::ostream &os,
bool binary);
313 const unordered_set<EventKeyType> &keys_to_map,
314 const unordered_map<EventValueType,EventValueType> &value_map)
const;
320 EventMap *yes,
EventMap *no): key_(key), yes_set_(yes_set), yes_(yes), no_(no) {
329 EventMap *yes,
EventMap *no): key_(key), yes_set_(yes_set), yes_(yes), no_(no) {
333 delete yes_;
delete no_;
358 std::vector<int32> *parents);
bool GetTreeStructure(const EventMap &map, int32 *num_leaves, std::vector< int32 > *parents)
This function gets the tree structure of the EventMap "map" in a convenient form. ...
This code computes Goodness of Pronunciation (GOP) and extracts phone-level pronunciation feature for...
virtual EventMap * Copy(const std::vector< EventMap *> &new_leaves) const
void DeletePointers(std::vector< A *> *v)
Deletes any non-NULL pointers in the vector v, and sets the corresponding entries of v to NULL...
virtual void GetChildren(std::vector< EventMap *> *out) const
std::pair< EventKeyType, EventValueType > MakeEventPair(EventKeyType k, EventValueType v)
virtual bool Map(const EventType &event, EventAnswerType *ans) const
virtual void GetChildren(std::vector< EventMap *> *out) const
ConstantEventMap(EventAnswerType answer)
virtual EventAnswerType MaxResult() const
std::string EventTypeToString(const EventType &evec)
virtual bool Map(const EventType &event, EventAnswerType *ans) const
virtual EventMap * Prune() const
#define KALDI_DISALLOW_COPY_AND_ASSIGN(type)
virtual bool Map(const EventType &event, EventAnswerType *ans) const
virtual void MultiMap(const EventType &, std::vector< EventAnswerType > *ans) const
std::vector< EventMap * > table_
void WriteEventType(std::ostream &os, bool binary, const EventType &evec)
virtual EventMap * Copy(const std::vector< EventMap *> &new_leaves) const
std::vector< std::pair< EventKeyType, EventValueType > > EventType
virtual EventMap * MapValues(const unordered_set< EventKeyType > &keys_to_map, const unordered_map< EventValueType, EventValueType > &value_map) const
SplitEventMap(EventKeyType key, const std::vector< EventValueType > &yes_set, EventMap *yes, EventMap *no)
This constructor takes ownership of the "yes" and "no" arguments.
virtual void MultiMap(const EventType &event, std::vector< EventAnswerType > *ans) const
SplitEventMap(EventKeyType key, const ConstIntegerSet< EventValueType > &yes_set, EventMap *yes, EventMap *no)
This constructor used in the Copy() function.
size_t operator()(const EventType &vec)
int32 EventKeyType
Things of type EventKeyType can take any value.
void ReadEventType(std::istream &is, bool binary, EventType *evec)
#define KALDI_PARANOID_ASSERT(cond)
virtual void MultiMap(const EventType &event, std::vector< EventAnswerType > *ans) const
virtual void GetChildren(std::vector< EventMap *> *out) const
virtual EventMap * Copy(const std::vector< EventMap *> &new_leaves) const =0
ConstIntegerSet< EventValueType > yes_set_
A class that is capable of representing a generic mapping from EventType (which is a vector of (key...
bool IsSorted(const std::vector< T > &vec)
Returns true if the vector is sorted.
#define KALDI_ASSERT(cond)
TableEventMap(EventKeyType key, const std::vector< EventMap *> &table)
Takes ownership of pointers.
int32 EventAnswerType
As far as the event-map code itself is concerned, things of type EventAnswerType may take any value e...
int32 EventValueType
Given current code, things of type EventValueType should generally be nonnegative and in a reasonably...
virtual EventMap * Copy(const std::vector< EventMap *> &new_leaves) const
void Copy(const CuMatrixBase< Real > &src, const CuArray< int32 > ©_from_indices, CuMatrixBase< Real > *tgt)
Copies elements from src into tgt as given by copy_from_indices.