Skip to content

Commit ff01708

Browse files
authored
Use short-circuit logic (#5824)
1 parent b3a3c66 commit ff01708

Some content is hidden

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

46 files changed

+195
-195
lines changed

src/Microsoft.Data.Analysis/TextFieldParser.cs

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ public bool EndOfData
199199
{
200200
return _endOfData;
201201
}
202-
if ((_reader == null) | (_buffer == null))
202+
if ((_reader == null) || (_buffer == null))
203203
{
204204
_endOfData = true;
205205
return true;
@@ -217,7 +217,7 @@ public long LineNumber
217217
{
218218
get
219219
{
220-
if (_lineNumber != -1 && ((_reader.Peek() == -1) & (_position == _charsRead)))
220+
if (_lineNumber != -1 && ((_reader.Peek() == -1) && (_position == _charsRead)))
221221
{
222222
// Side effect of a property. Not great. Just leaving it in for now.
223223
CloseReader();
@@ -405,7 +405,7 @@ public void SetFieldWidths(params int[] fieldWidths)
405405

406406
public string ReadLine()
407407
{
408-
if ((_reader == null) | (_buffer == null))
408+
if ((_reader == null) || (_buffer == null))
409409
{
410410
return null;
411411
}
@@ -424,7 +424,7 @@ public string ReadLine()
424424

425425
public string[] ReadFields()
426426
{
427-
if ((_reader == null) | (_buffer == null))
427+
if ((_reader == null) || (_buffer == null))
428428
{
429429
return null;
430430
}
@@ -453,7 +453,7 @@ public string PeekChars(int numberOfChars)
453453
throw new ArgumentException(string.Format(Strings.PositiveNumberOfCharacters, nameof(numberOfChars)));
454454
}
455455

456-
if ((_reader == null) | (_buffer == null))
456+
if ((_reader == null) || (_buffer == null))
457457
{
458458
return null;
459459
}
@@ -482,7 +482,7 @@ public string PeekChars(int numberOfChars)
482482

483483
public string ReadToEnd()
484484
{
485-
if ((_reader == null) | (_buffer == null))
485+
if ((_reader == null) || (_buffer == null))
486486
{
487487
return null;
488488
}
@@ -642,7 +642,7 @@ private int SlideCursorToStartOfBuffer()
642642
{
643643
Debug.Assert(_buffer != null, "There's no buffer");
644644
Debug.Assert(_reader != null, "There's no StreamReader");
645-
Debug.Assert((_position >= 0) & (_position <= _buffer.Length), "The cursor is out of range");
645+
Debug.Assert((_position >= 0) && (_position <= _buffer.Length), "The cursor is out of range");
646646
if (_position > 0)
647647
{
648648
int bufferLength = _buffer.Length;
@@ -710,7 +710,7 @@ private string PeekNextDataLine()
710710
private string ReadNextLine(ref int cursor, ChangeBufferFunction changeBuffer)
711711
{
712712
Debug.Assert(_buffer != null, "There's no buffer");
713-
Debug.Assert((cursor >= 0) & (cursor <= _charsRead), "The cursor is out of range");
713+
Debug.Assert((cursor >= 0) && (cursor <= _charsRead), "The cursor is out of range");
714714
if (cursor == _charsRead && changeBuffer() == 0)
715715
{
716716
return null;
@@ -722,7 +722,7 @@ private string ReadNextLine(ref int cursor, ChangeBufferFunction changeBuffer)
722722
for (int i = cursor; i <= _charsRead - 1; i++)
723723
{
724724
char Character = _buffer[i];
725-
if (!(Character.Equals('\r') | Character.Equals('\n')))
725+
if (!(Character.Equals('\r') || Character.Equals('\n')))
726726
{
727727
continue;
728728
}
@@ -912,16 +912,16 @@ private int GetEndOfLineIndex(string line)
912912
Debug.Assert(length > 0, "A blank line shouldn't be parsed");
913913
if (length == 1)
914914
{
915-
Debug.Assert(!line[0].Equals('\r') & !line[0].Equals('\n'), "A blank line shouldn't be parsed");
915+
Debug.Assert(!line[0].Equals('\r') && !line[0].Equals('\n'), "A blank line shouldn't be parsed");
916916
return length;
917917
}
918918
checked
919919
{
920-
if (line[length - 2].Equals('\r') | line[length - 2].Equals('\n'))
920+
if (line[length - 2].Equals('\r') || line[length - 2].Equals('\n'))
921921
{
922922
return length - 2;
923923
}
924-
if (line[length - 1].Equals('\r') | line[length - 1].Equals('\n'))
924+
if (line[length - 1].Equals('\r') || line[length - 1].Equals('\n'))
925925
{
926926
return length - 1;
927927
}
@@ -1020,7 +1020,7 @@ private void ValidateAndEscapeDelimiters()
10201020

10211021
private void ValidateReadyToRead()
10221022
{
1023-
if (!(_needPropertyCheck | ArrayHasChanged()))
1023+
if (!(_needPropertyCheck || ArrayHasChanged()))
10241024
{
10251025
return;
10261026
}
@@ -1041,7 +1041,7 @@ private void ValidateReadyToRead()
10411041
string[] commentTokens = _commentTokens;
10421042
foreach (string token in commentTokens)
10431043
{
1044-
if (token != string.Empty && (_hasFieldsEnclosedInQuotes & (_textFieldType == FieldType.Delimited)) && string.Compare(token.Trim(), "\"", StringComparison.Ordinal) == 0)
1044+
if (token != string.Empty && (_hasFieldsEnclosedInQuotes && (_textFieldType == FieldType.Delimited)) && string.Compare(token.Trim(), "\"", StringComparison.Ordinal) == 0)
10451045
{
10461046
throw new Exception(Strings.IllegalQuoteDelimiter);
10471047
}
@@ -1077,7 +1077,7 @@ private bool ArrayHasChanged()
10771077
{
10781078
case FieldType.Delimited:
10791079
{
1080-
Debug.Assert(((_delimitersCopy == null) & (_delimiters == null)) | ((_delimitersCopy != null) & (_delimiters != null)), "Delimiters and copy are not both Nothing or both not Nothing");
1080+
Debug.Assert(((_delimitersCopy == null) && (_delimiters == null)) || ((_delimitersCopy != null) && (_delimiters != null)), "Delimiters and copy are not both Nothing or both not Nothing");
10811081
if (_delimiters == null)
10821082
{
10831083
return false;
@@ -1097,7 +1097,7 @@ private bool ArrayHasChanged()
10971097
}
10981098
case FieldType.FixedWidth:
10991099
{
1100-
Debug.Assert(((_fieldWidthsCopy == null) & (_fieldWidths == null)) | ((_fieldWidthsCopy != null) & (_fieldWidths != null)), "FieldWidths and copy are not both Nothing or both not Nothing");
1100+
Debug.Assert(((_fieldWidthsCopy == null) && (_fieldWidths == null)) || ((_fieldWidthsCopy != null) && (_fieldWidths != null)), "FieldWidths and copy are not both Nothing or both not Nothing");
11011101
if (_fieldWidths == null)
11021102
{
11031103
return false;

src/Microsoft.ML.Core/CommandLine/CmdParser.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -620,7 +620,7 @@ private bool ParseArgumentList(ArgumentInfo info, string[] strs, object destinat
620620
}
621621
Contracts.AssertValue(arg);
622622
Contracts.Assert(arg != info.ArgDef);
623-
Contracts.Assert(0 <= arg.Index & arg.Index < info.Args.Length);
623+
Contracts.Assert(0 <= arg.Index && arg.Index < info.Args.Length);
624624
if (tag != null && !arg.IsTaggedCollection)
625625
{
626626
hadError = true;

src/Microsoft.ML.Core/Data/ModelHeader.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -517,7 +517,7 @@ public static bool TryValidate(ref ModelHeader header, BinaryReader reader, long
517517

518518
long offsetPrev = offset;
519519
offset = offsets[i];
520-
Contracts.CheckDecode(offsetPrev <= offset & offset <= header.CbStringChars);
520+
Contracts.CheckDecode(offsetPrev <= offset && offset <= header.CbStringChars);
521521
Contracts.CheckDecode(offset % sizeof(char) == 0);
522522
long cch = (offset - offsetPrev) / sizeof(char);
523523
Contracts.CheckDecode(cch < int.MaxValue);

src/Microsoft.ML.Core/Utilities/ArrayUtils.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,25 +48,25 @@ public static int FindIndexSorted(int[] input, int min, int lim, int value)
4848
/// </summary>
4949
public static int FindIndexSorted(ReadOnlySpan<int> input, int min, int lim, int value)
5050
{
51-
Debug.Assert(0 <= min & min <= lim & lim <= input.Length);
51+
Debug.Assert(0 <= min && min <= lim && lim <= input.Length);
5252

5353
int minCur = min;
5454
int limCur = lim;
5555
while (minCur < limCur)
5656
{
5757
int mid = (int)(((uint)minCur + (uint)limCur) / 2);
58-
Debug.Assert(minCur <= mid & mid < limCur);
58+
Debug.Assert(minCur <= mid && mid < limCur);
5959

6060
if (input[mid] >= value)
6161
limCur = mid;
6262
else
6363
minCur = mid + 1;
6464

65-
Debug.Assert(min <= minCur & minCur <= limCur & limCur <= lim);
65+
Debug.Assert(min <= minCur && minCur <= limCur && limCur <= lim);
6666
Debug.Assert(minCur == min || input[minCur - 1] < value);
6767
Debug.Assert(limCur == lim || input[limCur] >= value);
6868
}
69-
Debug.Assert(min <= minCur & minCur == limCur & limCur <= lim);
69+
Debug.Assert(min <= minCur && minCur == limCur && limCur <= lim);
7070
Debug.Assert(minCur == min || input[minCur - 1] < value);
7171
Debug.Assert(limCur == lim || input[limCur] >= value);
7272

src/Microsoft.ML.Core/Utilities/BigArray.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ public T this[long index]
9090
public BigArray(long size = 0)
9191
{
9292
// Verifies the preconditional invariant that BlockSize is a power of two.
93-
Contracts.Assert(BlockSize > 1 & (BlockSize & (BlockSize - 1)) == 0, "Block size is not a power of two.");
93+
Contracts.Assert(BlockSize > 1 && (BlockSize & (BlockSize - 1)) == 0, "Block size is not a power of two.");
9494

9595
Contracts.CheckParam(size >= 0, nameof(size), "Must be non-negative.");
9696
if (size == 0)
@@ -105,7 +105,7 @@ public BigArray(long size = 0)
105105
int blockCount = (int)longBlockCount;
106106
int lastBlockSize = (int)(((size - 1) & BlockSizeMinusOne) + 1);
107107
Contracts.Assert(blockCount > 0);
108-
Contracts.Assert(0 < lastBlockSize & lastBlockSize <= BlockSize);
108+
Contracts.Assert(0 < lastBlockSize && lastBlockSize <= BlockSize);
109109
_length = size;
110110
_entries = new T[blockCount][];
111111
for (int i = 0; i < blockCount - 1; i++)
@@ -223,10 +223,10 @@ public void Resize(long newLength)
223223
}
224224

225225
var longBlockCount = ((newLength - 1) >> BlockSizeBits) + 1;
226-
Contracts.Assert(0 < longBlockCount & longBlockCount <= Utils.ArrayMaxSize);
226+
Contracts.Assert(0 < longBlockCount && longBlockCount <= Utils.ArrayMaxSize);
227227
int newBlockCount = (int)longBlockCount;
228228
int newLastBlockLength = (int)(((newLength - 1) & BlockSizeMinusOne) + 1);
229-
Contracts.Assert(0 < newLastBlockLength & newLastBlockLength <= BlockSize);
229+
Contracts.Assert(0 < newLastBlockLength && newLastBlockLength <= BlockSize);
230230

231231
if (_length == 0)
232232
{
@@ -243,12 +243,12 @@ public void Resize(long newLength)
243243
Contracts.Assert(curBlockCount > 0);
244244
int curLastBlockSize = Utils.Size(_entries[curBlockCount - 1]);
245245
int curLastBlockLength = (int)(((_length - 1) & BlockSizeMinusOne) + 1);
246-
Contracts.Assert(0 < curLastBlockLength & curLastBlockLength <= curLastBlockSize & curLastBlockSize <= BlockSize);
246+
Contracts.Assert(0 < curLastBlockLength && curLastBlockLength <= curLastBlockSize && curLastBlockSize <= BlockSize);
247247

248248
if (newLength < _length)
249249
{
250250
// Shrink to a smaller array
251-
Contracts.Assert(newBlockCount < curBlockCount | (newBlockCount == curBlockCount & newLastBlockLength < curLastBlockLength));
251+
Contracts.Assert(newBlockCount < curBlockCount || (newBlockCount == curBlockCount && newLastBlockLength < curLastBlockLength));
252252
Array.Resize(ref _entries, newBlockCount);
253253
Array.Resize(ref _entries[newBlockCount - 1], newLastBlockLength);
254254
}

src/Microsoft.ML.Core/Utilities/DoubleParser.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -445,7 +445,7 @@ public static bool TryParse(ReadOnlySpan<char> span, out Double value, out int i
445445
}
446446

447447
// Multiply by the exponent adjustment.
448-
Contracts.Assert(0 < e2 & e2 < 0x7FF);
448+
Contracts.Assert(0 < e2 && e2 < 0x7FF);
449449
mul = (ulong)e2 << 52;
450450
unsafe { value *= *(Double*)&mul; }
451451

@@ -548,7 +548,7 @@ private static bool TryParseSpecial(ReadOnlySpan<char> span, ref int ich, out Si
548548

549549
private static bool TryParseCore(ReadOnlySpan<char> span, ref int ich, ref bool neg, ref ulong num, ref long exp, OptionFlags flags = OptionFlags.Default)
550550
{
551-
Contracts.Assert(0 <= ich & ich <= span.Length);
551+
Contracts.Assert(0 <= ich && ich <= span.Length);
552552
Contracts.Assert(!neg);
553553
Contracts.Assert(num == 0);
554554
Contracts.Assert(exp == 0);
@@ -949,7 +949,7 @@ static DoubleParser()
949949

950950
Double dbl = (Double)(ulong)man;
951951
int e2 = _mpe10e2[i] + (0x3FF - 63);
952-
Contracts.Assert(0 < e2 & e2 < 0x7FF);
952+
Contracts.Assert(0 < e2 && e2 < 0x7FF);
953953
ulong mul = (ulong)e2 << 52;
954954
unsafe { dbl *= *(Double*)&mul; }
955955
_mpe10Dbl[i] = dbl;
@@ -965,7 +965,7 @@ static DoubleParser()
965965
{
966966
Double dbl = _mpne10Man[i];
967967
int e2 = -_mpne10ne2[i] + (0x3FF - 64);
968-
Contracts.Assert(0 < e2 & e2 < 0x7FF);
968+
Contracts.Assert(0 < e2 && e2 < 0x7FF);
969969
ulong mul = (ulong)e2 << 52;
970970
unsafe { dbl *= *(Double*)&mul; }
971971
_mpne10Dbl[i] = dbl;

src/Microsoft.ML.Core/Utilities/FixedSizeQueue.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ public FixedSizeQueue(int capacity)
3131
private void AssertValid()
3232
{
3333
Contracts.Assert(Utils.Size(_array) >= 0);
34-
Contracts.Assert(0 <= _startIndex & _startIndex < _array.Length);
35-
Contracts.Assert(0 <= _count & _count <= _array.Length);
34+
Contracts.Assert(0 <= _startIndex && _startIndex < _array.Length);
35+
Contracts.Assert(0 <= _count && _count <= _array.Length);
3636
}
3737

3838
public int Count
@@ -67,7 +67,7 @@ public T this[int index]
6767
get
6868
{
6969
AssertValid();
70-
Contracts.Assert(index >= 0 & index < _count);
70+
Contracts.Assert(index >= 0 && index < _count);
7171
return _array[(_startIndex + index) % _array.Length];
7272
}
7373
}

src/Microsoft.ML.Core/Utilities/HashArray.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ private void AssertValid()
6262
Contracts.AssertValue(_rgit);
6363
Contracts.AssertNonEmpty(_rgit);
6464

65-
Contracts.Assert(0 <= _ct & _ct <= Utils.Size(_entries));
65+
Contracts.Assert(0 <= _ct && _ct <= Utils.Size(_entries));
6666

6767
// The number of buckets should be at least the number of items, unless we're reached the
6868
// biggest number of buckets allowed.

src/Microsoft.ML.Core/Utilities/Hashing.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ public static uint MurmurHashV2(uint hash, ReadOnlySpan<char> span, bool toUpper
274274
/// </summary>
275275
public static uint MurmurHash(uint hash, StringBuilder data, int ichMin, int ichLim, bool toUpper = false)
276276
{
277-
Contracts.Assert(0 <= ichMin & ichMin <= ichLim & ichLim <= Utils.Size(data));
277+
Contracts.Assert(0 <= ichMin && ichMin <= ichLim && ichLim <= Utils.Size(data));
278278

279279
uint seed = hash;
280280

src/Microsoft.ML.Core/Utilities/MathUtils.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -736,8 +736,8 @@ public static Double CosineSimilarity(ReadOnlySpan<float> a, ReadOnlySpan<float>
736736
{
737737
const Double epsilon = 1e-12f;
738738
Contracts.Assert(len > 0);
739-
Contracts.Assert(aIdx >= 0 & aIdx <= a.Length - len);
740-
Contracts.Assert(bIdx >= 0 & bIdx <= b.Length - len);
739+
Contracts.Assert(aIdx >= 0 && aIdx <= a.Length - len);
740+
Contracts.Assert(bIdx >= 0 && bIdx <= b.Length - len);
741741

742742
Double ab = 0;
743743
Double a2 = 0;
@@ -751,7 +751,7 @@ public static Double CosineSimilarity(ReadOnlySpan<float> a, ReadOnlySpan<float>
751751
}
752752

753753
Double similarity = ab / (Math.Sqrt(a2 * b2) + epsilon);
754-
Contracts.Assert(-1 - epsilon <= similarity & similarity <= 1 + epsilon);
754+
Contracts.Assert(-1 - epsilon <= similarity && similarity <= 1 + epsilon);
755755
if (Math.Abs(similarity) > 1)
756756
return similarity > 1 ? 1 : -1;
757757

0 commit comments

Comments
 (0)