Reserve vectors before pushing and other code quality changes (#11161)

This commit is contained in:
sfan5 2021-04-05 13:38:31 +02:00 committed by GitHub
parent 3e1904fa8c
commit f0bad0e2ba
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
20 changed files with 106 additions and 108 deletions

@ -499,12 +499,12 @@ int ClientMap::getBackgroundBrightness(float max_d, u32 daylight_factor,
static v3f z_directions[50] = { static v3f z_directions[50] = {
v3f(-100, 0, 0) v3f(-100, 0, 0)
}; };
static f32 z_offsets[sizeof(z_directions)/sizeof(*z_directions)] = { static f32 z_offsets[50] = {
-1000, -1000,
}; };
if(z_directions[0].X < -99){ if (z_directions[0].X < -99) {
for(u32 i=0; i<sizeof(z_directions)/sizeof(*z_directions); i++){ for (u32 i = 0; i < ARRLEN(z_directions); i++) {
// Assumes FOV of 72 and 16/9 aspect ratio // Assumes FOV of 72 and 16/9 aspect ratio
z_directions[i] = v3f( z_directions[i] = v3f(
0.02 * myrand_range(-100, 100), 0.02 * myrand_range(-100, 100),
@ -520,7 +520,8 @@ int ClientMap::getBackgroundBrightness(float max_d, u32 daylight_factor,
if(sunlight_min_d > 35*BS) if(sunlight_min_d > 35*BS)
sunlight_min_d = 35*BS; sunlight_min_d = 35*BS;
std::vector<int> values; std::vector<int> values;
for(u32 i=0; i<sizeof(z_directions)/sizeof(*z_directions); i++){ values.reserve(ARRLEN(z_directions));
for (u32 i = 0; i < ARRLEN(z_directions); i++) {
v3f z_dir = z_directions[i]; v3f z_dir = z_directions[i];
core::CMatrix4<f32> a; core::CMatrix4<f32> a;
a.buildRotateFromTo(v3f(0,1,0), z_dir); a.buildRotateFromTo(v3f(0,1,0), z_dir);

@ -170,7 +170,7 @@ void Clouds::render()
// Read noise // Read noise
std::vector<char> grid(m_cloud_radius_i * 2 * m_cloud_radius_i * 2); // vector<bool> is broken std::vector<bool> grid(m_cloud_radius_i * 2 * m_cloud_radius_i * 2);
std::vector<video::S3DVertex> vertices; std::vector<video::S3DVertex> vertices;
vertices.reserve(16 * m_cloud_radius_i * m_cloud_radius_i); vertices.reserve(16 * m_cloud_radius_i * m_cloud_radius_i);

@ -336,22 +336,22 @@ void Hud::drawLuaElements(const v3s16 &camera_offset)
irr::gui::IGUIFont* font = g_fontengine->getFont(); irr::gui::IGUIFont* font = g_fontengine->getFont();
// Reorder elements by z_index // Reorder elements by z_index
std::vector<size_t> ids; std::vector<HudElement*> elems;
elems.reserve(player->maxHudId());
for (size_t i = 0; i != player->maxHudId(); i++) { for (size_t i = 0; i != player->maxHudId(); i++) {
HudElement *e = player->getHud(i); HudElement *e = player->getHud(i);
if (!e) if (!e)
continue; continue;
auto it = ids.begin(); auto it = elems.begin();
while (it != ids.end() && player->getHud(*it)->z_index <= e->z_index) while (it != elems.end() && (*it)->z_index <= e->z_index)
++it; ++it;
ids.insert(it, i); elems.insert(it, e);
} }
for (size_t i : ids) { for (HudElement *e : elems) {
HudElement *e = player->getHud(i);
v2s32 pos(floor(e->pos.X * (float) m_screensize.X + 0.5), v2s32 pos(floor(e->pos.X * (float) m_screensize.X + 0.5),
floor(e->pos.Y * (float) m_screensize.Y + 0.5)); floor(e->pos.Y * (float) m_screensize.Y + 0.5));
@ -522,8 +522,8 @@ void Hud::drawLuaElements(const v3s16 &camera_offset)
client->getMinimap()->drawMinimap(rect); client->getMinimap()->drawMinimap(rect);
break; } break; }
default: default:
infostream << "Hud::drawLuaElements: ignoring drawform " << e->type << infostream << "Hud::drawLuaElements: ignoring drawform " << e->type
" of hud element ID " << i << " due to unrecognized type" << std::endl; << " due to unrecognized type" << std::endl;
} }
} }
} }

@ -82,13 +82,13 @@ Sky::Sky(s32 id, ITextureSource *tsrc, IShaderSource *ssrc) :
// Ensures that sun and moon textures and tonemaps are correct. // Ensures that sun and moon textures and tonemaps are correct.
setSkyDefaults(); setSkyDefaults();
m_sun_texture = tsrc->isKnownSourceImage(m_sun_params.texture) ? m_sun_texture = tsrc->isKnownSourceImage(m_sun_params.texture) ?
tsrc->getTextureForMesh(m_sun_params.texture) : NULL; tsrc->getTextureForMesh(m_sun_params.texture) : nullptr;
m_moon_texture = tsrc->isKnownSourceImage(m_moon_params.texture) ? m_moon_texture = tsrc->isKnownSourceImage(m_moon_params.texture) ?
tsrc->getTextureForMesh(m_moon_params.texture) : NULL; tsrc->getTextureForMesh(m_moon_params.texture) : nullptr;
m_sun_tonemap = tsrc->isKnownSourceImage(m_sun_params.tonemap) ? m_sun_tonemap = tsrc->isKnownSourceImage(m_sun_params.tonemap) ?
tsrc->getTexture(m_sun_params.tonemap) : NULL; tsrc->getTexture(m_sun_params.tonemap) : nullptr;
m_moon_tonemap = tsrc->isKnownSourceImage(m_moon_params.tonemap) ? m_moon_tonemap = tsrc->isKnownSourceImage(m_moon_params.tonemap) ?
tsrc->getTexture(m_moon_params.tonemap) : NULL; tsrc->getTexture(m_moon_params.tonemap) : nullptr;
if (m_sun_texture) { if (m_sun_texture) {
m_materials[3] = baseMaterial(); m_materials[3] = baseMaterial();
@ -744,14 +744,14 @@ void Sky::place_sky_body(
} }
} }
void Sky::setSunTexture(std::string sun_texture, void Sky::setSunTexture(const std::string &sun_texture,
std::string sun_tonemap, ITextureSource *tsrc) const std::string &sun_tonemap, ITextureSource *tsrc)
{ {
// Ignore matching textures (with modifiers) entirely, // Ignore matching textures (with modifiers) entirely,
// but lets at least update the tonemap before hand. // but lets at least update the tonemap before hand.
m_sun_params.tonemap = sun_tonemap; m_sun_params.tonemap = sun_tonemap;
m_sun_tonemap = tsrc->isKnownSourceImage(m_sun_params.tonemap) ? m_sun_tonemap = tsrc->isKnownSourceImage(m_sun_params.tonemap) ?
tsrc->getTexture(m_sun_params.tonemap) : NULL; tsrc->getTexture(m_sun_params.tonemap) : nullptr;
m_materials[3].Lighting = !!m_sun_tonemap; m_materials[3].Lighting = !!m_sun_tonemap;
if (m_sun_params.texture == sun_texture) if (m_sun_params.texture == sun_texture)
@ -780,7 +780,7 @@ void Sky::setSunTexture(std::string sun_texture,
} }
} }
void Sky::setSunriseTexture(std::string sunglow_texture, void Sky::setSunriseTexture(const std::string &sunglow_texture,
ITextureSource* tsrc) ITextureSource* tsrc)
{ {
// Ignore matching textures (with modifiers) entirely. // Ignore matching textures (with modifiers) entirely.
@ -792,14 +792,14 @@ void Sky::setSunriseTexture(std::string sunglow_texture,
); );
} }
void Sky::setMoonTexture(std::string moon_texture, void Sky::setMoonTexture(const std::string &moon_texture,
std::string moon_tonemap, ITextureSource *tsrc) const std::string &moon_tonemap, ITextureSource *tsrc)
{ {
// Ignore matching textures (with modifiers) entirely, // Ignore matching textures (with modifiers) entirely,
// but lets at least update the tonemap before hand. // but lets at least update the tonemap before hand.
m_moon_params.tonemap = moon_tonemap; m_moon_params.tonemap = moon_tonemap;
m_moon_tonemap = tsrc->isKnownSourceImage(m_moon_params.tonemap) ? m_moon_tonemap = tsrc->isKnownSourceImage(m_moon_params.tonemap) ?
tsrc->getTexture(m_moon_params.tonemap) : NULL; tsrc->getTexture(m_moon_params.tonemap) : nullptr;
m_materials[4].Lighting = !!m_moon_tonemap; m_materials[4].Lighting = !!m_moon_tonemap;
if (m_moon_params.texture == moon_texture) if (m_moon_params.texture == moon_texture)
@ -893,7 +893,7 @@ void Sky::setSkyColors(const SkyColor &sky_color)
} }
void Sky::setHorizonTint(video::SColor sun_tint, video::SColor moon_tint, void Sky::setHorizonTint(video::SColor sun_tint, video::SColor moon_tint,
std::string use_sun_tint) const std::string &use_sun_tint)
{ {
// Change sun and moon tinting: // Change sun and moon tinting:
m_sky_params.fog_sun_tint = sun_tint; m_sky_params.fog_sun_tint = sun_tint;
@ -907,7 +907,7 @@ void Sky::setHorizonTint(video::SColor sun_tint, video::SColor moon_tint,
m_default_tint = true; m_default_tint = true;
} }
void Sky::addTextureToSkybox(std::string texture, int material_id, void Sky::addTextureToSkybox(const std::string &texture, int material_id,
ITextureSource *tsrc) ITextureSource *tsrc)
{ {
// Sanity check for more than six textures. // Sanity check for more than six textures.

@ -65,15 +65,15 @@ public:
} }
void setSunVisible(bool sun_visible) { m_sun_params.visible = sun_visible; } void setSunVisible(bool sun_visible) { m_sun_params.visible = sun_visible; }
void setSunTexture(std::string sun_texture, void setSunTexture(const std::string &sun_texture,
std::string sun_tonemap, ITextureSource *tsrc); const std::string &sun_tonemap, ITextureSource *tsrc);
void setSunScale(f32 sun_scale) { m_sun_params.scale = sun_scale; } void setSunScale(f32 sun_scale) { m_sun_params.scale = sun_scale; }
void setSunriseVisible(bool glow_visible) { m_sun_params.sunrise_visible = glow_visible; } void setSunriseVisible(bool glow_visible) { m_sun_params.sunrise_visible = glow_visible; }
void setSunriseTexture(std::string sunglow_texture, ITextureSource* tsrc); void setSunriseTexture(const std::string &sunglow_texture, ITextureSource* tsrc);
void setMoonVisible(bool moon_visible) { m_moon_params.visible = moon_visible; } void setMoonVisible(bool moon_visible) { m_moon_params.visible = moon_visible; }
void setMoonTexture(std::string moon_texture, void setMoonTexture(const std::string &moon_texture,
std::string moon_tonemap, ITextureSource *tsrc); const std::string &moon_tonemap, ITextureSource *tsrc);
void setMoonScale(f32 moon_scale) { m_moon_params.scale = moon_scale; } void setMoonScale(f32 moon_scale) { m_moon_params.scale = moon_scale; }
void setStarsVisible(bool stars_visible) { m_star_params.visible = stars_visible; } void setStarsVisible(bool stars_visible) { m_star_params.visible = stars_visible; }
@ -87,21 +87,21 @@ public:
void setVisible(bool visible) { m_visible = visible; } void setVisible(bool visible) { m_visible = visible; }
// Set only from set_sky API // Set only from set_sky API
void setCloudsEnabled(bool clouds_enabled) { m_clouds_enabled = clouds_enabled; } void setCloudsEnabled(bool clouds_enabled) { m_clouds_enabled = clouds_enabled; }
void setFallbackBgColor(const video::SColor &fallback_bg_color) void setFallbackBgColor(video::SColor fallback_bg_color)
{ {
m_fallback_bg_color = fallback_bg_color; m_fallback_bg_color = fallback_bg_color;
} }
void overrideColors(const video::SColor &bgcolor, const video::SColor &skycolor) void overrideColors(video::SColor bgcolor, video::SColor skycolor)
{ {
m_bgcolor = bgcolor; m_bgcolor = bgcolor;
m_skycolor = skycolor; m_skycolor = skycolor;
} }
void setSkyColors(const SkyColor &sky_color); void setSkyColors(const SkyColor &sky_color);
void setHorizonTint(video::SColor sun_tint, video::SColor moon_tint, void setHorizonTint(video::SColor sun_tint, video::SColor moon_tint,
std::string use_sun_tint); const std::string &use_sun_tint);
void setInClouds(bool clouds) { m_in_clouds = clouds; } void setInClouds(bool clouds) { m_in_clouds = clouds; }
void clearSkyboxTextures() { m_sky_params.textures.clear(); } void clearSkyboxTextures() { m_sky_params.textures.clear(); }
void addTextureToSkybox(std::string texture, int material_id, void addTextureToSkybox(const std::string &texture, int material_id,
ITextureSource *tsrc); ITextureSource *tsrc);
const video::SColorf &getCurrentStarColor() const { return m_star_color; } const video::SColorf &getCurrentStarColor() const { return m_star_color; }
@ -126,7 +126,7 @@ private:
} }
// Mix two colors by a given amount // Mix two colors by a given amount
video::SColor m_mix_scolor(video::SColor col1, video::SColor col2, f32 factor) static video::SColor m_mix_scolor(video::SColor col1, video::SColor col2, f32 factor)
{ {
video::SColor result = video::SColor( video::SColor result = video::SColor(
col1.getAlpha() * (1 - factor) + col2.getAlpha() * factor, col1.getAlpha() * (1 - factor) + col2.getAlpha() * factor,
@ -135,7 +135,7 @@ private:
col1.getBlue() * (1 - factor) + col2.getBlue() * factor); col1.getBlue() * (1 - factor) + col2.getBlue() * factor);
return result; return result;
} }
video::SColorf m_mix_scolorf(video::SColorf col1, video::SColorf col2, f32 factor) static video::SColorf m_mix_scolorf(video::SColorf col1, video::SColorf col2, f32 factor)
{ {
video::SColorf result = video::SColorf result =
video::SColorf(col1.r * (1 - factor) + col2.r * factor, video::SColorf(col1.r * (1 - factor) + col2.r * factor,

@ -671,7 +671,6 @@ void ClientInterface::UpdatePlayerList()
std::vector<session_t> clients = getClientIDs(); std::vector<session_t> clients = getClientIDs();
m_clients_names.clear(); m_clients_names.clear();
if (!clients.empty()) if (!clients.empty())
infostream<<"Players:"<<std::endl; infostream<<"Players:"<<std::endl;

@ -30,7 +30,7 @@ using namespace gui;
GUIButtonItemImage::GUIButtonItemImage(gui::IGUIEnvironment *environment, GUIButtonItemImage::GUIButtonItemImage(gui::IGUIEnvironment *environment,
gui::IGUIElement *parent, s32 id, core::rect<s32> rectangle, gui::IGUIElement *parent, s32 id, core::rect<s32> rectangle,
ISimpleTextureSource *tsrc, std::string item, Client *client, ISimpleTextureSource *tsrc, const std::string &item, Client *client,
bool noclip) bool noclip)
: GUIButton (environment, parent, id, rectangle, tsrc, noclip) : GUIButton (environment, parent, id, rectangle, tsrc, noclip)
{ {
@ -44,7 +44,7 @@ GUIButtonItemImage::GUIButtonItemImage(gui::IGUIEnvironment *environment,
GUIButtonItemImage *GUIButtonItemImage::addButton(IGUIEnvironment *environment, GUIButtonItemImage *GUIButtonItemImage::addButton(IGUIEnvironment *environment,
const core::rect<s32> &rectangle, ISimpleTextureSource *tsrc, const core::rect<s32> &rectangle, ISimpleTextureSource *tsrc,
IGUIElement *parent, s32 id, const wchar_t *text, std::string item, IGUIElement *parent, s32 id, const wchar_t *text, const std::string &item,
Client *client) Client *client)
{ {
GUIButtonItemImage *button = new GUIButtonItemImage(environment, GUIButtonItemImage *button = new GUIButtonItemImage(environment,

@ -33,13 +33,13 @@ public:
//! constructor //! constructor
GUIButtonItemImage(gui::IGUIEnvironment *environment, gui::IGUIElement *parent, GUIButtonItemImage(gui::IGUIEnvironment *environment, gui::IGUIElement *parent,
s32 id, core::rect<s32> rectangle, ISimpleTextureSource *tsrc, s32 id, core::rect<s32> rectangle, ISimpleTextureSource *tsrc,
std::string item, Client *client, bool noclip = false); const std::string &item, Client *client, bool noclip = false);
//! Do not drop returned handle //! Do not drop returned handle
static GUIButtonItemImage *addButton(gui::IGUIEnvironment *environment, static GUIButtonItemImage *addButton(gui::IGUIEnvironment *environment,
const core::rect<s32> &rectangle, ISimpleTextureSource *tsrc, const core::rect<s32> &rectangle, ISimpleTextureSource *tsrc,
IGUIElement *parent, s32 id, const wchar_t *text, std::string item, IGUIElement *parent, s32 id, const wchar_t *text,
Client *client); const std::string &item, Client *client);
private: private:
Client *m_client; Client *m_client;

@ -965,13 +965,14 @@ InventoryList * Inventory::getList(const std::string &name)
{ {
s32 i = getListIndex(name); s32 i = getListIndex(name);
if(i == -1) if(i == -1)
return NULL; return nullptr;
return m_lists[i]; return m_lists[i];
} }
std::vector<const InventoryList*> Inventory::getLists() std::vector<const InventoryList*> Inventory::getLists()
{ {
std::vector<const InventoryList*> lists; std::vector<const InventoryList*> lists;
lists.reserve(m_lists.size());
for (auto list : m_lists) { for (auto list : m_lists) {
lists.push_back(list); lists.push_back(list);
} }
@ -990,11 +991,11 @@ bool Inventory::deleteList(const std::string &name)
return true; return true;
} }
const InventoryList * Inventory::getList(const std::string &name) const const InventoryList *Inventory::getList(const std::string &name) const
{ {
s32 i = getListIndex(name); s32 i = getListIndex(name);
if(i == -1) if(i == -1)
return NULL; return nullptr;
return m_lists[i]; return m_lists[i];
} }

@ -1544,10 +1544,10 @@ void NodeDefManager::deSerialize(std::istream &is)
} }
void NodeDefManager::addNameIdMapping(content_t i, std::string name) void NodeDefManager::addNameIdMapping(content_t i, const std::string &name)
{ {
m_name_id_mapping.set(i, name); m_name_id_mapping.set(i, name);
m_name_id_mapping_with_aliases.insert(std::make_pair(name, i)); m_name_id_mapping_with_aliases.emplace(name, i);
} }

@ -725,7 +725,7 @@ private:
* @param i a content ID * @param i a content ID
* @param name a node name * @param name a node name
*/ */
void addNameIdMapping(content_t i, std::string name); void addNameIdMapping(content_t i, const std::string &name);
/*! /*!
* Removes a content ID from all groups. * Removes a content ID from all groups.

@ -206,10 +206,9 @@ NodeMetadataList::~NodeMetadataList()
std::vector<v3s16> NodeMetadataList::getAllKeys() std::vector<v3s16> NodeMetadataList::getAllKeys()
{ {
std::vector<v3s16> keys; std::vector<v3s16> keys;
keys.reserve(m_data.size());
NodeMetadataMap::const_iterator it; for (const auto &it : m_data)
for (it = m_data.begin(); it != m_data.end(); ++it) keys.push_back(it.first);
keys.push_back(it->first);
return keys; return keys;
} }
@ -218,7 +217,7 @@ NodeMetadata *NodeMetadataList::get(v3s16 p)
{ {
NodeMetadataMap::const_iterator n = m_data.find(p); NodeMetadataMap::const_iterator n = m_data.find(p);
if (n == m_data.end()) if (n == m_data.end())
return NULL; return nullptr;
return n->second; return n->second;
} }
@ -235,7 +234,7 @@ void NodeMetadataList::remove(v3s16 p)
void NodeMetadataList::set(v3s16 p, NodeMetadata *d) void NodeMetadataList::set(v3s16 p, NodeMetadata *d)
{ {
remove(p); remove(p);
m_data.insert(std::make_pair(p, d)); m_data.emplace(p, d);
} }
void NodeMetadataList::clear() void NodeMetadataList::clear()
@ -251,9 +250,8 @@ void NodeMetadataList::clear()
int NodeMetadataList::countNonEmpty() const int NodeMetadataList::countNonEmpty() const
{ {
int n = 0; int n = 0;
NodeMetadataMap::const_iterator it; for (const auto &it : m_data) {
for (it = m_data.begin(); it != m_data.end(); ++it) { if (!it.second->empty())
if (!it->second->empty())
n++; n++;
} }
return n; return n;

@ -1428,7 +1428,7 @@ std::string Pathfinder::dirToName(PathDirections dir)
} }
/******************************************************************************/ /******************************************************************************/
void Pathfinder::printPath(std::vector<v3s16> path) void Pathfinder::printPath(const std::vector<v3s16> &path)
{ {
unsigned int current = 0; unsigned int current = 0;
for (std::vector<v3s16>::iterator i = path.begin(); for (std::vector<v3s16>::iterator i = path.begin();

@ -43,11 +43,11 @@ std::unordered_map<std::string, const FlagDesc *> Settings::s_flags;
Settings *Settings::createLayer(SettingsLayer sl, const std::string &end_tag) Settings *Settings::createLayer(SettingsLayer sl, const std::string &end_tag)
{ {
if ((int)sl < 0 || sl >= SL_TOTAL_COUNT) if ((int)sl < 0 || sl >= SL_TOTAL_COUNT)
throw new BaseException("Invalid settings layer"); throw BaseException("Invalid settings layer");
Settings *&pos = s_layers[(size_t)sl]; Settings *&pos = s_layers[(size_t)sl];
if (pos) if (pos)
throw new BaseException("Setting layer " + std::to_string(sl) + " already exists"); throw BaseException("Setting layer " + std::to_string(sl) + " already exists");
pos = new Settings(end_tag); pos = new Settings(end_tag);
pos->m_settingslayer = sl; pos->m_settingslayer = sl;
@ -638,6 +638,7 @@ std::vector<std::string> Settings::getNames() const
MutexAutoLock lock(m_mutex); MutexAutoLock lock(m_mutex);
std::vector<std::string> names; std::vector<std::string> names;
names.reserve(m_settings.size());
for (const auto &settings_it : m_settings) { for (const auto &settings_it : m_settings) {
names.push_back(settings_it.first); names.push_back(settings_it.first);
} }

@ -96,16 +96,15 @@ void AreaStore::deserialize(std::istream &is)
u16 num_areas = readU16(is); u16 num_areas = readU16(is);
std::vector<Area> areas; std::vector<Area> areas;
areas.reserve(num_areas);
for (u32 i = 0; i < num_areas; ++i) { for (u32 i = 0; i < num_areas; ++i) {
Area a(U32_MAX); Area a(U32_MAX);
a.minedge = readV3S16(is); a.minedge = readV3S16(is);
a.maxedge = readV3S16(is); a.maxedge = readV3S16(is);
u16 data_len = readU16(is); u16 data_len = readU16(is);
char *data = new char[data_len]; a.data = std::string(data_len, '\0');
is.read(data, data_len); is.read(&a.data[0], data_len);
a.data = std::string(data, data_len); areas.emplace_back(std::move(a));
areas.emplace_back(a);
delete [] data;
} }
bool read_ids = is.good(); // EOF for old formats bool read_ids = is.good(); // EOF for old formats

@ -90,8 +90,7 @@ public:
bool get(const Key &name, Value *result) const bool get(const Key &name, Value *result) const
{ {
MutexAutoLock lock(m_mutex); MutexAutoLock lock(m_mutex);
typename std::map<Key, Value>::const_iterator n = auto n = m_values.find(name);
m_values.find(name);
if (n == m_values.end()) if (n == m_values.end())
return false; return false;
if (result) if (result)
@ -103,11 +102,9 @@ public:
{ {
MutexAutoLock lock(m_mutex); MutexAutoLock lock(m_mutex);
std::vector<Value> result; std::vector<Value> result;
for (typename std::map<Key, Value>::const_iterator result.reserve(m_values.size());
it = m_values.begin(); for (auto it = m_values.begin(); it != m_values.end(); ++it)
it != m_values.end(); ++it){
result.push_back(it->second); result.push_back(it->second);
}
return result; return result;
} }
@ -136,7 +133,7 @@ public:
return m_queue.empty(); return m_queue.empty();
} }
void push_back(T t) void push_back(const T &t)
{ {
MutexAutoLock lock(m_mutex); MutexAutoLock lock(m_mutex);
m_queue.push_back(t); m_queue.push_back(t);
@ -151,7 +148,7 @@ public:
if (m_signal.wait(wait_time_max_ms)) { if (m_signal.wait(wait_time_max_ms)) {
MutexAutoLock lock(m_mutex); MutexAutoLock lock(m_mutex);
T t = m_queue.front(); T t = std::move(m_queue.front());
m_queue.pop_front(); m_queue.pop_front();
return t; return t;
} }
@ -164,7 +161,7 @@ public:
if (m_signal.wait(wait_time_max_ms)) { if (m_signal.wait(wait_time_max_ms)) {
MutexAutoLock lock(m_mutex); MutexAutoLock lock(m_mutex);
T t = m_queue.front(); T t = std::move(m_queue.front());
m_queue.pop_front(); m_queue.pop_front();
return t; return t;
} }
@ -178,7 +175,7 @@ public:
MutexAutoLock lock(m_mutex); MutexAutoLock lock(m_mutex);
T t = m_queue.front(); T t = std::move(m_queue.front());
m_queue.pop_front(); m_queue.pop_front();
return t; return t;
} }
@ -188,7 +185,7 @@ public:
if (m_signal.wait(wait_time_max_ms)) { if (m_signal.wait(wait_time_max_ms)) {
MutexAutoLock lock(m_mutex); MutexAutoLock lock(m_mutex);
T t = m_queue.back(); T t = std::move(m_queue.back());
m_queue.pop_back(); m_queue.pop_back();
return t; return t;
} }
@ -204,7 +201,7 @@ public:
if (m_signal.wait(wait_time_max_ms)) { if (m_signal.wait(wait_time_max_ms)) {
MutexAutoLock lock(m_mutex); MutexAutoLock lock(m_mutex);
T t = m_queue.back(); T t = std::move(m_queue.back());
m_queue.pop_back(); m_queue.pop_back();
return t; return t;
} }
@ -218,7 +215,7 @@ public:
MutexAutoLock lock(m_mutex); MutexAutoLock lock(m_mutex);
T t = m_queue.back(); T t = std::move(m_queue.back());
m_queue.pop_back(); m_queue.pop_back();
return t; return t;
} }

@ -65,12 +65,14 @@ void EnrichedString::operator=(const wchar_t *str)
addAtEnd(translate_string(std::wstring(str)), m_default_color); addAtEnd(translate_string(std::wstring(str)), m_default_color);
} }
void EnrichedString::addAtEnd(const std::wstring &s, const SColor &initial_color) void EnrichedString::addAtEnd(const std::wstring &s, SColor initial_color)
{ {
SColor color(initial_color); SColor color(initial_color);
bool use_default = (m_default_length == m_string.size() && bool use_default = (m_default_length == m_string.size() &&
color == m_default_color); color == m_default_color);
m_colors.reserve(m_colors.size() + s.size());
size_t i = 0; size_t i = 0;
while (i < s.length()) { while (i < s.length()) {
if (s[i] != L'\x1b') { if (s[i] != L'\x1b') {
@ -200,12 +202,6 @@ const std::wstring &EnrichedString::getString() const
return m_string; return m_string;
} }
void EnrichedString::setDefaultColor(const irr::video::SColor &color)
{
m_default_color = color;
updateDefaultColor();
}
void EnrichedString::updateDefaultColor() void EnrichedString::updateDefaultColor()
{ {
sanity_check(m_default_length <= m_colors.size()); sanity_check(m_default_length <= m_colors.size());

@ -23,18 +23,22 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include <vector> #include <vector>
#include <SColor.h> #include <SColor.h>
using namespace irr;
class EnrichedString { class EnrichedString {
public: public:
EnrichedString(); EnrichedString();
EnrichedString(const std::wstring &s, EnrichedString(const std::wstring &s,
const irr::video::SColor &color = irr::video::SColor(255, 255, 255, 255)); const video::SColor &color = video::SColor(255, 255, 255, 255));
EnrichedString(const wchar_t *str, EnrichedString(const wchar_t *str,
const irr::video::SColor &color = irr::video::SColor(255, 255, 255, 255)); const video::SColor &color = video::SColor(255, 255, 255, 255));
EnrichedString(const std::wstring &string, EnrichedString(const std::wstring &string,
const std::vector<irr::video::SColor> &colors); const std::vector<video::SColor> &colors);
void clear();
void operator=(const wchar_t *str); void operator=(const wchar_t *str);
void addAtEnd(const std::wstring &s, const irr::video::SColor &color);
void clear();
void addAtEnd(const std::wstring &s, video::SColor color);
// Adds the character source[i] at the end. // Adds the character source[i] at the end.
// An EnrichedString should always be able to be copied // An EnrichedString should always be able to be copied
@ -49,12 +53,16 @@ public:
EnrichedString operator+(const EnrichedString &other) const; EnrichedString operator+(const EnrichedString &other) const;
void operator+=(const EnrichedString &other); void operator+=(const EnrichedString &other);
const wchar_t *c_str() const; const wchar_t *c_str() const;
const std::vector<irr::video::SColor> &getColors() const; const std::vector<video::SColor> &getColors() const;
const std::wstring &getString() const; const std::wstring &getString() const;
void setDefaultColor(const irr::video::SColor &color); inline void setDefaultColor(video::SColor color)
{
m_default_color = color;
updateDefaultColor();
}
void updateDefaultColor(); void updateDefaultColor();
inline const irr::video::SColor &getDefaultColor() const inline const video::SColor &getDefaultColor() const
{ {
return m_default_color; return m_default_color;
} }
@ -80,11 +88,11 @@ public:
{ {
return m_has_background; return m_has_background;
} }
inline irr::video::SColor getBackground() const inline video::SColor getBackground() const
{ {
return m_background; return m_background;
} }
inline void setBackground(const irr::video::SColor &color) inline void setBackground(video::SColor color)
{ {
m_background = color; m_background = color;
m_has_background = true; m_has_background = true;
@ -92,10 +100,10 @@ public:
private: private:
std::wstring m_string; std::wstring m_string;
std::vector<irr::video::SColor> m_colors; std::vector<video::SColor> m_colors;
bool m_has_background; bool m_has_background;
irr::video::SColor m_default_color; video::SColor m_default_color;
irr::video::SColor m_background; video::SColor m_background;
// This variable defines the length of the default-colored text. // This variable defines the length of the default-colored text.
// Change this to a std::vector if an "end coloring" tag is wanted. // Change this to a std::vector if an "end coloring" tag is wanted.
size_t m_default_length = 0; size_t m_default_length = 0;

@ -65,7 +65,7 @@ struct ChangingLight {
ChangingLight() = default; ChangingLight() = default;
ChangingLight(const relative_v3 &rel_pos, const mapblock_v3 &block_pos, ChangingLight(relative_v3 rel_pos, mapblock_v3 block_pos,
MapBlock *b, direction source_dir) : MapBlock *b, direction source_dir) :
rel_position(rel_pos), rel_position(rel_pos),
block_position(block_pos), block_position(block_pos),
@ -125,8 +125,8 @@ struct LightQueue {
* The parameters are the same as in ChangingLight's constructor. * The parameters are the same as in ChangingLight's constructor.
* \param light light level of the ChangingLight * \param light light level of the ChangingLight
*/ */
inline void push(u8 light, const relative_v3 &rel_pos, inline void push(u8 light, relative_v3 rel_pos,
const mapblock_v3 &block_pos, MapBlock *block, mapblock_v3 block_pos, MapBlock *block,
direction source_dir) direction source_dir)
{ {
assert(light <= LIGHT_SUN); assert(light <= LIGHT_SUN);
@ -467,7 +467,7 @@ bool is_sunlight_above(Map *map, v3s16 pos, const NodeDefManager *ndef)
static const LightBank banks[] = { LIGHTBANK_DAY, LIGHTBANK_NIGHT }; static const LightBank banks[] = { LIGHTBANK_DAY, LIGHTBANK_NIGHT };
void update_lighting_nodes(Map *map, void update_lighting_nodes(Map *map,
std::vector<std::pair<v3s16, MapNode> > &oldnodes, const std::vector<std::pair<v3s16, MapNode>> &oldnodes,
std::map<v3s16, MapBlock*> &modified_blocks) std::map<v3s16, MapBlock*> &modified_blocks)
{ {
const NodeDefManager *ndef = map->getNodeDefManager(); const NodeDefManager *ndef = map->getNodeDefManager();
@ -482,8 +482,7 @@ void update_lighting_nodes(Map *map,
// won't change, since they didn't get their light from a // won't change, since they didn't get their light from a
// modified node. // modified node.
u8 min_safe_light = 0; u8 min_safe_light = 0;
for (std::vector<std::pair<v3s16, MapNode> >::iterator it = for (auto it = oldnodes.cbegin(); it < oldnodes.cend(); ++it) {
oldnodes.begin(); it < oldnodes.end(); ++it) {
u8 old_light = it->second.getLight(bank, ndef); u8 old_light = it->second.getLight(bank, ndef);
if (old_light > min_safe_light) { if (old_light > min_safe_light) {
min_safe_light = old_light; min_safe_light = old_light;
@ -495,8 +494,7 @@ void update_lighting_nodes(Map *map,
min_safe_light++; min_safe_light++;
} }
// For each changed node process sunlight and initialize // For each changed node process sunlight and initialize
for (std::vector<std::pair<v3s16, MapNode> >::iterator it = for (auto it = oldnodes.cbegin(); it < oldnodes.cend(); ++it) {
oldnodes.begin(); it < oldnodes.end(); ++it) {
// Get position and block of the changed node // Get position and block of the changed node
v3s16 p = it->first; v3s16 p = it->first;
relative_v3 rel_pos; relative_v3 rel_pos;

@ -45,7 +45,7 @@ namespace voxalgo
*/ */
void update_lighting_nodes( void update_lighting_nodes(
Map *map, Map *map,
std::vector<std::pair<v3s16, MapNode> > &oldnodes, const std::vector<std::pair<v3s16, MapNode>> &oldnodes,
std::map<v3s16, MapBlock*> &modified_blocks); std::map<v3s16, MapBlock*> &modified_blocks);
/*! /*!