feat: make the reverse map static so it's built once and shared across all MorseDecoder instances
This commit is contained in:
13
main.cpp
13
main.cpp
@@ -42,16 +42,21 @@ string read_input(const string &prompt) {
|
|||||||
|
|
||||||
class MorseDecoder {
|
class MorseDecoder {
|
||||||
private:
|
private:
|
||||||
map<string, char> morse_to_char;
|
static const map<string, char> &get_morse_to_char() {
|
||||||
|
static map<string, char> morse_to_char;
|
||||||
public:
|
static bool initialized = false;
|
||||||
MorseDecoder() {
|
if (!initialized) {
|
||||||
for (const auto &[ch, code] : morse_map) {
|
for (const auto &[ch, code] : morse_map) {
|
||||||
morse_to_char[code] = ch;
|
morse_to_char[code] = ch;
|
||||||
}
|
}
|
||||||
|
initialized = true;
|
||||||
|
}
|
||||||
|
return morse_to_char;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public:
|
||||||
char decode(const string &morse) {
|
char decode(const string &morse) {
|
||||||
|
const auto &morse_to_char = get_morse_to_char();
|
||||||
auto it = morse_to_char.find(morse);
|
auto it = morse_to_char.find(morse);
|
||||||
return it != morse_to_char.end() ? it->second : '?';
|
return it != morse_to_char.end() ? it->second : '?';
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user