Skip to content

Commit 260a4a1

Browse files
committed
Map: Minor Refactoring
1 parent df82f78 commit 260a4a1

File tree

1 file changed

+32
-51
lines changed

1 file changed

+32
-51
lines changed

src/core/map.cpp

Lines changed: 32 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include <cmath>
2626
#include <iterator>
2727
#include <memory>
28+
#include <numeric>
2829
#include <type_traits>
2930
#include <utility>
3031

@@ -1272,11 +1273,10 @@ void Map::deleteColor(int pos)
12721273

12731274
int Map::findColorIndex(const MapColor* color) const
12741275
{
1275-
std::size_t size = color_set->colors.size();
1276-
for (std::size_t i = 0; i < size; ++i)
1276+
for (int i = 0, size = getNumColors(); i < size; ++i)
12771277
{
12781278
if (color_set->colors[i] == color)
1279-
return (int)i;
1279+
return i;
12801280
}
12811281
if (color && color->getPriority() == MapColor::Registration)
12821282
{
@@ -1298,12 +1298,9 @@ void Map::useColorsFrom(Map* map)
12981298

12991299
bool Map::isColorUsedByASymbol(const MapColor* color) const
13001300
{
1301-
for (const Symbol* symbol : symbols)
1302-
{
1303-
if (symbol->containsColor(color))
1304-
return true;
1305-
}
1306-
return false;
1301+
return std::any_of(begin(symbols), end(symbols), [color](const Symbol* symbol) {
1302+
return symbol->containsColor(color);
1303+
});
13071304
}
13081305

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

13171314
Q_ASSERT(int(by_which_symbols.size()) == getNumSymbols());
1318-
out.assign(std::size_t(getNumColors()), false);
1319-
for (std::size_t c = 0, last = std::size_t(getNumColors()); c != last; ++c)
1315+
out.assign(getNumColors(), false);
1316+
for (int c = 0, last = getNumColors(); c != last; ++c)
13201317
{
1321-
for (std::size_t s = 0, last_s = std::size_t(getNumSymbols()); s != last_s; ++s)
1318+
for (int s = 0, last_s = getNumSymbols(); s != last_s; ++s)
13221319
{
1323-
if (by_which_symbols[s] && getSymbol(int(s))->containsColor(getColor(int(c))))
1320+
if (by_which_symbols[s] && getSymbol(s)->containsColor(getColor(c)))
13241321
{
13251322
out[c] = true;
13261323
break;
@@ -1329,21 +1326,21 @@ void Map::determineColorsInUse(const std::vector< bool >& by_which_symbols, std:
13291326
}
13301327

13311328
// Include required spot colors, too
1332-
for (std::size_t c = 0, last_c = std::size_t(getNumColors()); c != last_c; ++c)
1329+
for (int c = 0, last_c = getNumColors(); c != last_c; ++c)
13331330
{
13341331
if (out[c])
13351332
continue;
13361333

1337-
const auto* color = getColor(int(c));
1334+
const auto* color = getColor(c);
13381335
if (color->getSpotColorMethod() != MapColor::SpotColor)
13391336
continue;
13401337

1341-
for (std::size_t o = 0, last_o = std::size_t(getNumColors()); o != last_o; ++o)
1338+
for (int o = 0, last_o = getNumColors(); o != last_o; ++o)
13421339
{
13431340
if (!out[o])
13441341
continue;
13451342

1346-
const auto* other = getColor(int(o));
1343+
const auto* other = getColor(o);
13471344
if (other->getSpotColorMethod() != MapColor::CustomColor)
13481345
continue;
13491346

@@ -1371,12 +1368,9 @@ void Map::checkSpotColorPresence()
13711368

13721369
bool Map::hasSpotColors() const
13731370
{
1374-
for (const MapColor* color : color_set->colors)
1375-
{
1376-
if (color->getSpotColorMethod() == MapColor::SpotColor)
1377-
return true;
1378-
}
1379-
return false;
1371+
return std::any_of(begin(color_set->colors), end(color_set->colors), [](const MapColor* color) {
1372+
return color->getSpotColorMethod() == MapColor::SpotColor;
1373+
});
13801374
}
13811375

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

15791573
// Check if an object with this symbol is selected
1580-
bool object_with_symbol_selected = false;
1581-
for (const Object* object : object_selection)
1582-
{
1583-
if (object->getSymbol() == old_symbol || object->getSymbol()->containsSymbol(old_symbol))
1584-
{
1585-
object_with_symbol_selected = true;
1586-
break;
1587-
}
1588-
}
1574+
const bool object_with_symbol_selected = std::any_of(begin(object_selection), end(object_selection), [old_symbol](const Object* object) {
1575+
return object->getSymbol() == old_symbol || object->getSymbol()->containsSymbol(old_symbol);
1576+
});
15891577

15901578
changeSymbolForAllObjects(old_symbol, symbol);
15911579

1592-
int size = (int)symbols.size();
1593-
for (int i = 0; i < size; ++i)
1580+
for (int i = 0, size = getNumSymbols(); i < size; ++i)
15941581
{
15951582
if (i == pos)
15961583
continue;
@@ -1614,8 +1601,7 @@ void Map::deleteSymbol(int pos)
16141601
if (deleteAllObjectsWithSymbol(symbols[pos]))
16151602
undo_manager->clear();
16161603

1617-
int size = (int)symbols.size();
1618-
for (int i = 0; i < size; ++i)
1604+
for (int i = 0, size = getNumSymbols(); i < size; ++i)
16191605
{
16201606
if (i == pos)
16211607
continue;
@@ -1636,8 +1622,7 @@ int Map::findSymbolIndex(const Symbol* symbol) const
16361622
{
16371623
if (!symbol)
16381624
return -1;
1639-
int size = (int)symbols.size();
1640-
for (int i = 0; i < size; ++i)
1625+
for (int i = 0, size = getNumSymbols(); i < size; ++i)
16411626
{
16421627
if (symbols[i] == symbol)
16431628
return i;
@@ -1667,7 +1652,7 @@ void Map::setSymbolsDirty()
16671652

16681653
void Map::updateSymbolIcons(const MapColor* color)
16691654
{
1670-
for (std::size_t i = 0, size = symbols.size(); i < size; ++i)
1655+
for (int i = 0, size = getNumSymbols(); i < size; ++i)
16711656
{
16721657
if (symbols[i]->containsColor(color))
16731658
{
@@ -1679,8 +1664,7 @@ void Map::updateSymbolIcons(const MapColor* color)
16791664

16801665
void Map::scaleAllSymbols(double factor)
16811666
{
1682-
int size = getNumSymbols();
1683-
for (int i = 0; i < size; ++i)
1667+
for (int i = 0, size = getNumSymbols(); i < size; ++i)
16841668
{
16851669
Symbol* symbol = getSymbol(i);
16861670
symbol->scale(factor);
@@ -2087,8 +2071,7 @@ void Map::removePart(std::size_t index)
20872071

20882072
int Map::findPartIndex(const MapPart* part) const
20892073
{
2090-
std::size_t const size = parts.size();
2091-
for (std::size_t i = 0; i < size; ++i)
2074+
for (int i = 0, size = getNumSymbols(); i < size; ++i)
20922075
{
20932076
if (parts[i] == part)
20942077
return i;
@@ -2177,10 +2160,9 @@ int Map::mergeParts(std::size_t source, std::size_t destination)
21772160

21782161
int Map::getNumObjects() const
21792162
{
2180-
int num_objects = 0;
2181-
for (const MapPart* part : parts)
2182-
num_objects += part->getNumObjects();
2183-
return num_objects;
2163+
return std::accumulate(begin(parts), end(parts), 0, [](int num_objects, const auto part) {
2164+
return num_objects + part->getNumObjects();
2165+
});
21842166
}
21852167

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

22852267
int Map::countObjectsInRect(const QRectF& map_coord_rect, bool include_hidden_objects)
22862268
{
2287-
int count = 0;
2288-
for (const MapPart* part : parts)
2289-
count += part->countObjectsInRect(map_coord_rect, include_hidden_objects);
2290-
return count;
2269+
return std::accumulate(begin(parts), end(parts), 0, [&map_coord_rect, include_hidden_objects](int count, const auto part) {
2270+
return count + part->countObjectsInRect(map_coord_rect, include_hidden_objects);
2271+
});
22912272
}
22922273

22932274

0 commit comments

Comments
 (0)