BasicVectorHolder< BasicType > Class Template Reference

A Holder for a vector of basic types, e.g. More...

#include <kaldi-holder-inl.h>

Collaboration diagram for BasicVectorHolder< BasicType >:

Public Types

typedef std::vector< BasicType > T
 

Public Member Functions

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

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

Private Attributes

T t_
 

Detailed Description

template<class BasicType>
class kaldi::BasicVectorHolder< BasicType >

A Holder for a vector of basic types, e.g.

std::vector<int32>, std::vector<float>, and so on. 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 224 of file kaldi-holder-inl.h.

Member Typedef Documentation

◆ T

typedef std::vector<BasicType> T

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

Constructor & Destructor Documentation

◆ BasicVectorHolder()

BasicVectorHolder ( )
inline

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

228 { }

◆ ~BasicVectorHolder()

Member Function Documentation

◆ Clear()

void Clear ( )
inline

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

References KaldiObjectHolder< KaldiType >::t_.

260 { t_.clear(); }

◆ ExtractRange()

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

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

References KALDI_ERR.

326  {
327  KALDI_ERR << "ExtractRange is not defined for this type of holder.";
328  return false;
329  }
#define KALDI_ERR
Definition: kaldi-error.h:147

◆ IsReadInBinary()

static bool IsReadInBinary ( )
inlinestatic

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

317 { return true; }

◆ KALDI_DISALLOW_COPY_AND_ASSIGN()

KALDI_DISALLOW_COPY_AND_ASSIGN ( BasicVectorHolder< BasicType >  )
private

◆ Read()

bool Read ( std::istream &  is)
inline

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

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

263  {
264  t_.clear();
265  bool is_binary;
266  if (!InitKaldiInputStream(is, &is_binary)) {
267  KALDI_WARN << "Reading Table object [integer type], failed reading binary"
268  " header\n";
269  return false;
270  }
271  if (!is_binary) {
272  // In text mode, we terminate with newline.
273  std::string line;
274  getline(is, line); // this will discard the \n, if present.
275  if (is.fail()) {
276  KALDI_WARN << "BasicVectorHolder::Read, error reading line " <<
277  (is.eof() ? "[eof]" : "");
278  return false; // probably eof. fail in any case.
279  }
280  std::istringstream line_is(line);
281  try {
282  while (1) {
283  line_is >> std::ws; // eat up whitespace.
284  if (line_is.eof()) break;
285  BasicType bt;
286  ReadBasicType(line_is, false, &bt);
287  t_.push_back(bt);
288  }
289  return true;
290  } catch(const std::exception &e) {
291  KALDI_WARN << "BasicVectorHolder::Read, could not interpret line: "
292  << "'" << line << "'" << "\n" << e.what();
293  return false;
294  }
295  } else { // binary mode.
296  size_t filepos = is.tellg();
297  try {
298  int32 size;
299  ReadBasicType(is, true, &size);
300  t_.resize(size);
301  for (typename std::vector<BasicType>::iterator iter = t_.begin();
302  iter != t_.end();
303  ++iter) {
304  ReadBasicType(is, true, &(*iter));
305  }
306  return true;
307  } catch(...) {
308  KALDI_WARN << "BasicVectorHolder::Read, read error or unexpected data"
309  " at archive entry beginning at file position " << filepos;
310  return false;
311  }
312  }
313  }
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 ( BasicVectorHolder< BasicType > *  other)
inline

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

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

321  {
322  t_.swap(other->t_);
323  }

◆ Value()

T& Value ( )
inline

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

References KaldiObjectHolder< KaldiType >::t_.

319 { return t_; }

◆ Write()

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

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

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

230  {
231  InitKaldiOutputStream(os, binary); // Puts binary header if binary mode.
232  try {
233  if (binary) { // need to write the size, in binary mode.
234  KALDI_ASSERT(static_cast<size_t>(static_cast<int32>(t.size())) ==
235  t.size());
236  // Or this Write routine cannot handle such a large vector.
237  // use int32 because it's fixed size regardless of compilation.
238  // change to int64 (plus in Read function) if this becomes a problem.
239  WriteBasicType(os, binary, static_cast<int32>(t.size()));
240  for (typename std::vector<BasicType>::const_iterator iter = t.begin();
241  iter != t.end(); ++iter)
242  WriteBasicType(os, binary, *iter);
243 
244  } else {
245  for (typename std::vector<BasicType>::const_iterator iter = t.begin();
246  iter != t.end(); ++iter)
247  WriteBasicType(os, binary, *iter);
248  os << '\n'; // Makes output format more readable and
249  // easier to manipulate. In text mode, this function writes something
250  // like "1 2 3\n".
251  }
252  return os.good();
253  } catch(const std::exception &e) {
254  KALDI_WARN << "Exception caught writing Table object (BasicVector). "
255  << e.what();
256  return false; // Write failure.
257  }
258  }
#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 334 of file kaldi-holder-inl.h.

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


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