RandomAccessTableReaderSortedArchiveImpl< Holder > Class Template Reference

#include <kaldi-table-inl.h>

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

Classes

struct  PairCompare
 

Public Types

typedef Holder::T T
 
- Public Types inherited from RandomAccessTableReaderArchiveImplBase< Holder >
typedef Holder::T T
 
- Public Types inherited from RandomAccessTableReaderImplBase< Holder >
typedef Holder::T T
 

Public Member Functions

 RandomAccessTableReaderSortedArchiveImpl ()
 
virtual bool Close ()
 
virtual bool HasKey (const std::string &key)
 
virtual const TValue (const std::string &key)
 
virtual ~RandomAccessTableReaderSortedArchiveImpl ()
 
- 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 Member Functions

void HandlePendingDelete ()
 
bool FindKeyInternal (const std::string &key, size_t *index)
 

Private Attributes

std::vector< std::pair< std::string, Holder * > > seen_pairs_
 
size_t last_found_index_
 
size_t pending_delete_
 

Additional Inherited Members

- 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::RandomAccessTableReaderSortedArchiveImpl< Holder >

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

Member Typedef Documentation

◆ T

typedef Holder::T T

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

Constructor & Destructor Documentation

◆ RandomAccessTableReaderSortedArchiveImpl()

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

2185  :
2186  last_found_index_(static_cast<size_t>(-1)),
2187  pending_delete_(static_cast<size_t>(-1)) { }

◆ ~RandomAccessTableReaderSortedArchiveImpl()

virtual ~RandomAccessTableReaderSortedArchiveImpl ( )
inlinevirtual

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

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

2228  {
2229  if (this->IsOpen())
2230  if (!Close()) // more specific warning will already have been printed.
2231  // we are in some kind of error state & user did not find out by
2232  // calling Close().
2233  KALDI_ERR << "Error closing RandomAccessTableReader: rspecifier is "
2234  << rspecifier_;
2235  }
#define KALDI_ERR
Definition: kaldi-error.h:147

Member Function Documentation

◆ Close()

virtual bool Close ( )
inlinevirtual

Implements RandomAccessTableReaderImplBase< Holder >.

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

References rnnlm::i.

2189  {
2190  for (size_t i = 0; i < seen_pairs_.size(); i++)
2191  delete seen_pairs_[i].second;
2192  seen_pairs_.clear();
2193 
2194  pending_delete_ = static_cast<size_t>(-1);
2195  last_found_index_ = static_cast<size_t>(-1);
2196 
2197  return this->CloseInternal();
2198  }
std::vector< std::pair< std::string, Holder * > > seen_pairs_

◆ FindKeyInternal()

bool FindKeyInternal ( const std::string &  key,
size_t index 
)
inlineprivate

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

References KALDI_ASSERT, KALDI_ERR, and kaldi::PrintableRxfilename().

