Skip to content

Commit 06c77cc

Browse files
committed
SWEEP: Add unchecked to GetHashCode, #1065
1 parent 7cfc4c3 commit 06c77cc

File tree

128 files changed

+1183
-763
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

128 files changed

+1183
-763
lines changed

src/Lucene.Net.Analysis.Common/Analysis/Miscellaneous/PatternAnalyzer.cs

+9-6
Original file line numberDiff line numberDiff line change
@@ -277,12 +277,15 @@ public override int GetHashCode()
277277
return 1303507063;
278278
}
279279

280-
int h = 1;
281-
h = 31 * h + pattern.ToString().GetHashCode();
282-
h = 31 * h + (int)pattern.Options;
283-
h = 31 * h + (toLowerCase ? 1231 : 1237);
284-
h = 31 * h + (stopWords != null ? stopWords.GetHashCode() : 0);
285-
return h;
280+
unchecked
281+
{
282+
int h = 1;
283+
h = 31 * h + pattern.ToString().GetHashCode();
284+
h = 31 * h + (int)pattern.Options;
285+
h = 31 * h + (toLowerCase ? 1231 : 1237);
286+
h = 31 * h + (stopWords != null ? stopWords.GetHashCode() : 0);
287+
return h;
288+
}
286289
}
287290

288291
/// <summary>

src/Lucene.Net.Analysis.Common/Analysis/Util/CharArrayMap.cs

+43-20
Original file line numberDiff line numberDiff line change
@@ -1800,18 +1800,23 @@ public override bool Equals(object? obj)
18001800
/// <returns></returns>
18011801
public override int GetHashCode()
18021802
{
1803-
const int PRIME = 31; // arbitrary prime
1804-
int hash = PRIME;
1805-
using (var iter = GetEnumerator())
1803+
unchecked
18061804
{
1807-
while (iter.MoveNext())
1805+
const int PRIME = 31; // arbitrary prime
1806+
int hash = PRIME;
1807+
using (var iter = GetEnumerator())
18081808
{
1809-
hash = (hash * PRIME) ^ iter.CurrentKeyString.GetHashCode();
1810-
TValue? value = iter.CurrentValue;
1811-
hash = (hash * PRIME) ^ (value is null ? 0 : JCG.EqualityComparer<TValue>.Default.GetHashCode(value));
1809+
while (iter.MoveNext())
1810+
{
1811+
hash = (hash * PRIME) ^ iter.CurrentKeyString.GetHashCode();
1812+
TValue? value = iter.CurrentValue;
1813+
hash = (hash * PRIME) ^
1814+
(value is null ? 0 : JCG.EqualityComparer<TValue>.Default.GetHashCode(value));
1815+
}
18121816
}
1817+
1818+
return hash;
18131819
}
1814-
return hash;
18151820
}
18161821

18171822
[MethodImpl(MethodImplOptions.AggressiveInlining)]
@@ -1832,16 +1837,22 @@ private int GetHashCode(char[] text, int startIndex, int length)
18321837
{
18331838
for (int i = startIndex; i < stop;)
18341839
{
1835-
int codePointAt = charUtils.CodePointAt(text, i, stop);
1836-
code = code * 31 + Character.ToLower(codePointAt, CultureInfo.InvariantCulture); // LUCENENET specific - need to use invariant culture to match Java
1837-
i += Character.CharCount(codePointAt);
1840+
unchecked
1841+
{
1842+
int codePointAt = charUtils.CodePointAt(text, i, stop);
1843+
code = code * 31 + Character.ToLower(codePointAt, CultureInfo.InvariantCulture); // LUCENENET specific - need to use invariant culture to match Java
1844+
i += Character.CharCount(codePointAt);
1845+
}
18381846
}
18391847
}
18401848
else
18411849
{
18421850
for (int i = startIndex; i < stop; i++)
18431851
{
1844-
code = code * 31 + text[i];
1852+
unchecked
1853+
{
1854+
code = code * 31 + text[i];
1855+
}
18451856
}
18461857
}
18471858
return code;
@@ -1859,16 +1870,22 @@ private int GetHashCode(ICharSequence text)
18591870
{
18601871
for (int i = 0; i < length;)
18611872
{
1862-
int codePointAt = charUtils.CodePointAt(text, i);
1863-
code = code * 31 + Character.ToLower(codePointAt, CultureInfo.InvariantCulture); // LUCENENET specific - need to use invariant culture to match Java
1864-
i += Character.CharCount(codePointAt);
1873+
unchecked
1874+
{
1875+
int codePointAt = charUtils.CodePointAt(text, i);
1876+
code = code * 31 + Character.ToLower(codePointAt, CultureInfo.InvariantCulture); // LUCENENET specific - need to use invariant culture to match Java
1877+
i += Character.CharCount(codePointAt);
1878+
}
18651879
}
18661880
}
18671881
else
18681882
{
18691883
for (int i = 0; i < length; i++)
18701884
{
1871-
code = code * 31 + text[i];
1885+
unchecked
1886+
{
1887+
code = code * 31 + text[i];
1888+
}
18721889
}
18731890
}
18741891
return code;
@@ -1886,16 +1903,22 @@ private int GetHashCode(string text)
18861903
{
18871904
for (int i = 0; i < length;)
18881905
{
1889-
int codePointAt = charUtils.CodePointAt(text, i);
1890-
code = code * 31 + Character.ToLower(codePointAt, CultureInfo.InvariantCulture); // LUCENENET specific - need to use invariant culture to match Java
1891-
i += Character.CharCount(codePointAt);
1906+
unchecked
1907+
{
1908+
int codePointAt = charUtils.CodePointAt(text, i);
1909+
code = code * 31 + Character.ToLower(codePointAt, CultureInfo.InvariantCulture); // LUCENENET specific - need to use invariant culture to match Java
1910+
i += Character.CharCount(codePointAt);
1911+
}
18921912
}
18931913
}
18941914
else
18951915
{
18961916
for (int i = 0; i < length; i++)
18971917
{
1898-
code = code * 31 + text[i];
1918+
unchecked
1919+
{
1920+
code = code * 31 + text[i];
1921+
}
18991922
}
19001923
}
19011924
return code;

src/Lucene.Net.Analysis.SmartCn/Hhmm/PathNode.cs

+10-7
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,16 @@ public virtual int CompareTo(PathNode pn)
4848
/// </summary>
4949
public override int GetHashCode()
5050
{
51-
int prime = 31;
52-
int result = 1;
53-
result = prime * result + PreNode;
54-
long temp;
55-
temp = J2N.BitConversion.DoubleToInt64Bits(Weight);
56-
result = prime * result + (int)(temp ^ (temp >>> 32));
57-
return result;
51+
unchecked
52+
{
53+
const int prime = 31;
54+
int result = 1;
55+
result = prime * result + PreNode;
56+
long temp;
57+
temp = J2N.BitConversion.DoubleToInt64Bits(Weight);
58+
result = prime * result + (int)(temp ^ (temp >>> 32));
59+
return result;
60+
}
5861
}
5962

6063
/// <summary>

src/Lucene.Net.Analysis.SmartCn/Hhmm/SegToken.cs

