Skip to content

Commit 43c4514

Browse files
committed
Minor cleanup
1 parent be4ec7e commit 43c4514

4 files changed

Lines changed: 42 additions & 20 deletions

File tree

QrCodeGenerator/BitMatrix.cs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -331,20 +331,26 @@ internal BitMatrix Copy()
331331
}
332332

333333
/// <summary>
334-
/// Returns the number of set bits in this matrix.
334+
/// Returns the number of bits set in this matrix
335+
/// (aka population count).
335336
/// </summary>
336337
/// <returns>The number of bits.</returns>
337-
internal int BitCount()
338+
internal int PopCount()
338339
{
339340
var sum = 0;
340341
foreach (var v in Raw)
341342
{
342-
sum += BitCount(v);
343+
sum += PopCount(v);
343344
}
344345
return sum;
345346
}
346347

347-
internal static int BitCount(ulong i)
348+
/// <summary>
349+
/// Returns the number of bits set in this value
350+
/// (aka population count).
351+
/// </summary>
352+
/// <returns>The value.</returns>
353+
internal static int PopCount(ulong i)
348354
{
349355
i = i - ((i >> 1) & 0x5555555555555555UL);
350356
i = (i & 0x3333333333333333UL) + ((i >> 2) & 0x3333333333333333UL);

QrCodeGenerator/Penalty.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -119,10 +119,10 @@ internal static int CalcSameColor(BitMatrix modules)
119119
var rs2 = fw2 & ((t2 << 1) | (t1 >> 63));
120120
var rs3 = fw3 & ((t3 << 1) | (t2 >> 63));
121121

122-
fiveWindowCount += BitMatrix.BitCount(fw0) + BitMatrix.BitCount(fw1)
123-
+ BitMatrix.BitCount(fw2) + BitMatrix.BitCount(fw3);
124-
run5StartCount += BitMatrix.BitCount(rs0) + BitMatrix.BitCount(rs1)
125-
+ BitMatrix.BitCount(rs2) + BitMatrix.BitCount(rs3);
122+
fiveWindowCount += BitMatrix.PopCount(fw0) + BitMatrix.PopCount(fw1)
123+
+ BitMatrix.PopCount(fw2) + BitMatrix.PopCount(fw3);
124+
run5StartCount += BitMatrix.PopCount(rs0) + BitMatrix.PopCount(rs1)
125+
+ BitMatrix.PopCount(rs2) + BitMatrix.PopCount(rs3);
126126
}
127127

128128
return fiveWindowCount + 2 * run5StartCount;
@@ -160,7 +160,7 @@ internal static int Calc2By2Blocks(BitMatrix modules)
160160
var aShift = (a >> 1) | (aNext << 63);
161161
var bShift = (b >> 1) | (bNext << 63);
162162
var monochrome = ~((a ^ aShift) | (b ^ bShift) | (a ^ b)) & edgeMask[w];
163-
count += BitMatrix.BitCount(monochrome);
163+
count += BitMatrix.PopCount(monochrome);
164164
}
165165
}
166166

@@ -227,7 +227,7 @@ internal static int CalcColorBalance(BitMatrix modules)
227227
// Penalty for the proportion of dark modules in the entire symbol.
228228
// Penalty points: N4 * k, where k is the rating of the deviation of the proportion of
229229
// the dark modules in the symbol from 50%, in steps of 5%, and N4 is 10.
230-
var darkModules = modules.BitCount();
230+
var darkModules = modules.PopCount();
231231

232232
var size = modules.Size;
233233
var totalNumber = size * size;

QrCodeGeneratorTest/BitMatrixTest.cs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ public void FillRect_OrsIntoExistingBits()
107107
bitMatrix.FillRect(40, 40, 30, 30);
108108

109109
Assert.True(bitMatrix.Get(50, 50));
110-
Assert.Equal(30 * 30, bitMatrix.BitCount());
110+
Assert.Equal(30 * 30, bitMatrix.PopCount());
111111
}
112112

113113
[Theory]
@@ -122,7 +122,7 @@ public void FillRect_NoOpForEmpty(int x, int y, int width, int height)
122122

123123
bitMatrix.FillRect(x, y, width, height);
124124

