From 0482a1ec5bbd8c6b09300da2a4690e2acb68d3de Mon Sep 17 00:00:00 2001 From: Project Nayuki Date: Mon, 8 May 2017 07:30:53 +0000 Subject: [PATCH] Simplified a small bit of variables and logic of QrCode.getPenaltyScore() in all language versions, but in a subtly different way per language. --- c/qrcodegen.c | 12 ++++++------ cpp/QrCode.cpp | 12 ++++++------ java/io/nayuki/qrcodegen/QrCode.java | 12 ++++++------ javascript/qrcodegen.js | 10 ++++------ python/qrcodegen.py | 12 ++++-------- 5 files changed, 26 insertions(+), 32 deletions(-) diff --git a/c/qrcodegen.c b/c/qrcodegen.c index 45373e8..60d806c 100644 --- a/c/qrcodegen.c +++ b/c/qrcodegen.c @@ -716,9 +716,9 @@ static long getPenaltyScore(const uint8_t qrcode[]) { // Adjacent modules in row having same color for (int y = 0; y < qrsize; y++) { - bool colorX = getModule(qrcode, 0, y); - for (int x = 1, runX = 1; x < qrsize; x++) { - if (getModule(qrcode, x, y) != colorX) { + bool colorX; + for (int x = 0, runX; x < qrsize; x++) { + if (x == 0 || getModule(qrcode, x, y) != colorX) { colorX = getModule(qrcode, x, y); runX = 1; } else { @@ -732,9 +732,9 @@ static long getPenaltyScore(const uint8_t qrcode[]) { } // Adjacent modules in column having same color for (int x = 0; x < qrsize; x++) { - bool colorY = getModule(qrcode, x, 0); - for (int y = 1, runY = 1; y < qrsize; y++) { - if (getModule(qrcode, x, y) != colorY) { + bool colorY; + for (int y = 0, runY; y < qrsize; y++) { + if (y == 0 || getModule(qrcode, x, y) != colorY) { colorY = getModule(qrcode, x, y); runY = 1; } else { diff --git a/cpp/QrCode.cpp b/cpp/QrCode.cpp index 579c30f..ee668fe 100644 --- a/cpp/QrCode.cpp +++ b/cpp/QrCode.cpp @@ -422,9 +422,9 @@ long QrCode::getPenaltyScore() const { // Adjacent modules in row having same color for (int y = 0; y < size; y++) { - bool colorX = modules.at(y).at(0); - for (int x = 1, runX = 1; x < size; x++) { - if (modules.at(y).at(x) != colorX) { + bool colorX; + for (int x = 0, runX; x < size; x++) { + if (x == 0 || modules.at(y).at(x) != colorX) { colorX = modules.at(y).at(x); runX = 1; } else { @@ -438,9 +438,9 @@ long QrCode::getPenaltyScore() const { } // Adjacent modules in column having same color for (int x = 0; x < size; x++) { - bool colorY = modules.at(0).at(x); - for (int y = 1, runY = 1; y < size; y++) { - if (modules.at(y).at(x) != colorY) { + bool colorY; + for (int y = 0, runY; y < size; y++) { + if (y == 0 || modules.at(y).at(x) != colorY) { colorY = modules.at(y).at(x); runY = 1; } else { diff --git a/java/io/nayuki/qrcodegen/QrCode.java b/java/io/nayuki/qrcodegen/QrCode.java index 0b8646c..4af7429 100644 --- a/java/io/nayuki/qrcodegen/QrCode.java +++ b/java/io/nayuki/qrcodegen/QrCode.java @@ -579,9 +579,9 @@ public final class QrCode { // Adjacent modules in row having same color for (int y = 0; y < size; y++) { - boolean colorX = modules[y][0]; - for (int x = 1, runX = 1; x < size; x++) { - if (modules[y][x] != colorX) { + boolean colorX = false; + for (int x = 0, runX = 0; x < size; x++) { + if (x == 0 || modules[y][x] != colorX) { colorX = modules[y][x]; runX = 1; } else { @@ -595,9 +595,9 @@ public final class QrCode { } // Adjacent modules in column having same color for (int x = 0; x < size; x++) { - boolean colorY = modules[0][x]; - for (int y = 1, runY = 1; y < size; y++) { - if (modules[y][x] != colorY) { + boolean colorY = false; + for (int y = 0, runY = 0; y < size; y++) { + if (y == 0 || modules[y][x] != colorY) { colorY = modules[y][x]; runY = 1; } else { diff --git a/javascript/qrcodegen.js b/javascript/qrcodegen.js index 13fa456..08c817c 100644 --- a/javascript/qrcodegen.js +++ b/javascript/qrcodegen.js @@ -455,9 +455,8 @@ var qrcodegen = new function() { // Adjacent modules in row having same color for (var y = 0; y < size; y++) { - var colorX = modules[y][0]; - for (var x = 1, runX = 1; x < size; x++) { - if (modules[y][x] != colorX) { + for (var x = 0, runX, colorX; x < size; x++) { + if (x == 0 || modules[y][x] != colorX) { colorX = modules[y][x]; runX = 1; } else { @@ -471,9 +470,8 @@ var qrcodegen = new function() { } // Adjacent modules in column having same color for (var x = 0; x < size; x++) { - var colorY = modules[0][x]; - for (var y = 1, runY = 1; y < size; y++) { - if (modules[y][x] != colorY) { + for (var y = 0, runY, colorY; y < size; y++) { + if (y == 0 || modules[y][x] != colorY) { colorY = modules[y][x]; runY = 1; } else { diff --git a/python/qrcodegen.py b/python/qrcodegen.py index d6c197a..cb37db2 100644 --- a/python/qrcodegen.py +++ b/python/qrcodegen.py @@ -436,10 +436,8 @@ class QrCode(object): # Adjacent modules in row having same color for y in range(size): - colorx = modules[y][0] - runx = 1 - for x in range(1, size): - if modules[y][x] != colorx: + for x in range(size): + if x == 0 or modules[y][x] != colorx: colorx = modules[y][x] runx = 1 else: @@ -450,10 +448,8 @@ class QrCode(object): result += 1 # Adjacent modules in column having same color for x in range(size): - colory = modules[0][x] - runy = 1 - for y in range(1, size): - if modules[y][x] != colory: + for y in range(size): + if y == 0 or modules[y][x] != colory: colory = modules[y][x] runy = 1 else: