EventMap Class Referenceabstract

A class that is capable of representing a generic mapping from EventType (which is a vector of (key, value) pairs) to EventAnswerType which is just an integer. More...

#include <event-map.h>

Inheritance diagram for EventMap:

Public Member Functions

virtual bool Map (const EventType &event, EventAnswerType *ans) const =0
 
virtual void MultiMap (const EventType &event, std::vector< EventAnswerType > *ans) const =0
 
virtual void GetChildren (std::vector< EventMap *> *out) const =0
 
virtual EventMapCopy (const std::vector< EventMap *> &new_leaves) const =0
 
EventMapCopy () const
 
virtual EventMapMapValues (const unordered_set< EventKeyType > &keys_to_map, const unordered_map< EventValueType, EventValueType > &value_map) const =0
 
virtual EventMapPrune () const =0
 
virtual EventAnswerType MaxResult () const
 
virtual void Write (std::ostream &os, bool binary)=0
 Write to stream. More...
 
virtual ~EventMap ()
 

Static Public Member Functions

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...
 

Detailed Description

A class that is capable of representing a generic mapping from EventType (which is a vector of (key, value) pairs) to EventAnswerType which is just an integer.

See Decision tree internals for overview.

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

Constructor & Destructor Documentation

◆ ~EventMap()

virtual ~EventMap ( )
inlinevirtual

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

156 {}

Member Function Documentation

◆ Check()

void Check ( const EventType event)
static

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

References rnnlm::i, and KALDI_ASSERT.

Referenced by EventMap::Lookup(), kaldi::TestQuestionsInitRand(), and kaldi::TestSplitDecisionTree().

281  {
282  // will crash if not sorted or has duplicates
283  size_t sz = event.size();
284  for (size_t i = 0;i+1 < sz;i++)
285  KALDI_ASSERT(event[i].first < event[i+1].first);
286 }
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185

◆ Copy() [1/2]

◆ Copy() [2/2]

EventMap* Copy ( ) const
inline

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

References EventMap::Copy().

Referenced by EventMap::Copy().

117 { std::vector<EventMap*> new_leaves; return Copy(new_leaves); }
EventMap * Copy() const
Definition: event-map.h:117

◆ GetChildren()

virtual void GetChildren ( std::vector< EventMap *> *  out) const
pure virtual

◆ Lookup()

bool Lookup ( const EventType event,
EventKeyType  key,
EventValueType ans 
)
static

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

References EventMap::Check().

Referenced by DecisionTreeSplitter::DoSplitInternal(), kaldi::FilterStatsByKey(), kaldi::PossibleValues(), TreeRenderer::RenderSplit(), TreeRenderer::RenderTable(), kaldi::SplitStatsByKey(), kaldi::TestDoTableSplit(), kaldi::TestGenRandStats(), and kaldi::TestSplitStatsByKey().

291  {
292  // this assumes that the "event" array is sorted (e.g. on the KeyType value;
293  // just doing std::sort will do this) and has no duplicate values with the same
294  // key. call Check() to verify this.
295 #ifdef KALDI_PARANOID
296  Check(event);
297 #endif
298  std::vector<std::pair<EventKeyType, EventValueType> >::const_iterator
299  begin = event.begin(),
300  end = event.end(),
301  middle; // "middle" is used as a temporary variable in the algorithm.
302  // begin and sz store the current region where the first instance of
303  // "value" might appear.
304  // This is like this stl algorithm "lower_bound".
305  size_t sz = end-begin, half;
306  while (sz > 0) {
307  half = sz >> 1;
308  middle = begin + half; // "end" here is now reallly the middle.
309  if (middle->first < key) {
310  begin = middle;
311  ++begin;
312  sz = sz - half - 1;
313  } else {
314  sz = half;
315  }
316  }
317  if (begin != end && begin->first == key) {
318  *ans = begin->second;
319  return true;
320  } else {
321  return false;
322  }
323 }
static void Check(const EventType &event)
Definition: event-map.cc:281

