Skip to content

Commit

Permalink
Remove use of boost::unordered_map
Browse files Browse the repository at this point in the history
This also moves the LuaAI's push_location_key into public
view as the map_location hash function.
  • Loading branch information
CelticMinstrel committed Jul 29, 2016
1 parent 46bb3c3 commit 30e71ef
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 48 deletions.
17 changes: 4 additions & 13 deletions src/ai/lua/core.cpp
Expand Up @@ -136,16 +136,6 @@ void lua_ai_context::push_ai_table()
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)
{
lua_newtable(L);
Expand Down Expand Up @@ -679,12 +669,12 @@ static void push_move_map(lua_State *L, const move_map& m)

int index = 1;


std::hash<map_location> lhash;

do
{
map_location key = it->first;
push_location_key(L, key);
lua_pushinteger(L, lhash(key));

lua_createtable(L, 0, 0);

Expand Down Expand Up @@ -822,11 +812,12 @@ static int impl_ai_aspect_get(lua_State* L)
const unit_advancements_aspect& val = aspect->get();
int my_side = luaW_getglobal(L, "ai", "side") - 1;
lua_newtable(L);
std::hash<map_location> lhash;
for (unit_map::const_iterator u = resources::units->begin(); u != resources::units->end(); ++u) {
if (!u.valid() || u->side() != my_side) {
continue;
}
push_location_key(L, u->get_location());
lua_pushinteger(L, lhash(u->get_location()));
lua_push(L, val.get_advancements(u));
lua_settable(L, -3);
}
Expand Down
1 change: 0 additions & 1 deletion src/game_events/manager_impl.cpp
Expand Up @@ -29,7 +29,6 @@
#include "soundsource.hpp"
#include "util.hpp"

#include <boost/unordered_map.hpp>
#include <iostream>


Expand Down
6 changes: 3 additions & 3 deletions src/game_events/manager_impl.hpp
Expand Up @@ -17,14 +17,14 @@

#include "game_events/handlers.hpp"

#include <boost/unordered_map.hpp>
#include <unordered_map>

namespace game_events {

//t_event_handlers is essentially the implementation details of the manager
class t_event_handlers {
typedef boost::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, handler_list> map_t;
typedef std::unordered_map<std::string, std::weak_ptr<event_handler> > id_map_t;

public:
typedef handler_vec::iterator iterator;
Expand Down
53 changes: 30 additions & 23 deletions src/image.cpp
Expand Up @@ -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 {

Expand Down Expand Up @@ -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.
// 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).
Expand Down
9 changes: 3 additions & 6 deletions src/image.hpp
Expand Up @@ -21,7 +21,7 @@
#include "terrain/translation.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
Expand Down Expand Up @@ -60,11 +60,11 @@ namespace image {
int center_y_;
};

friend size_t hash_value(const value&);
friend struct std::hash<value>;

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
// through locators is fast. The idea is that calling functions
Expand Down Expand Up @@ -128,9 +128,6 @@ namespace image {
sdl::timage load_texture(const locator &loc);
#endif

size_t hash_value(const locator::value&);


typedef cache_type<surface> image_cache;
#ifdef SDL_GPU
typedef cache_type<sdl::timage> texture_cache;
Expand Down
12 changes: 12 additions & 0 deletions src/map/location.hpp
Expand Up @@ -26,6 +26,7 @@ class variable_set;
#include <string>
#include <vector>
#include <algorithm>
#include <utility>

/**
* Encapsulates the map of the game.
Expand Down Expand Up @@ -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. */
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 **/

/** Inline direction manipulators **/
Expand Down
4 changes: 2 additions & 2 deletions src/units/map.hpp
Expand Up @@ -25,7 +25,7 @@
#include <cassert>
#include <list>
#include <map>
#include <boost/unordered_map.hpp>
#include <unordered_map>

//#define DEBUG_UNIT_MAP

Expand Down Expand Up @@ -107,7 +107,7 @@ class unit_map {
///iterators pointing to this unit.
typedef std::map<size_t, unit_pod> t_umap;
///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:

Expand Down

0 comments on commit 30e71ef

Please sign in to comment.