2258  {
2259  // First, an optimization in case the previous call was for the
2260  // same key, and we found it.
2261  if (last_found_index_ < seen_pairs_.size()
2262  && seen_pairs_[last_found_index_].first == key) {
2263  *index = last_found_index_;
2264  return true;
2265  }
2266 
2267  if (state_ == kUninitialized)
2268  KALDI_ERR << "Trying to access a RandomAccessTableReader object that is"
2269  " not open.";
2270 
2271  // Step one is to see whether we have to read ahead for the object..
2272  // Note, the possible states right now are kNoObject, kEof or kError.
2273  // We are never in the state kHaveObject except just after calling
2274  // ReadNextObject().
2275  bool looped = false;
2276  while (state_ == kNoObject &&
2277  (seen_pairs_.empty() || key.compare(seen_pairs_.back().first) > 0)) {
2278  looped = true;
2279  // Read this as:
2280  // while ( the stream is potentially good for reading &&
2281  // ([got no keys] || key > most_recent_key) ) { ...
2282  // Try to read a new object.
2283  // Note that the keys in seen_pairs_ are ordered from least to greatest.
2284  ReadNextObject();
2285  if (state_ == kHaveObject) { // Successfully read object.
2286  if (!seen_pairs_.empty() && // This is just a check.
2287  cur_key_.compare(seen_pairs_.back().first) <= 0) {
2288  // read the expression above as: !( cur_key_ > previous_key).
2289  // it means we are not in sorted order [the user specified that we
2290  // are, or we would not be using this implementation].
2291  KALDI_ERR << "You provided the sorted (s) option but keys in archive "
2292  << PrintableRxfilename(archive_rxfilename_) << " are not "
2293  << "in sorted order: " << seen_pairs_.back().first
2294  << " is followed by " << cur_key_;
2295  }
2296  KALDI_ASSERT(holder_ != NULL);
2297  seen_pairs_.push_back(std::make_pair(cur_key_, holder_));
2298  holder_ = NULL;
2299  state_ = kNoObject;
2300  }
2301  }
2302  if (looped) { // We only need to check the last element of the seen_pairs_
2303  // array, since we would not have read more after getting "key".
2304  if (!seen_pairs_.empty() && seen_pairs_.back().first == key) {
2305  last_found_index_ = *index = seen_pairs_.size() - 1;
2306  return true;
2307  } else {
2308  return false;
2309  }
2310  }
2311  // Now we have do an actual binary search in the seen_pairs_ array.
2312  std::pair<std::string, Holder*> pr(key, static_cast<Holder*>(NULL));
2313  typename std::vector<std::pair<std::string, Holder*> >::iterator
2314  iter = std::lower_bound(seen_pairs_.begin(), seen_pairs_.end(),
2315  pr, PairCompare());
2316  if (iter != seen_pairs_.end() &&
2317  key == iter->first) {
2318  last_found_index_ = *index = (iter - seen_pairs_.begin());
2319  return true;
2320  } else {
2321  return false;
2322  }
2323  }
std::vector< std::pair< std::string, Holder * > > seen_pairs_
enum kaldi::RandomAccessTableReaderArchiveImplBase::@5 state_
#define KALDI_ERR
Definition: kaldi-error.h:147
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185
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

◆ HandlePendingDelete()

void HandlePendingDelete ( )
inlineprivate

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

References KALDI_ASSERT.

2237  {
2238  const size_t npos = static_cast<size_t>(-1);
2239  if (pending_delete_ != npos) {
2241  KALDI_ASSERT(seen_pairs_[pending_delete_].second != NULL);
2242  delete seen_pairs_[pending_delete_].second;
2243  seen_pairs_[pending_delete_].second = NULL;
2244  pending_delete_ = npos;
2245  }
2246  }
std::vector< std::pair< std::string, Holder * > > seen_pairs_
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185

◆ HasKey()

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

Implements RandomAccessTableReaderImplBase< Holder >.

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

References KALDI_ERR.

2199  {
2201  size_t index;
2202  bool ans = FindKeyInternal(key, &index);
2203  if (ans && opts_.once && seen_pairs_[index].second == NULL) {
2204  // Just do a check RE the once option. "&&opts_.once" is for
2205  // efficiency since this can only happen in that case.
2206  KALDI_ERR << "Error: HasKey called after Value() already called for "
2207  << " that key, and once (o) option specified: rspecifier is "
2208  << rspecifier_;
2209  }
2210  return ans;
2211  }
std::vector< std::pair< std::string, Holder * > > seen_pairs_
#define KALDI_ERR
Definition: kaldi-error.h:147
bool FindKeyInternal(const std::string &key, size_t *index)

◆ Value()

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

Implements RandomAccessTableReaderImplBase< Holder >.

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

References KALDI_ERR, and kaldi::PrintableRxfilename().

2212  {
2214  size_t index;
2215  if (!FindKeyInternal(key, &index)) {
2216  KALDI_ERR << "Value() called but no such key " << key
2217  << " in archive " << PrintableRxfilename(archive_rxfilename_);
2218  }
2219  if (seen_pairs_[index].second == NULL) { // can happen if opts.once_
2220  KALDI_ERR << "Error: Value() called more than once for key "
2221  << key << " and once (o) option specified: rspecifier is "
2222  << rspecifier_;
2223  }
2224  if (opts_.once)
2225  pending_delete_ = index; // mark this index to be deleted on next call.
2226  return seen_pairs_[index].second->Value();
2227  }
std::vector< std::pair< std::string, Holder * > > seen_pairs_
#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
bool FindKeyInternal(const std::string &key, size_t *index)

Member Data Documentation

◆ last_found_index_

size_t last_found_index_
private

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

◆ pending_delete_

size_t pending_delete_
private

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

◆ seen_pairs_

std::vector<std::pair<std::string, Holder*> > seen_pairs_
private

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


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