Electroneum
Language::Base Class Reference

A base language class which all languages have to inherit from for Polymorphism. More...

#include <language_base.h>

Inheritance diagram for Language::Base:
Collaboration diagram for Language::Base:

Public Member Functions

 Base (const char *language_name, const char *english_language_name, const std::vector< std::string > &words, uint32_t prefix_length)
 
virtual ~Base ()
 
void set_words (const char *const words[])
 
const std::vector< std::string > & get_word_list () const
 Returns a pointer to the word list. More...
 
const std::unordered_map< epee::wipeable_string, uint32_t, WordHash, WordEqual > & get_word_map () const
 Returns a pointer to the word map. More...
 
const std::unordered_map< epee::wipeable_string, uint32_t, WordHash, WordEqual > & get_trimmed_word_map () const
 Returns a pointer to the trimmed word map. More...
 
const std::string & get_language_name () const
 Returns the name of the language. More...
 
const std::string & get_english_language_name () const
 Returns the name of the language in English. More...
 
uint32_t get_unique_prefix_length () const
 Returns the number of unique starting characters to be used for matching. More...
 

Protected Types

enum  { ALLOW_SHORT_WORDS = 1<<0, ALLOW_DUPLICATE_PREFIXES = 1<<1 }
 
enum  { NWORDS = 1626 }
 

Protected Member Functions

void populate_maps (uint32_t flags=0)
 Populates the word maps after the list is ready. More...
 

Protected Attributes

std::vector< std::string > word_list
 
std::unordered_map< epee::wipeable_string, uint32_t, WordHash, WordEqualword_map
 
std::unordered_map< epee::wipeable_string, uint32_t, WordHash, WordEqualtrimmed_word_map
 
std::string language_name
 
std::string english_language_name
 
uint32_t unique_prefix_length
 

Detailed Description

A base language class which all languages have to inherit from for Polymorphism.

Definition at line 168 of file language_base.h.

Member Enumeration Documentation

◆ anonymous enum

anonymous enum
protected
Enumerator
ALLOW_SHORT_WORDS 
ALLOW_DUPLICATE_PREFIXES 

Definition at line 171 of file language_base.h.

◆ anonymous enum

anonymous enum
protected
Enumerator
NWORDS 

Definition at line 175 of file language_base.h.

175  {
176  NWORDS = 1626
177  };

Constructor & Destructor Documentation

◆ Base()

Language::Base::Base ( const char *  language_name,
const char *  english_language_name,
const std::vector< std::string > &  words,
uint32_t  prefix_length 
)
inline

Definition at line 223 of file language_base.h.

223  :
224  word_list(words),
225  unique_prefix_length(prefix_length),
228  {
229  }
std::vector< std::string > word_list
std::string language_name
std::string english_language_name
uint32_t unique_prefix_length

◆ ~Base()

virtual Language::Base::~Base ( )
inlinevirtual

Definition at line 230 of file language_base.h.

231  {
232  }

Member Function Documentation

◆ get_english_language_name()

const std::string& Language::Base::get_english_language_name ( ) const
inline

Returns the name of the language in English.

Returns
Name of the language.

Definition at line 275 of file language_base.h.

276  {
277  return english_language_name;
278  }
std::string english_language_name

◆ get_language_name()

const std::string& Language::Base::get_language_name ( ) const
inline

Returns the name of the language.

Returns
Name of the language.

Definition at line 267 of file language_base.h.

268  {
269  return language_name;
270  }
std::string language_name
Here is the caller graph for this function:

◆ get_trimmed_word_map()

const std::unordered_map<epee::wipeable_string, uint32_t, WordHash, WordEqual>& Language::Base::get_trimmed_word_map ( ) const
inline

Returns a pointer to the trimmed word map.

Returns
A pointer to the trimmed word map.

Definition at line 259 of file language_base.h.

260  {
261  return trimmed_word_map;
262  }
std::unordered_map< epee::wipeable_string, uint32_t, WordHash, WordEqual > trimmed_word_map

◆ get_unique_prefix_length()

uint32_t Language::Base::get_unique_prefix_length ( ) const
inline

Returns the number of unique starting characters to be used for matching.

Returns
Number of unique starting characters.

Definition at line 283 of file language_base.h.

284  {
285  return unique_prefix_length;
286  }
uint32_t unique_prefix_length

