feat: make the reverse map static so it's built once and shared across all MorseDecoder instances

This commit is contained in:
2025-12-11 14:25:08 +01:00
parent 9dec568a3f
commit 050bd619f8

View File

@@ -42,16 +42,21 @@ string read_input(const string &prompt) {
class MorseDecoder {
private:
map<string, char> morse_to_char;
public:
MorseDecoder() {
for (const auto &[ch, code] : morse_map) {
morse_to_char[code] = ch;
static const map<string, char> &get_morse_to_char() {
static map<string, char> morse_to_char;
static bool initialized = false;
if (!initialized) {
for (const auto &[ch, code] : morse_map) {
morse_to_char[code] = ch;
}
initialized = true;
}
return morse_to_char;
}
public:
char decode(const string &morse) {
const auto &morse_to_char = get_morse_to_char();
auto it = morse_to_char.find(morse);
return it != morse_to_char.end() ? it->second : '?';
}