Skip to content

Commit 2bdfd04

Browse files
committed
Eliminate foreach across BitArray, implement IEquatable for Point
1 parent 48ae1ce commit 2bdfd04

File tree

1 file changed

+12
-6
lines changed

1 file changed

+12
-6
lines changed

QRCoder/QRCodeGenerator.cs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -853,9 +853,9 @@ public static int Score(QRCodeData qrCode)
853853

854854
//Penalty 4
855855
int blackModules = 0;
856-
foreach (var row in qrCode.ModuleMatrix)
857-
foreach (bool bit in row)
858-
if (bit)
856+
foreach (var bitArray in qrCode.ModuleMatrix)
857+
for (var x = 0; x < size; x++)
858+
if (bitArray[x])
859859
blackModules++;
860860

861861
var percentDiv5 = blackModules * 20 / (qrCode.ModuleMatrix.Count * qrCode.ModuleMatrix.Count);
@@ -1311,7 +1311,7 @@ private static Polynom MultiplyAlphaPolynoms(Polynom polynomBase, Polynom polyno
13111311

13121312
int[] GetNotUniqueExponents(List<PolynomItem> list)
13131313
{
1314-
var dic = new Dictionary<int, bool>();
1314+
var dic = new Dictionary<int, bool>(list.Count);
13151315
foreach (var row in list)
13161316
{
13171317
#if NETCOREAPP
@@ -1385,7 +1385,7 @@ private static Dictionary<int, AlignmentPattern> CreateAlignmentPatternTable()
13851385

13861386
for (var i = 0; i < (7 * 40); i = i + 7)
13871387
{
1388-
var points = new List<Point>();
1388+
var points = new List<Point>(50);
13891389
for (var x = 0; x < 7; x++)
13901390
{
13911391
if (alignmentPatternBaseValues[i + x] != 0)
@@ -1654,7 +1654,7 @@ public override string ToString()
16541654
}
16551655
}
16561656

1657-
private readonly struct Point
1657+
private readonly struct Point : IEquatable<Point>
16581658
{
16591659
public int X { get; }
16601660
public int Y { get; }
@@ -1663,6 +1663,12 @@ public Point(int x, int y)
16631663
this.X = x;
16641664
this.Y = y;
16651665
}
1666+
1667+
public bool Equals(Point other)
1668+
{
1669+
return this.X == other.X && this.Y == other.Y;
1670+
}
1671+
16661672
}
16671673

16681674
private readonly struct Rectangle

0 commit comments

Comments
 (0)