fix: bucles cap a ranges algorithms (38 troballes)

This commit is contained in:
2026-05-14 21:36:21 +02:00
parent 0aa9f8fe0a
commit b4d3776239
14 changed files with 110 additions and 169 deletions

View File

@@ -1425,11 +1425,11 @@ auto MapEditor::deleteRoom() -> std::string { // NOLINT(readability-function-co
if (target == "0") {
// Buscar la primera room que no sea esta
for (const auto& r : Resource::Cache::get()->getRooms()) {
if (r.name != deleted_name) {
target = r.name;
break;
}
const auto& rooms = Resource::Cache::get()->getRooms();
auto it = std::ranges::find_if(rooms,
[&deleted_name](const auto& r) { return r.name != deleted_name; });
if (it != rooms.end()) {
target = it->name;
}
}
if (target == "0") { return "Cannot delete: no other room to navigate to"; }

View File

@@ -108,32 +108,23 @@ auto CollisionMap::getSlopeHeight(SDL_FPoint p, Tile slope) -> int {
// Comprueba las colisiones con paredes derechas
auto CollisionMap::checkRightSurfaces(const SDL_FRect& rect) -> int { // NOLINT(readability-convert-member-functions-to-static)
for (const auto& s : right_walls_) {
if (checkCollision(s, rect)) {
return s.x;
}
}
return Collision::NONE;
auto it = std::ranges::find_if(right_walls_,
[&rect](const auto& s) { return checkCollision(s, rect); });
return (it != right_walls_.end()) ? it->x : Collision::NONE;
}
// Comprueba las colisiones con paredes izquierdas
auto CollisionMap::checkLeftSurfaces(const SDL_FRect& rect) -> int { // NOLINT(readability-convert-member-functions-to-static)
for (const auto& s : left_walls_) {
if (checkCollision(s, rect)) {
return s.x;
}
}
return Collision::NONE;
auto it = std::ranges::find_if(left_walls_,
[&rect](const auto& s) { return checkCollision(s, rect); });
return (it != left_walls_.end()) ? it->x : Collision::NONE;
}
// Comprueba las colisiones con techos
auto CollisionMap::checkTopSurfaces(const SDL_FRect& rect) -> int { // NOLINT(readability-convert-member-functions-to-static)
for (const auto& s : top_floors_) {
if (checkCollision(s, rect)) {
return s.y;
}
}
return Collision::NONE;
auto it = std::ranges::find_if(top_floors_,
[&rect](const auto& s) { return checkCollision(s, rect); });
return (it != top_floors_.end()) ? it->y : Collision::NONE;
}
// Comprueba las colisiones punto con techos
@@ -145,22 +136,16 @@ auto CollisionMap::checkTopSurfaces(const SDL_FPoint& p) -> bool {
// Comprueba las colisiones con suelos
auto CollisionMap::checkBottomSurfaces(const SDL_FRect& rect) -> int { // NOLINT(readability-convert-member-functions-to-static)
for (const auto& s : bottom_floors_) {
if (checkCollision(s, rect)) {
return s.y;
}
}
return Collision::NONE;
auto it = std::ranges::find_if(bottom_floors_,
[&rect](const auto& s) { return checkCollision(s, rect); });
return (it != bottom_floors_.end()) ? it->y : Collision::NONE;
}
// Comprueba las colisiones con conveyor belts
auto CollisionMap::checkAutoSurfaces(const SDL_FRect& rect) -> int { // NOLINT(readability-convert-member-functions-to-static)
for (const auto& s : conveyor_belt_floors_) {
if (checkCollision(s, rect)) {
return s.y;
}
}
return Collision::NONE;
auto it = std::ranges::find_if(conveyor_belt_floors_,
[&rect](const auto& s) { return checkCollision(s, rect); });
return (it != conveyor_belt_floors_.end()) ? it->y : Collision::NONE;
}
// Comprueba las colisiones punto con conveyor belts
@@ -208,18 +193,16 @@ auto CollisionMap::checkRightSlopes(const SDL_FPoint& p) -> bool {
// Obtiene puntero a slope en un punto (prioriza left_slopes_ sobre right_slopes_)
auto CollisionMap::getSlopeAtPoint(const SDL_FPoint& p) const -> const LineDiagonal* { // NOLINT(readability-convert-member-functions-to-static)
// Primero busca en rampas izquierdas
for (const auto& slope : left_slopes_) {
if (checkCollision(p, slope)) {
return &slope;
}
auto pred = [&p](const auto& slope) { return static_cast<bool>(checkCollision(p, slope)); };
auto left_it = std::ranges::find_if(left_slopes_, pred);
if (left_it != left_slopes_.end()) {
return &(*left_it);
}
// Luego busca en rampas derechas
for (const auto& slope : right_slopes_) {
if (checkCollision(p, slope)) {
return &slope;
}
auto right_it = std::ranges::find_if(right_slopes_, pred);
if (right_it != right_slopes_.end()) {
return &(*right_it);
}
// No hay colisión con ninguna slope

View File

@@ -44,9 +44,7 @@ auto RoomLoader::flattenTilemap(const std::vector<std::vector<int>>& tilemap_2d)
tilemap_flat.reserve(512); // 16 rows × 32 cols
for (const auto& row : tilemap_2d) {
for (int tile : row) {
tilemap_flat.push_back(tile);
}
std::ranges::copy(row, std::back_inserter(tilemap_flat));
}
return tilemap_flat;
@@ -135,12 +133,9 @@ void RoomLoader::parseTilemap(const fkyaml::node& yaml, Room::Data& room, const
for (const auto& row_node : tilemap_node) {
std::vector<int> row;
row.reserve(32);
for (const auto& tile_node : row_node) {
row.push_back(tile_node.get_value<int>());
}
tilemap_2d.push_back(row);
std::ranges::transform(row_node, std::back_inserter(row),
[](const auto& tile_node) { return tile_node.template get_value<int>(); });
tilemap_2d.push_back(std::move(row));
}
// Convert to 1D flat array

View File

@@ -36,9 +36,9 @@ Scoreboard::Scoreboard(std::shared_ptr<Data> data)
// Inicializa el vector de colores
const std::vector<std::string> COLORS = {"blue", "magenta", "green", "cyan", "yellow", "white", "bright_blue", "bright_magenta", "bright_green", "bright_cyan", "bright_yellow", "bright_white"};
for (const auto& color : COLORS) {
color_.push_back(stringToColor(color));
}
color_.reserve(COLORS.size());
std::ranges::transform(COLORS, std::back_inserter(color_),
[](const auto& color) { return stringToColor(color); });
}
// Pinta el objeto en pantalla

View File

@@ -31,9 +31,9 @@ Ending2::Ending2()
// Inicializa el vector de colores
const std::vector<std::string> COLORS = {"white", "yellow", "cyan", "green", "magenta", "red", "blue", "black"};
for (const auto& color : COLORS) {
colors_.push_back(stringToColor(color));
}
colors_.reserve(COLORS.size());
std::ranges::transform(COLORS, std::back_inserter(colors_),
[](const auto& color) { return stringToColor(color); });
Screen::get()->setBorderColor(static_cast<Uint8>(PaletteColor::BLACK)); // Cambia el color del borde
iniSpriteList(); // Inicializa la lista de sprites

View File

@@ -3,6 +3,7 @@
#include <SDL3/SDL.h>
#include <cmath> // Para std::sqrt, std::min
#include <numeric> // Para std::accumulate
#include <utility>
#include <vector> // Para vector
@@ -865,14 +866,9 @@ auto Game::checkEndGame() -> bool {
// Obtiene la cantidad total de items que hay en el mapeado del juego
auto Game::getTotalItems() -> int {
int items = 0;
auto rooms = Resource::Cache::get()->getRooms();
for (const auto& room : rooms) {
items += room.room->items.size();
}
return items;
const auto& rooms = Resource::Cache::get()->getRooms();
return static_cast<int>(std::accumulate(rooms.begin(), rooms.end(), size_t{0},
[](size_t acc, const auto& room) { return acc + room.room->items.size(); }));
}
// Pone el juego en pausa

View File

@@ -38,9 +38,9 @@ GameOver::GameOver()
// Inicializa el vector de colores (de brillante a oscuro para fade)
const std::vector<std::string> COLORS = {"white", "yellow", "cyan", "green", "magenta", "red", "blue", "black"};
for (const auto& color : COLORS) {
colors_.push_back(stringToColor(color));
}
colors_.reserve(COLORS.size());
std::ranges::transform(COLORS, std::back_inserter(colors_),
[](const auto& color) { return stringToColor(color); });
color_ = colors_.back(); // Empieza en black
}

View File

@@ -259,9 +259,8 @@ void Logo::initColors() { // NOLINT(readability-convert-member-functions-to-sta
static_cast<Uint8>(PaletteColor::CYAN),
static_cast<Uint8>(PaletteColor::YELLOW),
static_cast<Uint8>(PaletteColor::BRIGHT_WHITE)};
for (const auto& color : COLORS) {
color_.push_back(color);
}
color_.reserve(COLORS.size());
std::ranges::copy(COLORS, std::back_inserter(color_));
}
// Crea los sprites de cada linea

View File

@@ -3,6 +3,7 @@
#include <SDL3/SDL.h>
#include <algorithm> // Para ranges::transform
#include <numeric> // Para std::accumulate
#include <cctype> // Para toupper
#include <sstream> // Para std::istringstream
#include <string> // Para string
@@ -181,8 +182,8 @@ void Console::update(float delta_time) { // NOLINT(readability-function-cogniti
// Efecto typewriter: revelar letras una a una (solo cuando ACTIVE)
if (status_ == Status::ACTIVE) {
int total_chars = 0;
for (const auto& line : msg_lines_) { total_chars += static_cast<int>(line.size()); }
const int total_chars = std::accumulate(msg_lines_.begin(), msg_lines_.end(), 0,
[](int acc, const auto& line) { return acc + static_cast<int>(line.size()); });
if (typewriter_chars_ < total_chars) {
typewriter_timer_ += delta_time;
while (typewriter_timer_ >= TYPEWRITER_CHAR_DELAY && typewriter_chars_ < total_chars) {
@@ -337,11 +338,9 @@ void Console::handleEvent(const SDL_Event& event) { // NOLINT(readability-funct
const size_t SPACE_POS = upper.rfind(' ');
if (SPACE_POS == std::string::npos) {
// Modo comando: ciclar keywords visibles que empiecen por el prefijo
for (const auto& kw : registry_.getVisibleKeywords()) {
if (upper.empty() || kw.starts_with(upper)) {
tab_matches_.emplace_back(kw);
}
}
const auto KEYWORDS = registry_.getVisibleKeywords();
std::ranges::copy_if(KEYWORDS, std::back_inserter(tab_matches_),
[&upper](const auto& kw) { return upper.empty() || kw.starts_with(upper); });
} else {
const std::string BASE_CMD = upper.substr(0, SPACE_POS);
const std::string SUB_PREFIX = upper.substr(SPACE_POS + 1);
@@ -357,7 +356,8 @@ void Console::handleEvent(const SDL_Event& event) { // NOLINT(readability-funct
if (tab_matches_.empty()) { break; }
tab_index_ = (tab_index_ + 1) % static_cast<int>(tab_matches_.size());
std::string result = tab_matches_[static_cast<size_t>(tab_index_)];
for (char& c : result) { c = static_cast<char>(std::tolower(static_cast<unsigned char>(c))); }
std::ranges::transform(result, result.begin(),
[](char c) { return static_cast<char>(std::tolower(static_cast<unsigned char>(c))); });
input_line_ = result;
break;
}
@@ -403,9 +403,8 @@ void Console::processCommand() {
// Typewriter: instantáneo si el comando lo requiere, letra a letra si no
if (instant) {
int total = 0;
for (const auto& l : msg_lines_) { total += static_cast<int>(l.size()); }
typewriter_chars_ = total;
typewriter_chars_ = std::accumulate(msg_lines_.begin(), msg_lines_.end(), 0,
[](int acc, const auto& l) { return acc + static_cast<int>(l.size()); });
} else {
typewriter_chars_ = 0;
}

View File

@@ -1012,9 +1012,9 @@ void CommandRegistry::registerHandlers() { // NOLINT(readability-function-cogni
dynamic_providers_["PALETTE"] = []() -> std::vector<std::string> {
std::vector<std::string> result = {"NEXT", "PREV", "SORT", "DEFAULT"};
if (Screen::get() != nullptr) {
for (const auto& name : Screen::get()->getPaletteNames()) {
result.push_back(toUpper(name));
}
const auto NAMES = Screen::get()->getPaletteNames();
std::ranges::transform(NAMES, std::back_inserter(result),
[](const auto& name) { return toUpper(name); });
}
return result;
};
@@ -1023,10 +1023,11 @@ void CommandRegistry::registerHandlers() { // NOLINT(readability-function-cogni
dynamic_providers_["SHADER PRESET"] = []() -> std::vector<std::string> {
std::vector<std::string> result = {"NEXT", "PREV"};
const bool IS_CRTPI = Options::video.shader.current_shader == Rendering::ShaderType::CRTPI;
auto upper_name = [](const auto& p) { return toUpper(p.name); };
if (IS_CRTPI) {
for (const auto& p : Options::crtpi_presets) { result.push_back(toUpper(p.name)); }
std::ranges::transform(Options::crtpi_presets, std::back_inserter(result), upper_name);
} else {
for (const auto& p : Options::postfx_presets) { result.push_back(toUpper(p.name)); }
std::ranges::transform(Options::postfx_presets, std::back_inserter(result), upper_name);
}
return result;
};
@@ -1119,7 +1120,8 @@ void CommandRegistry::load(const std::string& yaml_path) { // NOLINT(readabilit
if (cat_node.contains("scope")) {
const auto& scope_node = cat_node["scope"];
if (scope_node.is_sequence()) {
for (const auto& s : scope_node) { cat_scopes.push_back(s.get_value<std::string>()); }
std::ranges::transform(scope_node, std::back_inserter(cat_scopes),
[](const auto& s) { return s.template get_value<std::string>(); });
} else {
cat_scopes.push_back(scope_node.get_value<std::string>());
}
@@ -1160,9 +1162,8 @@ void CommandRegistry::load(const std::string& yaml_path) { // NOLINT(readabilit
for (auto it = completions_node.begin(); it != completions_node.end(); ++it) {
auto path = it.key().get_value<std::string>();
std::vector<std::string> opts;
for (const auto& opt : *it) {
opts.push_back(opt.get_value<std::string>());
}
std::ranges::transform(*it, std::back_inserter(opts),
[](const auto& opt) { return opt.template get_value<std::string>(); });
def.completions[path] = std::move(opts);
}
}
@@ -1181,9 +1182,8 @@ void CommandRegistry::load(const std::string& yaml_path) { // NOLINT(readabilit
for (auto it = extras_completions.begin(); it != extras_completions.end(); ++it) {
auto path = it.key().get_value<std::string>();
std::vector<std::string> opts;
for (const auto& opt : *it) {
opts.push_back(opt.get_value<std::string>());
}
std::ranges::transform(*it, std::back_inserter(opts),
[](const auto& opt) { return opt.template get_value<std::string>(); });
def.completions[path] = std::move(opts);
}
}
@@ -1236,10 +1236,9 @@ void CommandRegistry::load(const std::string& yaml_path) { // NOLINT(readabilit
}
auto CommandRegistry::findCommand(const std::string& keyword) const -> const CommandDef* {
for (const auto& cmd : commands_) {
if (cmd.keyword == keyword) { return &cmd; }
}
return nullptr;
auto it = std::ranges::find_if(commands_,
[&keyword](const auto& cmd) { return cmd.keyword == keyword; });
return (it != commands_.end()) ? &(*it) : nullptr;
}
auto CommandRegistry::execute(const std::string& keyword, const std::vector<std::string>& args) const -> std::string {

View File

@@ -306,8 +306,7 @@ auto Notifier::getVisibleHeight() const -> int {
auto Notifier::getCodes() -> std::vector<std::string> {
std::vector<std::string> codes;
codes.reserve(notifications_.size());
for (const auto& notification : notifications_) {
codes.emplace_back(notification.code);
}
std::ranges::transform(notifications_, std::back_inserter(codes),
[](const auto& notification) { return notification.code; });
return codes;
}