Skip to content

Commit

Permalink
Permit usage of std::unordered_map & std::unorderered_set on c++11 co…
Browse files Browse the repository at this point in the history
…mpilers (#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
  • Loading branch information
nerzhul authored and est31 committed Aug 10, 2016
1 parent 8df89db commit 058a869
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 18 deletions.
2 changes: 1 addition & 1 deletion src/mg_decoration.cpp
Expand Up @@ -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]);
Expand Down
4 changes: 2 additions & 2 deletions src/mg_decoration.h
Expand Up @@ -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"
Expand Down Expand Up @@ -83,7 +83,7 @@ class Decoration : public ObjDef, public NodeResolver {
float fill_ratio;
NoiseParams np;

std::set<u8> biomes;
UNORDERED_SET<u8> biomes;
//std::list<CutoffData> cutoffs;
//Mutex cutoff_mutex;
};
Expand Down
10 changes: 5 additions & 5 deletions src/mg_ore.cpp
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
}
Expand Down
3 changes: 2 additions & 1 deletion src/mg_ore.h
Expand Up @@ -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"
Expand Down Expand Up @@ -64,7 +65,7 @@ class Ore : public ObjDef, public NodeResolver {
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();
Expand Down
2 changes: 1 addition & 1 deletion src/nameidmapping.cpp
Expand Up @@ -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);
Expand Down
12 changes: 6 additions & 6 deletions src/nameidmapping.h
Expand Up @@ -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();
Expand All @@ -55,15 +55,15 @@ class NameIdMapping
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;
result = i->second;
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;
Expand All @@ -74,8 +74,8 @@ class NameIdMapping
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
Expand Down
4 changes: 2 additions & 2 deletions src/script/lua_api/l_mapgen.cpp
Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand Down
35 changes: 35 additions & 0 deletions src/util/cpp11_container.h
@@ -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

0 comments on commit 058a869

Please sign in to comment.