+13-10
Original file line numberDiff line numberDiff line change
@@ -83,18 +83,21 @@ public SegToken(char[] idArray, int start, int end, WordType wordType, int weigh
8383
/// </summary>
8484
public override int GetHashCode()
8585
{
86-
int prime = 31;
87-
int result = 1;
88-
for (int i = 0; i < CharArray.Length; i++)
86+
unchecked
8987
{
90-
result = prime * result + CharArray[i];
88+
const int prime = 31;
89+
int result = 1;
90+
for (int i = 0; i < CharArray.Length; i++)
91+
{
92+
result = prime * result + CharArray[i];
93+
}
94+
result = prime * result + EndOffset;
95+
result = prime * result + Index;
96+
result = prime * result + StartOffset;
97+
result = prime * result + Weight;
98+
result = prime * result + (int)WordType;
99+
return result;
91100
}
92-
result = prime * result + EndOffset;
93-
result = prime * result + Index;
94-
result = prime * result + StartOffset;
95-
result = prime * result + Weight;
96-
result = prime * result + (int)WordType;
97-
return result;
98101
}
99102

100103
/// <summary>

src/Lucene.Net.Analysis.SmartCn/Hhmm/SegTokenPair.cs

+13-10
Original file line numberDiff line numberDiff line change
@@ -55,18 +55,21 @@ public SegTokenPair(char[] idArray, int from, int to, double weight)
5555
/// </summary>
5656
public override int GetHashCode()
5757
{
58-
int prime = 31;
59-
int result = 1;
60-
for (int i = 0; i < CharArray.Length; i++)
58+
unchecked
6159
{
62-
result = prime * result + CharArray[i];
60+
const int prime = 31;
61+
int result = 1;
62+
for (int i = 0; i < CharArray.Length; i++)
63+
{
64+
result = prime * result + CharArray[i];
65+
}
66+
result = prime * result + From;
67+
result = prime * result + To;
68+
long temp;
69+
temp = J2N.BitConversion.DoubleToInt64Bits(Weight);
70+
result = prime * result + (int)(temp ^ (temp >>> 32));
71+
return result;
6372
}
64-
result = prime * result + From;
65-
result = prime * result + To;
66-
long temp;
67-
temp = J2N.BitConversion.DoubleToInt64Bits(Weight);
68-
result = prime * result + (int)(temp ^ (temp >>> 32));
69-
return result;
7073
}
7174

7275
/// <summary>

src/Lucene.Net.Codecs/BlockTerms/BlockTermsReader.cs

+9-6
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,15 @@ namespace Lucene.Net.Codecs.BlockTerms
2828

2929
/// <summary>
3030
/// Handles a terms dict, but decouples all details of
31-
/// doc/freqs/positions reading to an instance of
31+
/// doc/freqs/positions reading to an instance of
3232
/// <see cref="PostingsReaderBase"/>. This class is reusable for
3333
/// codecs that use a different format for
3434
/// docs/freqs/positions (though codecs are also free to
3535
/// make their own terms dict impl).
3636
/// <para/>
3737
/// This class also interacts with an instance of
3838
/// <see cref="TermsIndexReaderBase"/>, to abstract away the specific
39-
/// implementation of the terms dict index.
39+
/// implementation of the terms dict index.
4040
/// <para/>
4141
/// @lucene.experimental
4242
/// </summary>
@@ -90,7 +90,10 @@ public override object Clone()
9090

9191
public override int GetHashCode()
9292
{
93-
return Field.GetHashCode() * 31 + Term.GetHashCode();
93+
unchecked
94+
{
95+
return Field.GetHashCode() * 31 + Term.GetHashCode();
96+
}
9497
}
9598
}
9699

@@ -372,7 +375,7 @@ public SegmentTermsEnum(FieldReader outerInstance)
372375
/// the terms data from that"; eg FuzzyTermsEnum will
373376
/// (usually) just immediately call seek again if we
374377
/// return NOT_FOUND so it's a waste for us to fill in
375-
/// the term that was actually NOT_FOUND
378+
/// the term that was actually NOT_FOUND
376379
/// </remarks>
377380
public override SeekStatus SeekCeil(BytesRef target)
378381
{
@@ -876,7 +879,7 @@ public override long Ord
876879
// Does initial decode of next block of terms; this
877880
// doesn't actually decode the docFreq, totalTermFreq,
878881
// postings details (frq/prx offset, etc.) metadata;
879-
// it just loads them as byte[] blobs which are then
882+
// it just loads them as byte[] blobs which are then
880883
// decoded on-demand if the metadata is ever requested
881884
// for any term in this block. This enables terms-only
882885
// intensive consumes (eg certain MTQs, respelling) to
@@ -1014,4 +1017,4 @@ public override void CheckIntegrity()
10141017
postingsReader.CheckIntegrity();
10151018
}
10161019
}
1017-
}
1020+
}

