Skip to content

Commit db5caee

Browse files
committed
Map: Minor Refactoring
1 parent af48dfb commit db5caee

File tree

1 file changed

+33
-52
lines changed

1 file changed

+33
-52
lines changed

src/core/map.cpp

Lines changed: 33 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
22
* Copyright 2012-2014 Thomas Schöps
3-
* Copyright 2013-2020, 2024 Kai Pastor
3+
* Copyright 2013-2020, 2024, 2025 Kai Pastor
44
*
55
* This file is part of OpenOrienteering.
66
*
@@ -25,6 +25,7 @@
2525
#include <cmath>
2626
#include <iterator>
2727
#include <memory>
28+
#include <numeric>
2829
#include <type_traits>
2930
#include <utility>
3031

@@ -1274,11 +1275,10 @@ void Map::deleteColor(int pos)
12741275

12751276
int Map::findColorIndex(const MapColor* color) const
12761277
{
1277-
std::size_t size = color_set->colors.size();
1278-
for (std::size_t i = 0; i < size; ++i)
1278+
for (int i = 0, size = getNumColors(); i < size; ++i)
12791279
{
12801280
if (color_set->colors[i] == color)
1281-
return (int)i;
1281+
return i;
12821282
}
12831283
if (color && color->getPriority() == MapColor::Registration)
12841284
{
@@ -1300,12 +1300,9 @@ void Map::useColorsFrom(Map* map)
13001300

13011301
bool Map::isColorUsedByASymbol(const MapColor* color) const
13021302
{
1303-
for (const Symbol* symbol : symbols)
1304-
{
1305-
if (symbol->containsColor(color))
1306-
return true;
1307-
}
1308-
return false;
1303+
return std::any_of(begin(symbols), end(symbols), [color](const Symbol* symbol) {
1304+
return symbol->containsColor(color);
1305+
});
13091306
}
13101307

13111308
void Map::determineColorsInUse(const std::vector< bool >& by_which_symbols, std::vector< bool >& out) const
@@ -1317,12 +1314,12 @@ void Map::determineColorsInUse(const std::vector< bool >& by_which_symbols, std:
13171314
}
13181315

13191316
Q_ASSERT(int(by_which_symbols.size()) == getNumSymbols());
1320-
out.assign(std::size_t(getNumColors()), false);
1321-
for (std::size_t c = 0, last = std::size_t(getNumColors()); c != last; ++c)
1317+
out.assign(getNumColors(), false);
1318+
for (int c = 0, last = getNumColors(); c != last; ++c)
13221319
{
1323-
for (std::size_t s = 0, last_s = std::size_t(getNumSymbols()); s != last_s; ++s)
1320+
for (int s = 0, last_s = getNumSymbols(); s != last_s; ++s)
13241321
{
1325-
if (by_which_symbols[s] && getSymbol(int(s))->containsColor(getColor(int(c))))
1322+
if (by_which_symbols[s] && getSymbol(s)->containsColor(getColor(c)))
13261323
{
13271324
out[c] = true;
13281325
break;
@@ -1331,21 +1328,21 @@ void Map::determineColorsInUse(const std::vector< bool >& by_which_symbols, std:
13311328
}
13321329

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

1339-
const auto* color = getColor(int(c));
1336+
const auto* color = getColor(c);
13401337
if (color->getSpotColorMethod() != MapColor::SpotColor)
13411338
continue;
13421339

1343-
for (std::size_t o = 0, last_o = std::size_t(getNumColors()); o != last_o; ++o)
1340+
for (int o = 0, last_o = getNumColors(); o != last_o; ++o)
13441341
{
13451342
if (!out[o])
13461343
continue;
13471344

1348-
const auto* other = getColor(int(o));
1345+
const auto* other = getColor(o);
13491346
if (other->getSpotColorMethod() != MapColor::CustomColor)
13501347
continue;
13511348

@@ -1373,12 +1370,9 @@ void Map::checkSpotColorPresence()
13731370

13741371
bool Map::hasSpotColors() const
13751372
{
1376-
for (const MapColor* color : color_set->colors)
1377-
{
1378-
if (color->getSpotColorMethod() == MapColor::SpotColor)
1379-
return true;
1380-
}
1381-
return false;
1373+
return std::any_of(begin(color_set->colors), end(color_set->colors), [](const MapColor* color) {
1374+
return color->getSpotColorMethod() == MapColor::SpotColor;
1375+
});
13821376
}
13831377

13841378
bool Map::hasAlpha() const
@@ -1579,20 +1573,13 @@ void Map::setSymbol(Symbol* symbol, int pos)
15791573
Symbol* old_symbol = symbols[pos];
15801574

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

15921580
changeSymbolForAllObjects(old_symbol, symbol);
15931581

1594-
int size = (int)symbols.size();
1595-
for (int i = 0; i < size; ++i)
1582+
for (int i = 0, size = getNumSymbols(); i < size; ++i)
15961583
{
15971584
if (i == pos)
15981585
continue;
@@ -1616,8 +1603,7 @@ void Map::deleteSymbol(int pos)
16161603
if (deleteAllObjectsWithSymbol(symbols[pos]))
16171604
undo_manager->clear();
16181605

1619-
int size = (int)symbols.size();
1620-
for (int i = 0; i < size; ++i)
1606+
for (int i = 0, size = getNumSymbols(); i < size; ++i)
16211607
{
16221608
if (i == pos)
16231609
continue;
@@ -1638,8 +1624,7 @@ int Map::findSymbolIndex(const Symbol* symbol) const
16381624
{
16391625
if (!symbol)
16401626
return -1;
1641-
int size = (int)symbols.size();
1642-
for (int i = 0; i < size; ++i)
1627+
for (int i = 0, size = getNumSymbols(); i < size; ++i)
16431628
{
16441629
if (symbols[i] == symbol)
16451630
return i;
@@ -1669,7 +1654,7 @@ void Map::setSymbolsDirty()
16691654

16701655
void Map::updateSymbolIcons(const MapColor* color)
16711656
{
1672-
for (std::size_t i = 0, size = symbols.size(); i < size; ++i)
1657+
for (int i = 0, size = getNumSymbols(); i < size; ++i)
16731658
{
16741659
if (symbols[i]->containsColor(color))
16751660
{
@@ -1681,8 +1666,7 @@ void Map::updateSymbolIcons(const MapColor* color)
16811666

16821667
void Map::scaleAllSymbols(double factor)
16831668
{
1684-
int size = getNumSymbols();
1685-
for (int i = 0; i < size; ++i)
1669+
for (int i = 0, size = getNumSymbols(); i < size; ++i)
16861670
{
16871671
Symbol* symbol = getSymbol(i);
16881672
symbol->scale(factor);
@@ -2089,8 +2073,7 @@ void Map::removePart(std::size_t index)
20892073

20902074
int Map::findPartIndex(const MapPart* part) const
20912075
{
2092-
std::size_t const size = parts.size();
2093-
for (std::size_t i = 0; i < size; ++i)
2076+
for (int i = 0, size = getNumSymbols(); i < size; ++i)
20942077
{
20952078
if (parts[i] == part)
20962079
return i;
@@ -2179,10 +2162,9 @@ int Map::mergeParts(std::size_t source, std::size_t destination)
21792162

21802163
int Map::getNumObjects() const
21812164
{
2182-
int num_objects = 0;
2183-
for (const MapPart* part : parts)
2184-
num_objects += part->getNumObjects();
2185-
return num_objects;
2165+
return std::accumulate(begin(parts), end(parts), 0, [](int num_objects, const auto part) {
2166+
return num_objects + part->getNumObjects();
2167+
});
21862168
}
21872169

21882170
int Map::addObject(Object* object, int part_index)
@@ -2286,10 +2268,9 @@ void Map::findObjectsAtBox(
22862268

22872269
int Map::countObjectsInRect(const QRectF& map_coord_rect, bool include_hidden_objects)
22882270
{
2289-
int count = 0;
2290-
for (const MapPart* part : parts)
2291-
count += part->countObjectsInRect(map_coord_rect, include_hidden_objects);
2292-
return count;
2271+
return std::accumulate(begin(parts), end(parts), 0, [&map_coord_rect, include_hidden_objects](int count, const auto part) {
2272+
return count + part->countObjectsInRect(map_coord_rect, include_hidden_objects);
2273+
});
22932274
}
22942275

22952276

0 commit comments

Comments
 (0)