BasicVectorVectorHolder< BasicType > Class Template Reference

BasicVectorVectorHolder is a Holder for a vector of vector of a basic type, e.g. More...

#include <kaldi-holder-inl.h>

Collaboration diagram for BasicVectorVectorHolder< BasicType >:

Public Types

typedef std::vector< std::vector< BasicType > > T
 

Public Member Functions

 BasicVectorVectorHolder ()
 
void Clear ()
 
bool Read (std::istream &is)
 
TValue ()
 
void Swap (BasicVectorVectorHolder< BasicType > *other)
 
bool ExtractRange (BasicVectorVectorHolder< BasicType > &other, const std::string &range)
 
 ~BasicVectorVectorHolder ()
 

Static Public Member Functions

static bool Write (std::ostream &os, bool binary, const T &t)
 
static bool IsReadInBinary ()
 

Private Member Functions

 KALDI_DISALLOW_COPY_AND_ASSIGN (BasicVectorVectorHolder)
 

Private Attributes

T t_
 

Detailed Description

template<class BasicType>
class kaldi::BasicVectorVectorHolder< BasicType >

BasicVectorVectorHolder is a Holder for a vector of vector of a basic type, e.g.

std::vector<std::vector<int32> >. Note: a basic type is defined as a type for which ReadBasicType and WriteBasicType are implemented, i.e. integer and floating types, and bool.

Definition at line 343 of file kaldi-holder-inl.h.

Member Typedef Documentation

◆ T

typedef std::vector<std::vector<BasicType> > T

Definition at line 345 of file kaldi-holder-inl.h.

Constructor & Destructor Documentation

◆ BasicVectorVectorHolder()

Definition at line 347 of file kaldi-holder-inl.h.

347 { }

◆ ~BasicVectorVectorHolder()

Member Function Documentation

◆ Clear()

void Clear ( )
inline

Definition at line 396 of file kaldi-holder-inl.h.

References KaldiObjectHolder< KaldiType >::t_.

◆ ExtractRange()

bool ExtractRange ( BasicVectorVectorHolder< BasicType > &  other,
const std::string &  range 
)
inline

Definition at line 476 of file kaldi-holder-inl.h.

References KALDI_ERR.

477  {
478  KALDI_ERR << "ExtractRange is not defined for this type of holder.";
479  return false;
480  }
#define KALDI_ERR
Definition: kaldi-error.h:147

◆ IsReadInBinary()

static bool IsReadInBinary ( )
inlinestatic

Definition at line 468 of file kaldi-holder-inl.h.

468 { return true; }

◆ KALDI_DISALLOW_COPY_AND_ASSIGN()

KALDI_DISALLOW_COPY_AND_ASSIGN ( BasicVectorVectorHolder< BasicType >  )
private

◆ Read()

bool Read ( std::istream &  is)
inline

Definition at line 399 of file kaldi-holder-inl.h.

References rnnlm::i, kaldi::InitKaldiInputStream(), KALDI_WARN, kaldi::ReadBasicType(), and KaldiObjectHolder< KaldiType >::t_.

399  {
400  t_.clear();
401  bool is_binary;
402  if (!InitKaldiInputStream(is, &is_binary)) {
403  KALDI_WARN << "Failed reading binary header\n";
404  return false;
405  }
406  if (!is_binary) {
407  // In text mode, we terminate with newline.
408  try { // catching errors from ReadBasicType..
409  std::vector<BasicType> v; // temporary vector
410  while (1) {
411  int i = is.peek();
412  if (i == -1) {
413  KALDI_WARN << "Unexpected EOF";
414  return false;
415  } else if (static_cast<char>(i) == '\n') {
416  if (!v.empty()) {
417  KALDI_WARN << "No semicolon before newline (wrong format)";
418  return false;
419  } else {
420  is.get();
421  return true;
422  }
423  } else if (std::isspace(i)) {
424  is.get();
425  } else if (static_cast<char>(i) == ';') {
426  t_.push_back(v);
427  v.clear();
428  is.get();
429  } else { // some object we want to read...
430  BasicType b;
431  ReadBasicType(is, false, &b); // throws on error.
432  v.push_back(b);
433  }
434  }
435  } catch(const std::exception &e) {
436  KALDI_WARN << "BasicVectorVectorHolder::Read, read error. " << e.what();
437  return false;
438  }
439  } else { // binary mode.
440  size_t filepos = is.tellg();
441  try {
442  int32 size;
443  ReadBasicType(is, true, &size);
444  t_.resize(size);
445  for (typename std::vector<std::vector<BasicType> >::iterator
446  iter = t_.begin();
447  iter != t_.end();
448  ++iter) {
449  int32 size2;
450  ReadBasicType(is, true, &size2);
451  iter->resize(size2);
452  for (typename std::vector<BasicType>::iterator iter2 = iter->begin();
453  iter2 != iter->end();
454  ++iter2)
455  ReadBasicType(is, true, &(*iter2));
456  }
457  return true;
458  } catch(...) {
459  KALDI_WARN << "Read error or unexpected data at archive entry beginning"
460  " at file position " << filepos;
461  return false;
462  }
463  }
464  }
bool InitKaldiInputStream(std::istream &is, bool *binary)
Initialize an opened stream for reading by detecting the binary header and.
Definition: io-funcs-inl.h:306
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
kaldi::int32 int32
#define KALDI_WARN
Definition: kaldi-error.h:150

