All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
StringRepository< Label, StringId > Class Template Reference

#include <determinize-star-inl.h>

Collaboration diagram for StringRepository< Label, StringId >:

Classes

class  VectorEqual
 
class  VectorKey
 

Public Types

typedef unordered_map< const
vector< Label > *, StringId,
VectorKey, VectorEqual
MapType
 

Public Member Functions

StringId IdOfEmpty ()
 
StringId IdOfLabel (Label l)
 
StringId IdOfSeq (const vector< Label > &v)
 
bool IsEmptyString (StringId id)
 
void SeqOfId (StringId id, vector< Label > *v)
 
StringId RemovePrefix (StringId id, size_t prefix_len)
 
 StringRepository ()
 
void Destroy ()
 
 ~StringRepository ()
 

Private Member Functions

 KALDI_DISALLOW_COPY_AND_ASSIGN (StringRepository)
 
StringId IdOfSeqInternal (const vector< Label > &v)
 

Private Attributes

vector< vector< Label > * > vec_
 
MapType map_
 
StringId string_end
 
StringId no_symbol
 
StringId single_symbol_start
 
StringId single_symbol_range
 

Static Private Attributes

static const StringId string_start = (StringId) 0
 

Detailed Description

template<class Label, class StringId>
class fst::StringRepository< Label, StringId >

Definition at line 38 of file determinize-star-inl.h.

Member Typedef Documentation

typedef unordered_map<const vector<Label>*, StringId, VectorKey, VectorEqual> MapType

Definition at line 65 of file determinize-star-inl.h.

Constructor & Destructor Documentation

StringRepository ( )
inline

Definition at line 111 of file determinize-star-inl.h.

References StringRepository< Label, StringId >::no_symbol, StringRepository< Label, StringId >::single_symbol_range, StringRepository< Label, StringId >::single_symbol_start, and StringRepository< Label, StringId >::string_end.

111  {
112  // The following are really just constants but don't want to complicate compilation so make them
113  // class variables. Due to the brokenness of <limits>, they can't be accessed as constants.
114  string_end = (numeric_limits<StringId>::max() / 2) - 1; // all hash values must be <= this.
115  no_symbol = (numeric_limits<StringId>::max() / 2); // reserved for empty sequence.
116  single_symbol_start = (numeric_limits<StringId>::max() / 2) + 1;
117  single_symbol_range = numeric_limits<StringId>::max() - single_symbol_start;
118  }
~StringRepository ( )
inline

Definition at line 127 of file determinize-star-inl.h.

References StringRepository< Label, StringId >::Destroy().

127  {
128  Destroy();
129  }

Member Function Documentation

void Destroy ( )
inline

Definition at line 119 of file determinize-star-inl.h.

References StringRepository< Label, StringId >::map_, and StringRepository< Label, StringId >::vec_.

Referenced by StringRepository< Label, StringId >::~StringRepository().

119  {
120  for (typename vector<vector<Label>* >::iterator iter = vec_.begin(); iter != vec_.end(); ++iter)
121  delete *iter;
122  vector<vector<Label>* > tmp_vec;
123  tmp_vec.swap(vec_);
124  MapType tmp_map;
125  tmp_map.swap(map_);
126  }
unordered_map< const vector< Label > *, StringId, VectorKey, VectorEqual > MapType
vector< vector< Label > * > vec_
StringId IdOfEmpty ( )
inline
StringId IdOfLabel ( Label  l)
inline

Definition at line 69 of file determinize-star-inl.h.

References StringRepository< Label, StringId >::IdOfSeqInternal(), StringRepository< Label, StringId >::single_symbol_range, and StringRepository< Label, StringId >::single_symbol_start.

Referenced by StringRepository< Label, StringId >::IdOfSeq(), and fst::TestStringRepository().

