From 058a869b70072aba8baea47e359c45e82daaf152 Mon Sep 17 00:00:00 2001 From: Ner'zhul Date: Wed, 10 Aug 2016 12:08:05 +0200 Subject: [PATCH] Permit usage of std::unordered_map & std::unorderered_set on c++11 compilers (#4430) This fallback to std::map & std::set for older compilers Use UNORDERED_SET as an example in decoration and ore biome sets Use UNORDERED_MAP as an example in nameidmapping --- src/mg_decoration.cpp | 2 +- src/mg_decoration.h | 4 ++-- src/mg_ore.cpp | 10 +++++----- src/mg_ore.h | 3 ++- src/nameidmapping.cpp | 2 +- src/nameidmapping.h | 12 +++++------ src/script/lua_api/l_mapgen.cpp | 4 ++-- src/util/cpp11_container.h | 35 +++++++++++++++++++++++++++++++++ 8 files changed, 54 insertions(+), 18 deletions(-) create mode 100644 src/util/cpp11_container.h diff --git a/src/mg_decoration.cpp b/src/mg_decoration.cpp index 70dd9817a..127915b51 100644 --- a/src/mg_decoration.cpp +++ b/src/mg_decoration.cpp @@ -156,7 +156,7 @@ size_t Decoration::placeDeco(Mapgen *mg, u32 blockseed, v3s16 nmin, v3s16 nmax) } if (mg->biomemap) { - std::set::iterator iter; + UNORDERED_SET::iterator iter; if (!biomes.empty()) { iter = biomes.find(mg->biomemap[mapindex]); diff --git a/src/mg_decoration.h b/src/mg_decoration.h index ba3e9d3b2..da98fd482 100644 --- a/src/mg_decoration.h +++ b/src/mg_decoration.h @@ -20,7 +20,7 @@ with this program; if not, write to the Free Software Foundation, Inc., #ifndef MG_DECORATION_HEADER #define MG_DECORATION_HEADER -#include +#include "util/cpp11_container.h" #include "objdef.h" #include "noise.h" #include "nodedef.h" @@ -83,7 +83,7 @@ public: float fill_ratio; NoiseParams np; - std::set biomes; + UNORDERED_SET biomes; //std::list cutoffs; //Mutex cutoff_mutex; }; diff --git a/src/mg_ore.cpp b/src/mg_ore.cpp index 257901614..d840d745a 100644 --- a/src/mg_ore.cpp +++ b/src/mg_ore.cpp @@ -148,7 +148,7 @@ void OreScatter::generate(MMVManip *vm, int mapseed, u32 blockseed, if (biomemap && !biomes.empty()) { u32 index = sizex * (z0 - nmin.Z) + (x0 - nmin.X); - std::set::iterator it = biomes.find(biomemap[index]); + UNORDERED_SET::iterator it = biomes.find(biomemap[index]); if (it == biomes.end()) continue; } @@ -202,7 +202,7 @@ void OreSheet::generate(MMVManip *vm, int mapseed, u32 blockseed, continue; if (biomemap && !biomes.empty()) { - std::set::iterator it = biomes.find(biomemap[index]); + UNORDERED_SET::iterator it = biomes.find(biomemap[index]); if (it == biomes.end()) continue; } @@ -270,7 +270,7 @@ void OrePuff::generate(MMVManip *vm, int mapseed, u32 blockseed, continue; if (biomemap && !biomes.empty()) { - std::set::iterator it = biomes.find(biomemap[index]); + UNORDERED_SET::iterator it = biomes.find(biomemap[index]); if (it == biomes.end()) continue; } @@ -338,7 +338,7 @@ void OreBlob::generate(MMVManip *vm, int mapseed, u32 blockseed, if (biomemap && !biomes.empty()) { u32 bmapidx = sizex * (z0 - nmin.Z) + (x0 - nmin.X); - std::set::iterator it = biomes.find(biomemap[bmapidx]); + UNORDERED_SET::iterator it = biomes.find(biomemap[bmapidx]); if (it == biomes.end()) continue; } @@ -422,7 +422,7 @@ void OreVein::generate(MMVManip *vm, int mapseed, u32 blockseed, if (biomemap && !biomes.empty()) { u32 bmapidx = sizex * (z - nmin.Z) + (x - nmin.X); - std::set::iterator it = biomes.find(biomemap[bmapidx]); + UNORDERED_SET::iterator it = biomes.find(biomemap[bmapidx]); if (it == biomes.end()) continue; } diff --git a/src/mg_ore.h b/src/mg_ore.h index 2e065cee3..e95fdd330 100644 --- a/src/mg_ore.h +++ b/src/mg_ore.h @@ -20,6 +20,7 @@ with this program; if not, write to the Free Software Foundation, Inc., #ifndef MG_ORE_HEADER #define MG_ORE_HEADER +#include "util/cpp11_container.h" #include "objdef.h" #include "noise.h" #include "nodedef.h" @@ -64,7 +65,7 @@ public: float nthresh; // threshold for noise at which an ore is placed NoiseParams np; // noise for distribution of clusters (NULL for uniform scattering) Noise *noise; - std::set biomes; + UNORDERED_SET biomes; Ore(); virtual ~Ore(); diff --git a/src/nameidmapping.cpp b/src/nameidmapping.cpp index ed59ddd16..2af8befff 100644 --- a/src/nameidmapping.cpp +++ b/src/nameidmapping.cpp @@ -25,7 +25,7 @@ void NameIdMapping::serialize(std::ostream &os) const { writeU8(os, 0); // version writeU16(os, m_id_to_name.size()); - for(std::map::const_iterator + for(UNORDERED_MAP::const_iterator i = m_id_to_name.begin(); i != m_id_to_name.end(); ++i){ writeU16(os, i->first); diff --git a/src/nameidmapping.h b/src/nameidmapping.h index 417c441d2..23838c8ff 100644 --- a/src/nameidmapping.h +++ b/src/nameidmapping.h @@ -23,15 +23,15 @@ with this program; if not, write to the Free Software Foundation, Inc., #include #include #include -#include #include "irrlichttypes_bloated.h" +#include "util/cpp11_container.h" class NameIdMapping { public: void serialize(std::ostream &os) const; void deSerialize(std::istream &is); - + void clear(){ m_id_to_name.clear(); m_name_to_id.clear(); @@ -55,7 +55,7 @@ public: m_name_to_id.erase(name); } bool getName(u16 id, std::string &result) const{ - std::map::const_iterator i; + UNORDERED_MAP::const_iterator i; i = m_id_to_name.find(id); if(i == m_id_to_name.end()) return false; @@ -63,7 +63,7 @@ public: return true; } bool getId(const std::string &name, u16 &result) const{ - std::map::const_iterator i; + UNORDERED_MAP::const_iterator i; i = m_name_to_id.find(name); if(i == m_name_to_id.end()) return false; @@ -74,8 +74,8 @@ public: return m_id_to_name.size(); } private: - std::map m_id_to_name; - std::map m_name_to_id; + UNORDERED_MAP m_id_to_name; + UNORDERED_MAP m_name_to_id; }; #endif diff --git a/src/script/lua_api/l_mapgen.cpp b/src/script/lua_api/l_mapgen.cpp index 9f14838ce..9f28231eb 100644 --- a/src/script/lua_api/l_mapgen.cpp +++ b/src/script/lua_api/l_mapgen.cpp @@ -100,7 +100,7 @@ Biome *get_or_load_biome(lua_State *L, int index, BiomeManager *biomemgr); Biome *read_biome_def(lua_State *L, int index, INodeDefManager *ndef); size_t get_biome_list(lua_State *L, int index, - BiomeManager *biomemgr, std::set *biome_id_list); + BiomeManager *biomemgr, UNORDERED_SET *biome_id_list); Schematic *get_or_load_schematic(lua_State *L, int index, SchematicManager *schemmgr, StringMap *replace_names); @@ -401,7 +401,7 @@ Biome *read_biome_def(lua_State *L, int index, INodeDefManager *ndef) size_t get_biome_list(lua_State *L, int index, - BiomeManager *biomemgr, std::set *biome_id_list) + BiomeManager *biomemgr, UNORDERED_SET *biome_id_list) { if (index < 0) index = lua_gettop(L) + 1 + index; diff --git a/src/util/cpp11_container.h b/src/util/cpp11_container.h new file mode 100644 index 000000000..f839be19a --- /dev/null +++ b/src/util/cpp11_container.h @@ -0,0 +1,35 @@ +/* +Minetest +Copyright (C) 2016 nerzhul, Loic Blot + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU Lesser General Public License as published by +the Free Software Foundation; either version 2.1 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU Lesser General Public License for more details. + +You should have received a copy of the GNU Lesser General Public License along +with this program; if not, write to the Free Software Foundation, Inc., +51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +*/ + +#ifndef MT_CPP11CONTAINER_HEADER +#define MT_CPP11CONTAINER_HEADER + +#if __cplusplus >= 201103L + #include + #include + #define UNORDERED_MAP std::unordered_map + #define UNORDERED_SET std::unordered_set +#else + #include + #include + #define UNORDERED_MAP std::map + #define UNORDERED_SET std::set +#endif + +#endif