BasicPairVectorHolder< BasicType > Class Template Reference

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

#include <kaldi-holder-inl.h>

Collaboration diagram for BasicPairVectorHolder< BasicType >:

Public Types

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

Public Member Functions

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

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 (BasicPairVectorHolder)
 

Private Attributes

T t_
 

Detailed Description

template<class BasicType>
class kaldi::BasicPairVectorHolder< BasicType >

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

std::vector<std::pair<int32, 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 494 of file kaldi-holder-inl.h.

Member Typedef Documentation

◆ T

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

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

Constructor & Destructor Documentation

◆ BasicPairVectorHolder()

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

498 { }

◆ ~BasicPairVectorHolder()

Member Function Documentation

◆ Clear()

void Clear ( )
inline

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

References KaldiObjectHolder< KaldiType >::t_.

◆ ExtractRange()

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

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

References KALDI_ERR.

622  {
623  KALDI_ERR << "ExtractRange is not defined for this type of holder.";
624  return false;
625  }
#define KALDI_ERR
Definition: kaldi-error.h:147

◆ IsReadInBinary()

static bool IsReadInBinary ( )
inlinestatic

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

613 { return true; }

◆ KALDI_DISALLOW_COPY_AND_ASSIGN()

KALDI_DISALLOW_COPY_AND_ASSIGN ( BasicPairVectorHolder< BasicType >  )
private

◆ Read()

bool Read ( std::istream &  is)
inline

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

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

539  {
540  t_.clear();
541  bool is_binary;
542  if (!InitKaldiInputStream(is, &is_binary)) {
543  KALDI_WARN << "Reading Table object [integer type], failed reading binary"
544  " header\n";
545  return false;
546  }
547  if (!is_binary) {
548  // In text mode, we terminate with newline.
549  try { // catching errors from ReadBasicType..
550  std::vector<BasicType> v; // temporary vector
551  while (1) {
552  int i = is.peek();
553  if (i == -1) {
554  KALDI_WARN << "Unexpected EOF";
555  return false;
556  } else if (static_cast<char>(i) == '\n') {
557  if (t_.empty() && v.empty()) {
558  is.get();
559  return true;
560  } else if (v.size() == 2) {
561  t_.push_back(std::make_pair(v[0], v[1]));
562  is.get();
563  return true;
564  } else {
565  KALDI_WARN << "Unexpected newline, reading vector<pair<?> >; got "
566  << v.size() << " elements, expected 2.";
567  return false;
568  }
569  } else if (std::isspace(i)) {
570  is.get();
571  } else if (static_cast<char>(i) == ';') {
572  if (v.size() != 2) {
573  KALDI_WARN << "Wrong input format, reading vector<pair<?> >; got "
574  << v.size() << " elements, expected 2.";
575  return false;
576  }
577  t_.push_back(std::make_pair(v[0], v[1]));
578  v.clear();
579  is.get();
580  } else { // some object we want to read...
581  BasicType b;
582  ReadBasicType(is, false, &b); // throws on error.
583  v.push_back(b);
584  }
585  }
586  } catch(const std::exception &e) {
587  KALDI_WARN << "BasicPairVectorHolder::Read, read error. " << e.what();
588  return false;
589  }
590  } else { // binary mode.
591  size_t filepos = is.tellg();
592  try {
593  int32 size;
594  ReadBasicType(is, true, &size);
595  t_.resize(size);
596  for (typename T::iterator iter = t_.begin();
597  iter != t_.end();
598  ++iter) {
599  ReadBasicType(is, true, &(iter->first));
600  ReadBasicType(is, true, &(iter->second));
601  }
602  return true;
603  } catch(...) {
604  KALDI_WARN << "BasicVectorHolder::Read, read error or unexpected data"
605  " at archive entry beginning at file position " << filepos;
606  return false;
607  }
608  }
609  }
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 ( BasicPairVectorHolder< BasicType > *  other)
inline

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

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

617  {
618  t_.swap(other->t_);
619  }

◆ Value()

T& Value ( )
inline

Definition at line 615 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 500 of file kaldi-holder-inl.h.

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

500  {
501  InitKaldiOutputStream(os, binary); // Puts binary header if binary mode.
502  try {
503  if (binary) { // need to write the size, in binary mode.
504  KALDI_ASSERT(static_cast<size_t>(static_cast<int32>(t.size())) ==
505  t.size());
506  // Or this Write routine cannot handle such a large vector.
507  // use int32 because it's fixed size regardless of compilation.
508  // change to int64 (plus in Read function) if this becomes a problem.
509  WriteBasicType(os, binary, static_cast<int32>(t.size()));
510  for (typename T::const_iterator iter = t.begin();
511  iter != t.end(); ++iter) {
512  WriteBasicType(os, binary, iter->first);
513  WriteBasicType(os, binary, iter->second);
514  }
515  } else { // text mode...
516  // In text mode, we write out something like (for integers):
517  // "1 2 ; 4 5 ; 6 7 ; 8 9 \n"
518  // where the semicolon is a separator, not a terminator.
519  for (typename T::const_iterator iter = t.begin();
520  iter != t.end();) {
521  WriteBasicType(os, binary, iter->first);
522  WriteBasicType(os, binary, iter->second);
523  ++iter;
524  if (iter != t.end())
525  os << "; ";
526  }
527  os << '\n';
528  }
529  return os.good();
530  } catch(const std::exception &e) {
531  KALDI_WARN << "Exception caught writing Table object. " << e.what();
532  return false; // Write failure.
533  }
534  }
#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 630 of file kaldi-holder-inl.h.

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


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