◆ get_word_list()

const std::vector<std::string>& Language::Base::get_word_list ( ) const
inline

Returns a pointer to the word list.

Returns
A pointer to the word list.

Definition at line 243 of file language_base.h.

244  {
245  return word_list;
246  }
std::vector< std::string > word_list
Here is the caller graph for this function:

◆ get_word_map()

const std::unordered_map<epee::wipeable_string, uint32_t, WordHash, WordEqual>& Language::Base::get_word_map ( ) const
inline

Returns a pointer to the word map.

Returns
A pointer to the word map.

Definition at line 251 of file language_base.h.

252  {
253  return word_map;
254  }
std::unordered_map< epee::wipeable_string, uint32_t, WordHash, WordEqual > word_map

◆ populate_maps()

void Language::Base::populate_maps ( uint32_t  flags = 0)
inlineprotected

Populates the word maps after the list is ready.

Definition at line 187 of file language_base.h.

188  {
189  int ii;
190  std::vector<std::string>::const_iterator it;
191  if (word_list.size () != NWORDS)
192  throw std::runtime_error("Wrong word list length for " + language_name);
193  for (it = word_list.begin(), ii = 0; it != word_list.end(); it++, ii++)
194  {
195  word_map[*it] = ii;
196  if ((*it).size() < unique_prefix_length)
197  {
198  if (flags & ALLOW_SHORT_WORDS)
199  MWARNING(language_name << " word '" << *it << "' is shorter than its prefix length, " << unique_prefix_length);
200  else
201  throw std::runtime_error("Too short word in " + language_name + " word list: " + *it);
202  }
203  epee::wipeable_string trimmed;
204  if (it->length() > unique_prefix_length)
205  {
206  trimmed = utf8prefix(*it, unique_prefix_length);
207  }
208  else
209  {
210  trimmed = *it;
211  }
212  if (trimmed_word_map.find(trimmed) != trimmed_word_map.end())
213  {
214  if (flags & ALLOW_DUPLICATE_PREFIXES)
215  MWARNING("Duplicate prefix in " << language_name << " word list: " << std::string(trimmed.data(), trimmed.size()));
216  else
217  throw std::runtime_error("Duplicate prefix in " + language_name + " word list: " + std::string(trimmed.data(), trimmed.size()));
218  }
219  trimmed_word_map[trimmed] = ii;
220  }
221  }
size_t size() const noexcept
::std::string string
Definition: gtest-port.h:1097
std::vector< std::string > word_list
std::unordered_map< epee::wipeable_string, uint32_t, WordHash, WordEqual > word_map
std::string language_name
std::unordered_map< epee::wipeable_string, uint32_t, WordHash, WordEqual > trimmed_word_map
#define MWARNING(x)
Definition: misc_log_ex.h:74
const char * data() const noexcept
uint32_t unique_prefix_length
T utf8prefix(const T &s, size_t count)
Returns a string made of (at most) the first count characters in s. Assumes well formedness. No check is made for this.
Definition: language_base.h:60

◆ set_words()

void Language::Base::set_words ( const char *const  words[])
inline

Definition at line 233 of file language_base.h.

234  {
235  word_list.resize(NWORDS);
236  for (size_t i = 0; i < NWORDS; ++i)
237  word_list[i] = words[i];
238  }
std::vector< std::string > word_list

Member Data Documentation

◆ english_language_name

std::string Language::Base::english_language_name
protected

Name of language

Definition at line 182 of file language_base.h.

◆ language_name

std::string Language::Base::language_name
protected

Name of language

Definition at line 181 of file language_base.h.

◆ trimmed_word_map

std::unordered_map<epee::wipeable_string, uint32_t, WordHash, WordEqual> Language::Base::trimmed_word_map
protected

hash table to find word's trimmed index

Definition at line 180 of file language_base.h.

◆ unique_prefix_length

uint32_t Language::Base::unique_prefix_length
protected

Number of unique starting characters to trim the wordlist to when matching

Definition at line 183 of file language_base.h.

◆ word_list

std::vector<std::string> Language::Base::word_list
protected

A pointer to the array of words

Definition at line 178 of file language_base.h.

◆ word_map

std::unordered_map<epee::wipeable_string, uint32_t, WordHash, WordEqual> Language::Base::word_map
protected

hash table to find word's index

Definition at line 179 of file language_base.h.


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