Skip to content

Commit

Permalink
Replace various std::map with UNORDERED_MAP + various cleanups
Browse files Browse the repository at this point in the history
This is part 2 for 5f084cd

Other improvements:

* Use the defined ItemGroupList when used
* make Client::checkPrivilege const
* inline some trivial functions
* Add ActiveObjectMap typedef
* Add SettingsEntries typedef
  • Loading branch information
nerzhul committed Oct 5, 2016
1 parent 5f084cd commit 613797a
Show file tree
Hide file tree
Showing 23 changed files with 110 additions and 167 deletions.
4 changes: 2 additions & 2 deletions src/client.cpp
Expand Up @@ -303,7 +303,7 @@ Client::~Client()
delete m_inventory_from_server;

// Delete detached inventories
for (std::map<std::string, Inventory*>::iterator
for (UNORDERED_MAP<std::string, Inventory*>::iterator
i = m_detached_inventories.begin();
i != m_detached_inventories.end(); ++i) {
delete i->second;
Expand Down Expand Up @@ -1437,7 +1437,7 @@ Inventory* Client::getInventory(const InventoryLocation &loc)
break;
case InventoryLocation::DETACHED:
{
if(m_detached_inventories.count(loc.name) == 0)
if (m_detached_inventories.count(loc.name) == 0)
return NULL;
return m_detached_inventories[loc.name];
}
Expand Down
6 changes: 3 additions & 3 deletions src/client.h
Expand Up @@ -462,7 +462,7 @@ class Client : public con::PeerHandler, public InventoryManager, public IGameDef
u16 getHP();
u16 getBreath();

bool checkPrivilege(const std::string &priv)
bool checkPrivilege(const std::string &priv) const
{ return (m_privileges.count(priv) != 0); }

bool getChatMessage(std::wstring &message);
Expand Down Expand Up @@ -670,11 +670,11 @@ class Client : public con::PeerHandler, public InventoryManager, public IGameDef
std::map<int, u16> m_sounds_to_objects;

// Privileges
std::set<std::string> m_privileges;
UNORDERED_SET<std::string> m_privileges;

// Detached inventories
// key = name
std::map<std::string, Inventory*> m_detached_inventories;
UNORDERED_MAP<std::string, Inventory*> m_detached_inventories;

// Storage for mesh data for creating multiple instances of the same mesh
StringMap m_mesh_data;
Expand Down
55 changes: 20 additions & 35 deletions src/clientiface.cpp
Expand Up @@ -605,11 +605,8 @@ ClientInterface::~ClientInterface()
{
MutexAutoLock clientslock(m_clients_mutex);

for(std::map<u16, RemoteClient*>::iterator
i = m_clients.begin();
i != m_clients.end(); ++i)
{

for (UNORDERED_MAP<u16, RemoteClient*>::iterator i = m_clients.begin();
i != m_clients.end(); ++i) {
// Delete client
delete i->second;
}
Expand All @@ -621,10 +618,8 @@ std::vector<u16> ClientInterface::getClientIDs(ClientState min_state)
std::vector<u16> reply;
MutexAutoLock clientslock(m_clients_mutex);

for(std::map<u16, RemoteClient*>::iterator
i = m_clients.begin();
i != m_clients.end(); ++i)
{
for(UNORDERED_MAP<u16, RemoteClient*>::iterator i = m_clients.begin();
i != m_clients.end(); ++i) {
if (i->second->getState() >= min_state)
reply.push_back(i->second->peer_id);
}
Expand Down Expand Up @@ -691,8 +686,7 @@ void ClientInterface::sendToAll(u16 channelnum,
NetworkPacket* pkt, bool reliable)
{
MutexAutoLock clientslock(m_clients_mutex);
for(std::map<u16, RemoteClient*>::iterator
i = m_clients.begin();
for(UNORDERED_MAP<u16, RemoteClient*>::iterator i = m_clients.begin();
i != m_clients.end(); ++i) {
RemoteClient *client = i->second;

Expand All @@ -705,11 +699,10 @@ void ClientInterface::sendToAll(u16 channelnum,
RemoteClient* ClientInterface::getClientNoEx(u16 peer_id, ClientState state_min)
{
MutexAutoLock clientslock(m_clients_mutex);
std::map<u16, RemoteClient*>::iterator n;
n = m_clients.find(peer_id);
UNORDERED_MAP<u16, RemoteClient*>::iterator n = m_clients.find(peer_id);
// The client may not exist; clients are immediately removed if their
// access is denied, and this event occurs later then.
if(n == m_clients.end())
if (n == m_clients.end())
return NULL;

if (n->second->getState() >= state_min)
Expand All @@ -720,11 +713,10 @@ RemoteClient* ClientInterface::getClientNoEx(u16 peer_id, ClientState state_min)

RemoteClient* ClientInterface::lockedGetClientNoEx(u16 peer_id, ClientState state_min)
{
std::map<u16, RemoteClient*>::iterator n;
n = m_clients.find(peer_id);
UNORDERED_MAP<u16, RemoteClient*>::iterator n = m_clients.find(peer_id);
// The client may not exist; clients are immediately removed if their
// access is denied, and this event occurs later then.
if(n == m_clients.end())
if (n == m_clients.end())
return NULL;

if (n->second->getState() >= state_min)
Expand All @@ -736,11 +728,10 @@ RemoteClient* ClientInterface::lockedGetClientNoEx(u16 peer_id, ClientState stat
ClientState ClientInterface::getClientState(u16 peer_id)
{
MutexAutoLock clientslock(m_clients_mutex);
std::map<u16, RemoteClient*>::iterator n;
n = m_clients.find(peer_id);
UNORDERED_MAP<u16, RemoteClient*>::iterator n = m_clients.find(peer_id);
// The client may not exist; clients are immediately removed if their
// access is denied, and this event occurs later then.
if(n == m_clients.end())
if (n == m_clients.end())
return CS_Invalid;

return n->second->getState();
Expand All @@ -749,11 +740,10 @@ ClientState ClientInterface::getClientState(u16 peer_id)
void ClientInterface::setPlayerName(u16 peer_id,std::string name)
{
MutexAutoLock clientslock(m_clients_mutex);
std::map<u16, RemoteClient*>::iterator n;
n = m_clients.find(peer_id);
UNORDERED_MAP<u16, RemoteClient*>::iterator n = m_clients.find(peer_id);
// The client may not exist; clients are immediately removed if their
// access is denied, and this event occurs later then.
if(n != m_clients.end())
if (n != m_clients.end())
n->second->setName(name);
}

Expand All @@ -762,11 +752,10 @@ void ClientInterface::DeleteClient(u16 peer_id)
MutexAutoLock conlock(m_clients_mutex);

// Error check
std::map<u16, RemoteClient*>::iterator n;
n = m_clients.find(peer_id);
UNORDERED_MAP<u16, RemoteClient*>::iterator n = m_clients.find(peer_id);
// The client may not exist; clients are immediately removed if their
// access is denied, and this event occurs later then.
if(n == m_clients.end())
if (n == m_clients.end())
return;

/*
Expand Down Expand Up @@ -797,10 +786,9 @@ void ClientInterface::CreateClient(u16 peer_id)
MutexAutoLock conlock(m_clients_mutex);

// Error check
std::map<u16, RemoteClient*>::iterator n;
n = m_clients.find(peer_id);
UNORDERED_MAP<u16, RemoteClient*>::iterator n = m_clients.find(peer_id);
// The client shouldn't already exist
if(n != m_clients.end()) return;
if (n != m_clients.end()) return;

// Create client
RemoteClient *client = new RemoteClient();
Expand All @@ -814,8 +802,7 @@ void ClientInterface::event(u16 peer_id, ClientStateEvent event)
MutexAutoLock clientlock(m_clients_mutex);

// Error check
std::map<u16, RemoteClient*>::iterator n;
n = m_clients.find(peer_id);
UNORDERED_MAP<u16, RemoteClient*>::iterator n = m_clients.find(peer_id);

// No client to deliver event
if (n == m_clients.end())
Expand All @@ -836,8 +823,7 @@ u16 ClientInterface::getProtocolVersion(u16 peer_id)
MutexAutoLock conlock(m_clients_mutex);

// Error check
std::map<u16, RemoteClient*>::iterator n;
n = m_clients.find(peer_id);
UNORDERED_MAP<u16, RemoteClient*>::iterator n = m_clients.find(peer_id);

// No client to get version
if (n == m_clients.end())
Expand All @@ -851,8 +837,7 @@ void ClientInterface::setClientVersion(u16 peer_id, u8 major, u8 minor, u8 patch
MutexAutoLock conlock(m_clients_mutex);

// Error check
std::map<u16, RemoteClient*>::iterator n;
n = m_clients.find(peer_id);
UNORDERED_MAP<u16, RemoteClient*>::iterator n = m_clients.find(peer_id);

// No client to set versions
if (n == m_clients.end())
Expand Down
7 changes: 3 additions & 4 deletions src/clientiface.h
Expand Up @@ -25,10 +25,10 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "serialization.h" // for SER_FMT_VER_INVALID
#include "threading/mutex.h"
#include "network/networkpacket.h"
#include "util/cpp11_container.h"

#include <list>
#include <vector>
#include <map>
#include <set>

class MapBlock;
Expand Down Expand Up @@ -502,8 +502,7 @@ class ClientInterface {
void lock() { m_clients_mutex.lock(); }
void unlock() { m_clients_mutex.unlock(); }

std::map<u16, RemoteClient*>& getClientList()
{ return m_clients; }
UNORDERED_MAP<u16, RemoteClient*>& getClientList() { return m_clients; }

private:
/* update internal player list */
Expand All @@ -513,7 +512,7 @@ class ClientInterface {
con::Connection* m_con;
Mutex m_clients_mutex;
// Connected clients (behind the con mutex)
std::map<u16, RemoteClient*> m_clients;
UNORDERED_MAP<u16, RemoteClient*> m_clients;
std::vector<std::string> m_clients_names; //for announcing masterserver

// Environment
Expand Down
6 changes: 2 additions & 4 deletions src/content_cao.cpp
Expand Up @@ -1505,10 +1505,8 @@ void GenericCAO::updateBonePosition()
return;

m_animated_meshnode->setJointMode(irr::scene::EJUOR_CONTROL); // To write positions to the mesh on render
for(std::map<std::string,
core::vector2d<v3f> >::const_iterator ii = m_bone_position.begin();
ii != m_bone_position.end(); ++ii)
{
for(UNORDERED_MAP<std::string, core::vector2d<v3f> >::const_iterator
ii = m_bone_position.begin(); ii != m_bone_position.end(); ++ii) {
std::string bone_name = (*ii).first;
v3f bone_pos = (*ii).second.X;
v3f bone_rot = (*ii).second.Y;
Expand Down
2 changes: 1 addition & 1 deletion src/content_cao.h
Expand Up @@ -90,7 +90,7 @@ class GenericCAO : public ClientActiveObject
int m_animation_speed;
int m_animation_blend;
bool m_animation_loop;
std::map<std::string, core::vector2d<v3f> > m_bone_position; // stores position and rotation for each bone name
UNORDERED_MAP<std::string, core::vector2d<v3f> > m_bone_position; // stores position and rotation for each bone name
std::string m_attachment_bone;
v3f m_attachment_position;
v3f m_attachment_rotation;
Expand Down
6 changes: 2 additions & 4 deletions src/emerge.cpp
Expand Up @@ -369,12 +369,10 @@ bool EmergeManager::pushBlockEmergeData(
}


bool EmergeManager::popBlockEmergeData(
v3s16 pos,
BlockEmergeData *bedata)
bool EmergeManager::popBlockEmergeData(v3s16 pos, BlockEmergeData *bedata)
{
std::map<v3s16, BlockEmergeData>::iterator it;
std::map<u16, u16>::iterator it2;
UNORDERED_MAP<u16, u16>::iterator it2;

it = m_blocks_enqueued.find(pos);
if (it == m_blocks_enqueued.end())
Expand Down
2 changes: 1 addition & 1 deletion src/emerge.h
Expand Up @@ -156,7 +156,7 @@ class EmergeManager {

Mutex m_queue_mutex;
std::map<v3s16, BlockEmergeData> m_blocks_enqueued;
std::map<u16, u16> m_peer_queue_count;
UNORDERED_MAP<u16, u16> m_peer_queue_count;

u16 m_qlimit_total;
u16 m_qlimit_diskonly;
Expand Down

4 comments on commit 613797a

@PyroLagus
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think your commit might have broken something. When I try to compile on master, I get:

/home/codehero/code/minetest/src/content_cao.cpp: In constructor ‘GenericCAO::GenericCAO(IGameDef*, ClientEnvironment*)’: /home/codehero/code/minetest/src/content_cao.cpp:582:21: error: no matching function for call to ‘std::unordered_map<std::__cxx11::basic_string<char>, irr::core::vector2d<irr::core::vector3d<float> > >::unordered_map(std::map<std::__cxx11::basic_string<char>, irr::core::vector2d<irr::core::vector3d<float> > >)’ m_is_visible(false) ^ In file included from /usr/include/c++/6.2.1/unordered_map:48:0, from /home/codehero/code/minetest/src/util/cpp11_container.h:24, from /home/codehero/code/minetest/src/itemgroup.h:24, from /home/codehero/code/minetest/src/content_cao.h:27, from /home/codehero/code/minetest/src/content_cao.cpp:26:

But if I checkout 5f084cd, the code compiles without errors.

@nerzhul
Copy link
Member Author

@nerzhul nerzhul commented on 613797a Oct 5, 2016 via email

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nerzhul
Copy link
Member Author

@nerzhul nerzhul commented on 613797a Oct 5, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@C0DEHERO i have the fix. Strangely travis didn't report it. I will look at compilation matrix to ensure C++11 builds are enabled, and it compiled also in my clion with gcc 6.2... but now i'm able to reproduce.
This is now fixed. Thanks

@PyroLagus
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice. For the record, I was just using the default build instructions for Linux. So I'm pretty sure it used g++.

Please sign in to comment.