Commit 27f9ed3a authored by Andrius Štikonas's avatar Andrius Štikonas

Port Q_FOREACH to ranged based for.

parent 96981373
project(ksudoku)
cmake_minimum_required (VERSION 2.8.12 FATAL_ERROR)
set (QT_MIN_VERSION "5.3.0")
set (QT_MIN_VERSION "5.7.0")
set (KF5_MIN_VERSION "5.31.0")
find_package(ECM ${KF5_MIN_VERSION} REQUIRED CONFIG)
......
......@@ -126,7 +126,7 @@ int CageGenerator::makeCages (SKGraph * graph, QList<int> * solutionMoves,
int cageValue;
int chosenSize;
int index = -1;
Q_FOREACH (int n, mUnusedCells) {
for (const int n : qAsConst(mUnusedCells)) {
switch (mNeighbourFlags.at (n)){
case 7:
case 11:
......@@ -200,7 +200,7 @@ int CageGenerator::makeCages (SKGraph * graph, QList<int> * solutionMoves,
#ifdef MATHDOKU_LOG
qCDebug(KSudokuLog) << "CAGE" << mGraph->cageCount() << cage;
char tag = 'a' + mGraph->cageCount() - 1;
Q_FOREACH (int cell, cage) {
for (const int cell : qAsConst(cage)) {
usedCells[cell] = tag;
}
qCDebug(KSudokuLog) << "LAYOUT" << tag << usedCells;
......@@ -213,7 +213,7 @@ int CageGenerator::makeCages (SKGraph * graph, QList<int> * solutionMoves,
fprintf (stderr, "\n");
#endif
QList<int> flagsList;
Q_FOREACH (int cell, mUnusedCells) {
for (const int cell : qAsConst(mUnusedCells)) {
flagsList.append (mNeighbourFlags.at (cell));
}
#ifdef MATHDOKU_LOG
......@@ -371,7 +371,7 @@ QVector<int> CageGenerator::makeOneCage (int seedCell, int requiredSize)
// Pick a neighbour to be added to the cage.
index = -1;
Q_FOREACH (unb, unusedNeighbours) {
for (unb : qAsConst(unusedNeighbours)) {
flags = mNeighbourFlags.at (unb);
if (flags == 15) {
// Choose a cell that has been surrounded and isolated.
......@@ -701,8 +701,8 @@ bool CageGenerator::isSelfConsistent (const QVector<int> cage,
for (int n = 0; n < nDigits; n++) {
cell = cage.at (n);
mask = 1 << digits[n];
QList<int> groupList = mGraph->cliqueList (cell);
Q_FOREACH (int group, groupList) {
const QList<int> groupList = mGraph->cliqueList (cell);
for (const int group : groupList) {
if (mask & usedGroups.at (group)) {
return false;
}
......
......@@ -148,7 +148,8 @@ void DLXSolver::recordSolution (const int solutionNum, QList<DLXNode *> & soluti
<< "cageSize" << cageSize << "combo" << comboNum
<< "values at" << comboValues;
#endif
Q_FOREACH (int cell, mGraph->cage (nCage)) {
const QVector<int> cage = mGraph->cage (nCage);
for (const int cell : cage) {
#ifdef DLX_LOG
fprintf (stderr, "%d:%d ", cell,
mPossibilities->at (comboValues));
......@@ -168,7 +169,7 @@ void DLXSolver::recordSolution (const int solutionNum, QList<DLXNode *> & soluti
}
}
else { // Sudoku or Roxdoku variant.
Q_FOREACH (DLXNode * node, solution) {
for (DLXNode * node : qAsConst(solution)) {
int rowNumDLX = node->value;
mBoardValues [rowNumDLX/order] = (rowNumDLX % order) + 1;
}
......@@ -289,7 +290,7 @@ int DLXSolver::solveSudoku (SKGraph * graph, const BoardContents & boardValues,
continue;
}
// Get a list of groups (row, column, etc.) that contain this cell.
QList<int> groups = graph->cliqueList (index);
const QList<int> groups = graph->cliqueList (index);
#ifdef DLX_LOG
int row = graph->cellPosY (index);
int col = graph->cellPosX (index);
......@@ -298,12 +299,13 @@ int DLXSolver::solveSudoku (SKGraph * graph, const BoardContents & boardValues,
<< "row" << row << "col" << col << "groups" << groups;
#endif
int val = boardValues.at(index) - 1;
Q_FOREACH (int group, groups) {
for (const int group : groups) {
#ifdef DLX_LOG
qCDebug(KSudokuLog) << "EXCLUDE CONSTRAINT" << (boardArea+group*order+val);
#endif
mColumns[boardArea + group*order + val] = 0;
Q_FOREACH (int cell, graph->clique (group)) {
const QVector<int> clique = graph->clique (group);
for (const int cell : clique) {
mRows[cell*order + val] = 0; // Drop row.
}
}
......@@ -311,7 +313,7 @@ int DLXSolver::solveSudoku (SKGraph * graph, const BoardContents & boardValues,
}
// Create the initial set of columns.
Q_FOREACH (DLXNode * colDLX, mColumns) {
for (DLXNode * colDLX : qAsConst(mColumns)) {
mEndColNum++;
// If the constraint is not excluded, put an empty column in the matrix.
if (colDLX != 0) {
......@@ -326,7 +328,7 @@ int DLXSolver::solveSudoku (SKGraph * graph, const BoardContents & boardValues,
int rowNumDLX = 0;
for (int index = 0; index < boardArea; index++) {
// Get a list of groups (row, column, etc.) that contain this cell.
QList<int> groups = graph->cliqueList (index);
const QList<int> groups = graph->cliqueList (index);
#ifdef DLX_LOG
int row = graph->cellPosY (index);
int col = graph->cellPosX (index);
......@@ -347,7 +349,7 @@ int DLXSolver::solveSudoku (SKGraph * graph, const BoardContents & boardValues,
if (mRows.at (rowNumDLX) != 0) { // Skip already-excluded rows.
mRows[rowNumDLX] = 0; // Re-initialise a "live" row.
addNode (rowNumDLX, index); // Mark cell fill-in constraint.
Q_FOREACH (int group, groups) {
for (const int group : groups) {
// Mark possibly-satisfied constraints for row, column, etc.
addNode (rowNumDLX, boardArea + group*order + possValue);
}
......@@ -425,10 +427,12 @@ int DLXSolver::solveMathdoku (SKGraph * graph, QList<int> * solutionMoves,
qCDebug(KSudokuLog) << "Add cage-node: row" << rowNumDLX << "cage" << n
<< graph->cage (n);
#endif
Q_FOREACH (int cell, graph->cage (n)) {
int possVal = possibilities->at (index);
const QVector<int> cage = graph->cage (n);
for (const int cell : cage ) {
int possVal = possibilities->at (index);
// qCDebug(KSudokuLog) << " Cell" << cell << "possVal" << possVal;
Q_FOREACH (int group, graph->cliqueList (cell)) {
const QList<int> cliqueList = graph->cliqueList (cell);
for (const int group : cliqueList) {
// Poss values go from 0 to (order - 1) in DLX (so -1 here).
addNode (rowNumDLX, nCages + group * order + possVal - 1);
counter++;
......@@ -557,7 +561,7 @@ int DLXSolver::solveDLX (int solutionLimit)
solution.append (currNode);
#ifdef DLX_LOG
fprintf (stderr, "CURRENT SOLUTION: %d rows:", solution.size());
Q_FOREACH (DLXNode * q, solution) {
for (DLXNode * q : qAsConst(solution)) {
fprintf (stderr, " %d", q->value);
}
fprintf (stderr, "\n");
......
......@@ -1026,8 +1026,8 @@ void SudokuBoard::updateValueRequirements (BoardContents & boardValues, int cell
m_validCellValues [cell] = 0;
// Update the requirements for each group to which this cell belongs.
QList<int> groupList = m_graph->cliqueList(cell);
foreach (int group, groupList) {
const QList<int> groupList = m_graph->cliqueList(cell);
for (int group : groupList) {
m_requiredGroupValues [group] &= bitPattern;
QVector<int> cellList = m_graph->clique (group);
......
......@@ -185,12 +185,12 @@ void KSudoku::updateShapesList()
variant->setIcon("ksudoku-roxdoku_3x3x3");
#endif
QStringList gamevariantdirs = QStandardPaths::locateAll(QStandardPaths::GenericDataLocation, "ksudoku", QStandardPaths::LocateDirectory);
const QStringList gamevariantdirs = QStandardPaths::locateAll(QStandardPaths::GenericDataLocation, "ksudoku", QStandardPaths::LocateDirectory);
QStringList filepaths;
Q_FOREACH (const QString& gamevariantdir, gamevariantdirs) {
for (const QString& gamevariantdir : gamevariantdirs) {
const QStringList fileNames = QDir(gamevariantdir).entryList(QStringList() << QStringLiteral("*.desktop"));
Q_FOREACH (const QString &file, fileNames) {
for (const QString &file : fileNames) {
if (!filepaths.contains(gamevariantdir + '/' + file)) {
filepaths.append(gamevariantdir + '/' + file);
}
......@@ -202,7 +202,7 @@ void KSudoku::updateShapesList()
QString variantDataPath;
QString variantIcon;
foreach(const QString &filepath, filepaths) {
for (const QString &filepath : qAsConst(filepaths)) {
const QFileInfo configFileInfo(filepath);
const QDir variantDir = configFileInfo.dir();
KConfig variantConfig(filepath, KConfig::SimpleConfig);
......
......@@ -297,10 +297,10 @@ bool Serializer::deserializeClique(SKGraph * graph, const QString & size,
return false;
}
QStringList splitData = text.split(QString(" "), QString::SkipEmptyParts);
const QStringList splitData = text.split(QString(" "), QString::SkipEmptyParts);
QVector<int> data;
data.clear();
foreach (QString s, splitData) {
for (QString s : splitData) {
--cellCount;
data << s.toInt();
if(cellCount <= 0) {
......@@ -325,9 +325,9 @@ bool Serializer::deserializeCage(SKGraph * graph, const QDomElement & e) {
return false;
}
QStringList cells = text.split(QString(" "), QString::SkipEmptyParts);
const QStringList cells = text.split(QString(" "), QString::SkipEmptyParts);
cage.clear();
Q_FOREACH (QString s, cells) {
for (const QString& s : cells) {
cage << s.toInt();
size--;
if (size <= 0) {
......@@ -584,14 +584,14 @@ bool Serializer::serializeGraph(QDomElement &parent, const SKGraph *graph)
// Add cages if this is a Mathdoku or Killer Sudoku puzzle.
for (int n = 0; n < graph->cageCount(); n++) {
QDomElement e = parent.ownerDocument().createElement("cage");
QVector<int> cage = graph->cage(n);
const QVector<int> cage = graph->cage(n);
e.setAttribute("operator", graph->cageOperator(n));
e.setAttribute("value", graph->cageValue(n));
e.setAttribute("size", cage.size());
// Serialize the cell-numbers in the cage.
QString contentStr = " ";
Q_FOREACH (int cell, cage) {
for (const int cell : cage) {
contentStr += QString::number(cell) + ' ';
}
e.appendChild(parent.ownerDocument().
......
......@@ -22,8 +22,6 @@
#include "view2d.h"
#include <QDebug>
#include <QGraphicsPixmapItem>
#include <QGraphicsSceneEvent>
......@@ -544,8 +542,8 @@ void View2DScene::setSceneSize(const QSize& size) {
m_cells[i]->resize(grid);
}
for(int i = 0; i < m_groups.size(); ++i) {
m_groups[i]->resize(grid, m_highlightsOn);
for (ksudoku::GroupGraphicsItem* group : qAsConst(m_groups)) {
group->resize(grid, m_highlightsOn);
}
m_cursor->setPixmap(Renderer::instance()->renderSpecial(SpecialCursor, grid*2));
......@@ -555,7 +553,7 @@ void View2DScene::hover(int cell) {
m_cursorPos = cell;
// qCDebug(KSudokuLog) << "hover cell" << cell << m_cells[cell];
QPoint pos(m_cells[cell]->pos());
foreach(GroupGraphicsItem* item, m_groups) {
for (GroupGraphicsItem* item : qAsConst(m_groups)) {
item->setHighlight(pos, m_highlightsOn);
}
......
......@@ -161,7 +161,7 @@ void SKGraph::addCage(const QVector<int> cage, CageOperator cageOperator,
int topY = m_order;
int leftX = m_order;
newCage->cageTopLeft = 0;
Q_FOREACH (int cell, cage) {
for (const int cell : cage) {
if (cellPosY(cell) > topY) {
continue; // Below the best so far.
}
......@@ -227,8 +227,8 @@ void SKGraph::indexCellsToCliques()
int index = 0;
for (int cell = 0; cell < nCells; cell++) {
m_cellIndex [cell] = index;
QList<int> cliqueList = cellsToCliques.values (cell);
foreach (int g, cliqueList) {
const QList<int> cliqueList = cellsToCliques.values (cell);
for (int g : cliqueList) {
m_cellCliques [index] = g;
index++;
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment