Skip to content

Commit 17fd5fe

Browse files
kahrlsfan5
authored andcommittedSep 12, 2017
Make INodeDefManager::getIds return a vector, not a set
1 parent 5b3fbf9 commit 17fd5fe

File tree

7 files changed

+64
-83
lines changed

7 files changed

+64
-83
lines changed
 

Diff for: ‎src/nodedef.cpp

+20-41
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
3838
#include "gamedef.h"
3939
#include "mapnode.h"
4040
#include <fstream> // Used in applyTextureOverrides()
41+
#include <algorithm>
4142

4243
/*
4344
NodeBox
@@ -485,7 +486,7 @@ void ContentFeatures::deSerialize(std::istream &is)
485486
u16 connects_to_size = readU16(is);
486487
connects_to_ids.clear();
487488
for (u16 i = 0; i < connects_to_size; i++)
488-
connects_to_ids.insert(readU16(is));
489+
connects_to_ids.push_back(readU16(is));
489490
post_effect_color.setAlpha(readU8(is));
490491
post_effect_color.setRed(readU8(is));
491492
post_effect_color.setGreen(readU8(is));
@@ -845,7 +846,7 @@ class CNodeDefManager: public IWritableNodeDefManager {
845846
inline virtual const ContentFeatures& get(const MapNode &n) const;
846847
virtual bool getId(const std::string &name, content_t &result) const;
847848
virtual content_t getId(const std::string &name) const;
848-
virtual bool getIds(const std::string &name, std::set<content_t> &result) const;
849+
virtual bool getIds(const std::string &name, std::vector<content_t> &result) const;
849850
virtual const ContentFeatures& get(const std::string &name) const;
850851
content_t allocateId();
851852
virtual content_t set(const std::string &name, const ContentFeatures &def);
@@ -892,10 +893,10 @@ class CNodeDefManager: public IWritableNodeDefManager {
892893

893894
std::unordered_map<std::string, content_t> m_name_id_mapping_with_aliases;
894895

895-
// A mapping from groups to a list of content_ts (and their levels)
896-
// that belong to it. Necessary for a direct lookup in getIds().
896+
// A mapping from groups to a vector of content_ts that belong to it.
897+
// Necessary for a direct lookup in getIds().
897898
// Note: Not serialized.
898-
std::unordered_map<std::string, GroupItems> m_group_to_items;
899+
std::unordered_map<std::string, std::vector<content_t>> m_group_to_items;
899900

900901
// Next possibly free id
901902
content_t m_next_id;
@@ -1037,28 +1038,25 @@ content_t CNodeDefManager::getId(const std::string &name) const
10371038

10381039

10391040
bool CNodeDefManager::getIds(const std::string &name,
1040-
std::set<content_t> &result) const
1041+
std::vector<content_t> &result) const
10411042
{
10421043
//TimeTaker t("getIds", NULL, PRECISION_MICRO);
10431044
if (name.substr(0,6) != "group:") {
10441045
content_t id = CONTENT_IGNORE;
10451046
bool exists = getId(name, id);
10461047
if (exists)
1047-
result.insert(id);
1048+
result.push_back(id);
10481049
return exists;
10491050
}
10501051
std::string group = name.substr(6);
10511052

1052-
std::unordered_map<std::string, GroupItems>::const_iterator
1053+
std::unordered_map<std::string, std::vector<content_t>>::const_iterator
10531054
i = m_group_to_items.find(group);
10541055
if (i == m_group_to_items.end())
10551056
return true;
10561057

1057-
const GroupItems &items = i->second;
1058-
for (const auto &item : items) {
1059-
if (item.second != 0)
1060-
result.insert(item.first);
1061-
}
1058+
const std::vector<content_t> &items = i->second;
1059+
result.insert(result.end(), items.begin(), items.end());
10621060
//printf("getIds: %dus\n", t.stop());
10631061
return true;
10641062
}
@@ -1247,15 +1245,7 @@ content_t CNodeDefManager::set(const std::string &name, const ContentFeatures &d
12471245
// belongs to when a node is re-registered
12481246
for (const auto &group : def.groups) {
12491247
const std::string &group_name = group.first;
1250-
1251-
std::unordered_map<std::string, GroupItems>::iterator
1252-
j = m_group_to_items.find(group_name);
1253-
if (j == m_group_to_items.end()) {
1254-
m_group_to_items[group_name].emplace_back(id, group.second);
1255-
} else {
1256-
GroupItems &items = j->second;
1257-
items.emplace_back(id, group.second);
1258-
}
1248+
m_group_to_items[group_name].push_back(id);
12591249
}
12601250
return id;
12611251
}
@@ -1283,16 +1273,10 @@ void CNodeDefManager::removeNode(const std::string &name)
12831273
}
12841274

12851275
// Erase node content from all groups it belongs to
1286-
for (std::unordered_map<std::string, GroupItems>::iterator iter_groups =
1276+
for (std::unordered_map<std::string, std::vector<content_t>>::iterator iter_groups =
12871277
m_group_to_items.begin(); iter_groups != m_group_to_items.end();) {
1288-
GroupItems &items = iter_groups->second;
1289-
for (GroupItems::iterator iter_groupitems = items.begin();
1290-
iter_groupitems != items.end();) {
1291-
if (iter_groupitems->first == id)
1292-
items.erase(iter_groupitems++);
1293-
else
1294-
++iter_groupitems;
1295-
}
1278+
std::vector<content_t> &items = iter_groups->second;
1279+
items.erase(std::remove(items.begin(), items.end(), id), items.end());
12961280

12971281
// Check if group is empty
12981282
if (items.empty())
@@ -1545,9 +1529,8 @@ void CNodeDefManager::mapNodeboxConnections()
15451529
if (f.drawtype != NDT_NODEBOX || f.node_box.type != NODEBOX_CONNECTED)
15461530
continue;
15471531

1548-
for (std::vector<std::string>::const_iterator it = f.connects_to.begin();
1549-
it != f.connects_to.end(); ++it) {
1550-
getIds(*it, f.connects_to_ids);
1532+
for (const std::string &name : f.connects_to) {
1533+
getIds(name, f.connects_to_ids);
15511534
}
15521535
}
15531536
}
@@ -1560,14 +1543,14 @@ bool CNodeDefManager::nodeboxConnects(MapNode from, MapNode to, u8 connect_face)
15601543
return false;
15611544

15621545
// lookup target in connected set
1563-
if (f1.connects_to_ids.find(to.param0) == f1.connects_to_ids.end())
1546+
if (!CONTAINS(f1.connects_to_ids, to.param0))
15641547
return false;
15651548

15661549
const ContentFeatures &f2 = get(to);
15671550

15681551
if ((f2.drawtype == NDT_NODEBOX) && (f2.node_box.type == NODEBOX_CONNECTED))
15691552
// ignores actually looking if back connection exists
1570-
return (f2.connects_to_ids.find(from.param0) != f2.connects_to_ids.end());
1553+
return CONTAINS(f2.connects_to_ids, from.param0);
15711554

15721555
// does to node declare usable faces?
15731556
if (f2.connect_sides > 0) {
@@ -1686,11 +1669,7 @@ bool NodeResolver::getIdsFromNrBacklog(std::vector<content_t> *result_out,
16861669
success = false;
16871670
}
16881671
} else {
1689-
std::set<content_t> cids;
1690-
std::set<content_t>::iterator it;
1691-
m_ndef->getIds(name, cids);
1692-
for (it = cids.begin(); it != cids.end(); ++it)
1693-
result_out->push_back(*it);
1672+
m_ndef->getIds(name, *result_out);
16941673
}
16951674
}
16961675

Diff for: ‎src/nodedef.h

+3-6
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ with this program; if not, write to the Free Software Foundation, Inc.,
2323
#include <string>
2424
#include <iostream>
2525
#include <map>
26-
#include <list>
2726
#include "mapnode.h"
2827
#ifndef SERVER
2928
#include "client/tile.h"
@@ -42,8 +41,6 @@ class IShaderSource;
4241
class IGameDef;
4342
class NodeResolver;
4443

45-
typedef std::list<std::pair<content_t, int> > GroupItems;
46-
4744
enum ContentParamType
4845
{
4946
CPT_NONE,
@@ -290,7 +287,7 @@ struct ContentFeatures
290287
// for NDT_CONNECTED pairing
291288
u8 connect_sides;
292289
std::vector<std::string> connects_to;
293-
std::set<content_t> connects_to_ids;
290+
std::vector<content_t> connects_to_ids;
294291
// Post effect color, drawn when the camera is inside the node.
295292
video::SColor post_effect_color;
296293
// Flowing liquid or snow, value = default level
@@ -424,7 +421,7 @@ class INodeDefManager {
424421
virtual content_t getId(const std::string &name) const=0;
425422
// Allows "group:name" in addition to regular node names
426423
// returns false if node name not found, true otherwise
427-
virtual bool getIds(const std::string &name, std::set<content_t> &result)
424+
virtual bool getIds(const std::string &name, std::vector<content_t> &result)
428425
const=0;
429426
virtual const ContentFeatures &get(const std::string &name) const=0;
430427

@@ -452,7 +449,7 @@ class IWritableNodeDefManager : public INodeDefManager {
452449
// If not found, returns CONTENT_IGNORE
453450
virtual content_t getId(const std::string &name) const=0;
454451
// Allows "group:name" in addition to regular node names
455-
virtual bool getIds(const std::string &name, std::set<content_t> &result)
452+
virtual bool getIds(const std::string &name, std::vector<content_t> &result)
456453
const=0;
457454
// If not found, returns the features of CONTENT_UNKNOWN
458455
virtual const ContentFeatures &get(const std::string &name) const=0;

Diff for: ‎src/script/cpp_api/s_env.cpp

+6-6
Original file line numberDiff line numberDiff line change
@@ -108,37 +108,37 @@ void ScriptApiEnv::initializeEnvironment(ServerEnvironment *env)
108108
int id = lua_tonumber(L, -2);
109109
int current_abm = lua_gettop(L);
110110

111-
std::set<std::string> trigger_contents;
111+
std::vector<std::string> trigger_contents;
112112
lua_getfield(L, current_abm, "nodenames");
113113
if (lua_istable(L, -1)) {
114114
int table = lua_gettop(L);
115115
lua_pushnil(L);
116116
while (lua_next(L, table)) {
117117
// key at index -2 and value at index -1
118118
luaL_checktype(L, -1, LUA_TSTRING);
119-
trigger_contents.insert(lua_tostring(L, -1));
119+
trigger_contents.push_back(lua_tostring(L, -1));
120120
// removes value, keeps key for next iteration
121121
lua_pop(L, 1);
122122
}
123123
} else if (lua_isstring(L, -1)) {
124-
trigger_contents.insert(lua_tostring(L, -1));
124+
trigger_contents.push_back(lua_tostring(L, -1));
125125
}
126126
lua_pop(L, 1);
127127

128-
std::set<std::string> required_neighbors;
128+
std::vector<std::string> required_neighbors;
129129
lua_getfield(L, current_abm, "neighbors");
130130
if (lua_istable(L, -1)) {
131131
int table = lua_gettop(L);
132132
lua_pushnil(L);
133133
while (lua_next(L, table)) {
134134
// key at index -2 and value at index -1
135135
luaL_checktype(L, -1, LUA_TSTRING);
136-
required_neighbors.insert(lua_tostring(L, -1));
136+
required_neighbors.push_back(lua_tostring(L, -1));
137137
// removes value, keeps key for next iteration
138138
lua_pop(L, 1);
139139
}
140140
} else if (lua_isstring(L, -1)) {
141-
required_neighbors.insert(lua_tostring(L, -1));
141+
required_neighbors.push_back(lua_tostring(L, -1));
142142
}
143143
lua_pop(L, 1);
144144

Diff for: ‎src/script/lua_api/l_env.cpp

+16-11
Original file line numberDiff line numberDiff line change
@@ -719,7 +719,7 @@ int ModApiEnvMod::l_find_node_near(lua_State *L)
719719
INodeDefManager *ndef = getGameDef(L)->ndef();
720720
v3s16 pos = read_v3s16(L, 1);
721721
int radius = luaL_checkinteger(L, 2);
722-
std::set<content_t> filter;
722+
std::vector<content_t> filter;
723723
if (lua_istable(L, 3)) {
724724
lua_pushnil(L);
725725
while (lua_next(L, 3) != 0) {
@@ -748,7 +748,7 @@ int ModApiEnvMod::l_find_node_near(lua_State *L)
748748
for (const v3s16 &i : list) {
749749
v3s16 p = pos + i;
750750
content_t c = env->getMap().getNodeNoEx(p).getContent();
751-
if (filter.count(c) != 0) {
751+
if (CONTAINS(filter, c)) {
752752
push_v3s16(L, p);
753753
return 1;
754754
}
@@ -780,7 +780,7 @@ int ModApiEnvMod::l_find_nodes_in_area(lua_State *L)
780780
return 0;
781781
}
782782

783-
std::set<content_t> filter;
783+
std::vector<content_t> filter;
784784
if (lua_istable(L, 3)) {
785785
lua_pushnil(L);
786786
while (lua_next(L, 3) != 0) {
@@ -794,7 +794,8 @@ int ModApiEnvMod::l_find_nodes_in_area(lua_State *L)
794794
ndef->getIds(lua_tostring(L, 3), filter);
795795
}
796796

797-
std::unordered_map<content_t, u32> individual_count;
797+
std::vector<u32> individual_count;
798+
individual_count.resize(filter.size());
798799

799800
lua_newtable(L);
800801
u64 i = 0;
@@ -803,16 +804,20 @@ int ModApiEnvMod::l_find_nodes_in_area(lua_State *L)
803804
for (s16 z = minp.Z; z <= maxp.Z; z++) {
804805
v3s16 p(x, y, z);
805806
content_t c = env->getMap().getNodeNoEx(p).getContent();
806-
if (filter.count(c) != 0) {
807+
808+
std::vector<content_t>::iterator it = std::find(filter.begin(), filter.end(), c);
809+
if (it != filter.end()) {
807810
push_v3s16(L, p);
808811
lua_rawseti(L, -2, ++i);
809-
individual_count[c]++;
812+
813+
u32 filt_index = it - filter.begin();
814+
individual_count[filt_index]++;
810815
}
811816
}
812817
lua_newtable(L);
813-
for (content_t it : filter) {
814-
lua_pushnumber(L, individual_count[it]);
815-
lua_setfield(L, -2, ndef->get(it).name.c_str());
818+
for (u32 i = 0; i < filter.size(); i++) {
819+
lua_pushnumber(L, individual_count[i]);
820+
lua_setfield(L, -2, ndef->get(filter[i]).name.c_str());
816821
}
817822
return 2;
818823
}
@@ -847,7 +852,7 @@ int ModApiEnvMod::l_find_nodes_in_area_under_air(lua_State *L)
847852
return 0;
848853
}
849854

850-
std::set<content_t> filter;
855+
std::vector<content_t> filter;
851856

852857
if (lua_istable(L, 3)) {
853858
lua_pushnil(L);
@@ -873,7 +878,7 @@ int ModApiEnvMod::l_find_nodes_in_area_under_air(lua_State *L)
873878
v3s16 psurf(x, y + 1, z);
874879
content_t csurf = env->getMap().getNodeNoEx(psurf).getContent();
875880
if (c != CONTENT_AIR && csurf == CONTENT_AIR &&
876-
filter.count(c) != 0) {
881+
CONTAINS(filter, c)) {
877882
push_v3s16(L, v3s16(x, y, z));
878883
lua_rawseti(L, -2, ++i);
879884
}

Diff for: ‎src/script/lua_api/l_env.h

+6-6
Original file line numberDiff line numberDiff line change
@@ -188,15 +188,15 @@ class LuaABM : public ActiveBlockModifier {
188188
private:
189189
int m_id;
190190

191-
std::set<std::string> m_trigger_contents;
192-
std::set<std::string> m_required_neighbors;
191+
std::vector<std::string> m_trigger_contents;
192+
std::vector<std::string> m_required_neighbors;
193193
float m_trigger_interval;
194194
u32 m_trigger_chance;
195195
bool m_simple_catch_up;
196196
public:
197197
LuaABM(lua_State *L, int id,
198-
const std::set<std::string> &trigger_contents,
199-
const std::set<std::string> &required_neighbors,
198+
const std::vector<std::string> &trigger_contents,
199+
const std::vector<std::string> &required_neighbors,
200200
float trigger_interval, u32 trigger_chance, bool simple_catch_up):
201201
m_id(id),
202202
m_trigger_contents(trigger_contents),
@@ -206,11 +206,11 @@ class LuaABM : public ActiveBlockModifier {
206206
m_simple_catch_up(simple_catch_up)
207207
{
208208
}
209-
virtual const std::set<std::string> &getTriggerContents() const
209+
virtual const std::vector<std::string> &getTriggerContents() const
210210
{
211211
return m_trigger_contents;
212212
}
213-
virtual const std::set<std::string> &getRequiredNeighbors() const
213+
virtual const std::vector<std::string> &getRequiredNeighbors() const
214214
{
215215
return m_required_neighbors;
216216
}

0 commit comments

Comments
 (0)
Please sign in to comment.