From 5ac0e2a938f98e4e3d0155db558b0a48909a0dea Mon Sep 17 00:00:00 2001 From: Project Nayuki Date: Fri, 26 Oct 2018 05:24:21 +0000 Subject: [PATCH] Inverted some if-else statements in QrCode.getPenaltyScore() without changing behavior, in all languages. --- c/qrcodegen.c | 16 ++++++++-------- cpp/QrCode.cpp | 16 ++++++++-------- java/io/nayuki/qrcodegen/QrCode.java | 16 ++++++++-------- javascript/qrcodegen.js | 16 ++++++++-------- python/qrcodegen.py | 16 ++++++++-------- rust/src/lib.rs | 16 ++++++++-------- typescript/qrcodegen.ts | 16 ++++++++-------- 7 files changed, 56 insertions(+), 56 deletions(-) diff --git a/c/qrcodegen.c b/c/qrcodegen.c index 0aa39c9..dc3d99b 100644 --- a/c/qrcodegen.c +++ b/c/qrcodegen.c @@ -635,15 +635,15 @@ static long getPenaltyScore(const uint8_t qrcode[]) { bool color = false; int runX = 0; for (int x = 0; x < qrsize; x++) { - if (getModule(qrcode, x, y) != color) { - color = getModule(qrcode, x, y); - runX = 1; - } else { + if (getModule(qrcode, x, y) == color) { runX++; if (runX == 5) result += PENALTY_N1; else if (runX > 5) result++; + } else { + color = getModule(qrcode, x, y); + runX = 1; } } } @@ -652,15 +652,15 @@ static long getPenaltyScore(const uint8_t qrcode[]) { bool color = false; int runY = 0; for (int y = 0; y < qrsize; y++) { - if (getModule(qrcode, x, y) != color) { - color = getModule(qrcode, x, y); - runY = 1; - } else { + if (getModule(qrcode, x, y) == color) { runY++; if (runY == 5) result += PENALTY_N1; else if (runY > 5) result++; + } else { + color = getModule(qrcode, x, y); + runY = 1; } } } diff --git a/cpp/QrCode.cpp b/cpp/QrCode.cpp index 7e71887..d91343b 100644 --- a/cpp/QrCode.cpp +++ b/cpp/QrCode.cpp @@ -433,15 +433,15 @@ long QrCode::getPenaltyScore() const { bool color = false; int runX = 0; for (int x = 0; x < size; x++) { - if (module(x, y) != color) { - color = module(x, y); - runX = 1; - } else { + if (module(x, y) == color) { runX++; if (runX == 5) result += PENALTY_N1; else if (runX > 5) result++; + } else { + color = module(x, y); + runX = 1; } } } @@ -450,15 +450,15 @@ long QrCode::getPenaltyScore() const { bool color = false; int runY = 0; for (int y = 0; y < size; y++) { - if (module(x, y) != color) { - color = module(x, y); - runY = 1; - } else { + if (module(x, y) == color) { runY++; if (runY == 5) result += PENALTY_N1; else if (runY > 5) result++; + } else { + color = module(x, y); + runY = 1; } } } diff --git a/java/io/nayuki/qrcodegen/QrCode.java b/java/io/nayuki/qrcodegen/QrCode.java index 2e327b6..fc2c708 100644 --- a/java/io/nayuki/qrcodegen/QrCode.java +++ b/java/io/nayuki/qrcodegen/QrCode.java @@ -600,15 +600,15 @@ public final class QrCode { boolean color = false; int runX = 0; for (int x = 0; x < size; x++) { - if (modules[y][x] != color) { - color = modules[y][x]; - runX = 1; - } else { + if (modules[y][x] == color) { runX++; if (runX == 5) result += PENALTY_N1; else if (runX > 5) result++; + } else { + color = modules[y][x]; + runX = 1; } } } @@ -617,15 +617,15 @@ public final class QrCode { boolean color = false; int runY = 0; for (int y = 0; y < size; y++) { - if (modules[y][x] != color) { - color = modules[y][x]; - runY = 1; - } else { + if (modules[y][x] == color) { runY++; if (runY == 5) result += PENALTY_N1; else if (runY > 5) result++; + } else { + color = modules[y][x]; + runY = 1; } } } diff --git a/javascript/qrcodegen.js b/javascript/qrcodegen.js index 18ae79e..398c850 100644 --- a/javascript/qrcodegen.js +++ b/javascript/qrcodegen.js @@ -432,15 +432,15 @@ var qrcodegen = new function() { var color = false; var runX = 0; for (var x = 0; x < size; x++) { - if (modules[y][x] != color) { - color = modules[y][x]; - runX = 1; - } else { + if (modules[y][x] == color) { runX++; if (runX == 5) result += QrCode.PENALTY_N1; else if (runX > 5) result++; + } else { + color = modules[y][x]; + runX = 1; } } } @@ -449,15 +449,15 @@ var qrcodegen = new function() { var color = false; var runY = 0; for (var y = 0; y < size; y++) { - if (modules[y][x] != color) { - color = modules[y][x]; - runY = 1; - } else { + if (modules[y][x] == color) { runY++; if (runY == 5) result += QrCode.PENALTY_N1; else if (runY > 5) result++; + } else { + color = modules[y][x]; + runY = 1; } } } diff --git a/python/qrcodegen.py b/python/qrcodegen.py index 1459644..c32f02c 100644 --- a/python/qrcodegen.py +++ b/python/qrcodegen.py @@ -467,29 +467,29 @@ class QrCode(object): color = False runx = 0 for x in range(size): - if modules[y][x] != color: - color = modules[y][x] - runx = 1 - else: + if modules[y][x] == color: runx += 1 if runx == 5: result += QrCode._PENALTY_N1 elif runx > 5: result += 1 + else: + color = modules[y][x] + runx = 1 # Adjacent modules in column having same color for x in range(size): color = False runy = 0 for y in range(size): - if modules[y][x] != color: - color = modules[y][x] - runy = 1 - else: + if modules[y][x] == color: runy += 1 if runy == 5: result += QrCode._PENALTY_N1 elif runy > 5: result += 1 + else: + color = modules[y][x] + runy = 1 # 2*2 blocks of modules having same color for y in range(size - 1): diff --git a/rust/src/lib.rs b/rust/src/lib.rs index 9a2c2ae..305939f 100644 --- a/rust/src/lib.rs +++ b/rust/src/lib.rs @@ -650,16 +650,16 @@ impl QrCode { let mut color = false; let mut runx: i32 = 0; for x in 0 .. size { - if self.module(x, y) != color { - color = self.module(x, y); - runx = 1; - } else { + if self.module(x, y) == color { runx += 1; if runx == 5 { result += PENALTY_N1; } else if runx > 5 { result += 1; } + } else { + color = self.module(x, y); + runx = 1; } } } @@ -668,16 +668,16 @@ impl QrCode { let mut color = false; let mut runy: i32 = 0; for y in 0 .. size { - if self.module(x, y) != color { - color = self.module(x, y); - runy = 1; - } else { + if self.module(x, y) == color { runy += 1; if runy == 5 { result += PENALTY_N1; } else if runy > 5 { result += 1; } + } else { + color = self.module(x, y); + runy = 1; } } } diff --git a/typescript/qrcodegen.ts b/typescript/qrcodegen.ts index 45d5c1b..5ad0dbf 100644 --- a/typescript/qrcodegen.ts +++ b/typescript/qrcodegen.ts @@ -515,15 +515,15 @@ namespace qrcodegen { let color = false; let runX = 0; for (let x = 0; x < this.size; x++) { - if (this.modules[y][x] != color) { - color = this.modules[y][x]; - runX = 1; - } else { + if (this.modules[y][x] == color) { runX++; if (runX == 5) result += QrCode.PENALTY_N1; else if (runX > 5) result++; + } else { + color = this.modules[y][x]; + runX = 1; } } } @@ -532,15 +532,15 @@ namespace qrcodegen { let color = false; let runY = 0; for (let y = 0; y < this.size; y++) { - if (this.modules[y][x] != color) { - color = this.modules[y][x]; - runY = 1; - } else { + if (this.modules[y][x] == color) { runY++; if (runY == 5) result += QrCode.PENALTY_N1; else if (runY > 5) result++; + } else { + color = this.modules[y][x]; + runY = 1; } } }