Added a template function for std::map search (#3107)

Thanks to @secretdataz for his help.
This commit is contained in:
Lemongrass3110 2018-05-16 08:26:48 +02:00 committed by GitHub
parent 620e397558
commit 0a84542220
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 36 additions and 25 deletions

View File

@ -1,6 +1,7 @@
#pragma once
#include <memory>
#include <string>
#include <map>
// Class used to perform time measurement
class cScopeTimer {
@ -11,3 +12,17 @@ class cScopeTimer {
};
int levenshtein( const std::string &s1, const std::string &s2 );
namespace rathena {
namespace util {
template <typename K, typename V> V* map_find( std::map<K,V>& map, K key ){
auto it = map.find( key );
if( it != map.end() ){
return &it->second;
}else{
return nullptr;
}
}
}
}

View File

@ -7,6 +7,8 @@
#include <map>
#include <math.h>
#include "../common/utilities.hpp"
#include "../common/cbasetypes.h"
#include "../common/malloc.h"
#include "../common/timer.h"
@ -26,6 +28,8 @@
#include "trade.hpp"
#include "npc.hpp"
using namespace rathena;
std::map<uint16, struct s_mercenary_db> mercenary_db_data;
/**
@ -34,11 +38,7 @@ std::map<uint16, struct s_mercenary_db> mercenary_db_data;
* @return A pointer to the mercenary db entry or nullptr if not found
**/
struct s_mercenary_db *mercenary_db( uint16 class_ ){
if( mercenary_db_data.find(class_) != mercenary_db_data.end() ){
return &mercenary_db_data.at(class_);
}else{
return nullptr;
}
return util::map_find( mercenary_db_data, class_ );
}
/**

View File

@ -7,6 +7,8 @@
#include <map>
#include <math.h>
#include "../common/utilities.hpp"
#include "../common/cbasetypes.h"
#include "../common/timer.h"
#include "../common/db.h"
@ -40,6 +42,8 @@
#include <unordered_map>
#include <algorithm>
using namespace rathena;
#define ACTIVE_AI_RANGE 2 //Distance added on top of 'AREA_SIZE' at which mobs enter active AI mode.
#define IDLE_SKILL_INTERVAL 10 //Active idle skills should be triggered every 1 second (1000/MIN_MOBTHINKTIME)
@ -76,11 +80,7 @@
//Dynamic mob database
std::map<uint16, struct mob_db> mob_db_data;
struct mob_db *mob_db( int mob_id ){
if( mob_db_data.find( mob_id ) != mob_db_data.end() ){
return &mob_db_data.at(mob_id);
}else{
return nullptr;
}
return util::map_find( mob_db_data, (uint16)mob_id );
}
// holds Monster Spawn informations
@ -89,11 +89,7 @@ std::unordered_map<uint16, std::vector<spawn_info>> mob_spawn_data;
//Dynamic mob chat database
std::map<short,struct mob_chat> mob_chat_db;
struct mob_chat *mob_chat(short id) {
if( mob_chat_db.find(id) != mob_chat_db.end() ){
return &mob_chat_db.at(id);
}else{
return nullptr;
}
return util::map_find( mob_chat_db, id );
}
//Dynamic item drop ratio database for per-item drop ratio modifiers overriding global drop ratios.

View File

@ -7,6 +7,8 @@
#include <stdlib.h>
#include "../common/utilities.hpp"
#include "../common/db.h"
#include "../common/timer.h"
#include "../common/nullpo.h"
@ -27,16 +29,14 @@
#include "log.hpp"
#include "achievement.hpp"
using namespace rathena;
#define MIN_PETTHINKTIME 100
//Dynamic pet database
std::map<uint16, struct s_pet_db> pet_db_data;
struct s_pet_db *pet_db( uint16 pet_id ){
if( pet_db_data.find(pet_id) != pet_db_data.end() ){
return &pet_db_data.at(pet_id);
}else{
return nullptr;
}
return util::map_find( pet_db_data, pet_id );
}
static struct eri *item_drop_ers; //For loot drops delay structures.

View File

@ -8,6 +8,8 @@
#include <stdlib.h>
#include <string.h>
#include "../common/utilities.hpp"
#include "../common/cbasetypes.h"
#include "../common/nullpo.h"
#include "../common/malloc.h"
@ -24,6 +26,8 @@
#include "log.hpp"
#include "battle.hpp"
using namespace rathena;
///Databases of guild_storage : int guild_id -> struct guild_storage
std::map<int, struct s_storage> guild_storage_db;
@ -562,11 +566,7 @@ struct s_storage *guild2storage(int guild_id)
* @return s_storage or nullptr
*/
struct s_storage *guild2storage2(int guild_id){
if( guild_storage_db.find(guild_id) != guild_storage_db.end() ){
return &guild_storage_db[guild_id];
}else{
return nullptr;
}
return util::map_find( guild_storage_db, guild_id );
}
/**