Permit usage of std::unordered_map & std::unorderered_set on c++11 compilers (#4430)
authorNer'zhul <nerzhul@users.noreply.github.com>
Wed, 10 Aug 2016 10:08:05 +0000 (12:08 +0200)
committerest31 <est31@users.noreply.github.com>
Wed, 10 Aug 2016 10:08:05 +0000 (12:08 +0200)
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
src/mg_decoration.h
src/mg_ore.cpp
src/mg_ore.h
src/nameidmapping.cpp
src/nameidmapping.h
src/script/lua_api/l_mapgen.cpp
src/util/cpp11_container.h [new file with mode: 0644]

index 70dd9817aa13ed0f2fbc7451bd2df4092730a818..127915b51aecd2688ceece503f2d4fff17c89914 100644 (file)
@@ -156,7 +156,7 @@ size_t Decoration::placeDeco(Mapgen *mg, u32 blockseed, v3s16 nmin, v3s16 nmax)
                        }
 
                        if (mg->biomemap) {
-                               std::set<u8>::iterator iter;
+                               UNORDERED_SET<u8>::iterator iter;
 
                                if (!biomes.empty()) {
                                        iter = biomes.find(mg->biomemap[mapindex]);
index ba3e9d3b258d8abf2cc880c5f5119cf5be8a0c16..da98fd4820ce4dd6d4eae92e091f9ce9019843e4 100644 (file)
@@ -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 <set>
+#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<u8> biomes;
+       UNORDERED_SET<u8> biomes;
        //std::list<CutoffData> cutoffs;
        //Mutex cutoff_mutex;
 };
index 2579016144742a6ad95a0fd3d3a69e77c181975a..d840d745a1ae6136f6ef76aeb5e98b3fdfa3d4cd 100644 (file)
@@ -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<u8>::iterator it = biomes.find(biomemap[index]);
+                       UNORDERED_SET<u8>::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<u8>::iterator it = biomes.find(biomemap[index]);
+                       UNORDERED_SET<u8>::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<u8>::iterator it = biomes.find(biomemap[index]);
+                       UNORDERED_SET<u8>::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<u8>::iterator it = biomes.find(biomemap[bmapidx]);
+                       UNORDERED_SET<u8>::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<u8>::iterator it = biomes.find(biomemap[bmapidx]);
+                       UNORDERED_SET<u8>::iterator it = biomes.find(biomemap[bmapidx]);
                        if (it == biomes.end())
                                continue;
                }
index 2e065cee317557235220eeff88b226937a38dbd9..e95fdd330dedeaaf0ee006f1cdea8dfba407197b 100644 (file)
@@ -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<u8> biomes;
+       UNORDERED_SET<u8> biomes;
 
        Ore();
        virtual ~Ore();
index ed59ddd16848b7c0a7db53d198c797562b14752a..2af8beffffa6c4ebaa3aec770c795c611655879c 100644 (file)
@@ -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<u16, std::string>::const_iterator
+       for(UNORDERED_MAP<u16, std::string>::const_iterator
                        i = m_id_to_name.begin();
                        i != m_id_to_name.end(); ++i){
                writeU16(os, i->first);
index 417c441d23a7e1f0d486b70f9f74a5ec755053c7..23838c8ff221338b5c35a504597aed655b63484b 100644 (file)
@@ -23,15 +23,15 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 #include <string>
 #include <iostream>
 #include <set>
-#include <map>
 #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<u16, std::string>::const_iterator i;
+               UNORDERED_MAP<u16, std::string>::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<std::string, u16>::const_iterator i;
+               UNORDERED_MAP<std::string, u16>::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<u16, std::string> m_id_to_name;
-       std::map<std::string, u16> m_name_to_id;
+       UNORDERED_MAP<u16, std::string> m_id_to_name;
+       UNORDERED_MAP<std::string, u16> m_name_to_id;
 };
 
 #endif
index 9f14838ce4b4ac6c56fe1d92ef1a509a444013c2..9f28231eb18a2cdb89ab058a85c0cdbf89ab4610 100644 (file)
@@ -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<u8> *biome_id_list);
+       BiomeManager *biomemgr, UNORDERED_SET<u8> *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<u8> *biome_id_list)
+       BiomeManager *biomemgr, UNORDERED_SET<u8> *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 (file)
index 0000000..f839be1
--- /dev/null
@@ -0,0 +1,35 @@
+/*
+Minetest
+Copyright (C) 2016 nerzhul, Loic Blot <loic.blot@unix-experience.fr>
+
+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 <unordered_map>
+       #include <unordered_set>
+       #define UNORDERED_MAP std::unordered_map
+       #define UNORDERED_SET std::unordered_set
+#else
+       #include <map>
+       #include <set>
+       #define UNORDERED_MAP std::map
+       #define UNORDERED_SET std::set
+#endif
+
+#endif