Skip to content

Commit

Permalink
Use short-circuit logic (#5824)
Browse files Browse the repository at this point in the history
  • Loading branch information
feiyun0112 committed Jun 17, 2021
1 parent b3a3c66 commit ff01708
Show file tree
Hide file tree
Showing 46 changed files with 195 additions and 195 deletions.
32 changes: 16 additions & 16 deletions src/Microsoft.Data.Analysis/TextFieldParser.cs
Expand Up @@ -199,7 +199,7 @@ public bool EndOfData
{
return _endOfData;
}
if ((_reader == null) | (_buffer == null))
if ((_reader == null) || (_buffer == null))
{
_endOfData = true;
return true;
Expand All @@ -217,7 +217,7 @@ public long LineNumber
{
get
{
if (_lineNumber != -1 && ((_reader.Peek() == -1) & (_position == _charsRead)))
if (_lineNumber != -1 && ((_reader.Peek() == -1) && (_position == _charsRead)))
{
// Side effect of a property. Not great. Just leaving it in for now.
CloseReader();
Expand Down Expand Up @@ -405,7 +405,7 @@ public void SetFieldWidths(params int[] fieldWidths)

public string ReadLine()
{
if ((_reader == null) | (_buffer == null))
if ((_reader == null) || (_buffer == null))
{
return null;
}
Expand All @@ -424,7 +424,7 @@ public string ReadLine()

public string[] ReadFields()
{
if ((_reader == null) | (_buffer == null))
if ((_reader == null) || (_buffer == null))
{
return null;
}
Expand Down Expand Up @@ -453,7 +453,7 @@ public string PeekChars(int numberOfChars)
throw new ArgumentException(string.Format(Strings.PositiveNumberOfCharacters, nameof(numberOfChars)));
}

if ((_reader == null) | (_buffer == null))
if ((_reader == null) || (_buffer == null))
{
return null;
}
Expand Down Expand Up @@ -482,7 +482,7 @@ public string PeekChars(int numberOfChars)

public string ReadToEnd()
{
if ((_reader == null) | (_buffer == null))
if ((_reader == null) || (_buffer == null))
{
return null;
}
Expand Down Expand Up @@ -642,7 +642,7 @@ private int SlideCursorToStartOfBuffer()
{
Debug.Assert(_buffer != null, "There's no buffer");
Debug.Assert(_reader != null, "There's no StreamReader");
Debug.Assert((_position >= 0) & (_position <= _buffer.Length), "The cursor is out of range");
Debug.Assert((_position >= 0) && (_position <= _buffer.Length), "The cursor is out of range");
if (_position > 0)
{
int bufferLength = _buffer.Length;
Expand Down Expand Up @@ -710,7 +710,7 @@ private string PeekNextDataLine()
private string ReadNextLine(ref int cursor, ChangeBufferFunction changeBuffer)
{
Debug.Assert(_buffer != null, "There's no buffer");
Debug.Assert((cursor >= 0) & (cursor <= _charsRead), "The cursor is out of range");
Debug.Assert((cursor >= 0) && (cursor <= _charsRead), "The cursor is out of range");
if (cursor == _charsRead && changeBuffer() == 0)
{
return null;
Expand All @@ -722,7 +722,7 @@ private string ReadNextLine(ref int cursor, ChangeBufferFunction changeBuffer)
for (int i = cursor; i <= _charsRead - 1; i++)
{
char Character = _buffer[i];
if (!(Character.Equals('\r') | Character.Equals('\n')))
if (!(Character.Equals('\r') || Character.Equals('\n')))
{
continue;
}
Expand Down Expand Up @@ -912,16 +912,16 @@ private int GetEndOfLineIndex(string line)
Debug.Assert(length > 0, "A blank line shouldn't be parsed");
if (length == 1)
{
Debug.Assert(!line[0].Equals('\r') & !line[0].Equals('\n'), "A blank line shouldn't be parsed");
Debug.Assert(!line[0].Equals('\r') && !line[0].Equals('\n'), "A blank line shouldn't be parsed");
return length;
}
checked
{
if (line[length - 2].Equals('\r') | line[length - 2].Equals('\n'))
if (line[length - 2].Equals('\r') || line[length - 2].Equals('\n'))
{
return length - 2;
}
if (line[length - 1].Equals('\r') | line[length - 1].Equals('\n'))
if (line[length - 1].Equals('\r') || line[length - 1].Equals('\n'))
{
return length - 1;
}
Expand Down Expand Up @@ -1020,7 +1020,7 @@ private void ValidateAndEscapeDelimiters()

private void ValidateReadyToRead()
{
if (!(_needPropertyCheck | ArrayHasChanged()))
if (!(_needPropertyCheck || ArrayHasChanged()))
{
return;
}
Expand All @@ -1041,7 +1041,7 @@ private void ValidateReadyToRead()
string[] commentTokens = _commentTokens;
foreach (string token in commentTokens)
{
if (token != string.Empty && (_hasFieldsEnclosedInQuotes & (_textFieldType == FieldType.Delimited)) && string.Compare(token.Trim(), "\"", StringComparison.Ordinal) == 0)
if (token != string.Empty && (_hasFieldsEnclosedInQuotes && (_textFieldType == FieldType.Delimited)) && string.Compare(token.Trim(), "\"", StringComparison.Ordinal) == 0)
{
throw new Exception(Strings.IllegalQuoteDelimiter);
}
Expand Down Expand Up @@ -1077,7 +1077,7 @@ private bool ArrayHasChanged()
{
case FieldType.Delimited:
{
Debug.Assert(((_delimitersCopy == null) & (_delimiters == null)) | ((_delimitersCopy != null) & (_delimiters != null)), "Delimiters and copy are not both Nothing or both not Nothing");
Debug.Assert(((_delimitersCopy == null) && (_delimiters == null)) || ((_delimitersCopy != null) && (_delimiters != null)), "Delimiters and copy are not both Nothing or both not Nothing");
if (_delimiters == null)
{
return false;
Expand All @@ -1097,7 +1097,7 @@ private bool ArrayHasChanged()
}
case FieldType.FixedWidth:
{
Debug.Assert(((_fieldWidthsCopy == null) & (_fieldWidths == null)) | ((_fieldWidthsCopy != null) & (_fieldWidths != null)), "FieldWidths and copy are not both Nothing or both not Nothing");
Debug.Assert(((_fieldWidthsCopy == null) && (_fieldWidths == null)) || ((_fieldWidthsCopy != null) && (_fieldWidths != null)), "FieldWidths and copy are not both Nothing or both not Nothing");
if (_fieldWidths == null)
{
return false;
Expand Down
2 changes: 1 addition & 1 deletion src/Microsoft.ML.Core/CommandLine/CmdParser.cs
Expand Up @@ -620,7 +620,7 @@ private bool ParseArgumentList(ArgumentInfo info, string[] strs, object destinat
}
Contracts.AssertValue(arg);
Contracts.Assert(arg != info.ArgDef);
Contracts.Assert(0 <= arg.Index & arg.Index < info.Args.Length);
Contracts.Assert(0 <= arg.Index && arg.Index < info.Args.Length);
if (tag != null && !arg.IsTaggedCollection)
{
hadError = true;
Expand Down
2 changes: 1 addition & 1 deletion src/Microsoft.ML.Core/Data/ModelHeader.cs
Expand Up @@ -517,7 +517,7 @@ public static bool TryValidate(ref ModelHeader header, BinaryReader reader, long

long offsetPrev = offset;
offset = offsets[i];
Contracts.CheckDecode(offsetPrev <= offset & offset <= header.CbStringChars);
Contracts.CheckDecode(offsetPrev <= offset && offset <= header.CbStringChars);
Contracts.CheckDecode(offset % sizeof(char) == 0);
long cch = (offset - offsetPrev) / sizeof(char);
Contracts.CheckDecode(cch < int.MaxValue);
Expand Down
8 changes: 4 additions & 4 deletions src/Microsoft.ML.Core/Utilities/ArrayUtils.cs
Expand Up @@ -48,25 +48,25 @@ public static int FindIndexSorted(int[] input, int min, int lim, int value)
/// </summary>
public static int FindIndexSorted(ReadOnlySpan<int> input, int min, int lim, int value)
{
Debug.Assert(0 <= min & min <= lim & lim <= input.Length);
Debug.Assert(0 <= min && min <= lim && lim <= input.Length);

int minCur = min;
int limCur = lim;
while (minCur < limCur)
{
int mid = (int)(((uint)minCur + (uint)limCur) / 2);
Debug.Assert(minCur <= mid & mid < limCur);
Debug.Assert(minCur <= mid && mid < limCur);

if (input[mid] >= value)
limCur = mid;
else
minCur = mid + 1;

Debug.Assert(min <= minCur & minCur <= limCur & limCur <= lim);
Debug.Assert(min <= minCur && minCur <= limCur && limCur <= lim);
Debug.Assert(minCur == min || input[minCur - 1] < value);
Debug.Assert(limCur == lim || input[limCur] >= value);
}
Debug.Assert(min <= minCur & minCur == limCur & limCur <= lim);
Debug.Assert(min <= minCur && minCur == limCur && limCur <= lim);
Debug.Assert(minCur == min || input[minCur - 1] < value);
Debug.Assert(limCur == lim || input[limCur] >= value);

Expand Down
12 changes: 6 additions & 6 deletions src/Microsoft.ML.Core/Utilities/BigArray.cs
Expand Up @@ -90,7 +90,7 @@ internal sealed class BigArray<T> : IEnumerable<T>
public BigArray(long size = 0)
{
// Verifies the preconditional invariant that BlockSize is a power of two.
Contracts.Assert(BlockSize > 1 & (BlockSize & (BlockSize - 1)) == 0, "Block size is not a power of two.");
Contracts.Assert(BlockSize > 1 && (BlockSize & (BlockSize - 1)) == 0, "Block size is not a power of two.");

Contracts.CheckParam(size >= 0, nameof(size), "Must be non-negative.");
if (size == 0)
Expand All @@ -105,7 +105,7 @@ public BigArray(long size = 0)
int blockCount = (int)longBlockCount;
int lastBlockSize = (int)(((size - 1) & BlockSizeMinusOne) + 1);
Contracts.Assert(blockCount > 0);
Contracts.Assert(0 < lastBlockSize & lastBlockSize <= BlockSize);
Contracts.Assert(0 < lastBlockSize && lastBlockSize <= BlockSize);
_length = size;
_entries = new T[blockCount][];
for (int i = 0; i < blockCount - 1; i++)
Expand Down Expand Up @@ -223,10 +223,10 @@ public void Resize(long newLength)
}

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

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

if (newLength < _length)
{
// Shrink to a smaller array
Contracts.Assert(newBlockCount < curBlockCount | (newBlockCount == curBlockCount & newLastBlockLength < curLastBlockLength));
Contracts.Assert(newBlockCount < curBlockCount || (newBlockCount == curBlockCount && newLastBlockLength < curLastBlockLength));
Array.Resize(ref _entries, newBlockCount);
Array.Resize(ref _entries[newBlockCount - 1], newLastBlockLength);
}
Expand Down
8 changes: 4 additions & 4 deletions src/Microsoft.ML.Core/Utilities/DoubleParser.cs
Expand Up @@ -445,7 +445,7 @@ public static bool TryParse(ReadOnlySpan<char> span, out Double value, out int i
}

// Multiply by the exponent adjustment.
Contracts.Assert(0 < e2 & e2 < 0x7FF);
Contracts.Assert(0 < e2 && e2 < 0x7FF);
mul = (ulong)e2 << 52;
unsafe { value *= *(Double*)&mul; }

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

private static bool TryParseCore(ReadOnlySpan<char> span, ref int ich, ref bool neg, ref ulong num, ref long exp, OptionFlags flags = OptionFlags.Default)
{
Contracts.Assert(0 <= ich & ich <= span.Length);
Contracts.Assert(0 <= ich && ich <= span.Length);
Contracts.Assert(!neg);
Contracts.Assert(num == 0);
Contracts.Assert(exp == 0);
Expand Down Expand Up @@ -949,7 +949,7 @@ static DoubleParser()

Double dbl = (Double)(ulong)man;
int e2 = _mpe10e2[i] + (0x3FF - 63);
Contracts.Assert(0 < e2 & e2 < 0x7FF);
Contracts.Assert(0 < e2 && e2 < 0x7FF);
ulong mul = (ulong)e2 << 52;
unsafe { dbl *= *(Double*)&mul; }
_mpe10Dbl[i] = dbl;
Expand All @@ -965,7 +965,7 @@ static DoubleParser()
{
Double dbl = _mpne10Man[i];
int e2 = -_mpne10ne2[i] + (0x3FF - 64);
Contracts.Assert(0 < e2 & e2 < 0x7FF);
Contracts.Assert(0 < e2 && e2 < 0x7FF);
ulong mul = (ulong)e2 << 52;
unsafe { dbl *= *(Double*)&mul; }
_mpne10Dbl[i] = dbl;
Expand Down
6 changes: 3 additions & 3 deletions src/Microsoft.ML.Core/Utilities/FixedSizeQueue.cs
Expand Up @@ -31,8 +31,8 @@ public FixedSizeQueue(int capacity)
private void AssertValid()
{
Contracts.Assert(Utils.Size(_array) >= 0);
Contracts.Assert(0 <= _startIndex & _startIndex < _array.Length);
Contracts.Assert(0 <= _count & _count <= _array.Length);
Contracts.Assert(0 <= _startIndex && _startIndex < _array.Length);
Contracts.Assert(0 <= _count && _count <= _array.Length);
}

public int Count
Expand Down Expand Up @@ -67,7 +67,7 @@ public bool IsFull
get
{
AssertValid();
Contracts.Assert(index >= 0 & index < _count);
Contracts.Assert(index >= 0 && index < _count);
return _array[(_startIndex + index) % _array.Length];
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/Microsoft.ML.Core/Utilities/HashArray.cs
Expand Up @@ -62,7 +62,7 @@ private void AssertValid()
Contracts.AssertValue(_rgit);
Contracts.AssertNonEmpty(_rgit);

Contracts.Assert(0 <= _ct & _ct <= Utils.Size(_entries));
Contracts.Assert(0 <= _ct && _ct <= Utils.Size(_entries));

// The number of buckets should be at least the number of items, unless we're reached the
// biggest number of buckets allowed.
Expand Down
2 changes: 1 addition & 1 deletion src/Microsoft.ML.Core/Utilities/Hashing.cs
Expand Up @@ -274,7 +274,7 @@ public static uint MurmurHashV2(uint hash, ReadOnlySpan<char> span, bool toUpper
/// </summary>
public static uint MurmurHash(uint hash, StringBuilder data, int ichMin, int ichLim, bool toUpper = false)
{
Contracts.Assert(0 <= ichMin & ichMin <= ichLim & ichLim <= Utils.Size(data));
Contracts.Assert(0 <= ichMin && ichMin <= ichLim && ichLim <= Utils.Size(data));

uint seed = hash;

Expand Down
6 changes: 3 additions & 3 deletions src/Microsoft.ML.Core/Utilities/MathUtils.cs
Expand Up @@ -736,8 +736,8 @@ public static Double CosineSimilarity(ReadOnlySpan<float> a, ReadOnlySpan<float>
{
const Double epsilon = 1e-12f;
Contracts.Assert(len > 0);
Contracts.Assert(aIdx >= 0 & aIdx <= a.Length - len);
Contracts.Assert(bIdx >= 0 & bIdx <= b.Length - len);
Contracts.Assert(aIdx >= 0 && aIdx <= a.Length - len);
Contracts.Assert(bIdx >= 0 && bIdx <= b.Length - len);

Double ab = 0;
Double a2 = 0;
Expand All @@ -751,7 +751,7 @@ public static Double CosineSimilarity(ReadOnlySpan<float> a, ReadOnlySpan<float>
}

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

Expand Down
2 changes: 1 addition & 1 deletion src/Microsoft.ML.Core/Utilities/NormStr.cs
Expand Up @@ -73,7 +73,7 @@ private void AssertValid()
Contracts.Assert(_rgins.Length == _mask + 1);
Contracts.Assert(Utils.IsPowerOfTwo(_mask + 1));

Contracts.Assert(0 <= _cns & _cns <= Utils.Size(_rgns));
Contracts.Assert(0 <= _cns && _cns <= Utils.Size(_rgns));
Contracts.Assert(Utils.Size(_rgns) == Utils.Size(_rgmeta));
}

Expand Down
4 changes: 2 additions & 2 deletions src/Microsoft.ML.Core/Utilities/Stream.cs
Expand Up @@ -146,7 +146,7 @@ public static void WriteByteArray(this BinaryWriter writer, byte[] values, int c
{
Contracts.AssertValue(writer);
Contracts.AssertValueOrNull(values);
Contracts.Assert(0 <= count & count <= Utils.Size(values));
Contracts.Assert(0 <= count && count <= Utils.Size(values));

writer.Write(count);
writer.Write(values, 0, count);
Expand All @@ -159,7 +159,7 @@ public static void WriteBytesNoCount(this BinaryWriter writer, byte[] values, in
{
Contracts.AssertValue(writer);
Contracts.AssertValueOrNull(values);
Contracts.Assert(0 <= count & count <= Utils.Size(values));
Contracts.Assert(0 <= count && count <= Utils.Size(values));

writer.Write(values, 0, count);
}
Expand Down

0 comments on commit ff01708

Please sign in to comment.