Remove use of boost::unordered_map

This also moves the LuaAI's push_location_key into public
view as the map_location hash function.
This commit is contained in:
Celtic Minstrel 2016-07-29 18:04:45 -04:00
parent 7212e5963d
commit ad4fe895e9
7 changed files with 54 additions and 48 deletions

View File

@ -136,16 +136,6 @@ void lua_ai_context::push_ai_table()
lua_ai_load ctx(*this, false); lua_ai_load ctx(*this, false);
} }
static void push_location_key(lua_State* L, const map_location& loc)
{
// This should be factored out. The same function is defined in data/lua/location_set.lua
// At this point, it is not clear, where this(hashing) function can be placed
// Implemented it this way, to test the new version of the data structure
// as requested from the users of LuaAI <Nephro>
int hashed_index = (loc.x + 1) * 16384 + (loc.y + 1) + 2000;
lua_pushinteger(L, hashed_index);
}
static int transform_ai_action(lua_State *L, ai::action_result_ptr action_result) static int transform_ai_action(lua_State *L, ai::action_result_ptr action_result)
{ {
lua_newtable(L); lua_newtable(L);
@ -679,12 +669,12 @@ static void push_move_map(lua_State *L, const move_map& m)
int index = 1; int index = 1;
std::hash<map_location> lhash;
do do
{ {
map_location key = it->first; map_location key = it->first;
push_location_key(L, key); lua_pushinteger(L, lhash(key));
lua_createtable(L, 0, 0); lua_createtable(L, 0, 0);
@ -822,11 +812,12 @@ static int impl_ai_aspect_get(lua_State* L)
const unit_advancements_aspect& val = aspect->get(); const unit_advancements_aspect& val = aspect->get();
int my_side = luaW_getglobal(L, "ai", "side") - 1; int my_side = luaW_getglobal(L, "ai", "side") - 1;
lua_newtable(L); lua_newtable(L);
std::hash<map_location> lhash;
for (unit_map::const_iterator u = resources::units->begin(); u != resources::units->end(); ++u) { for (unit_map::const_iterator u = resources::units->begin(); u != resources::units->end(); ++u) {
if (!u.valid() || u->side() != my_side) { if (!u.valid() || u->side() != my_side) {
continue; continue;
} }
push_location_key(L, u->get_location()); lua_pushinteger(L, lhash(u->get_location()));
lua_push(L, val.get_advancements(u)); lua_push(L, val.get_advancements(u));
lua_settable(L, -3); lua_settable(L, -3);
} }

View File

@ -29,7 +29,6 @@
#include "soundsource.hpp" #include "soundsource.hpp"
#include "util.hpp" #include "util.hpp"
#include <boost/unordered_map.hpp>
#include <iostream> #include <iostream>

View File

@ -17,14 +17,14 @@
#include "game_events/handlers.hpp" #include "game_events/handlers.hpp"
#include <boost/unordered_map.hpp> #include <unordered_map>
namespace game_events { namespace game_events {
//t_event_handlers is essentially the implementation details of the manager //t_event_handlers is essentially the implementation details of the manager
class t_event_handlers { class t_event_handlers {
typedef boost::unordered_map<std::string, handler_list> map_t; typedef std::unordered_map<std::string, handler_list> map_t;
typedef boost::unordered_map<std::string, std::weak_ptr<event_handler> > id_map_t; typedef std::unordered_map<std::string, std::weak_ptr<event_handler> > id_map_t;
public: public:
typedef handler_vec::iterator iterator; typedef handler_vec::iterator iterator;

View File

@ -71,6 +71,36 @@ struct cache_item
}; };
namespace std {
template<>
struct hash<image::locator::value> {
size_t operator()(const image::locator::value& val) const {
using boost::hash_value;
using boost::hash_combine;
/*
* Boost 1.51.0 seems not longer accept an enumerate value in its hash
* function so cast it to a type it does like.
*/
size_t hash = hash_value(static_cast<unsigned>(val.type_));
if (val.type_ == image::locator::FILE || val.type_ == image::locator::SUB_FILE) {
hash_combine(hash, val.filename_);
}
if (val.type_ == image::locator::SUB_FILE) {
hash_combine(hash, val.loc_.x);
hash_combine(hash, val.loc_.y);
hash_combine(hash, val.center_x_);
hash_combine(hash, val.center_y_);
hash_combine(hash, val.modifications_);
}
return hash;
}
};
}
namespace image { namespace image {
@ -371,29 +401,6 @@ bool locator::value::operator<(const value& a) const
} }
} }
size_t hash_value(const locator::value& val) {
using boost::hash_value;
using boost::hash_combine;
/*
* Boost 1.51.0 seems not longer accept an enumerate value in its hash
* function so cast it to a type it does like.
*/
size_t hash = hash_value(static_cast<unsigned>(val.type_));
if (val.type_ == locator::FILE || val.type_ == locator::SUB_FILE) {
hash_combine(hash, val.filename_);
}
if (val.type_ == locator::SUB_FILE) {
hash_combine(hash, val.loc_.x);
hash_combine(hash, val.loc_.y);
hash_combine(hash, val.center_x_);
hash_combine(hash, val.center_y_);
hash_combine(hash, val.modifications_);
}
return hash;
}
// Check if localized file is up-to-date according to l10n track index. // Check if localized file is up-to-date according to l10n track index.
// Make sure only that the image is not explicitly recorded as fuzzy, // Make sure only that the image is not explicitly recorded as fuzzy,
// in order to be able to use non-tracked images (e.g. from UMC). // in order to be able to use non-tracked images (e.g. from UMC).

View File

@ -21,7 +21,7 @@
#include "terrain/translation.hpp" #include "terrain/translation.hpp"
#include "game_config.hpp" #include "game_config.hpp"
#include <boost/unordered_map.hpp> #include <unordered_map>
///this module manages the cache of images. With an image name, you can get ///this module manages the cache of images. With an image name, you can get
@ -60,11 +60,11 @@ namespace image {
int center_y_; int center_y_;
}; };
friend size_t hash_value(const value&); friend struct std::hash<value>;
public: public:
typedef boost::unordered_map<value, int> locator_finder_t; typedef std::unordered_map<value, int> locator_finder_t;
// Constructing locators is somewhat slow, accessing image // Constructing locators is somewhat slow, accessing image
// through locators is fast. The idea is that calling functions // through locators is fast. The idea is that calling functions
@ -128,9 +128,6 @@ namespace image {
sdl::timage load_texture(const locator &loc); sdl::timage load_texture(const locator &loc);
#endif #endif
size_t hash_value(const locator::value&);
typedef cache_type<surface> image_cache; typedef cache_type<surface> image_cache;
#ifdef SDL_GPU #ifdef SDL_GPU
typedef cache_type<sdl::timage> texture_cache; typedef cache_type<sdl::timage> texture_cache;

View File

@ -26,6 +26,7 @@ class variable_set;
#include <string> #include <string>
#include <vector> #include <vector>
#include <algorithm> #include <algorithm>
#include <utility>
/** /**
* Encapsulates the map of the game. * Encapsulates the map of the game.
@ -149,6 +150,17 @@ std::ostream &operator<<(std::ostream &s, map_location const &l);
/** Dumps a vector of positions on a stream, for debug purposes. */ /** Dumps a vector of positions on a stream, for debug purposes. */
std::ostream &operator<<(std::ostream &s, std::vector<map_location> const &v); std::ostream &operator<<(std::ostream &s, std::vector<map_location> const &v);
namespace std {
template<>
struct hash<map_location> {
size_t operator()(const map_location& l) const {
// The 2000 bias supposedly ensures that the correct x is recovered for negative y
// This implementation copied from the Lua location_set
return (l.x + 1) * 16384 + (l.y + 1) + 2000;
}
};
}
/** Inlined bodies **/ /** Inlined bodies **/
/** Inline direction manipulators **/ /** Inline direction manipulators **/

View File

@ -25,7 +25,7 @@
#include <cassert> #include <cassert>
#include <list> #include <list>
#include <map> #include <map>
#include <boost/unordered_map.hpp> #include <unordered_map>
//#define DEBUG_UNIT_MAP //#define DEBUG_UNIT_MAP
@ -107,7 +107,7 @@ class unit_map {
///iterators pointing to this unit. ///iterators pointing to this unit.
typedef std::map<size_t, unit_pod> t_umap; typedef std::map<size_t, unit_pod> t_umap;
///Map of location to umap iterator. ///Map of location to umap iterator.
typedef boost::unordered_map<map_location, t_umap::iterator> t_lmap; typedef std::unordered_map<map_location, t_umap::iterator> t_lmap;
public: public: