From 0d8120eeb2e3de692705d0eda0ddf8e0e3aea4f1 Mon Sep 17 00:00:00 2001 From: Timothy Flynn Date: Tue, 11 Jan 2022 18:50:15 -0500 Subject: LibUnicode: Perform number system lookups by enumeration value Now that number systems are generated as an enum, we can generated the number system data in the order of that enum. This lets us perform lookups of that data by index instead of a loop of string comparisons. --- .../LibUnicode/GenerateUnicodeNumberFormat.cpp | 53 +++++++++++++--------- 1 file changed, 31 insertions(+), 22 deletions(-) (limited to 'Meta/Lagom/Tools') diff --git a/Meta/Lagom/Tools/CodeGenerators/LibUnicode/GenerateUnicodeNumberFormat.cpp b/Meta/Lagom/Tools/CodeGenerators/LibUnicode/GenerateUnicodeNumberFormat.cpp index 4bc5988f52..605f9d21e5 100644 --- a/Meta/Lagom/Tools/CodeGenerators/LibUnicode/GenerateUnicodeNumberFormat.cpp +++ b/Meta/Lagom/Tools/CodeGenerators/LibUnicode/GenerateUnicodeNumberFormat.cpp @@ -135,7 +135,7 @@ using NumericSymbolList = Vector; struct NumberSystem { unsigned hash() const { - auto hash = pair_int_hash(system, symbols); + auto hash = int_hash(symbols); hash = pair_int_hash(hash, primary_grouping_size); hash = pair_int_hash(hash, secondary_grouping_size); hash = pair_int_hash(hash, decimal_format); @@ -152,8 +152,7 @@ struct NumberSystem { bool operator==(NumberSystem const& other) const { - return (system == other.system) - && (symbols == other.symbols) + return (symbols == other.symbols) && (primary_grouping_size == other.primary_grouping_size) && (secondary_grouping_size == other.secondary_grouping_size) && (decimal_format == other.decimal_format) @@ -167,7 +166,6 @@ struct NumberSystem { && (scientific_format == other.scientific_format); } - StringIndexType system { 0 }; NumericSymbolListIndexType symbols { 0 }; u8 primary_grouping_size { 0 }; @@ -191,8 +189,7 @@ struct AK::Formatter : Formatter { ErrorOr format(FormatBuilder& builder, NumberSystem const& system) { return Formatter::format(builder, - "{{ {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {} }}", - system.system, + "{{ {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {} }}", system.symbols, system.primary_grouping_size, system.secondary_grouping_size, @@ -256,7 +253,7 @@ struct AK::Traits : public GenericTraits { }; struct Locale { - HashMap number_systems; + Vector number_systems; HashMap units {}; }; @@ -459,13 +456,18 @@ static ErrorOr parse_number_systems(String locale_numbers_path, UnicodeLoc auto const& locale_object = main_object.as_object().get(numbers_path.parent().basename()); auto const& locale_numbers_object = locale_object.as_object().get("numbers"sv); - HashMap number_systems; + Vector> number_systems; + number_systems.resize(locale_data.number_systems.size()); auto ensure_number_system = [&](auto const& system) -> NumberSystem& { - return number_systems.ensure(system, [&]() { - auto system_index = locale_data.unique_strings.ensure(system); - return NumberSystem { .system = system_index }; - }); + auto system_index = locale_data.number_systems.find_first_index(system).value(); + VERIFY(system_index < number_systems.size()); + + auto& number_system = number_systems.at(system_index); + if (!number_system.has_value()) + number_system = NumberSystem {}; + + return number_system.value(); }; auto parse_number_format = [&](auto const& format_object) { @@ -593,9 +595,14 @@ static ErrorOr parse_number_systems(String locale_numbers_path, UnicodeLoc } }); + locale.number_systems.ensure_capacity(number_systems.size()); + for (auto& number_system : number_systems) { - auto system_index = locale_data.unique_systems.ensure(move(number_system.value)); - locale.number_systems.set(number_system.key, system_index); + NumberSystemIndexType system_index = 0; + if (number_system.has_value()) + system_index = locale_data.unique_systems.ensure(number_system.release_value()); + + locale.number_systems.append(system_index); } return {}; @@ -833,7 +840,6 @@ struct NumberFormatImpl { }; struct NumberSystemData { - @string_index_type@ system { 0 }; @numeric_symbol_list_index_type@ symbols { 0 }; u8 primary_grouping_size { 0 }; @@ -920,17 +926,20 @@ static NumberSystemData const* find_number_system(StringView locale, StringView if (!locale_value.has_value()) return nullptr; + auto number_system_value = number_system_from_string(system); + if (!number_system_value.has_value()) + return nullptr; + auto locale_index = to_underlying(*locale_value) - 1; // Subtract 1 because 0 == Locale::None. - auto const& number_systems = s_locale_number_systems.at(locale_index); + auto number_system_index = to_underlying(*number_system_value); - for (auto system_index : number_systems) { - auto const& number_system = s_number_systems.at(system_index); + auto const& number_systems = s_locale_number_systems.at(locale_index); + number_system_index = number_systems.at(number_system_index); - if (system == s_string_list[number_system.system]) - return &number_system; - }; + if (number_system_index == 0) + return nullptr; - return nullptr; + return &s_number_systems.at(number_system_index); } Optional get_number_system_symbol(StringView locale, StringView system, NumericSymbol symbol) -- cgit v1.2.3