All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
TableEventMap Class Reference

#include <event-map.h>

Inheritance diagram for TableEventMap:
Collaboration diagram for TableEventMap:

Public Member Functions

virtual bool Map (const EventType &event, EventAnswerType *ans) const
 
virtual void GetChildren (std::vector< EventMap * > *out) const
 
virtual void MultiMap (const EventType &event, std::vector< EventAnswerType > *ans) const
 
virtual EventMapPrune () const
 
virtual EventMapMapValues (const unordered_set< EventKeyType > &keys_to_map, const unordered_map< EventValueType, EventValueType > &value_map) const
 
 TableEventMap (EventKeyType key, const std::vector< EventMap * > &table)
 Takes ownership of pointers. More...
 
 TableEventMap (EventKeyType key, const std::map< EventValueType, EventMap * > &map_in)
 Takes ownership of pointers. More...
 
 TableEventMap (EventKeyType key, const std::map< EventValueType, EventAnswerType > &map_in)
 This initializer creates a ConstantEventMap for each value in the map. More...
 
virtual void Write (std::ostream &os, bool binary)
 Write to stream. More...
 
virtual EventMapCopy (const std::vector< EventMap * > &new_leaves) const
 
virtual ~TableEventMap ()
 
- Public Member Functions inherited from EventMap
EventMapCopy () const
 
virtual EventAnswerType MaxResult () const
 
virtual ~EventMap ()
 

Static Public Member Functions

static TableEventMapRead (std::istream &is, bool binary)
 
- Static Public Member Functions inherited from EventMap
static void Check (const EventType &event)
 
static bool Lookup (const EventType &event, EventKeyType key, EventValueType *ans)
 
static void Write (std::ostream &os, bool binary, EventMap *emap)
 a Write function that takes care of NULL pointers. More...
 
static EventMapRead (std::istream &is, bool binary)
 a Read function that reads an arbitrary EventMap; also works for NULL pointers. More...
 

Private Member Functions

 KALDI_DISALLOW_COPY_AND_ASSIGN (TableEventMap)
 

Private Attributes

EventKeyType key_
 
std::vector< EventMap * > table_
 

Detailed Description

Definition at line 206 of file event-map.h.

Constructor & Destructor Documentation

TableEventMap ( EventKeyType  key,
const std::vector< EventMap * > &  table 
)
inlineexplicit

Takes ownership of pointers.

Definition at line 243 of file event-map.h.

Referenced by TableEventMap::Copy(), TableEventMap::MapValues(), TableEventMap::Prune(), and TableEventMap::Read().

243 : key_(key), table_(table) {}
EventKeyType key_
Definition: event-map.h:261
std::vector< EventMap * > table_
Definition: event-map.h:262
TableEventMap ( EventKeyType  key,
const std::map< EventValueType, EventMap * > &  map_in 
)
explicit

Takes ownership of pointers.

Definition at line 325 of file event-map.cc.

References KALDI_ASSERT, and TableEventMap::table_.

325  : key_(key) {
326  if (map_in.size() == 0)
327  return; // empty table.
328  else {
329  EventValueType highest_val = map_in.rbegin()->first;
330  table_.resize(highest_val+1, NULL);
331  std::map<EventValueType, EventMap*>::const_iterator iter = map_in.begin(), end = map_in.end();
332  for (; iter != end; ++iter) {
333  KALDI_ASSERT(iter->first >= 0 && iter->first <= highest_val);
334  table_[iter->first] = iter->second;
335  }
336  }
337 }
EventKeyType key_
Definition: event-map.h:261
std::vector< EventMap * > table_
Definition: event-map.h:262
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169
int32 EventValueType
Given current code, things of type EventValueType should generally be nonnegative and in a reasonably...
Definition: event-map.h:51
TableEventMap ( EventKeyType  key,
const std::map< EventValueType, EventAnswerType > &  map_in 
)
explicit

This initializer creates a ConstantEventMap for each value in the map.

Definition at line 339 of file event-map.cc.

References KALDI_ASSERT, and TableEventMap::table_.

339  : key_(key) {
340  if (map_in.size() == 0)
341  return; // empty table.
342  else {
343  EventValueType highest_val = map_in.rbegin()->first;
344  table_.resize(highest_val+1, NULL);
345  std::map<EventValueType, EventAnswerType>::const_iterator iter = map_in.begin(), end = map_in.end();
346  for (; iter != end; ++iter) {
347  KALDI_ASSERT(iter->first >= 0 && iter->first <= highest_val);
348  table_[iter->first] = new ConstantEventMap(iter->second);
349  }
350  }
351 }
EventKeyType key_
Definition: event-map.h:261
std::vector< EventMap * > table_
Definition: event-map.h:262
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169
int32 EventValueType
Given current code, things of type EventValueType should generally be nonnegative and in a reasonably...
Definition: event-map.h:51
virtual ~TableEventMap ( )
inlinevirtual

Definition at line 257 of file event-map.h.

References kaldi::DeletePointers(), and TableEventMap::table_.