125-
Assert.Equal(0, bitMatrix.BitCount());
125+
Assert.Equal(0, bitMatrix.PopCount());
126126
}
127127

128128
[Fact]
@@ -159,7 +159,7 @@ public void And_WithDisjointMatrices_YieldsEmpty()
159159

160160
a.And(b);
161161

162-
Assert.Equal(0, a.BitCount());
162+
Assert.Equal(0, a.PopCount());
163163
}
164164

165165
[Fact]
@@ -170,10 +170,10 @@ public void And_WithSelf_LeavesMatrixUnchanged()
170170
a.FillRect(5, 7, 91, 33);
171171
a.Set(199, 199, true);
172172

173-
var expected = a.BitCount();
173+
var expected = a.PopCount();
174174
a.And(a);
175175

176-
Assert.Equal(expected, a.BitCount());
176+
Assert.Equal(expected, a.PopCount());
177177
Assert.True(a.Get(199, 199));
178178
}
179179

@@ -219,7 +219,7 @@ public void Xor_WithSelf_YieldsEmpty()
219219

220220
a.Xor(a);
221221

222-
Assert.Equal(0, a.BitCount());
222+
Assert.Equal(0, a.PopCount());
223223
}
224224

225225
[Fact]
@@ -231,12 +231,12 @@ public void Xor_TwiceRestoresOriginal()
231231

232232
a.FillRect(20, 20, 40, 40);
233233
b.FillRect(50, 50, 30, 30);
234-
var expected = a.BitCount();
234+
var expected = a.PopCount();
235235

236236
a.Xor(b);
237237
a.Xor(b);
238238

239-
Assert.Equal(expected, a.BitCount());
239+
Assert.Equal(expected, a.PopCount());
240240
Assert.True(a.Get(20, 20));
241241
Assert.False(a.Get(80, 80));
242242
}
@@ -314,7 +314,7 @@ public void Transpose_SingleBit_Identity()
314314
m.Transpose();
315315

316316
Assert.True(m.Get(0, 0));
317-
Assert.Equal(1, m.BitCount());
317+
Assert.Equal(1, m.PopCount());
318318
}
319319

320320
[Fact]
@@ -385,7 +385,7 @@ public void FillRect_MultipleRectsCombine()
385385
bitMatrix.FillRect(0, 0, 100, 100);
386386
bitMatrix.FillRect(150, 150, 50, 50);
387387

388-
Assert.Equal(100 * 100 + 50 * 50, bitMatrix.BitCount());
388+
Assert.Equal(100 * 100 + 50 * 50, bitMatrix.PopCount());
389389
Assert.True(bitMatrix.Get(0, 0));
390390
Assert.True(bitMatrix.Get(99, 99));
391391
Assert.False(bitMatrix.Get(100, 100));

QrCodeGeneratorTest/PenaltyTest.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
* https://github.com/manuelbl/QrCodeGenerator
66
*/
77

8+
using System.Diagnostics.CodeAnalysis;
89
using Xunit;
910

1011
namespace Net.Codecrete.QrCodeGenerator.Test
@@ -248,6 +249,20 @@ public void Calc2By2Blocks_BlockAtLastValidPosition(int size)
248249
Assert.Equal(3, Penalty.Calc2By2Blocks(modules));
249250
}
250251

252+
[Theory]
253+
[InlineData(21, 3)]
254+
[InlineData(65, 61)]
255+
[InlineData(69, 62)]
256+
[InlineData(73, 63)]
257+
[InlineData(77, 64)]
258+
[InlineData(81, 65)]
259+
public void Calc2By2Blocks_OverlappingBlocksCountedMultipleTimes(int size, int x)
260+
{
261+
var modules = CreateCheckerboard(size);
262+
modules.FillRect(x, 4, 3, 3);
263+
Assert.Equal(12, Penalty.Calc2By2Blocks(modules));
264+
}
265+
251266
[Theory]
252267
[InlineData(17, 0.5, 0)]
253268
[InlineData(21, 0.545, 0)]
@@ -413,6 +428,7 @@ private static void Invert(BitMatrix modules)
413428
}
414429
}
415430

431+
[SuppressMessage("squid", "S2234")]
416432
// Transpose modules (mirror diagonally)
417433
private static void Transpose(BitMatrix modules)
418434
{

0 commit comments

Comments
 (0)