src/Lucene.Net.Codecs/Memory/FSTTermOutputs.cs

+17-14
Original file line numberDiff line numberDiff line change
@@ -73,26 +73,29 @@ internal TermData(long[] longs, byte[] bytes, int docFreq, long totalTermFreq)
7373
// aren't NO_OUTPUTs.
7474
public override int GetHashCode()
7575
{
76-
var hash = 0;
77-
if (longs != null)
76+
unchecked
7877
{
79-
var end = longs.Length;
80-
for (var i = 0; i < end; i++)
78+
var hash = 0;
79+
if (longs != null)
8180
{
82-
hash -= (int) longs[i];
81+
var end = longs.Length;
82+
for (var i = 0; i < end; i++)
83+
{
84+
hash -= (int) longs[i];
85+
}
8386
}
84-
}
85-
if (bytes != null)
86-
{
87-
hash = -hash;
88-
var end = bytes.Length;
89-
for (var i = 0; i < end; i++)
87+
if (bytes != null)
9088
{
91-
hash += bytes[i];
89+
hash = -hash;
90+
var end = bytes.Length;
91+
for (var i = 0; i < end; i++)
92+
{
93+
hash += bytes[i];
94+
}
9295
}
96+
hash += (int) (docFreq + totalTermFreq);
97+
return hash;
9398
}
94-
hash += (int) (docFreq + totalTermFreq);
95-
return hash;
9699
}
97100

98101
public override bool Equals(object other)

src/Lucene.Net.Expressions/ExpressionSortField.cs

+8-5
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ internal class ExpressionSortField : SortField
2727
{
2828
private readonly ExpressionValueSource source;
2929

30-
internal ExpressionSortField(string name, ExpressionValueSource source, bool reverse)
30+
internal ExpressionSortField(string name, ExpressionValueSource source, bool reverse)
3131
: base(name, SortFieldType.CUSTOM, reverse)
3232
{
3333
this.source = source;
@@ -40,10 +40,13 @@ public override FieldComparer GetComparer(int numHits, int sortPos)
4040

4141
public override int GetHashCode()
4242
{
43-
int prime = 31;
44-
int result = base.GetHashCode();
45-
result = prime * result + ((source is null) ? 0 : source.GetHashCode());
46-
return result;
43+
unchecked
44+
{
45+
const int prime = 31;
46+
int result = base.GetHashCode();
47+
result = prime * result + (source is null ? 0 : source.GetHashCode());
48+
return result;
49+
}
4750
}
4851

4952
public override bool Equals(object obj)

src/Lucene.Net.Expressions/ExpressionValueSource.cs

+9-6
Original file line numberDiff line numberDiff line change
@@ -120,12 +120,15 @@ public override string GetDescription()
120120

121121
public override int GetHashCode()
122122
{
123-
int prime = 31;
124-
int result = 1;
125-
result = prime * result + ((expression is null) ? 0 : expression.GetHashCode());
126-
result = prime * result + (needsScores ? 1231 : 1237);
127-
result = prime * result + Arrays.GetHashCode(variables);
128-
return result;
123+
unchecked
124+
{
125+
const int prime = 31;
126+
int result = 1;
127+
result = prime * result + (expression is null ? 0 : expression.GetHashCode());
128+
result = prime * result + (needsScores ? 1231 : 1237);
129+
result = prime * result + Arrays.GetHashCode(variables);
130+
return result;
131+
}
129132
}
130133

131134
public override bool Equals(object obj)

0 commit comments

Comments
 (0)