257  {
259  }
std::vector< EventMap * > table_
Definition: event-map.h:262
void DeletePointers(std::vector< A * > *v)
Deletes any non-NULL pointers in the vector v, and sets the corresponding entries of v to NULL...
Definition: stl-utils.h:186

Member Function Documentation

virtual EventMap* Copy ( const std::vector< EventMap * > &  new_leaves) const
inlinevirtual

Implements EventMap.

Definition at line 252 of file event-map.h.

References rnnlm::i, TableEventMap::key_, TableEventMap::table_, and TableEventMap::TableEventMap().

252  {
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);
255  return new TableEventMap(key_, new_table_);
256  }
EventKeyType key_
Definition: event-map.h:261
std::vector< EventMap * > table_
Definition: event-map.h:262
TableEventMap(EventKeyType key, const std::vector< EventMap * > &table)
Takes ownership of pointers.
Definition: event-map.h:243
virtual void GetChildren ( std::vector< EventMap * > *  out) const
inlinevirtual

Implements EventMap.

Definition at line 218 of file event-map.h.

References rnnlm::i, and TableEventMap::table_.

218  {
219  out->clear();
220  for (size_t i = 0; i<table_.size(); i++)
221  if (table_[i] != NULL) out->push_back(table_[i]);
222  }
std::vector< EventMap * > table_
Definition: event-map.h:262
KALDI_DISALLOW_COPY_AND_ASSIGN ( TableEventMap  )
private
virtual bool Map ( const EventType event,
EventAnswerType ans 
) const
inlinevirtual

Implements EventMap.

Definition at line 209 of file event-map.h.

References TableEventMap::key_, EventMap::Lookup(), and TableEventMap::table_.

Referenced by kaldi::TestEventMap().

209  {
210  EventValueType tmp; *ans = -1; // means no answer
211  if (Lookup(event, key_, &tmp) && tmp >= 0
212  && tmp < (EventValueType)table_.size() && table_[tmp] != NULL) {
213  return table_[tmp]->Map(event, ans);
214  }
215  return false;
216  }
EventKeyType key_
Definition: event-map.h:261
std::vector< EventMap * > table_
Definition: event-map.h:262
static bool Lookup(const EventType &event, EventKeyType key, EventValueType *ans)
Definition: event-map.cc:290
int32 EventValueType
Given current code, things of type EventValueType should generally be nonnegative and in a reasonably...
Definition: event-map.h:51
EventMap * MapValues ( const unordered_set< EventKeyType > &  keys_to_map,
const unordered_map< EventValueType, EventValueType > &  value_map 
) const
virtual

Implements EventMap.

Definition at line 88 of file event-map.cc.

References KALDI_ASSERT, KALDI_ERR, TableEventMap::key_, TableEventMap::table_, and TableEventMap::TableEventMap().

90  {
91  std::vector<EventMap*> table;
92  table.reserve(table_.size());
93  EventValueType size = table_.size();
94  for (EventValueType value = 0; value < size; value++) {
95  if (table_[value] != NULL) {
96  EventMap *this_map = table_[value]->MapValues(keys_to_map, value_map);
97  EventValueType mapped_value;
98 
99  if (keys_to_map.count(key_) == 0) mapped_value = value;
100  else {
101  unordered_map<EventValueType,EventValueType>::const_iterator
102  iter = value_map.find(value);
103  if (iter == value_map.end()) {
104  KALDI_ERR << "Could not map value " << value
105  << " for key " << key_;
106  }
107  mapped_value = iter->second;
108  }
109  KALDI_ASSERT(mapped_value >= 0);
110  if (static_cast<EventValueType>(table.size()) <= mapped_value)
111  table.resize(mapped_value + 1, NULL);
112  if (table[mapped_value] != NULL)
113  KALDI_ERR << "Multiple values map to the same point: this code cannot "
114  << "handle this case.";
115  table[mapped_value] = this_map;
116  }
117  }
118  return new TableEventMap(key_, table);
119 }
EventKeyType key_
Definition: event-map.h:261
std::vector< EventMap * > table_
Definition: event-map.h:262
#define KALDI_ERR
Definition: kaldi-error.h:127
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169
int32 EventValueType
Given current code, things of type EventValueType should generally be nonnegative and in a reasonably...
Definition: event-map.h:51
TableEventMap(EventKeyType key, const std::vector< EventMap * > &table)
Takes ownership of pointers.
Definition: event-map.h:243
virtual void MultiMap ( const EventType event,
std::vector< EventAnswerType > *  ans 
) const
inlinevirtual

Implements EventMap.

Definition at line 224 of file event-map.h.

References rnnlm::i, TableEventMap::key_, EventMap::Lookup(), and TableEventMap::table_.