◆ Map()

◆ MapValues()

virtual EventMap* MapValues ( const unordered_set< EventKeyType > &  keys_to_map,
const unordered_map< EventValueType, EventValueType > &  value_map 
) const
pure virtual

◆ MaxResult()

virtual EventAnswerType MaxResult ( ) const
inlinevirtual

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

References KALDI_WARN.

Referenced by kaldi::BuildTreeTwoLevel(), kaldi::ComputeTreeMapping(), kaldi::GetOccs(), kaldi::InitAmGmm(), kaldi::InitAmGmmFromOld(), ContextDependency::NumPdfs(), kaldi::TestClusterEventMapGetMappingAndRenumberEventMap(), and kaldi::TestEventMap().

142  { // child classes may override this for efficiency; here is basic version.
143  // returns -1 if nothing found.
144  std::vector<EventAnswerType> tmp; EventType empty_event;
145  MultiMap(empty_event, &tmp);
146  if (tmp.empty()) {
147  KALDI_WARN << "EventMap::MaxResult(), empty result";
148  return std::numeric_limits<EventAnswerType>::min();
149  }
150  else { return * std::max_element(tmp.begin(), tmp.end()); }
151  }
std::vector< std::pair< EventKeyType, EventValueType > > EventType
Definition: event-map.h:58
#define KALDI_WARN
Definition: kaldi-error.h:150
virtual void MultiMap(const EventType &event, std::vector< EventAnswerType > *ans) const =0

◆ MultiMap()

◆ Prune()

virtual EventMap* Prune ( ) const
pure virtual

◆ Read()

EventMap * Read ( std::istream &  is,
bool  binary 
)
static

a Read function that reads an arbitrary EventMap; also works for NULL pointers.

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

References kaldi::CharToString(), kaldi::ExpectToken(), KALDI_ERR, kaldi::Peek(), ConstantEventMap::Read(), TableEventMap::Read(), and SplitEventMap::Read().

Referenced by ContextDependency::Read(), TableEventMap::Read(), SplitEventMap::Read(), and kaldi::TestEventMapIo().

36  {
37  char c = Peek(is, binary);
38  if (c == 'N') {
39  ExpectToken(is, binary, "NULL");
40  return NULL;
41  } else if (c == 'C') {
42  return ConstantEventMap::Read(is, binary);
43  } else if (c == 'T') {
44  return TableEventMap::Read(is, binary);
45  } else if (c == 'S') {
46  return SplitEventMap::Read(is, binary);
47  } else {
48  KALDI_ERR << "EventMap::read, was not expecting character " << CharToString(c)
49  << ", at file position " << is.tellg();
50  return NULL; // suppress warning.
51  }
52 }
static TableEventMap * Read(std::istream &is, bool binary)
Definition: event-map.cc:140
int Peek(std::istream &is, bool binary)
Peek consumes whitespace (if binary == false) and then returns the peek() value of the stream...
Definition: io-funcs.cc:145
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:191
#define KALDI_ERR
Definition: kaldi-error.h:147
std::string CharToString(const char &c)
Definition: kaldi-utils.cc:36
static SplitEventMap * Read(std::istream &is, bool binary)
Definition: event-map.cc:209
static ConstantEventMap * Read(std::istream &is, bool binary)
Definition: event-map.cc:64

◆ Write() [1/2]

virtual void Write ( std::ostream &  os,
bool  binary 
)
pure virtual

◆ Write() [2/2]

void Write ( std::ostream &  os,
bool  binary,
EventMap emap 
)
static

a Write function that takes care of NULL pointers.

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

References EventMap::Write(), and kaldi::WriteToken().

28  {
29  if (emap == NULL) {
30  WriteToken(os, binary, "NULL");
31  } else {
32  emap->Write(os, binary);
33  }
34 }
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

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