Skip to content

Commit 57fb2bf

Browse files
committed
add: try_get
1 parent 8991b82 commit 57fb2bf

10 files changed

Lines changed: 62 additions & 114 deletions

File tree

Library/PAX_MAHOROBA/Rendering/FontSystem.hpp

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -267,23 +267,22 @@ namespace paxs {
267267
const std::uint_least64_t cache_key = createFontCacheKey(language_key, size, buffer_thickness);
268268

269269
// キャッシュに存在すればそれを返す
270-
const auto iterator = font_cache_.find(cache_key);
271-
if (iterator != font_cache_.end()) {
272-
return &(iterator->second);
270+
if (auto* cached_font = font_cache_.try_get(cache_key)) {
271+
return cached_font;
273272
}
274273

275274
// フォントパスを取得
276-
const auto path_it = language_font_paths_.find(language_key);
275+
const auto* path_ptr = language_font_paths_.try_get(language_key);
277276

278-
if (path_it == language_font_paths_.end()) {
277+
if (path_ptr == nullptr) {
279278
PAXS_WARNING("FontSystem::getFont - Font path not found for language key: " + std::to_string(language_key));
280279
return nullptr;
281280
}
282281

283282
// 新しいフォントを作成してキャッシュに追加
284283
font_cache_.emplace(
285284
cache_key,
286-
paxg::Font(static_cast<int>(size), path_it->second, static_cast<int>(buffer_thickness))
285+
paxg::Font(static_cast<int>(size), *path_ptr, static_cast<int>(buffer_thickness))
287286
);
288287

289288
return &(font_cache_.at(cache_key));

Library/PAX_MAHOROBA/UI/MenuBar/DropDownMenu.hpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -229,13 +229,13 @@ namespace paxs {
229229

230230
/// @brief 項目の状態を設定(キー指定)
231231
void setIsItems(const std::uint_least32_t key, const bool is_item) {
232-
if (item_index_key.find(key) == item_index_key.end()) {
232+
const auto* index = item_index_key.try_get(key);
233+
if (index == nullptr) {
233234
PAXS_WARNING("DropDownMenu: Key not found in item_index_key.");
234235
return;
235236
}
236-
const std::size_t index = item_index_key.at(key);
237-
if (index < is_items.size()) {
238-
is_items[index] = is_item;
237+
if (*index < is_items.size()) {
238+
is_items[*index] = is_item;
239239
}
240240
}
241241

Library/PAX_MAHOROBA/UI/MenuBar/MenuSystem.hpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -117,12 +117,12 @@ namespace paxs {
117117
/// @param type メニュー項目の種類
118118
/// @return メニュー項目のポインタ(存在しない場合はnullptr)
119119
paxs::DropDownMenu* getDropDownMenu(const paxs::MenuBarType type) {
120-
const auto iterator = menu_list_key.find(type);
121-
return iterator != menu_list_key.end() ? &menu_list[iterator->second] : nullptr;
120+
const auto* index_ptr = menu_list_key.try_get(type);
121+
return index_ptr != nullptr ? &menu_list[*index_ptr] : nullptr;
122122
}
123123
const paxs::DropDownMenu* getDropDownMenu(const paxs::MenuBarType type) const {
124-
const auto iterator = menu_list_key.find(type);
125-
return iterator != menu_list_key.end() ? &menu_list[iterator->second] : nullptr;
124+
const auto* index_ptr = menu_list_key.try_get(type);
125+
return index_ptr != nullptr ? &menu_list[*index_ptr] : nullptr;
126126
}
127127

128128
bool isHit(const paxs::Vector2<int>& pos) const override {

Library/PAX_SAPIENTICA/Core/Type/UnorderedMap.hpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,30 @@ namespace paxs {
107107
return iterator->second;
108108
}
109109

110+
/// @brief キーが存在すればその値を返し、存在しなければ default_value を返す
111+
/// @example auto val = map.value_or("key", 0);
112+
Value value_or(const Key& key, const Value& default_value) const {
113+
const auto iterator = map_.find(key);
114+
if (iterator != map_.end()) {
115+
return iterator->second;
116+
}
117+
return default_value;
118+
}
119+
120+
/// @brief キーが存在すれば値へのポインタを返し、存在しなければ nullptr を返す
121+
/// @note ポインタ経由で値を変更可能
122+
/// @example if (auto* val = map.try_get("key")) { *val = 10; }
123+
Value* try_get(const Key& key) {
124+
auto iterator = map_.find(key);
125+
return (iterator != map_.end()) ? &(iterator->second) : nullptr;
126+
}
127+
128+
/// @brief キーが存在すれば値へのポインタを返し、存在しなければ nullptr を返す (const版)
129+
const Value* try_get(const Key& key) const {
130+
auto iterator = map_.find(key);
131+
return (iterator != map_.end()) ? &(iterator->second) : nullptr;
132+
}
133+
110134
/// @brief キーが存在するかチェック
111135
bool contains(const Key& key) const {
112136
return map_.find(key) != map_.end();

Library/PAX_SAPIENTICA/IO/Data/SimulationRange.hpp

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,6 @@ namespace paxs {
4040

4141
paxs::UnorderedMap<std::uint_least32_t, StartAndEnd> path_list;
4242
public:
43-
44-
StartAndEnd operator[](const std::uint_least32_t key_) {
45-
return (path_list.find(key_) == path_list.end()) ? StartAndEnd{} : path_list.at(key_);
46-
}
47-
4843
paxs::Vector2<int>& getStart(const std::uint_least32_t key_) {
4944
return path_list.at(key_).start_position;
5045
}

Library/PAX_SAPIENTICA/Simulation/Config/JapanProvinces.hpp

Lines changed: 3 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -286,9 +286,6 @@ namespace paxs {
286286

287287
public:
288288
explicit JapanProvinces(const std::string& japan_provinces_path) noexcept {
289-
290-
//const std::string japan_region_tsv_path = japan_provinces_path + "/JapanRegion.tsv";
291-
292289
inputMtDNA_List(japan_provinces_path);
293290
inputMtDNA_Region(japan_provinces_path);
294291
inputLanguage_List(japan_provinces_path);
@@ -308,21 +305,6 @@ namespace paxs {
308305
}
309306
}
310307

311-
/// @brief 日本の地方区分のIDから人口を取得する
312-
/// @param id 日本の地方区分のID
313-
/// @return 人口
314-
/// @note IDが不正な場合は0を返す
315-
std::uint_least32_t getJapanRegionPopulation(const std::uint_least8_t id) const noexcept {
316-
for (const auto& japan_region : japan_regions) {
317-
if (japan_region.id == id) {
318-
return japan_region.population;
319-
}
320-
}
321-
PAXS_WARNING("Failed to get Japan region population: " + std::to_string(id));
322-
323-
return 0;
324-
}
325-
326308
// 言語を取得
327309
std::uint_least32_t getLanguage(const std::uint_least8_t id) const noexcept {
328310
for (auto& district : district_list) {
@@ -383,7 +365,7 @@ namespace paxs {
383365

384366
return district_list[0];
385367
}
386-
const District& cgetDistrict(const std::uint_least8_t id) const noexcept {
368+
const District& getDistrict(const std::uint_least8_t id) const noexcept {
387369
for (const auto& district : district_list) {
388370
if (district.id == id) {
389371
return district;
@@ -405,7 +387,7 @@ namespace paxs {
405387
auto& weight_list = language_region_list.at(district_list[0].language_region_hash);
406388
return weight_list.id[weight_list.dist(gen)];
407389
}
408-
const std::string& getLanguage_Name(const std::uint_least8_t id) const noexcept {
390+
const std::string& getLanguageName(const std::uint_least8_t id) const noexcept {
409391
return language_list[id];
410392
}
411393
std::size_t getSizeLanguage() const noexcept {
@@ -431,21 +413,6 @@ namespace paxs {
431413
return mtdna_list.size();
432414
}
433415

434-
/// @brief 日本の地区のIDから人口を取得する
435-
/// @param id 日本の地区のID
436-
/// @return 人口
437-
/// @note IDが不正な場合は0を返す
438-
std::uint_least32_t getDistrictPopulationAd200(const std::uint_least8_t id) const noexcept {
439-
for (const auto& district : district_list) {
440-
if (district.id == id) {
441-
return district.init_pop;
442-
}
443-
}
444-
PAXS_WARNING("Failed to get District population: " + std::to_string(id));
445-
446-
return 0;
447-
}
448-
449416
/// @brief 日本の地区のIDから地方区分のIDを取得する
450417
/// @param id 日本の地区のID
451418
/// @return 地方区分のID
@@ -461,20 +428,14 @@ namespace paxs {
461428
return 0;
462429
}
463430

464-
/// @brief Get a list of District
465-
/// @brief 地区のリストを取得する
466-
std::vector<District>& getDistrictList() noexcept {
467-
return district_list;
468-
}
469-
const std::vector<District>& cgetDistrictList() const noexcept {
431+
const std::vector<District>& getDistrictList() const noexcept {
470432
return district_list;
471433
}
472434
private:
473435
std::vector<JapanRegion> japan_regions; // 日本の地方区分
474436
std::vector<District> district_list; // 日本の地区
475437
paxs::UnorderedMap<std::uint_least32_t, mtDNA_Region> mtdna_region_list; // mtDNA 地方区分
476438
paxs::UnorderedMap<std::uint_least32_t, mtDNA_Region> language_region_list; // 言語 地方区分
477-
//std::vector<std::uint_least32_t> mtdna_region_hash_list; // mtDNA ハッシュ計算用
478439
std::vector<std::string> mtdna_list; // mtDNA
479440
std::vector<std::string> language_list; // 言語
480441
};

Library/PAX_SAPIENTICA/Simulation/Entity/Settlement.hpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,7 @@ namespace paxs {
309309
if (female_is_agricultural) {
310310
const std::uint_least8_t female_district_id = environment->template getData<std::uint_least8_t>(
311311
SimulationConstants::getInstance().district_key, data_.getPosition());
312-
const District& female_district = japan_provinces_ptr->cgetDistrict(female_district_id);
312+
const District& female_district = japan_provinces_ptr->getDistrict(female_district_id);
313313
if (female_district.agricultural_capable == 0) {
314314
// 女性が農耕不可能な地域にいる場合、婚姻できない
315315
continue;
@@ -320,7 +320,7 @@ namespace paxs {
320320
if (male_is_agricultural) {
321321
const std::uint_least8_t male_district_id = environment->template getData<std::uint_least8_t>(
322322
SimulationConstants::getInstance().district_key, close_settlement.getPosition());
323-
const District& male_district = japan_provinces_ptr->cgetDistrict(male_district_id);
323+
const District& male_district = japan_provinces_ptr->getDistrict(male_district_id);
324324
if (male_district.agricultural_capable == 0) {
325325
// 男性が農耕不可能な地域にいる場合、婚姻できない
326326
continue;
@@ -331,7 +331,7 @@ namespace paxs {
331331
if (SimulationConstants::getInstance().maternal_residence_probability > 0.0f && male_is_agricultural) {
332332
const std::uint_least8_t female_district_id = environment->template getData<std::uint_least8_t>(
333333
SimulationConstants::getInstance().district_key, data_.getPosition());
334-
const District& female_district = japan_provinces_ptr->cgetDistrict(female_district_id);
334+
const District& female_district = japan_provinces_ptr->getDistrict(female_district_id);
335335
if (female_district.agricultural_capable == 0) {
336336
continue; // 移動先が農耕不可能なので婚姻不可
337337
}
@@ -341,7 +341,7 @@ namespace paxs {
341341
if (SimulationConstants::getInstance().maternal_residence_probability < 1.0f && female_is_agricultural) {
342342
const std::uint_least8_t male_district_id = environment->template getData<std::uint_least8_t>(
343343
SimulationConstants::getInstance().district_key, close_settlement.getPosition());
344-
const District& male_district = japan_provinces_ptr->cgetDistrict(male_district_id);
344+
const District& male_district = japan_provinces_ptr->getDistrict(male_district_id);
345345
if (male_district.agricultural_capable == 0) {
346346
continue; // 移動先が農耕不可能なので婚姻不可
347347
}
@@ -594,7 +594,7 @@ namespace paxs {
594594
if (has_agricultural_agents && japan_provinces_ptr != nullptr) {
595595
const std::uint_least8_t target_district_id = environment->template getData<std::uint_least8_t>(
596596
SimulationConstants::getInstance().district_key, target_position);
597-
const District& target_district = japan_provinces_ptr->cgetDistrict(target_district_id);
597+
const District& target_district = japan_provinces_ptr->getDistrict(target_district_id);
598598
if (target_district.agricultural_capable == 0) {
599599
++loop_count;
600600
continue;
@@ -656,7 +656,7 @@ namespace paxs {
656656
if (has_agricultural_agents && japan_provinces_ptr != nullptr) {
657657
const std::uint_least8_t target_district_id = environment->template getData<std::uint_least8_t>(
658658
SimulationConstants::getInstance().district_key, target_position);
659-
const District& target_district = japan_provinces_ptr->cgetDistrict(target_district_id);
659+
const District& target_district = japan_provinces_ptr->getDistrict(target_district_id);
660660
if (target_district.agricultural_capable == 0) {
661661
++loop_count;
662662
continue;
@@ -794,7 +794,7 @@ namespace paxs {
794794
if (japan_provinces_ptr != nullptr) {
795795
const std::uint_least8_t current_district_id = environment->template getData<std::uint_least8_t>(
796796
SimulationConstants::getInstance().district_key, data_.getPosition());
797-
const District& current_district = japan_provinces_ptr->cgetDistrict(current_district_id);
797+
const District& current_district = japan_provinces_ptr->getDistrict(current_district_id);
798798
is_agricultural_capable = (current_district.agricultural_capable != 0);
799799
}
800800

Library/PAX_SAPIENTICA/Simulation/Manager/SettlementSimulator.hpp

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -205,12 +205,12 @@ namespace paxs {
205205
}
206206
// 結果の地区名を出力
207207
void outputResultDistrictName(std::ofstream& ofs_, const std::size_t i_) const {
208-
ofs_ << japan_provinces->cgetDistrictList()[i_].name << '\t';
208+
ofs_ << japan_provinces->getDistrictList()[i_].name << '\t';
209209
}
210210
// 集落の初期化時にシミュレーション変数を出力
211211
void initResults() {
212212
result_writer_ = std::make_unique<SimulationResultWriter>();
213-
result_writer_->initialize(SimulationConstants::getInstance().output_directory_name, japan_provinces->cgetDistrictList());
213+
result_writer_->initialize(SimulationConstants::getInstance().output_directory_name, japan_provinces->getDistrictList());
214214
}
215215

216216
/// @brief Initialize the simulator.
@@ -241,7 +241,7 @@ namespace paxs {
241241
// 可住地の数を出力
242242
for (std::size_t i = 1; i < max_number_of_districts; ++i) {
243243
result_writer_->writeHabitableLand(
244-
japan_provinces->cgetDistrictList()[i].name,
244+
japan_provinces->getDistrictList()[i].name,
245245
(*live_list)[i + 1].habitable_land_positions.size()
246246
);
247247
}
@@ -266,23 +266,23 @@ namespace paxs {
266266
const auto target_index = target_key.to(SettlementGridsType{});
267267

268268
// ターゲットの地域が登録されているか?
269-
const auto target_iterator = settlement_grids.find(target_index);
270-
const auto current_iterator = settlement_grids.find(current_index);
271-
if (current_iterator == settlement_grids.end()) {
269+
auto* const target_ptr = settlement_grids.try_get(target_index);
270+
auto* const current_ptr = settlement_grids.try_get(current_index);
271+
if (current_ptr == nullptr) {
272272
return; // current_indexが存在しない場合は何もしない
273273
}
274274

275-
if (target_iterator != settlement_grids.end()) {
275+
if (target_ptr != nullptr) {
276276
// 登録されている場合はそのターゲット地域へ移動
277-
target_iterator->second.moveSettlementToThis(current_iterator->second.getSettlement(settlement_id));
277+
target_ptr->moveSettlementToThis(current_ptr->getSettlement(settlement_id));
278278
}
279279
else {
280280
// 登録されていない場合は新しく地域を作成
281281
SettlementGrid settlement_grid = SettlementGrid(target_key * SimulationConstants::getInstance().cell_group_length, environment, gen);
282-
settlement_grid.moveSettlementToThis(current_iterator->second.getSettlement(settlement_id));
282+
settlement_grid.moveSettlementToThis(current_ptr->getSettlement(settlement_id));
283283
settlement_grids.emplace(target_index, std::move(settlement_grid));
284284
}
285-
current_iterator->second.deleteSettlement(settlement_id);
285+
current_ptr->deleteSettlement(settlement_id);
286286
}
287287

288288
/// @brief Execute the simulation for the one step.
@@ -345,7 +345,7 @@ namespace paxs {
345345
stats.settlement_count = sat_num;
346346
stats.population_count = pop_num;
347347
stats.get_mtdna_name = [this](std::uint_least8_t id) { return japan_provinces->getMtDNA_Name(id); };
348-
stats.get_language_name = [this](std::uint_least8_t id) { return japan_provinces->getLanguage_Name(id); };
348+
stats.get_language_name = [this](std::uint_least8_t id) { return japan_provinces->getLanguageName(id); };
349349

350350
// Aggregate region stats and prepare district stats
351351
for (std::size_t i = 1; i < max_number_of_districts; ++i) {
@@ -701,7 +701,7 @@ namespace paxs {
701701

702702
// 地区と人口のマップ
703703
paxs::UnorderedMap<std::uint_least8_t, std::uint_least32_t> district_population_map;
704-
for (const auto& district : japan_provinces->cgetDistrictList()) {
704+
for (const auto& district : japan_provinces->getDistrictList()) {
705705
if (((is_ad200) ? district.init_pop : district.immigrant) == 0) {
706706
continue;
707707
}
@@ -759,7 +759,7 @@ namespace paxs {
759759
}
760760

761761
// 配置する集落の人口を決定
762-
paxs::District district = japan_provinces->cgetDistrict(district_id);
762+
paxs::District district = japan_provinces->getDistrict(district_id);
763763
int settlement_population = std::uniform_int_distribution<>(district.settlement_pop_min, district.settlement_pop_max)(gen);
764764
settlement_population = (std::min)(settlement_population, static_cast<int>(district_population_it->second));
765765

0 commit comments

Comments
 (0)