69  {
70  if (l>= 0 && l <= (Label) single_symbol_range) {
71  return l + single_symbol_start;
72  } else {
73  // l is out of the allowed range so we have to treat it as a sequence of length one. Should be v. rare.
74  vector<Label> v; v.push_back(l);
75  return IdOfSeqInternal(v);
76  }
77  }
StringId IdOfSeqInternal(const vector< Label > &v)
fst::StdArc::Label Label
StringId IdOfSeq ( const vector< Label > &  v)
inline

Definition at line 79 of file determinize-star-inl.h.

References StringRepository< Label, StringId >::IdOfLabel(), StringRepository< Label, StringId >::IdOfSeqInternal(), and StringRepository< Label, StringId >::no_symbol.

Referenced by StringRepository< Label, StringId >::RemovePrefix(), and fst::TestStringRepository().

79  { // also works for sizes 0 and 1.
80  size_t sz = v.size();
81  if (sz == 0) return no_symbol;
82  else if (v.size() == 1) return IdOfLabel(v[0]);
83  else return IdOfSeqInternal(v);
84  }
StringId IdOfSeqInternal(const vector< Label > &v)
StringId IdOfLabel(Label l)
StringId IdOfSeqInternal ( const vector< Label > &  v)
inlineprivate

Definition at line 134 of file determinize-star-inl.h.

References StringRepository< Label, StringId >::map_, StringRepository< Label, StringId >::string_end, and StringRepository< Label, StringId >::vec_.

Referenced by StringRepository< Label, StringId >::IdOfLabel(), and StringRepository< Label, StringId >::IdOfSeq().

134  {
135  typename MapType::iterator iter = map_.find(&v);
136  if (iter != map_.end()) {
137  return iter->second;
138  } else { // must add it to map.
139  StringId this_id = (StringId) vec_.size();
140  vector<Label> *v_new = new vector<Label> (v);
141  vec_.push_back(v_new);
142  map_[v_new] = this_id;
143  assert(this_id < string_end); // or we used up the labels.
144  return this_id;
145  }
146  }
vector< vector< Label > * > vec_
bool IsEmptyString ( StringId  id)
inline

Definition at line 86 of file determinize-star-inl.h.

References StringRepository< Label, StringId >::no_symbol.

86  {
87  return id == no_symbol;
88  }
KALDI_DISALLOW_COPY_AND_ASSIGN ( StringRepository< Label, StringId >  )
private
StringId RemovePrefix ( StringId  id,
size_t  prefix_len 
)
inline

Definition at line 98 of file determinize-star-inl.h.

References rnnlm::i, StringRepository< Label, StringId >::IdOfSeq(), and StringRepository< Label, StringId >::SeqOfId().

Referenced by fst::TestStringRepository().

98  {
99  if (prefix_len == 0) return id;
100  else {
101  vector<Label> v;
102  SeqOfId(id, &v);
103  size_t sz = v.size();
104  assert(sz >= prefix_len);
105  vector<Label> v_noprefix(sz - prefix_len);
106  for (size_t i = 0;i < sz-prefix_len;i++) v_noprefix[i] = v[i+prefix_len];
107  return IdOfSeq(v_noprefix);
108  }
109  }
StringId IdOfSeq(const vector< Label > &v)
void SeqOfId(StringId id, vector< Label > *v)
void SeqOfId ( StringId  id,
vector< Label > *  v 
)
inline

Definition at line 89 of file determinize-star-inl.h.

References StringRepository< Label, StringId >::no_symbol, StringRepository< Label, StringId >::single_symbol_start, and StringRepository< Label, StringId >::vec_.

Referenced by StringRepository< Label, StringId >::RemovePrefix(), and fst::TestStringRepository().

89  {
90  if (id == no_symbol) v->clear();
91  else if (id>=single_symbol_start) {
92  v->resize(1); (*v)[0] = id - single_symbol_start;
93  } else {
94  assert(static_cast<size_t>(id) < vec_.size());
95  *v = *(vec_[id]);
96  }
97  }
vector< vector< Label > * > vec_

Member Data Documentation

StringId single_symbol_range
private
const StringId string_start = (StringId) 0
staticprivate

Definition at line 151 of file determinize-star-inl.h.


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