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

 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 46 of file determinize-star-inl.h.

Member Typedef Documentation

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

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

Constructor & Destructor Documentation

StringRepository ( )
inline

Definition at line 119 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.

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

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

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

135  {
136  Destroy();
137  }

Member Function Documentation

void Destroy ( )
inline

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

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

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

127  {
128  for (typename vector<vector<Label>* >::iterator iter = vec_.begin(); iter != vec_.end(); ++iter)
129  delete *iter;
130  vector<vector<Label>* > tmp_vec;
131  tmp_vec.swap(vec_);
132  MapType tmp_map;
133  tmp_map.swap(map_);
134  }
unordered_map< const vector< Label > *, StringId, VectorKey, VectorEqual > MapType
vector< vector< Label > * > vec_
DISALLOW_COPY_AND_ASSIGN ( StringRepository< Label, StringId >  )
private
StringId IdOfEmpty ( )
inline
StringId IdOfLabel ( Label  l)
inline

Definition at line 77 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().

77  {
78  if (l>= 0 && l <= (Label) single_symbol_range) {
79  return l + single_symbol_start;
80  } else {
81  // l is out of the allowed range so we have to treat it as a sequence of length one. Should be v. rare.
82  vector<Label> v; v.push_back(l);
83  return IdOfSeqInternal(v);
84  }
85  }
StringId IdOfSeqInternal(const vector< Label > &v)
fst::StdArc::Label Label
StringId IdOfSeq ( const vector< Label > &  v)
inline

Definition at line 87 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().

87  { // also works for sizes 0 and 1.
88  size_t sz = v.size();
89  if (sz == 0) return no_symbol;
90  else if (v.size() == 1) return IdOfLabel(v[0]);
91  else return IdOfSeqInternal(v);
92  }
StringId IdOfSeqInternal(const vector< Label > &v)
StringId IdOfLabel(Label l)
StringId IdOfSeqInternal ( const vector< Label > &  v)
inlineprivate

Definition at line 142 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().

142  {
143  typename MapType::iterator iter = map_.find(&v);
144  if (iter != map_.end()) {
145  return iter->second;
146  } else { // must add it to map.
147  StringId this_id = (StringId) vec_.size();
148  vector<Label> *v_new = new vector<Label> (v);
149  vec_.push_back(v_new);
150  map_[v_new] = this_id;
151  assert(this_id < string_end); // or we used up the labels.
152  return this_id;
153  }
154  }
vector< vector< Label > * > vec_
bool IsEmptyString ( StringId  id)
inline

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

References StringRepository< Label, StringId >::no_symbol.

94  {
95  return id == no_symbol;
96  }
StringId RemovePrefix ( StringId  id,
size_t  prefix_len 
)
inline

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

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

Referenced by fst::TestStringRepository().

106  {
107  if (prefix_len == 0) return id;
108  else {
109  vector<Label> v;
110  SeqOfId(id, &v);
111  size_t sz = v.size();
112  assert(sz >= prefix_len);
113  vector<Label> v_noprefix(sz - prefix_len);
114  for (size_t i = 0;i < sz-prefix_len;i++) v_noprefix[i] = v[i+prefix_len];
115  return IdOfSeq(v_noprefix);
116  }
117  }
StringId IdOfSeq(const vector< Label > &v)
void SeqOfId(StringId id, vector< Label > *v)
void SeqOfId ( StringId  id,
vector< Label > *  v 
)
inline

Definition at line 97 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().

97  {
98  if (id == no_symbol) v->clear();
99  else if (id>=single_symbol_start) {
100  v->resize(1); (*v)[0] = id - single_symbol_start;
101  } else {
102  assert(static_cast<size_t>(id) < vec_.size());
103  *v = *(vec_[id]);
104  }
105  }
vector< vector< Label > * > vec_

Member Data Documentation

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

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


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