Skip to content

Commit 058a869

Browse files
nerzhulest31
authored andcommitted
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
1 parent 8df89db commit 058a869

8 files changed

+54
-18
lines changed

src/mg_decoration.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ size_t Decoration::placeDeco(Mapgen *mg, u32 blockseed, v3s16 nmin, v3s16 nmax)
156156
}
157157

158158
if (mg->biomemap) {
159-
std::set<u8>::iterator iter;
159+
UNORDERED_SET<u8>::iterator iter;
160160

161161
if (!biomes.empty()) {
162162
iter = biomes.find(mg->biomemap[mapindex]);

src/mg_decoration.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
2020
#ifndef MG_DECORATION_HEADER
2121
#define MG_DECORATION_HEADER
2222

23-
#include <set>
23+
#include "util/cpp11_container.h"
2424
#include "objdef.h"
2525
#include "noise.h"
2626
#include "nodedef.h"
@@ -83,7 +83,7 @@ class Decoration : public ObjDef, public NodeResolver {
8383
float fill_ratio;
8484
NoiseParams np;
8585

86-
std::set<u8> biomes;
86+
UNORDERED_SET<u8> biomes;
8787
//std::list<CutoffData> cutoffs;
8888
//Mutex cutoff_mutex;
8989
};

src/mg_ore.cpp

+5-5
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ void OreScatter::generate(MMVManip *vm, int mapseed, u32 blockseed,
148148

149149
if (biomemap && !biomes.empty()) {
150150
u32 index = sizex * (z0 - nmin.Z) + (x0 - nmin.X);
151-
std::set<u8>::iterator it = biomes.find(biomemap[index]);
151+
UNORDERED_SET<u8>::iterator it = biomes.find(biomemap[index]);
152152
if (it == biomes.end())
153153
continue;
154154
}
@@ -202,7 +202,7 @@ void OreSheet::generate(MMVManip *vm, int mapseed, u32 blockseed,
202202
continue;
203203

204204
if (biomemap && !biomes.empty()) {
205-
std::set<u8>::iterator it = biomes.find(biomemap[index]);
205+
UNORDERED_SET<u8>::iterator it = biomes.find(biomemap[index]);
206206
if (it == biomes.end())
207207
continue;
208208
}
@@ -270,7 +270,7 @@ void OrePuff::generate(MMVManip *vm, int mapseed, u32 blockseed,
270270
continue;
271271

272272
if (biomemap && !biomes.empty()) {
273-
std::set<u8>::iterator it = biomes.find(biomemap[index]);
273+
UNORDERED_SET<u8>::iterator it = biomes.find(biomemap[index]);
274274
if (it == biomes.end())
275275
continue;
276276
}
@@ -338,7 +338,7 @@ void OreBlob::generate(MMVManip *vm, int mapseed, u32 blockseed,
338338

339339
if (biomemap && !biomes.empty()) {
340340
u32 bmapidx = sizex * (z0 - nmin.Z) + (x0 - nmin.X);
341-
std::set<u8>::iterator it = biomes.find(biomemap[bmapidx]);
341+
UNORDERED_SET<u8>::iterator it = biomes.find(biomemap[bmapidx]);
342342
if (it == biomes.end())
343343
continue;
344344
}
@@ -422,7 +422,7 @@ void OreVein::generate(MMVManip *vm, int mapseed, u32 blockseed,
422422

423423
if (biomemap && !biomes.empty()) {
424424
u32 bmapidx = sizex * (z - nmin.Z) + (x - nmin.X);
425-
std::set<u8>::iterator it = biomes.find(biomemap[bmapidx]);
425+
UNORDERED_SET<u8>::iterator it = biomes.find(biomemap[bmapidx]);
426426
if (it == biomes.end())
427427
continue;
428428
}

src/mg_ore.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
2020
#ifndef MG_ORE_HEADER
2121
#define MG_ORE_HEADER
2222

23+
#include "util/cpp11_container.h"
2324
#include "objdef.h"
2425
#include "noise.h"
2526
#include "nodedef.h"
@@ -64,7 +65,7 @@ class Ore : public ObjDef, public NodeResolver {
6465
float nthresh; // threshold for noise at which an ore is placed
6566
NoiseParams np; // noise for distribution of clusters (NULL for uniform scattering)
6667
Noise *noise;
67-
std::set<u8> biomes;
68+
UNORDERED_SET<u8> biomes;
6869

6970
Ore();
7071
virtual ~Ore();

src/nameidmapping.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ void NameIdMapping::serialize(std::ostream &os) const
2525
{
2626
writeU8(os, 0); // version
2727
writeU16(os, m_id_to_name.size());
28-
for(std::map<u16, std::string>::const_iterator
28+
for(UNORDERED_MAP<u16, std::string>::const_iterator
2929
i = m_id_to_name.begin();
3030
i != m_id_to_name.end(); ++i){
3131
writeU16(os, i->first);

src/nameidmapping.h

+6-6
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,15 @@ with this program; if not, write to the Free Software Foundation, Inc.,
2323
#include <string>
2424
#include <iostream>
2525
#include <set>
26-
#include <map>
2726
#include "irrlichttypes_bloated.h"
27+
#include "util/cpp11_container.h"
2828

2929
class NameIdMapping
3030
{
3131
public:
3232
void serialize(std::ostream &os) const;
3333
void deSerialize(std::istream &is);
34-
34+
3535
void clear(){
3636
m_id_to_name.clear();
3737
m_name_to_id.clear();
@@ -55,15 +55,15 @@ class NameIdMapping
5555
m_name_to_id.erase(name);
5656
}
5757
bool getName(u16 id, std::string &result) const{
58-
std::map<u16, std::string>::const_iterator i;
58+
UNORDERED_MAP<u16, std::string>::const_iterator i;
5959
i = m_id_to_name.find(id);
6060
if(i == m_id_to_name.end())
6161
return false;
6262
result = i->second;
6363
return true;
6464
}
6565
bool getId(const std::string &name, u16 &result) const{
66-
std::map<std::string, u16>::const_iterator i;
66+
UNORDERED_MAP<std::string, u16>::const_iterator i;
6767
i = m_name_to_id.find(name);
6868
if(i == m_name_to_id.end())
6969
return false;
@@ -74,8 +74,8 @@ class NameIdMapping
7474
return m_id_to_name.size();
7575
}
7676
private:
77-
std::map<u16, std::string> m_id_to_name;
78-
std::map<std::string, u16> m_name_to_id;
77+
UNORDERED_MAP<u16, std::string> m_id_to_name;
78+
UNORDERED_MAP<std::string, u16> m_name_to_id;
7979
};
8080

8181
#endif

src/script/lua_api/l_mapgen.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ Biome *get_or_load_biome(lua_State *L, int index,
100100
BiomeManager *biomemgr);
101101
Biome *read_biome_def(lua_State *L, int index, INodeDefManager *ndef);
102102
size_t get_biome_list(lua_State *L, int index,
103-
BiomeManager *biomemgr, std::set<u8> *biome_id_list);
103+
BiomeManager *biomemgr, UNORDERED_SET<u8> *biome_id_list);
104104

105105
Schematic *get_or_load_schematic(lua_State *L, int index,
106106
SchematicManager *schemmgr, StringMap *replace_names);
@@ -401,7 +401,7 @@ Biome *read_biome_def(lua_State *L, int index, INodeDefManager *ndef)
401401

402402

403403
size_t get_biome_list(lua_State *L, int index,
404-
BiomeManager *biomemgr, std::set<u8> *biome_id_list)
404+
BiomeManager *biomemgr, UNORDERED_SET<u8> *biome_id_list)
405405
{
406406
if (index < 0)
407407
index = lua_gettop(L) + 1 + index;

src/util/cpp11_container.h

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
Minetest
3+
Copyright (C) 2016 nerzhul, Loic Blot <loic.blot@unix-experience.fr>
4+
5+
This program is free software; you can redistribute it and/or modify
6+
it under the terms of the GNU Lesser General Public License as published by
7+
the Free Software Foundation; either version 2.1 of the License, or
8+
(at your option) any later version.
9+
10+
This program is distributed in the hope that it will be useful,
11+
but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
GNU Lesser General Public License for more details.
14+
15+
You should have received a copy of the GNU Lesser General Public License along
16+
with this program; if not, write to the Free Software Foundation, Inc.,
17+
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*/
19+
20+
#ifndef MT_CPP11CONTAINER_HEADER
21+
#define MT_CPP11CONTAINER_HEADER
22+
23+
#if __cplusplus >= 201103L
24+
#include <unordered_map>
25+
#include <unordered_set>
26+
#define UNORDERED_MAP std::unordered_map
27+
#define UNORDERED_SET std::unordered_set
28+
#else
29+
#include <map>
30+
#include <set>
31+
#define UNORDERED_MAP std::map
32+
#define UNORDERED_SET std::set
33+
#endif
34+
35+
#endif

0 commit comments

Comments
 (0)