Skip to content

Commit 5ee8d65

Browse files
committed
Updated to use bool[][]
1 parent 6407949 commit 5ee8d65

File tree

1 file changed

+15
-20
lines changed

1 file changed

+15
-20
lines changed

QRCoder/QRCodeGenerator.ModulePlacer.BlockedModules.cs

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Collections;
3+
using System.Threading;
34

45
namespace QRCoder
56
{
@@ -12,30 +13,27 @@ private static partial class ModulePlacer
1213
/// </summary>
1314
public struct BlockedModules : IDisposable
1415
{
15-
private readonly bool[,] _blockedModules;
16+
private readonly bool[][] _blockedModules;
1617

17-
[ThreadStatic]
18-
private static bool[,] _staticBlockedModules;
18+
private static bool[][] _staticBlockedModules;
1919

2020
/// <summary>
2121
/// Initializes a new instance of the <see cref="BlockedModules"/> struct with a specified capacity.
2222
/// </summary>
2323
/// <param name="capacity">The initial capacity of the blocked modules list.</param>
2424
public BlockedModules(int size)
2525
{
26-
if (_staticBlockedModules != null && _staticBlockedModules.GetUpperBound(0) >= (size - 1))
26+
_blockedModules = Interlocked.Exchange(ref _staticBlockedModules, null);
27+
if (_blockedModules != null && _blockedModules.Length >= size)
2728
{
28-
_blockedModules = _staticBlockedModules;
29-
_staticBlockedModules = null;
30-
#if NET6_0_OR_GREATER
31-
Array.Clear(_blockedModules);
32-
#else
33-
Array.Clear(_blockedModules, 0, _blockedModules.Length);
34-
#endif
29+
for (int i = 0; i < size; i++)
30+
Array.Clear(_blockedModules[i], 0, size);
3531
}
3632
else
3733
{
38-
_blockedModules = new bool[size, size];
34+
_blockedModules = new bool[size][];
35+
for (int i = 0; i < size; i++)
36+
_blockedModules[i] = new bool[size];
3937
}
4038
}
4139

@@ -46,7 +44,7 @@ public BlockedModules(int size)
4644
/// <param name="y">The y-coordinate of the module.</param>
4745
public void Add(int x, int y)
4846
{
49-
_blockedModules[y, x] = true;
47+
_blockedModules[y][x] = true;
5048
}
5149

5250
/// <summary>
@@ -59,7 +57,7 @@ public void Add(Rectangle rect)
5957
{
6058
for (int x = rect.X; x < rect.X + rect.Width; x++)
6159
{
62-
_blockedModules[y, x] = true;
60+
_blockedModules[y][x] = true;
6361
}
6462
}
6563
}
@@ -72,7 +70,7 @@ public void Add(Rectangle rect)
7270
/// <returns><c>true</c> if the coordinates are blocked; otherwise, <c>false</c>.</returns>
7371
public bool IsBlocked(int x, int y)
7472
{
75-
return _blockedModules[y, x];
73+
return _blockedModules[y][x];
7674
}
7775

7876
/// <summary>
@@ -86,7 +84,7 @@ public bool IsBlocked(Rectangle r1)
8684
{
8785
for (int x = r1.X; x < r1.X + r1.Width; x++)
8886
{
89-
if (_blockedModules[y, x])
87+
if (_blockedModules[y][x])
9088
return true;
9189
}
9290
}
@@ -95,10 +93,7 @@ public bool IsBlocked(Rectangle r1)
9593

9694
public void Dispose()
9795
{
98-
if (_staticBlockedModules == null || _staticBlockedModules.GetUpperBound(0) < _blockedModules.GetUpperBound(0))
99-
{
100-
_staticBlockedModules = _blockedModules;
101-
}
96+
Interlocked.CompareExchange(ref _staticBlockedModules, _blockedModules, null);
10297
}
10398
}
10499
}

0 commit comments

Comments
 (0)