Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Map: Minor Refactoring #2319

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
101 changes: 41 additions & 60 deletions src/core/map.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include <cmath>
#include <iterator>
#include <memory>
#include <numeric>
#include <type_traits>
#include <utility>

Expand Down Expand Up @@ -888,17 +889,17 @@ std::size_t Map::deleteIrregularObjects()
std::set<Object*> unhandled;
for (auto object : irregular_objects)
{
for (auto part : parts)
if (std::none_of(begin(parts), end(parts), [&result, object](MapPart* const part) {
if (part->deleteObject(object))
{
++result;
return true;
}
return false;
}))
{
if (part->deleteObject(object))
{
++result;
goto next_object;
}
unhandled.insert(object);
}
unhandled.insert(object);
next_object:
; // nothing else
}

irregular_objects.swap(unhandled);
Expand Down Expand Up @@ -1272,11 +1273,10 @@ void Map::deleteColor(int pos)

int Map::findColorIndex(const MapColor* color) const
{
std::size_t size = color_set->colors.size();
for (std::size_t i = 0; i < size; ++i)
for (int i = 0, size = getNumColors(); i < size; ++i)
{
if (color_set->colors[i] == color)
return (int)i;
return i;
}
if (color && color->getPriority() == MapColor::Registration)
{
Expand All @@ -1298,12 +1298,9 @@ void Map::useColorsFrom(Map* map)

bool Map::isColorUsedByASymbol(const MapColor* color) const
{
for (const Symbol* symbol : symbols)
{
if (symbol->containsColor(color))
return true;
}
return false;
return std::any_of(begin(symbols), end(symbols), [color](const Symbol* symbol) {
return symbol->containsColor(color);
});
}

void Map::determineColorsInUse(const std::vector< bool >& by_which_symbols, std::vector< bool >& out) const
Expand All @@ -1315,12 +1312,12 @@ void Map::determineColorsInUse(const std::vector< bool >& by_which_symbols, std:
}

Q_ASSERT(int(by_which_symbols.size()) == getNumSymbols());
out.assign(std::size_t(getNumColors()), false);
for (std::size_t c = 0, last = std::size_t(getNumColors()); c != last; ++c)
out.assign(getNumColors(), false);
for (int c = 0, last = getNumColors(); c != last; ++c)
{
for (std::size_t s = 0, last_s = std::size_t(getNumSymbols()); s != last_s; ++s)
for (int s = 0, last_s = getNumSymbols(); s != last_s; ++s)
{
if (by_which_symbols[s] && getSymbol(int(s))->containsColor(getColor(int(c))))
if (by_which_symbols[s] && getSymbol(s)->containsColor(getColor(c)))
{
out[c] = true;
break;
Expand All @@ -1329,21 +1326,21 @@ void Map::determineColorsInUse(const std::vector< bool >& by_which_symbols, std:
}

// Include required spot colors, too
for (std::size_t c = 0, last_c = std::size_t(getNumColors()); c != last_c; ++c)
for (int c = 0, last_c = getNumColors(); c != last_c; ++c)
{
if (out[c])
continue;

const auto* color = getColor(int(c));
const auto* color = getColor(c);
if (color->getSpotColorMethod() != MapColor::SpotColor)
continue;

for (std::size_t o = 0, last_o = std::size_t(getNumColors()); o != last_o; ++o)
for (int o = 0, last_o = getNumColors(); o != last_o; ++o)
{
if (!out[o])
continue;

const auto* other = getColor(int(o));
const auto* other = getColor(o);
if (other->getSpotColorMethod() != MapColor::CustomColor)
continue;

Expand Down Expand Up @@ -1371,12 +1368,9 @@ void Map::checkSpotColorPresence()

bool Map::hasSpotColors() const
{
for (const MapColor* color : color_set->colors)
{
if (color->getSpotColorMethod() == MapColor::SpotColor)
return true;
}
return false;
return std::any_of(begin(color_set->colors), end(color_set->colors), [](const MapColor* color) {
return color->getSpotColorMethod() == MapColor::SpotColor;
});
}

bool Map::hasAlpha() const
Expand Down Expand Up @@ -1577,20 +1571,13 @@ void Map::setSymbol(Symbol* symbol, int pos)
Symbol* old_symbol = symbols[pos];

// Check if an object with this symbol is selected
bool object_with_symbol_selected = false;
for (const Object* object : object_selection)
{
if (object->getSymbol() == old_symbol || object->getSymbol()->containsSymbol(old_symbol))
{
object_with_symbol_selected = true;
break;
}
}
const bool object_with_symbol_selected = std::any_of(begin(object_selection), end(object_selection), [old_symbol](const Object* object) {
return object->getSymbol() == old_symbol || object->getSymbol()->containsSymbol(old_symbol);
});

changeSymbolForAllObjects(old_symbol, symbol);

int size = (int)symbols.size();
for (int i = 0; i < size; ++i)
for (int i = 0, size = getNumSymbols(); i < size; ++i)
{
if (i == pos)
continue;
Expand All @@ -1614,8 +1601,7 @@ void Map::deleteSymbol(int pos)
if (deleteAllObjectsWithSymbol(symbols[pos]))
undo_manager->clear();

int size = (int)symbols.size();
for (int i = 0; i < size; ++i)
for (int i = 0, size = getNumSymbols(); i < size; ++i)
{
if (i == pos)
continue;
Expand All @@ -1636,8 +1622,7 @@ int Map::findSymbolIndex(const Symbol* symbol) const
{
if (!symbol)
return -1;
int size = (int)symbols.size();
for (int i = 0; i < size; ++i)
for (int i = 0, size = getNumSymbols(); i < size; ++i)
{
if (symbols[i] == symbol)
return i;
Expand Down Expand Up @@ -1667,7 +1652,7 @@ void Map::setSymbolsDirty()

void Map::updateSymbolIcons(const MapColor* color)
{
for (std::size_t i = 0, size = symbols.size(); i < size; ++i)
for (int i = 0, size = getNumSymbols(); i < size; ++i)
{
if (symbols[i]->containsColor(color))
{
Expand All @@ -1679,8 +1664,7 @@ void Map::updateSymbolIcons(const MapColor* color)

void Map::scaleAllSymbols(double factor)
{
int size = getNumSymbols();
for (int i = 0; i < size; ++i)
for (int i = 0, size = getNumSymbols(); i < size; ++i)
{
Symbol* symbol = getSymbol(i);
symbol->scale(factor);
Expand Down Expand Up @@ -2087,8 +2071,7 @@ void Map::removePart(std::size_t index)

int Map::findPartIndex(const MapPart* part) const
{
std::size_t const size = parts.size();
for (std::size_t i = 0; i < size; ++i)
for (int i = 0, size = getNumSymbols(); i < size; ++i)
{
if (parts[i] == part)
return i;
Expand Down Expand Up @@ -2177,10 +2160,9 @@ int Map::mergeParts(std::size_t source, std::size_t destination)

int Map::getNumObjects() const
{
int num_objects = 0;
for (const MapPart* part : parts)
num_objects += part->getNumObjects();
return num_objects;
return std::accumulate(begin(parts), end(parts), 0, [](int num_objects, const auto part) {
return num_objects + part->getNumObjects();
});
}

int Map::addObject(Object* object, int part_index)
Expand Down Expand Up @@ -2284,10 +2266,9 @@ void Map::findObjectsAtBox(

int Map::countObjectsInRect(const QRectF& map_coord_rect, bool include_hidden_objects)
{
int count = 0;
for (const MapPart* part : parts)
count += part->countObjectsInRect(map_coord_rect, include_hidden_objects);
return count;
return std::accumulate(begin(parts), end(parts), 0, [&map_coord_rect, include_hidden_objects](int count, const auto part) {
return count + part->countObjectsInRect(map_coord_rect, include_hidden_objects);
});
}


Expand Down