RandomAccessTableReaderUnsortedArchiveImpl< Holder > Class Template Reference

#include <kaldi-table-inl.h>

Inheritance diagram for RandomAccessTableReaderUnsortedArchiveImpl< Holder >:
Collaboration diagram for RandomAccessTableReaderUnsortedArchiveImpl< Holder >:

Public Member Functions

 RandomAccessTableReaderUnsortedArchiveImpl ()
 
virtual bool Close ()
 
virtual bool HasKey (const std::string &key)
 
virtual const TValue (const std::string &key)
 
virtual ~RandomAccessTableReaderUnsortedArchiveImpl ()
 
- Public Member Functions inherited from RandomAccessTableReaderArchiveImplBase< Holder >
 RandomAccessTableReaderArchiveImplBase ()
 
virtual bool Open (const std::string &rspecifier)
 
void ReadNextObject ()
 
virtual bool IsOpen () const
 
bool CloseInternal ()
 
 ~RandomAccessTableReaderArchiveImplBase ()
 
- Public Member Functions inherited from RandomAccessTableReaderImplBase< Holder >
virtual ~RandomAccessTableReaderImplBase ()
 

Private Types

typedef Holder::T T
 
typedef unordered_map< std::string, Holder *, StringHasherMapType
 

Private Member Functions

void HandlePendingDelete ()
 
bool FindKeyInternal (const std::string &key, const T **value_ptr=NULL)
 

Private Attributes

MapType map_
 
MapType::iterator to_delete_iter_
 
bool to_delete_iter_valid_
 
std::string first_deleted_string_
 

Additional Inherited Members

- Public Types inherited from RandomAccessTableReaderArchiveImplBase< Holder >
typedef Holder::T T
 
- Public Types inherited from RandomAccessTableReaderImplBase< Holder >
typedef Holder::T T
 
- Protected Types inherited from RandomAccessTableReaderArchiveImplBase< Holder >
enum  {
  kUninitialized, kNoObject, kHaveObject, kEof,
  kError
}
 
- Protected Attributes inherited from RandomAccessTableReaderArchiveImplBase< Holder >
std::string cur_key_
 
Holder * holder_
 
std::string rspecifier_
 
std::string archive_rxfilename_
 
RspecifierOptions opts_
 
enum kaldi::RandomAccessTableReaderArchiveImplBase:: { ... }  state_
 

Detailed Description

template<class Holder>
class kaldi::RandomAccessTableReaderUnsortedArchiveImpl< Holder >

Definition at line 2358 of file kaldi-table-inl.h.

Member Typedef Documentation

◆ MapType

typedef unordered_map<std::string, Holder*, StringHasher> MapType
private

Definition at line 2492 of file kaldi-table-inl.h.

◆ T

typedef Holder::T T
private

Definition at line 2373 of file kaldi-table-inl.h.

Constructor & Destructor Documentation

◆ RandomAccessTableReaderUnsortedArchiveImpl()

Definition at line 2376 of file kaldi-table-inl.h.

2376  : to_delete_iter_(map_.end()),
2377  to_delete_iter_valid_(false) {
2378  map_.max_load_factor(0.5); // make it quite empty -> quite efficient.
2379  // default seems to be 1.
2380  }

◆ ~RandomAccessTableReaderUnsortedArchiveImpl()

virtual ~RandomAccessTableReaderUnsortedArchiveImpl ( )
inlinevirtual

Definition at line 2406 of file kaldi-table-inl.h.

References TableWriter< Holder >::Close(), TableWriter< Holder >::IsOpen(), and KALDI_ERR.

2406  {
2407  if (this->IsOpen())
2408  if (!Close()) // more specific warning will already have been printed.
2409  // we are in some kind of error state & user did not find out by
2410  // calling Close().
2411  KALDI_ERR << "Error closing RandomAccessTableReader: rspecifier is "
2412  << rspecifier_;
2413  }
#define KALDI_ERR
Definition: kaldi-error.h:147

Member Function Documentation

◆ Close()

virtual bool Close ( )
inlinevirtual

Implements RandomAccessTableReaderImplBase< Holder >.

Definition at line 2382 of file kaldi-table-inl.h.

2382  {
2383  for (typename MapType::iterator iter = map_.begin();
2384  iter != map_.end();
2385  ++iter) {
2386  delete iter->second;
2387  }
2388  map_.clear();
2389  first_deleted_string_ = "";
2390  to_delete_iter_valid_ = false;
2391  return this->CloseInternal();
2392  }

◆ FindKeyInternal()

bool FindKeyInternal ( const std::string &  key,
const T **  value_ptr = NULL 
)
inlineprivate

Definition at line 2435 of file kaldi-table-inl.h.

