diff options
author | hop311 <hop3114@gmail.com> | 2024-01-22 21:02:58 +0100 |
---|---|---|
committer | hop311 <hop3114@gmail.com> | 2024-01-23 23:14:53 +0100 |
commit | 268a6948c0400905dfc335427395519689f067f5 (patch) | |
tree | b30e9b5774130552fe97e27deaf0370d83920c43 /src/openvic-simulation/map | |
parent | d4e597da089a81f719a9c33b46111d1c2c590124 (diff) |
Added reserve_more, expect_dictionary_key[s|_map]_reserve_length[_and_default]reserve_more
Diffstat (limited to 'src/openvic-simulation/map')
-rw-r--r-- | src/openvic-simulation/map/Map.cpp | 146 | ||||
-rw-r--r-- | src/openvic-simulation/map/Province.cpp | 2 | ||||
-rw-r--r-- | src/openvic-simulation/map/Region.cpp | 4 | ||||
-rw-r--r-- | src/openvic-simulation/map/Region.hpp | 1 | ||||
-rw-r--r-- | src/openvic-simulation/map/TerrainType.cpp | 7 |
5 files changed, 89 insertions, 71 deletions
diff --git a/src/openvic-simulation/map/Map.cpp b/src/openvic-simulation/map/Map.cpp index 1415e2c..9a97143 100644 --- a/src/openvic-simulation/map/Map.cpp +++ b/src/openvic-simulation/map/Map.cpp @@ -279,7 +279,7 @@ bool Map::set_water_province_list(std::vector<std::string_view> const& list) { return false; } bool ret = true; - water_provinces.reserve(water_provinces.size() + list.size()); + water_provinces.reserve_more(list.size()); for (std::string_view const& identifier : list) { ret &= set_water_province(identifier); } @@ -521,6 +521,7 @@ bool Map::load_province_definitions(std::vector<LineObject> const& lines) { Logger::error("No header or entries in province definition file!"); return false; } + { LineObject const& header = lines.front(); if (!_validate_province_definitions_header(header)) { @@ -529,11 +530,14 @@ bool Map::load_province_definitions(std::vector<LineObject> const& lines) { ); } } + if (lines.size() <= 1) { Logger::error("No entries in province definition file!"); return false; } - provinces.reserve(lines.size() - 1); + + reserve_more_provinces(lines.size() - 1); + bool ret = true; std::for_each(lines.begin() + 1, lines.end(), [this, &ret](LineObject const& line) -> void { const std::string_view identifier = line.get_value_for(0); @@ -546,7 +550,9 @@ bool Map::load_province_definitions(std::vector<LineObject> const& lines) { ret &= add_province(identifier, colour); } }); + lock_provinces(); + return ret; } @@ -557,7 +563,8 @@ bool Map::load_province_positions(BuildingTypeManager const& building_type_manag } bool Map::load_region_file(ast::NodeCPtr root) { - const bool ret = expect_dictionary_reserve_length(regions, + const bool ret = expect_dictionary_reserve_length( + regions, [this](std::string_view region_identifier, ast::NodeCPtr region_node) -> bool { Region::provinces_t provinces; bool ret = expect_list_reserve_length( @@ -567,7 +574,9 @@ bool Map::load_region_file(ast::NodeCPtr root) { return ret; } )(root); + lock_regions(); + for (Region const& region : regions.get_items()) { if (!region.meta) { for (Province const* province : region.get_provinces()) { @@ -575,6 +584,7 @@ bool Map::load_region_file(ast::NodeCPtr root) { } } } + return ret; } @@ -830,45 +840,48 @@ bool Map::generate_and_load_province_adjacencies(std::vector<LineObject> const& } bool Map::load_climate_file(ModifierManager const& modifier_manager, ast::NodeCPtr root) { - bool ret = expect_dictionary_reserve_length(climates, [this, &modifier_manager](std::string_view identifier, ast::NodeCPtr node) -> bool { - if (identifier.empty()) { - Logger::error("Invalid climate identifier - empty!"); - return false; - } + bool ret = expect_dictionary_reserve_length( + climates, + [this, &modifier_manager](std::string_view identifier, ast::NodeCPtr node) -> bool { + if (identifier.empty()) { + Logger::error("Invalid climate identifier - empty!"); + return false; + } - bool ret = true; - Climate* cur_climate = climates.get_item_by_identifier(identifier); - if (cur_climate == nullptr) { - ModifierValue values; - ret &= modifier_manager.expect_modifier_value(move_variable_callback(values))(node); - ret &= climates.add_item({ identifier, std::move(values) }); - } else { - ret &= expect_list_reserve_length(*cur_climate, expect_province_identifier( - [cur_climate, &identifier](Province& province) { - if (province.climate != cur_climate) { - cur_climate->add_province(&province); - if (province.climate != nullptr) { - Climate* old_climate = const_cast<Climate*>(province.climate); - old_climate->remove_province(&province); + bool ret = true; + Climate* cur_climate = climates.get_item_by_identifier(identifier); + if (cur_climate == nullptr) { + ModifierValue values; + ret &= modifier_manager.expect_modifier_value(move_variable_callback(values))(node); + ret &= climates.add_item({ identifier, std::move(values) }); + } else { + ret &= expect_list_reserve_length(*cur_climate, expect_province_identifier( + [cur_climate, &identifier](Province& province) { + if (province.climate != cur_climate) { + cur_climate->add_province(&province); + if (province.climate != nullptr) { + Climate* old_climate = const_cast<Climate*>(province.climate); + old_climate->remove_province(&province); + Logger::warning( + "Province with id ", province.get_identifier(), + " found in multiple climates: ", identifier, + " and ", old_climate->get_identifier() + ); + } + province.climate = cur_climate; + } else { Logger::warning( "Province with id ", province.get_identifier(), - " found in multiple climates: ", identifier, - " and ", old_climate->get_identifier() + " defined twice in climate ", identifier ); } - province.climate = cur_climate; - } else { - Logger::warning( - "Province with id ", province.get_identifier(), - " defined twice in climate ", identifier - ); + return true; } - return true; - } - ))(node); + ))(node); + } + return ret; } - return ret; - })(root); + )(root); for (Climate& climate : climates.get_items()) { climate.lock(); @@ -880,42 +893,45 @@ bool Map::load_climate_file(ModifierManager const& modifier_manager, ast::NodeCP } bool Map::load_continent_file(ModifierManager const& modifier_manager, ast::NodeCPtr root) { - bool ret = expect_dictionary_reserve_length(continents, [this, &modifier_manager](std::string_view identifier, ast::NodeCPtr node) -> bool { - if (identifier.empty()) { - Logger::error("Invalid continent identifier - empty!"); - return false; - } + bool ret = expect_dictionary_reserve_length( + continents, + [this, &modifier_manager](std::string_view identifier, ast::NodeCPtr node) -> bool { + if (identifier.empty()) { + Logger::error("Invalid continent identifier - empty!"); + return false; + } - ModifierValue values; - ProvinceSetModifier::provinces_t prov_list; - bool ret = modifier_manager.expect_modifier_value_and_keys(move_variable_callback(values), - "provinces", ONE_EXACTLY, expect_list_reserve_length(prov_list, expect_province_identifier( - [&prov_list](Province const& province) -> bool { - if (province.continent == nullptr) { - prov_list.emplace_back(&province); - } else { - Logger::warning("Province ", province, " found in multiple continents"); + ModifierValue values; + ProvinceSetModifier::provinces_t prov_list; + bool ret = modifier_manager.expect_modifier_value_and_keys(move_variable_callback(values), + "provinces", ONE_EXACTLY, expect_list_reserve_length(prov_list, expect_province_identifier( + [&prov_list](Province const& province) -> bool { + if (province.continent == nullptr) { + prov_list.emplace_back(&province); + } else { + Logger::warning("Province ", province, " found in multiple continents"); + } + return true; } - return true; - } - )) - )(node); + )) + )(node); - Continent continent = { identifier, std::move(values) }; - continent.add_provinces(prov_list); - continent.lock(); + Continent continent = { identifier, std::move(values) }; + continent.add_provinces(prov_list); + continent.lock(); - if (continents.add_item(std::move(continent))) { - Continent const& moved_continent = continents.get_items().back(); - for (Province const* prov : moved_continent.get_provinces()) { - remove_province_const(prov)->continent = &moved_continent; + if (continents.add_item(std::move(continent))) { + Continent const& moved_continent = continents.get_items().back(); + for (Province const* prov : moved_continent.get_provinces()) { + remove_province_const(prov)->continent = &moved_continent; + } + } else { + ret = false; } - } else { - ret = false; - } - return ret; - })(root); + return ret; + } + )(root); lock_continents(); diff --git a/src/openvic-simulation/map/Province.cpp b/src/openvic-simulation/map/Province.cpp index acabbf8..94c8dcb 100644 --- a/src/openvic-simulation/map/Province.cpp +++ b/src/openvic-simulation/map/Province.cpp @@ -77,7 +77,7 @@ bool Province::add_pop(Pop&& pop) { bool Province::add_pop_vec(std::vector<Pop> const& pop_vec) { if (!is_water()) { - pops.reserve(pops.size() + pop_vec.size()); + reserve_more(pops, pop_vec.size()); for (Pop const& pop : pop_vec) { pops.push_back(pop); } diff --git a/src/openvic-simulation/map/Region.cpp b/src/openvic-simulation/map/Region.cpp index 6f40338..c735279 100644 --- a/src/openvic-simulation/map/Region.cpp +++ b/src/openvic-simulation/map/Region.cpp @@ -83,6 +83,10 @@ void ProvinceSet::reserve(size_t size) { } } +void ProvinceSet::reserve_more(size_t size) { + OpenVic::reserve_more(*this, size); +} + bool ProvinceSet::contains_province(Province const* province) const { return province && std::find(provinces.begin(), provinces.end(), province) != provinces.end(); } diff --git a/src/openvic-simulation/map/Region.hpp b/src/openvic-simulation/map/Region.hpp index d8948b7..420d221 100644 --- a/src/openvic-simulation/map/Region.hpp +++ b/src/openvic-simulation/map/Region.hpp @@ -23,6 +23,7 @@ namespace OpenVic { bool empty() const; size_t size() const; void reserve(size_t size); + void reserve_more(size_t size); bool contains_province(Province const* province) const; provinces_t const& get_provinces() const; }; diff --git a/src/openvic-simulation/map/TerrainType.cpp b/src/openvic-simulation/map/TerrainType.cpp index a4529bf..e7dd068 100644 --- a/src/openvic-simulation/map/TerrainType.cpp +++ b/src/openvic-simulation/map/TerrainType.cpp @@ -130,11 +130,8 @@ TerrainTypeMapping::index_t TerrainTypeManager::get_terrain_texture_limit() cons } bool TerrainTypeManager::load_terrain_types(ModifierManager const& modifier_manager, ast::NodeCPtr root) { - const bool ret = expect_dictionary_keys_and_length_and_default( - [this](size_t size) -> size_t { - terrain_type_mappings.reserve(size - 2); - return size; - }, + const bool ret = expect_dictionary_keys_reserve_length_and_default( + terrain_type_mappings, std::bind_front(&TerrainTypeManager::_load_terrain_type_mapping, this), "terrain", ONE_EXACTLY, expect_uint(assign_variable_callback(terrain_texture_limit)), "categories", ONE_EXACTLY, _load_terrain_type_categories(modifier_manager) |