1
0
Fork 0

l_get_modnames: Compare using std::sort instead of a custom function which does same work

mutilcraft-mt53
Loic Blot 2015-03-05 20:12:54 +01:00
parent c00eed90d3
commit 40c2c18a3f
1 changed files with 6 additions and 22 deletions

View File

@ -367,34 +367,18 @@ int ModApiServer::l_get_modnames(lua_State *L)
NO_MAP_LOCK_REQUIRED; NO_MAP_LOCK_REQUIRED;
// Get a list of mods // Get a list of mods
std::vector<std::string> mods_unsorted; std::vector<std::string> modlist;
std::list<std::string> mods_sorted; getServer(L)->getModNames(modlist);
getServer(L)->getModNames(mods_unsorted);
// Take unsorted items from mods_unsorted and sort them into // Take unsorted items from mods_unsorted and sort them into
// mods_sorted; not great performance but the number of mods on a // mods_sorted; not great performance but the number of mods on a
// server will likely be small. // server will likely be small.
for(std::vector<std::string>::iterator i = mods_unsorted.begin(); std::sort(modlist.begin(), modlist.end());
i != mods_unsorted.end(); ++i) {
bool added = false;
for(std::list<std::string>::iterator x = mods_sorted.begin();
x != mods_sorted.end(); ++x) {
// I doubt anybody using Minetest will be using
// anything not ASCII based :)
if(i->compare(*x) <= 0) {
mods_sorted.insert(x, *i);
added = true;
break;
}
}
if(!added)
mods_sorted.push_back(*i);
}
// Package them up for Lua // Package them up for Lua
lua_createtable(L, mods_sorted.size(), 0); lua_createtable(L, modlist.size(), 0);
std::list<std::string>::iterator iter = mods_sorted.begin(); std::vector<std::string>::iterator iter = modlist.begin();
for (u16 i = 0; iter != mods_sorted.end(); iter++) { for (u16 i = 0; iter != modlist.end(); iter++) {
lua_pushstring(L, iter->c_str()); lua_pushstring(L, iter->c_str());
lua_rawseti(L, -2, ++i); lua_rawseti(L, -2, ++i);
} }