224  {
225  EventValueType tmp;
226  if (Lookup(event, key_, &tmp)) {
227  if (tmp >= 0 && tmp < (EventValueType)table_.size() && table_[tmp] != NULL)
228  return table_[tmp]->MultiMap(event, ans);
229  // else no answers.
230  } else { // all answers are possible if no such key.
231  for (size_t i = 0;i < table_.size();i++)
232  if (table_[i] != NULL) table_[i]->MultiMap(event, ans); // append.
233  }
234  }
EventKeyType key_
Definition: event-map.h:261
std::vector< EventMap * > table_
Definition: event-map.h:262
virtual void MultiMap(const EventType &event, std::vector< EventAnswerType > *ans) const
Definition: event-map.h:224
static bool Lookup(const EventType &event, EventKeyType key, EventValueType *ans)
Definition: event-map.cc:290
int32 EventValueType
Given current code, things of type EventValueType should generally be nonnegative and in a reasonably...
Definition: event-map.h:51
EventMap * Prune ( ) const
virtual

Implements EventMap.

Definition at line 71 of file event-map.cc.

References TableEventMap::key_, TableEventMap::table_, and TableEventMap::TableEventMap().

71  {
72  std::vector<EventMap*> table;
73  table.reserve(table_.size());
74  EventValueType size = table_.size();
75  for (EventKeyType value = 0; value < size; value++) {
76  if (table_[value] != NULL) {
77  EventMap *pruned_map = table_[value]->Prune();
78  if (pruned_map != NULL) {
79  table.resize(value + 1, NULL);
80  table[value] = pruned_map;
81  }
82  }
83  }
84  if (table.empty()) return NULL;
85  else return new TableEventMap(key_, table);
86 }
EventKeyType key_
Definition: event-map.h:261
std::vector< EventMap * > table_
Definition: event-map.h:262
int32 EventKeyType
Things of type EventKeyType can take any value.
Definition: event-map.h:45
int32 EventValueType
Given current code, things of type EventValueType should generally be nonnegative and in a reasonably...
Definition: event-map.h:51
TableEventMap(EventKeyType key, const std::vector< EventMap * > &table)
Takes ownership of pointers.
Definition: event-map.h:243
TableEventMap * Read ( std::istream &  is,
bool  binary 
)
static

Definition at line 140 of file event-map.cc.

References kaldi::ExpectToken(), EventMap::Read(), kaldi::ReadBasicType(), and TableEventMap::TableEventMap().

Referenced by EventMap::Read().

140  {
141  ExpectToken(is, binary, "TE");
142  EventKeyType key;
143  ReadBasicType(is, binary, &key);
144  uint32 size;
145  ReadBasicType(is, binary, &size);
146  std::vector<EventMap*> table(size);
147  ExpectToken(is, binary, "(");
148  for (size_t t = 0; t < size; t++) {
149  // This Read function works for NULL pointers.
150  table[t] = EventMap::Read(is, binary);
151  }
152  ExpectToken(is, binary, ")");
153  return new TableEventMap(key, table);
154 }
void ReadBasicType(std::istream &is, bool binary, T *t)
ReadBasicType is the name of the read function for bool, integer types, and floating-point types...
Definition: io-funcs-inl.h:55
static EventMap * Read(std::istream &is, bool binary)
a Read function that reads an arbitrary EventMap; also works for NULL pointers.
Definition: event-map.cc:36
void ExpectToken(std::istream &is, bool binary, const char *token)
ExpectToken tries to read in the given token, and throws an exception on failure. ...
Definition: io-funcs.cc:188
int32 EventKeyType
Things of type EventKeyType can take any value.
Definition: event-map.h:45
TableEventMap(EventKeyType key, const std::vector< EventMap * > &table)
Takes ownership of pointers.
Definition: event-map.h:243
void Write ( std::ostream &  os,
bool  binary 
)
virtual

Write to stream.

Implements EventMap.

Definition at line 122 of file event-map.cc.

References KALDI_ERR, TableEventMap::key_, TableEventMap::table_, EventMap::Write(), kaldi::WriteBasicType(), and kaldi::WriteToken().

122  {
123  WriteToken(os, binary, "TE");
124  WriteBasicType(os, binary, key_);
125  uint32 size = table_.size();
126  WriteBasicType(os, binary, size);
127  WriteToken(os, binary, "(");
128  for (size_t t = 0; t < size; t++) {
129  // This Write function works for NULL pointers.
130  EventMap::Write(os, binary, table_[t]);
131  }
132  WriteToken(os, binary, ")");
133  if (!binary) os << '\n';
134  if (os.fail()) {
135  KALDI_ERR << "TableEventMap::Write(), could not write to stream.";
136  }
137 }
EventKeyType key_
Definition: event-map.h:261
std::vector< EventMap * > table_
Definition: event-map.h:262
#define KALDI_ERR
Definition: kaldi-error.h:127
void WriteToken(std::ostream &os, bool binary, const char *token)
The WriteToken functions are for writing nonempty sequences of non-space characters.
Definition: io-funcs.cc:134
void WriteBasicType(std::ostream &os, bool binary, T t)
WriteBasicType is the name of the write function for bool, integer types, and floating-point types...
Definition: io-funcs-inl.h:34
virtual void Write(std::ostream &os, bool binary)=0
Write to stream.

Member Data Documentation


The documentation for this class was generated from the following files: