From 050bd619f81cf766f37ccdac4a647ca3b3b7e624 Mon Sep 17 00:00:00 2001 From: Kharec Date: Thu, 11 Dec 2025 14:25:08 +0100 Subject: [PATCH] feat: make the reverse map static so it's built once and shared across all MorseDecoder instances --- main.cpp | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/main.cpp b/main.cpp index e095695..f04284b 100644 --- a/main.cpp +++ b/main.cpp @@ -42,16 +42,21 @@ string read_input(const string &prompt) { class MorseDecoder { private: - map morse_to_char; - -public: - MorseDecoder() { - for (const auto &[ch, code] : morse_map) { - morse_to_char[code] = ch; + static const map &get_morse_to_char() { + static map 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 : '?'; }