Skip to content

Commit b0af418

Browse files
committed
Apply code suggestions
1 parent 6101a18 commit b0af418

File tree

1 file changed

+36
-21
lines changed

1 file changed

+36
-21
lines changed

QRCoder/QRCodeGenerator.Polynom.cs

Lines changed: 36 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Diagnostics;
34
using System.Text;
45

56
namespace QRCoder
@@ -21,15 +22,15 @@ private struct Polynom : IDisposable
2122
public Polynom(int count)
2223
{
2324
_length = 0;
24-
_polyItems = Allocate(count);
25+
_polyItems = RentArray(count);
2526
}
2627

2728
/// <summary>
2829
/// Adds a polynomial term to the polynomial.
2930
/// </summary>
3031
public void Add(PolynomItem item)
3132
{
32-
EnsureCapacity(_length + 1);
33+
AssertCapacity(_length + 1);
3334
_polyItems[_length++] = item;
3435
}
3536

@@ -53,17 +54,26 @@ public void RemoveAt(int index)
5354
public PolynomItem this[int index]
5455
{
5556
get {
56-
if (index < 0 || index >= _length)
57-
throw new IndexOutOfRangeException();
57+
if ((uint)index >= _length)
58+
ThrowIndexOutOfRangeException();
5859
return _polyItems[index];
5960
}
6061
set {
61-
if (index < 0 || index >= _length)
62-
throw new IndexOutOfRangeException();
62+
if ((uint)index >= _length)
63+
ThrowIndexOutOfRangeException();
6364
_polyItems[index] = value;
6465
}
6566
}
6667

68+
#if NET6_0_OR_GREATER
69+
[StackTraceHidden]
70+
#endif
71+
private static void ThrowIndexOutOfRangeException()
72+
{
73+
throw new IndexOutOfRangeException();
74+
}
75+
76+
6777
/// <summary>
6878
/// Gets the number of polynomial terms in the polynomial.
6979
/// </summary>
@@ -100,7 +110,9 @@ public void Sort(Func<PolynomItem, PolynomItem, int> comparer)
100110
if (comparer == null) throw new ArgumentNullException(nameof(comparer));
101111

102112
var items = _polyItems;
103-
if (items == null || _length <= 1)
113+
if (items == null) throw new ObjectDisposedException(nameof(Polynom));
114+
115+
if (_length <= 1)
104116
{
105117
return; // Nothing to sort if the list is empty or contains only one element
106118
}
@@ -158,27 +170,30 @@ public override string ToString()
158170
/// <inheritdoc/>
159171
public void Dispose()
160172
{
161-
Free(_polyItems);
173+
ReturnArray(_polyItems);
162174
_polyItems = null;
163175
}
164176

165177
/// <summary>
166178
/// Ensures that the polynomial has enough capacity to store the specified number of polynomial terms.
167179
/// </summary>
168-
private void EnsureCapacity(int min)
180+
private void AssertCapacity(int min)
169181
{
170182
if (_polyItems.Length < min)
171183
{
172184
// All math by QRCoder should be done with fixed polynomials, so we don't need to grow the capacity.
173185
ThrowNotSupportedException();
174186

175187
// Sample code for growing the capacity:
176-
//var newArray = Allocate(Math.Max(min - 1, 8) * 2); // Grow by 2x, but at least by 8
188+
//var newArray = RentArray(Math.Max(min - 1, 8) * 2); // Grow by 2x, but at least by 8
177189
//Array.Copy(_polyItems, newArray, _length);
178-
//Free(_polyItems);
190+
//ReturnArray(_polyItems);
179191
//_polyItems = newArray;
180192
}
181193

194+
#if NET6_0_OR_GREATER
195+
[StackTraceHidden]
196+
#endif
182197
void ThrowNotSupportedException()
183198
{
184199
throw new NotSupportedException("The polynomial capacity is fixed and cannot be increased.");
@@ -187,29 +202,29 @@ void ThrowNotSupportedException()
187202

188203
#if NETCOREAPP
189204
/// <summary>
190-
/// Allocates memory for the polynomial terms.
205+
/// Rents memory for the polynomial terms from the shared memory pool.
191206
/// </summary>
192-
private static PolynomItem[] Allocate(int count)
207+
private static PolynomItem[] RentArray(int count)
193208
{
194209
return System.Buffers.ArrayPool<PolynomItem>.Shared.Rent(count);
195210
}
196211

197212
/// <summary>
198-
/// Frees memory allocated for the polynomial terms.
213+
/// Returns memory allocated for the polynomial terms back to the shared memory pool.
199214
/// </summary>
200-
private static void Free(PolynomItem[] array)
215+
private static void ReturnArray(PolynomItem[] array)
201216
{
202217
System.Buffers.ArrayPool<PolynomItem>.Shared.Return(array);
203218
}
204219
#else
205220
// Implement a poor-man's array pool for .NET Framework
206221
[ThreadStatic]
207222
private static List<PolynomItem[]> _arrayPool;
208-
223+
209224
/// <summary>
210-
/// Allocates memory for the polynomial terms.
225+
/// Rents memory for the polynomial terms from a shared memory pool.
211226
/// </summary>
212-
private static PolynomItem[] Allocate(int count)
227+
private static PolynomItem[] RentArray(int count)
213228
{
214229
if (count <= 0)
215230
ThrowArgumentOutOfRangeException();
@@ -236,11 +251,11 @@ void ThrowArgumentOutOfRangeException()
236251
throw new ArgumentOutOfRangeException(nameof(count), "The count must be a positive number.");
237252
}
238253
}
239-
254+
240255
/// <summary>
241-
/// Frees memory allocated for the polynomial terms.
256+
/// Returns memory allocated for the polynomial terms back to a shared memory pool.
242257
/// </summary>
243-
private static void Free(PolynomItem[] array)
258+
private static void ReturnArray(PolynomItem[] array)
244259
{
245260
if (array == null)
246261
ThrowArgumentNullException();

0 commit comments

Comments
 (0)