References KALDI_ASSERT, and KALDI_ERR.

2435  {
2436  typename MapType::iterator iter = map_.find(key);
2437  if (iter != map_.end()) { // Found in the map...
2438  if (value_ptr == NULL) { // called from HasKey
2439  return true; // this is all we have to do.
2440  } else {
2441  *value_ptr = &(iter->second->Value());
2442  if (opts_.once) { // value won't be needed again, so mark
2443  // for deletion.
2444  to_delete_iter_ = iter; // pending delete.
2446  to_delete_iter_valid_ = true;
2447  }
2448  return true;
2449  }
2450  }
2451  while (state_ == kNoObject) {
2452  ReadNextObject();
2453  if (state_ == kHaveObject) { // Successfully read object.
2454  state_ = kNoObject; // we are about to transfer ownership
2455  // of the object in holder_ to map_.
2456  // Insert it into map_.
2457  std::pair<typename MapType::iterator, bool> pr =
2458  map_.insert(typename MapType::value_type(cur_key_, holder_));
2459 
2460  if (!pr.second) { // Was not inserted-- previous element w/ same key
2461  delete holder_; // map was not changed, no ownership transferred.
2462  holder_ = NULL;
2463  KALDI_ERR << "Error in RandomAccessTableReader: duplicate key "
2464  << cur_key_ << " in archive " << archive_rxfilename_;
2465  }
2466  holder_ = NULL; // ownership transferred to map_.
2467  if (cur_key_ == key) { // the one we wanted..
2468  if (value_ptr == NULL) { // called from HasKey
2469  return true;
2470  } else { // called from Value()
2471  *value_ptr = &(pr.first->second->Value()); // this gives us the
2472  // Value() from the Holder in the map.
2473  if (opts_.once) { // mark for deletion, as won't be needed again.
2474  to_delete_iter_ = pr.first;
2476  to_delete_iter_valid_ = true;
2477  }
2478  return true;
2479  }
2480  }
2481  }
2482  }
2483  if (opts_.once && key == first_deleted_string_) {
2484  KALDI_ERR << "You specified the once (o) option but "
2485  << "you are calling using key " << key
2486  << " more than once: rspecifier is " << rspecifier_;
2487  }
2488  return false; // We read the entire archive (or got to error state) and
2489  // didn't find it.
2490  }
enum kaldi::RandomAccessTableReaderArchiveImplBase::@5 state_
#define KALDI_ERR
Definition: kaldi-error.h:147
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185

◆ HandlePendingDelete()

void HandlePendingDelete ( )
inlineprivate

Definition at line 2415 of file kaldi-table-inl.h.

2415  {
2416  if (to_delete_iter_valid_) {
2417  to_delete_iter_valid_ = false;
2418  delete to_delete_iter_->second; // Delete Holder object.
2419  if (first_deleted_string_.length() == 0)
2421  map_.erase(to_delete_iter_); // delete that element.
2422  }
2423  }

◆ HasKey()

virtual bool HasKey ( const std::string &  key)
inlinevirtual

Implements RandomAccessTableReaderImplBase< Holder >.

Definition at line 2394 of file kaldi-table-inl.h.

2394  {
2396  return FindKeyInternal(key, NULL);
2397  }
bool FindKeyInternal(const std::string &key, const T **value_ptr=NULL)

◆ Value()

virtual const T& Value ( const std::string &  key)
inlinevirtual

Implements RandomAccessTableReaderImplBase< Holder >.

Definition at line 2398 of file kaldi-table-inl.h.

References KALDI_ERR, and kaldi::PrintableRxfilename().

2398  {
2400  const T *ans_ptr = NULL;
2401  if (!FindKeyInternal(key, &ans_ptr))
2402  KALDI_ERR << "Value() called but no such key " << key
2403  << " in archive " << PrintableRxfilename(archive_rxfilename_);
2404  return *ans_ptr;
2405  }
bool FindKeyInternal(const std::string &key, const T **value_ptr=NULL)
#define KALDI_ERR
Definition: kaldi-error.h:147
std::string PrintableRxfilename(const std::string &rxfilename)
PrintableRxfilename turns the rxfilename into a more human-readable form for error reporting...
Definition: kaldi-io.cc:61

Member Data Documentation

◆ first_deleted_string_

std::string first_deleted_string_
private

Definition at line 2498 of file kaldi-table-inl.h.

◆ map_

MapType map_
private

Definition at line 2493 of file kaldi-table-inl.h.

◆ to_delete_iter_

MapType::iterator to_delete_iter_
private

Definition at line 2495 of file kaldi-table-inl.h.

◆ to_delete_iter_valid_

bool to_delete_iter_valid_
private

Definition at line 2496 of file kaldi-table-inl.h.


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