◆ Swap()

void Swap ( BasicVectorVectorHolder< BasicType > *  other)
inline

Definition at line 472 of file kaldi-holder-inl.h.

References KaldiObjectHolder< KaldiType >::t_, and BasicVectorVectorHolder< BasicType >::t_.

472  {
473  t_.swap(other->t_);
474  }

◆ Value()

T& Value ( )
inline

Definition at line 470 of file kaldi-holder-inl.h.

References KaldiObjectHolder< KaldiType >::t_.

◆ Write()

static bool Write ( std::ostream &  os,
bool  binary,
const T t 
)
inlinestatic

Definition at line 349 of file kaldi-holder-inl.h.

References kaldi::InitKaldiOutputStream(), KALDI_ASSERT, KALDI_WARN, and kaldi::WriteBasicType().

349  {
350  InitKaldiOutputStream(os, binary); // Puts binary header if binary mode.
351  try {
352  if (binary) { // need to write the size, in binary mode.
353  KALDI_ASSERT(static_cast<size_t>(static_cast<int32>(t.size())) ==
354  t.size());
355  // Or this Write routine cannot handle such a large vector.
356  // use int32 because it's fixed size regardless of compilation.
357  // change to int64 (plus in Read function) if this becomes a problem.
358  WriteBasicType(os, binary, static_cast<int32>(t.size()));
359  for (typename std::vector<std::vector<BasicType> >::const_iterator
360  iter = t.begin();
361  iter != t.end(); ++iter) {
362  KALDI_ASSERT(static_cast<size_t>(static_cast<int32>(iter->size()))
363  == iter->size());
364  WriteBasicType(os, binary, static_cast<int32>(iter->size()));
365  for (typename std::vector<BasicType>::const_iterator
366  iter2 = iter->begin();
367  iter2 != iter->end(); ++iter2) {
368  WriteBasicType(os, binary, *iter2);
369  }
370  }
371  } else { // text mode...
372  // In text mode, we write out something like (for integers):
373  // "1 2 3 ; 4 5 ; 6 ; ; 7 8 9 ;\n"
374  // where the semicolon is a terminator, not a separator
375  // (a separator would cause ambiguity between an
376  // empty list, and a list containing a single empty list).
377  for (typename std::vector<std::vector<BasicType> >::const_iterator
378  iter = t.begin();
379  iter != t.end();
380  ++iter) {
381  for (typename std::vector<BasicType>::const_iterator
382  iter2 = iter->begin();
383  iter2 != iter->end(); ++iter2)
384  WriteBasicType(os, binary, *iter2);
385  os << "; ";
386  }
387  os << '\n';
388  }
389  return os.good();
390  } catch(const std::exception &e) {
391  KALDI_WARN << "Exception caught writing Table object. " << e.what();
392  return false; // Write failure.
393  }
394  }
#define KALDI_WARN
Definition: kaldi-error.h:150
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185
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
void InitKaldiOutputStream(std::ostream &os, bool binary)
InitKaldiOutputStream initializes an opened stream for writing by writing an optional binary header a...
Definition: io-funcs-inl.h:291

Member Data Documentation

◆ t_

T t_
private

Definition at line 485 of file kaldi-holder-inl.h.

Referenced by BasicVectorVectorHolder< BasicType >::Swap().


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