From ff01708399c49822dca5544286a33e4aaaf155c4 Mon Sep 17 00:00:00 2001 From: feiyun0112 Date: Fri, 18 Jun 2021 04:25:26 +0800 Subject: [PATCH] Use short-circuit logic (#5824) --- .../TextFieldParser.cs | 32 ++++++------- .../CommandLine/CmdParser.cs | 2 +- src/Microsoft.ML.Core/Data/ModelHeader.cs | 2 +- src/Microsoft.ML.Core/Utilities/ArrayUtils.cs | 8 ++-- src/Microsoft.ML.Core/Utilities/BigArray.cs | 12 ++--- .../Utilities/DoubleParser.cs | 8 ++-- .../Utilities/FixedSizeQueue.cs | 6 +-- src/Microsoft.ML.Core/Utilities/HashArray.cs | 2 +- src/Microsoft.ML.Core/Utilities/Hashing.cs | 2 +- src/Microsoft.ML.Core/Utilities/MathUtils.cs | 6 +-- src/Microsoft.ML.Core/Utilities/NormStr.cs | 2 +- src/Microsoft.ML.Core/Utilities/Stream.cs | 4 +- src/Microsoft.ML.Core/Utilities/Utils.cs | 46 +++++++++---------- src/Microsoft.ML.Data/Data/BufferBuilder.cs | 12 ++--- .../DataLoadSave/Binary/BinaryLoader.cs | 4 +- .../DataLoadSave/Database/DatabaseLoader.cs | 2 +- .../DataLoadSave/Text/TextLoader.cs | 6 +-- .../DataLoadSave/Text/TextLoaderParser.cs | 16 +++---- .../DataLoadSave/Transpose/TransposeLoader.cs | 2 +- .../DataView/ArrayDataViewBuilder.cs | 2 +- .../DataView/CacheDataView.cs | 12 ++--- .../DataView/LambdaFilter.cs | 4 +- .../Depricated/Instances/HeaderSchema.cs | 2 +- .../Transforms/GenerateNumberTransform.cs | 10 ++-- .../Transforms/LabelConvertTransform.cs | 2 +- .../Transforms/NormalizeColumnDbl.cs | 46 +++++++++---------- .../Transforms/NormalizeColumnSng.cs | 46 +++++++++---------- .../Transforms/RowShufflingTransformer.cs | 6 +-- .../ValueToKeyMappingTransformerImpl.cs | 2 +- .../ImagePixelExtractor.cs | 2 +- .../VectorToImageTransform.cs | 2 +- .../OlsLinearRegression.cs | 12 ++--- .../Standard/LinearModelParameters.cs | 2 +- .../Standard/SdcaBinary.cs | 10 ++-- .../Standard/SdcaMulticlass.cs | 2 +- .../TimeSeriesUtils.cs | 4 +- .../Expression/CodeGen.cs | 8 ++-- .../Expression/LambdaBinder.cs | 16 +++---- .../Expression/LambdaParser.cs | 2 +- .../Expression/Lexer.cs | 2 +- src/Microsoft.ML.Transforms/GroupTransform.cs | 2 +- .../MissingValueIndicatorTransform.cs | 8 ++-- .../MissingValueReplacing.cs | 4 +- .../OptionalColumnTransform.cs | 2 +- .../Text/NgramHashingTransformer.cs | 2 +- .../Text/StopWordsRemovingTransformer.cs | 4 +- 46 files changed, 195 insertions(+), 195 deletions(-) diff --git a/src/Microsoft.Data.Analysis/TextFieldParser.cs b/src/Microsoft.Data.Analysis/TextFieldParser.cs index e5c279db1d..b72e5a77cf 100644 --- a/src/Microsoft.Data.Analysis/TextFieldParser.cs +++ b/src/Microsoft.Data.Analysis/TextFieldParser.cs @@ -199,7 +199,7 @@ public bool EndOfData { return _endOfData; } - if ((_reader == null) | (_buffer == null)) + if ((_reader == null) || (_buffer == null)) { _endOfData = true; return true; @@ -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(); @@ -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; } @@ -424,7 +424,7 @@ public string ReadLine() public string[] ReadFields() { - if ((_reader == null) | (_buffer == null)) + if ((_reader == null) || (_buffer == null)) { return null; } @@ -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; } @@ -482,7 +482,7 @@ public string PeekChars(int numberOfChars) public string ReadToEnd() { - if ((_reader == null) | (_buffer == null)) + if ((_reader == null) || (_buffer == null)) { return null; } @@ -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; @@ -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; @@ -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; } @@ -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; } @@ -1020,7 +1020,7 @@ private void ValidateAndEscapeDelimiters() private void ValidateReadyToRead() { - if (!(_needPropertyCheck | ArrayHasChanged())) + if (!(_needPropertyCheck || ArrayHasChanged())) { return; } @@ -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); } @@ -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; @@ -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; diff --git a/src/Microsoft.ML.Core/CommandLine/CmdParser.cs b/src/Microsoft.ML.Core/CommandLine/CmdParser.cs index d782208ef9..07e7c39d60 100644 --- a/src/Microsoft.ML.Core/CommandLine/CmdParser.cs +++ b/src/Microsoft.ML.Core/CommandLine/CmdParser.cs @@ -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; diff --git a/src/Microsoft.ML.Core/Data/ModelHeader.cs b/src/Microsoft.ML.Core/Data/ModelHeader.cs index 1ca6d4c75b..19850d3f0e 100644 --- a/src/Microsoft.ML.Core/Data/ModelHeader.cs +++ b/src/Microsoft.ML.Core/Data/ModelHeader.cs @@ -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); diff --git a/src/Microsoft.ML.Core/Utilities/ArrayUtils.cs b/src/Microsoft.ML.Core/Utilities/ArrayUtils.cs index bb73eba91b..765f752b31 100644 --- a/src/Microsoft.ML.Core/Utilities/ArrayUtils.cs +++ b/src/Microsoft.ML.Core/Utilities/ArrayUtils.cs @@ -48,25 +48,25 @@ public static int FindIndexSorted(int[] input, int min, int lim, int value) /// public static int FindIndexSorted(ReadOnlySpan 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); diff --git a/src/Microsoft.ML.Core/Utilities/BigArray.cs b/src/Microsoft.ML.Core/Utilities/BigArray.cs index 32b56a3eb7..e68697a1a5 100644 --- a/src/Microsoft.ML.Core/Utilities/BigArray.cs +++ b/src/Microsoft.ML.Core/Utilities/BigArray.cs @@ -90,7 +90,7 @@ internal sealed class BigArray : IEnumerable 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) @@ -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++) @@ -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) { @@ -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); } diff --git a/src/Microsoft.ML.Core/Utilities/DoubleParser.cs b/src/Microsoft.ML.Core/Utilities/DoubleParser.cs index 9e35b7fb14..d8b59afd52 100644 --- a/src/Microsoft.ML.Core/Utilities/DoubleParser.cs +++ b/src/Microsoft.ML.Core/Utilities/DoubleParser.cs @@ -445,7 +445,7 @@ public static bool TryParse(ReadOnlySpan 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; } @@ -548,7 +548,7 @@ private static bool TryParseSpecial(ReadOnlySpan span, ref int ich, out Si private static bool TryParseCore(ReadOnlySpan 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); @@ -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; @@ -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; diff --git a/src/Microsoft.ML.Core/Utilities/FixedSizeQueue.cs b/src/Microsoft.ML.Core/Utilities/FixedSizeQueue.cs index f63594afd1..c4b510d18d 100644 --- a/src/Microsoft.ML.Core/Utilities/FixedSizeQueue.cs +++ b/src/Microsoft.ML.Core/Utilities/FixedSizeQueue.cs @@ -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 @@ -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]; } } diff --git a/src/Microsoft.ML.Core/Utilities/HashArray.cs b/src/Microsoft.ML.Core/Utilities/HashArray.cs index d32d590975..087de56e4c 100644 --- a/src/Microsoft.ML.Core/Utilities/HashArray.cs +++ b/src/Microsoft.ML.Core/Utilities/HashArray.cs @@ -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. diff --git a/src/Microsoft.ML.Core/Utilities/Hashing.cs b/src/Microsoft.ML.Core/Utilities/Hashing.cs index d23f6d1a9a..6215a9bbcd 100644 --- a/src/Microsoft.ML.Core/Utilities/Hashing.cs +++ b/src/Microsoft.ML.Core/Utilities/Hashing.cs @@ -274,7 +274,7 @@ public static uint MurmurHashV2(uint hash, ReadOnlySpan span, bool toUpper /// 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; diff --git a/src/Microsoft.ML.Core/Utilities/MathUtils.cs b/src/Microsoft.ML.Core/Utilities/MathUtils.cs index 258f9db212..54f5fc4f99 100644 --- a/src/Microsoft.ML.Core/Utilities/MathUtils.cs +++ b/src/Microsoft.ML.Core/Utilities/MathUtils.cs @@ -736,8 +736,8 @@ public static Double CosineSimilarity(ReadOnlySpan a, ReadOnlySpan { 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; @@ -751,7 +751,7 @@ public static Double CosineSimilarity(ReadOnlySpan a, ReadOnlySpan } 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; diff --git a/src/Microsoft.ML.Core/Utilities/NormStr.cs b/src/Microsoft.ML.Core/Utilities/NormStr.cs index f130883179..4fae0d5f78 100644 --- a/src/Microsoft.ML.Core/Utilities/NormStr.cs +++ b/src/Microsoft.ML.Core/Utilities/NormStr.cs @@ -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)); } diff --git a/src/Microsoft.ML.Core/Utilities/Stream.cs b/src/Microsoft.ML.Core/Utilities/Stream.cs index 27ac7c2328..218ceb5962 100644 --- a/src/Microsoft.ML.Core/Utilities/Stream.cs +++ b/src/Microsoft.ML.Core/Utilities/Stream.cs @@ -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); @@ -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); } diff --git a/src/Microsoft.ML.Core/Utilities/Utils.cs b/src/Microsoft.ML.Core/Utilities/Utils.cs index 698a97fee1..6cc8dcc1c1 100644 --- a/src/Microsoft.ML.Core/Utilities/Utils.cs +++ b/src/Microsoft.ML.Core/Utilities/Utils.cs @@ -285,25 +285,25 @@ public static int FindIndexSorted(this ReadOnlySpan input, int min, int lim public static int FindIndexSorted(this IList input, int min, int lim, int value) { Contracts.AssertValue(input); - Contracts.Assert(0 <= min & min <= lim & lim <= input.Count); + Contracts.Assert(0 <= min && min <= lim && lim <= input.Count); int minCur = min; int limCur = lim; while (minCur < limCur) { int mid = (int)(((uint)minCur + (uint)limCur) / 2); - Contracts.Assert(minCur <= mid & mid < limCur); + Contracts.Assert(minCur <= mid && mid < limCur); if (input[mid] >= value) limCur = mid; else minCur = mid + 1; - Contracts.Assert(min <= minCur & minCur <= limCur & limCur <= lim); + Contracts.Assert(min <= minCur && minCur <= limCur && limCur <= lim); Contracts.Assert(minCur == min || input[minCur - 1] < value); Contracts.Assert(limCur == lim || input[limCur] >= value); } - Contracts.Assert(min <= minCur & minCur == limCur & limCur <= lim); + Contracts.Assert(min <= minCur && minCur == limCur && limCur <= lim); Contracts.Assert(minCur == min || input[minCur - 1] < value); Contracts.Assert(limCur == lim || input[limCur] >= value); @@ -319,7 +319,7 @@ public static int FindIndexSorted(this IList input, int min, int lim, int v public static int FindIndexSorted(this IList input, int min, int lim, float value) { Contracts.AssertValue(input); - Contracts.Assert(0 <= min & min <= lim & lim <= input.Count); + Contracts.Assert(0 <= min && min <= lim && lim <= input.Count); Contracts.Assert(!float.IsNaN(value)); int minCur = min; @@ -327,7 +327,7 @@ public static int FindIndexSorted(this IList input, int min, int lim, flo while (minCur < limCur) { int mid = (int)(((uint)minCur + (uint)limCur) / 2); - Contracts.Assert(minCur <= mid & mid < limCur); + Contracts.Assert(minCur <= mid && mid < limCur); Contracts.Assert(!float.IsNaN(input[mid])); if (input[mid] >= value) @@ -335,11 +335,11 @@ public static int FindIndexSorted(this IList input, int min, int lim, flo else minCur = mid + 1; - Contracts.Assert(min <= minCur & minCur <= limCur & limCur <= lim); + Contracts.Assert(min <= minCur && minCur <= limCur && limCur <= lim); Contracts.Assert(minCur == min || input[minCur - 1] < value); Contracts.Assert(limCur == lim || input[limCur] >= value); } - Contracts.Assert(min <= minCur & minCur == limCur & limCur <= lim); + Contracts.Assert(min <= minCur && minCur == limCur && limCur <= lim); Contracts.Assert(minCur == min || input[minCur - 1] < value); Contracts.Assert(limCur == lim || input[limCur] >= value); @@ -355,7 +355,7 @@ public static int FindIndexSorted(this IList input, int min, int lim, flo public static int FindIndexSorted(this Double[] input, int min, int lim, Double value) { Contracts.AssertValue(input); - Contracts.Assert(0 <= min & min <= lim & lim <= input.Length); + Contracts.Assert(0 <= min && min <= lim && lim <= input.Length); Contracts.Assert(!Double.IsNaN(value)); int minCur = min; @@ -363,7 +363,7 @@ public static int FindIndexSorted(this Double[] input, int min, int lim, Double while (minCur < limCur) { int mid = (int)(((uint)minCur + (uint)limCur) / 2); - Contracts.Assert(minCur <= mid & mid < limCur); + Contracts.Assert(minCur <= mid && mid < limCur); Contracts.Assert(!Double.IsNaN(input[mid])); if (input[mid] >= value) @@ -371,11 +371,11 @@ public static int FindIndexSorted(this Double[] input, int min, int lim, Double else minCur = mid + 1; - Contracts.Assert(min <= minCur & minCur <= limCur & limCur <= lim); + Contracts.Assert(min <= minCur && minCur <= limCur && limCur <= lim); Contracts.Assert(minCur == min || input[minCur - 1] < value); Contracts.Assert(limCur == lim || input[limCur] >= value); } - Contracts.Assert(min <= minCur & minCur == limCur & limCur <= lim); + Contracts.Assert(min <= minCur && minCur == limCur && limCur <= lim); Contracts.Assert(minCur == min || input[minCur - 1] < value); Contracts.Assert(limCur == lim || input[limCur] >= value); @@ -390,25 +390,25 @@ public static int FindIndexSorted(this Double[] input, int min, int lim, Double public static int FindIndexSorted(this T[] input, int min, int lim, Func func) { Contracts.AssertValue(input); - Contracts.Assert(0 <= min & min <= lim & lim <= input.Length); + Contracts.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); - Contracts.Assert(minCur <= mid & mid < limCur); + Contracts.Assert(minCur <= mid && mid < limCur); if (func(input[mid])) limCur = mid; else minCur = mid + 1; - Contracts.Assert(min <= minCur & minCur <= limCur & limCur <= lim); + Contracts.Assert(min <= minCur && minCur <= limCur && limCur <= lim); Contracts.Assert(minCur == min || !func(input[minCur - 1])); Contracts.Assert(limCur == lim || func(input[limCur])); } - Contracts.Assert(min <= minCur & minCur == limCur & limCur <= lim); + Contracts.Assert(min <= minCur && minCur == limCur && limCur <= lim); Contracts.Assert(minCur == min || !func(input[minCur - 1])); Contracts.Assert(limCur == lim || func(input[limCur])); @@ -423,25 +423,25 @@ public static int FindIndexSorted(this T[] input, int min, int lim, Func(this T[] input, int min, int lim, Func func, TValue value) { Contracts.AssertValue(input); - Contracts.Assert(0 <= min & min <= lim & lim <= input.Length); + Contracts.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); - Contracts.Assert(minCur <= mid & mid < limCur); + Contracts.Assert(minCur <= mid && mid < limCur); if (func(input[mid], value)) limCur = mid; else minCur = mid + 1; - Contracts.Assert(min <= minCur & minCur <= limCur & limCur <= lim); + Contracts.Assert(min <= minCur && minCur <= limCur && limCur <= lim); Contracts.Assert(minCur == min || !func(input[minCur - 1], value)); Contracts.Assert(limCur == lim || func(input[limCur], value)); } - Contracts.Assert(min <= minCur & minCur == limCur & limCur <= lim); + Contracts.Assert(min <= minCur && minCur == limCur && limCur <= lim); Contracts.Assert(minCur == min || !func(input[minCur - 1], value)); Contracts.Assert(limCur == lim || func(input[limCur], value)); @@ -460,7 +460,7 @@ public static int[] GetIdentityPermutation(int size) public static void FillIdentity(Span a, int lim) { - Contracts.Assert(0 <= lim & lim <= a.Length); + Contracts.Assert(0 <= lim && lim <= a.Length); for (int i = 0; i < lim; ++i) a[i] = i; @@ -491,8 +491,8 @@ public static int[] InvertPermutation(int[] perm) for (int i = 0; i < perm.Length; i++) { int j = perm[i]; - Contracts.Assert(0 <= j & j < perm.Length); - Contracts.Assert(res[j] == 0 & (j != perm[0] | i == 0)); + Contracts.Assert(0 <= j && j < perm.Length); + Contracts.Assert(res[j] == 0 && (j != perm[0] || i == 0)); res[j] = i; } return res; diff --git a/src/Microsoft.ML.Data/Data/BufferBuilder.cs b/src/Microsoft.ML.Data/Data/BufferBuilder.cs index 003e8aa6ad..fce56d89a5 100644 --- a/src/Microsoft.ML.Data/Data/BufferBuilder.cs +++ b/src/Microsoft.ML.Data/Data/BufferBuilder.cs @@ -73,7 +73,7 @@ private void AssertValid() Contracts.Assert(_count >= 0); Contracts.AssertValue(_values); Contracts.Assert(_values.Length >= _count); - Contracts.Assert(0 <= _ifeatCur & 0 <= _cfeatCur & _ifeatCur <= _length - _cfeatCur); + Contracts.Assert(0 <= _ifeatCur && 0 <= _cfeatCur && _ifeatCur <= _length - _cfeatCur); if (_dense) Contracts.Assert(_count == _length); @@ -84,7 +84,7 @@ private void AssertValid() Contracts.Assert(Utils.Size(_indices) >= _count); // If we have no more than InsertThreshold items, we always keep things sorted. - Contracts.Assert(_sorted | _count > InsertThreshold); + Contracts.Assert(_sorted || _count > InsertThreshold); } #endif } @@ -142,7 +142,7 @@ private void ResetImpl(int length, bool dense) private void SetActiveRangeImpl(int ifeat, int cfeat) { AssertValid(); - Contracts.Assert(0 <= ifeat & 0 <= cfeat & ifeat <= _length - cfeat); + Contracts.Assert(0 <= ifeat && 0 <= cfeat && ifeat <= _length - cfeat); _ifeatCur = ifeat; _cfeatCur = cfeat; AssertValid(); @@ -155,7 +155,7 @@ private void SetActiveRangeImpl(int ifeat, int cfeat) public void AddFeature(int index, T value) { AssertValid(); - Contracts.Assert(0 <= index & index < _cfeatCur); + Contracts.Assert(0 <= index && index < _cfeatCur); // Ignore default values. if (_comb.IsDefault(value)) @@ -301,7 +301,7 @@ private void SortAndSumDups() } ivDst++; } - Contracts.Assert(0 < ivDst & ivDst <= _count); + Contracts.Assert(0 < ivDst && ivDst <= _count); _count = ivDst; _sorted = true; AssertValid(); @@ -347,7 +347,7 @@ private void MakeDense() public bool TryGetFeature(int index, out T v) { AssertValid(); - Contracts.Assert(0 <= index & index < _cfeatCur); + Contracts.Assert(0 <= index && index < _cfeatCur); int ifeat = index + _ifeatCur; if (_dense) diff --git a/src/Microsoft.ML.Data/DataLoadSave/Binary/BinaryLoader.cs b/src/Microsoft.ML.Data/DataLoadSave/Binary/BinaryLoader.cs index 94e6f79720..ec9703efb6 100644 --- a/src/Microsoft.ML.Data/DataLoadSave/Binary/BinaryLoader.cs +++ b/src/Microsoft.ML.Data/DataLoadSave/Binary/BinaryLoader.cs @@ -1460,7 +1460,7 @@ protected ReadPipe(Cursor parent, int columnIndex) { Contracts.AssertValue(parent); Parent = parent; - Ectx.Assert(0 <= columnIndex & columnIndex < Utils.Size(parent._actives)); + Ectx.Assert(0 <= columnIndex && columnIndex < Utils.Size(parent._actives)); ColumnIndex = columnIndex; } @@ -1535,7 +1535,7 @@ private sealed class Block public Block(long blockSequence, long min, long lim) { Contracts.Assert(blockSequence >= 0); - Contracts.Assert(0 <= min & min <= lim); + Contracts.Assert(0 <= min && min <= lim); Contracts.Assert(lim - min <= int.MaxValue); BlockSequence = blockSequence; RowIndexMin = min; diff --git a/src/Microsoft.ML.Data/DataLoadSave/Database/DatabaseLoader.cs b/src/Microsoft.ML.Data/DataLoadSave/Database/DatabaseLoader.cs index febe9d71ff..930edd32b6 100644 --- a/src/Microsoft.ML.Data/DataLoadSave/Database/DatabaseLoader.cs +++ b/src/Microsoft.ML.Data/DataLoadSave/Database/DatabaseLoader.cs @@ -365,7 +365,7 @@ public sealed class Options public Segment(int min, int lim, bool forceVector) { - Contracts.Assert(0 <= min & min < lim); + Contracts.Assert(0 <= min && min < lim); Name = null; Min = min; Lim = lim; diff --git a/src/Microsoft.ML.Data/DataLoadSave/Text/TextLoader.cs b/src/Microsoft.ML.Data/DataLoadSave/Text/TextLoader.cs index 117cc6e075..551e99bbbc 100644 --- a/src/Microsoft.ML.Data/DataLoadSave/Text/TextLoader.cs +++ b/src/Microsoft.ML.Data/DataLoadSave/Text/TextLoader.cs @@ -581,7 +581,7 @@ internal struct Segment /// public Segment(int min, int lim, bool forceVector) { - Contracts.Assert(0 <= min & min < lim & lim <= SrcLim); + Contracts.Assert(0 <= min && min < lim && lim <= SrcLim); Min = min; Lim = lim; ForceVector = forceVector; @@ -592,7 +592,7 @@ public Segment(int min, int lim, bool forceVector) /// public Segment(int min) { - Contracts.Assert(0 <= min & min < SrcLim); + Contracts.Assert(0 <= min && min < SrcLim); Min = min; Lim = SrcLim; ForceVector = true; @@ -722,7 +722,7 @@ public Bindings(TextLoader parent, Column[] cols, IMultiStreamSource headerFile, } int inputSize = parent._inputSize; - ch.Assert(0 <= inputSize & inputSize < SrcLim); + ch.Assert(0 <= inputSize && inputSize < SrcLim); List> lines = null; if (headerFile != null) Cursor.GetSomeLines(headerFile, 1, parent.ReadMultilines, parent._separators, parent._escapeChar, ref lines); diff --git a/src/Microsoft.ML.Data/DataLoadSave/Text/TextLoaderParser.cs b/src/Microsoft.ML.Data/DataLoadSave/Text/TextLoaderParser.cs index 33dba6a843..23e382c5e3 100644 --- a/src/Microsoft.ML.Data/DataLoadSave/Text/TextLoaderParser.cs +++ b/src/Microsoft.ML.Data/DataLoadSave/Text/TextLoaderParser.cs @@ -117,14 +117,14 @@ private ValueCreatorCache(DoubleParser.OptionFlags doubleParserOptionFlags = Dou public Func GetCreatorOne(InternalDataKind kind) { int index = kind.ToIndex(); - Contracts.Assert(0 <= index & index < _creatorsOne.Length); + Contracts.Assert(0 <= index && index < _creatorsOne.Length); return _creatorsOne[index]; } public Func GetCreatorVec(InternalDataKind kind) { int index = kind.ToIndex(); - Contracts.Assert(0 <= index & index < _creatorsOne.Length); + Contracts.Assert(0 <= index && index < _creatorsOne.Length); return _creatorsVec[index]; } } @@ -357,7 +357,7 @@ public void Reset(int size) public bool Consume(int index, ref ReadOnlyMemory text) { AssertValid(); - Contracts.Assert(_indexPrev < index & index < _size); + Contracts.Assert(_indexPrev < index && index < _size); TItem tmp = default(TItem); bool f = _conv(in text, out tmp); @@ -619,7 +619,7 @@ public void AssertValid() { Contracts.AssertValue(Spans); Contracts.AssertValue(Indices); - Contracts.Assert(0 <= Count & Count <= Indices.Length & Indices.Length <= Spans.Length); + Contracts.Assert(0 <= Count && Count <= Indices.Length && Indices.Length <= Spans.Length); } [Conditional("DEBUG")] @@ -814,7 +814,7 @@ public static void ParseSlotNames(TextLoader parent, ReadOnlyMemory textHe for (; isrc < isrcLim; isrc++) { var srcCur = header.Indices[isrc]; - Contracts.Assert(min <= srcCur & srcCur < lim); + Contracts.Assert(min <= srcCur && srcCur < lim); bldr.AddFeature(indexBase + srcCur, ReadOnlyMemoryUtils.TrimWhiteSpace(header.Spans[isrc])); } } @@ -862,7 +862,7 @@ public void ParseRow(RowSet rows, int irow, Helper helper, bool[] active, string Contracts.AssertValue(rows); Contracts.Assert(irow >= 0); Contracts.Assert(helper is HelperImpl); - Contracts.Assert(active == null | Utils.Size(active) == _infos.Length); + Contracts.Assert(active == null || Utils.Size(active) == _infos.Length); var impl = (HelperImpl)helper; var lineSpan = text.AsMemory(); @@ -1364,7 +1364,7 @@ private bool FetchNextField(ref ScanInfo scan, ReadOnlySpan span) private void ProcessItems(RowSet rows, int irow, bool[] active, FieldSet fields, int srcLim, long line) { - Contracts.Assert(active == null | Utils.Size(active) == _infos.Length); + Contracts.Assert(active == null || Utils.Size(active) == _infos.Length); fields.AssertValid(); Contracts.Assert(0 <= irow && irow < rows.Count); @@ -1427,7 +1427,7 @@ private void ProcessVec(int srcLim, FieldSet fields, ColInfo info, ColumnPipe v, for (; isrc < isrcLim; isrc++) { var srcCur = fields.Indices[isrc]; - Contracts.Assert(min <= srcCur & srcCur < lim); + Contracts.Assert(min <= srcCur && srcCur < lim); if (!v.Consume(irow, indexBase + srcCur, ref fields.Spans[isrc])) { if (!v.HasNA) diff --git a/src/Microsoft.ML.Data/DataLoadSave/Transpose/TransposeLoader.cs b/src/Microsoft.ML.Data/DataLoadSave/Transpose/TransposeLoader.cs index 8a0ee23f1a..e6da0bd083 100644 --- a/src/Microsoft.ML.Data/DataLoadSave/Transpose/TransposeLoader.cs +++ b/src/Microsoft.ML.Data/DataLoadSave/Transpose/TransposeLoader.cs @@ -717,7 +717,7 @@ public override int SlotIndex private Transposer EnsureAndGetTransposer(int col) { - _host.Assert(0 <= col & col < _header.ColumnCount); + _host.Assert(0 <= col && col < _header.ColumnCount); // Used to "fake" row data when we don't actually have it. _host.Assert(!HasRowData); diff --git a/src/Microsoft.ML.Data/DataView/ArrayDataViewBuilder.cs b/src/Microsoft.ML.Data/DataView/ArrayDataViewBuilder.cs index e5aa1383d8..b008310805 100644 --- a/src/Microsoft.ML.Data/DataView/ArrayDataViewBuilder.cs +++ b/src/Microsoft.ML.Data/DataView/ArrayDataViewBuilder.cs @@ -410,7 +410,7 @@ public Column(DataViewType type, TIn[] values) /// public override void CopyOut(int index, ref TOut value) { - Contracts.Assert(0 <= index & index < _values.Length); + Contracts.Assert(0 <= index && index < _values.Length); CopyOut(in _values[index], ref value); } } diff --git a/src/Microsoft.ML.Data/DataView/CacheDataView.cs b/src/Microsoft.ML.Data/DataView/CacheDataView.cs index a0f5ad4d31..41efc71760 100644 --- a/src/Microsoft.ML.Data/DataView/CacheDataView.cs +++ b/src/Microsoft.ML.Data/DataView/CacheDataView.cs @@ -1281,7 +1281,7 @@ public static ColumnCache Create(CacheDataView parent, DataViewRowCursor input, Contracts.AssertValue(parent); var host = parent._host; host.AssertValue(input); - host.Assert(0 <= srcCol & srcCol < input.Schema.Count); + host.Assert(0 <= srcCol && srcCol < input.Schema.Count); host.Assert(input.IsColumnActive(input.Schema[srcCol])); var type = input.Schema[srcCol].Type; @@ -1357,7 +1357,7 @@ public ImplVec(CacheDataView parent, DataViewRowCursor input, int srcCol, Ordere public override void CacheCurrent() { - Ctx.Assert(0 <= _rowCount & _rowCount < int.MaxValue); + Ctx.Assert(0 <= _rowCount && _rowCount < int.MaxValue); Ctx.AssertValue(Waiter); Ctx.AssertValue(_getter); @@ -1384,7 +1384,7 @@ public override void CacheCurrent() public override void Fetch(int idx, ref VBuffer value) { - Ctx.Assert(0 <= idx & idx < _rowCount); + Ctx.Assert(0 <= idx && idx < _rowCount); Ctx.Assert(_rowCount < Utils.Size(_indexBoundaries)); Ctx.Assert(_rowCount < Utils.Size(_valueBoundaries)); Ctx.Assert(_uniformLength > 0 || _rowCount <= Utils.Size(_lengths)); @@ -1437,7 +1437,7 @@ public ImplOne(CacheDataView parent, DataViewRowCursor input, int srcCol, Ordere public override void CacheCurrent() { - Contracts.Assert(0 <= _rowCount & _rowCount < int.MaxValue); + Contracts.Assert(0 <= _rowCount && _rowCount < int.MaxValue); Contracts.AssertValue(Waiter); Contracts.AssertValue(_getter); Utils.EnsureSize(ref _values, _rowCount + 1); @@ -1447,7 +1447,7 @@ public override void CacheCurrent() public override void Fetch(int idx, ref T value) { - Contracts.Assert(0 <= idx & idx < _rowCount); + Contracts.Assert(0 <= idx && idx < _rowCount); value = _values[idx]; } @@ -1466,7 +1466,7 @@ public ColumnCache(CacheDataView parent, DataViewRowCursor input, int srcCol, Or : base(parent._host, waiter) { Contracts.AssertValue(input); - Contracts.Assert(0 <= srcCol & srcCol < input.Schema.Count); + Contracts.Assert(0 <= srcCol && srcCol < input.Schema.Count); Contracts.Assert(input.Schema[srcCol].Type.RawType == typeof(T)); } diff --git a/src/Microsoft.ML.Data/DataView/LambdaFilter.cs b/src/Microsoft.ML.Data/DataView/LambdaFilter.cs index 70e46156d1..29824fa2dc 100644 --- a/src/Microsoft.ML.Data/DataView/LambdaFilter.cs +++ b/src/Microsoft.ML.Data/DataView/LambdaFilter.cs @@ -87,8 +87,8 @@ private sealed class Impl : FilterBase : base(env, name, input) { Host.AssertValue(pred); - Host.Assert(conv != null | typeof(T1) == typeof(T2)); - Host.Assert(0 <= colSrc & colSrc < Source.Schema.Count); + Host.Assert(conv != null || typeof(T1) == typeof(T2)); + Host.Assert(0 <= colSrc && colSrc < Source.Schema.Count); _colSrc = colSrc; _pred = pred; diff --git a/src/Microsoft.ML.Data/Depricated/Instances/HeaderSchema.cs b/src/Microsoft.ML.Data/Depricated/Instances/HeaderSchema.cs index e15d5f57a2..8a48475252 100644 --- a/src/Microsoft.ML.Data/Depricated/Instances/HeaderSchema.cs +++ b/src/Microsoft.ML.Data/Depricated/Instances/HeaderSchema.cs @@ -245,7 +245,7 @@ IEnumerator IEnumerable.GetEnumerator() private string GetDefault(int index) { - Contracts.Assert(0 <= index & index < Count); + Contracts.Assert(0 <= index && index < Count); return string.Format(DefaultFmt, index); } diff --git a/src/Microsoft.ML.Data/Transforms/GenerateNumberTransform.cs b/src/Microsoft.ML.Data/Transforms/GenerateNumberTransform.cs index 8da3554c27..54c332f6f2 100644 --- a/src/Microsoft.ML.Data/Transforms/GenerateNumberTransform.cs +++ b/src/Microsoft.ML.Data/Transforms/GenerateNumberTransform.cs @@ -189,13 +189,13 @@ internal void Save(ModelSaveContext ctx) protected override DataViewType GetColumnTypeCore(int iinfo) { - Contracts.Assert(0 <= iinfo & iinfo < InfoCount); + Contracts.Assert(0 <= iinfo && iinfo < InfoCount); return UseCounter[iinfo] ? NumberDataViewType.Int64 : NumberDataViewType.Single; } protected override IEnumerable> GetAnnotationTypesCore(int iinfo) { - Contracts.Assert(0 <= iinfo & iinfo < InfoCount); + Contracts.Assert(0 <= iinfo && iinfo < InfoCount); var items = base.GetAnnotationTypesCore(iinfo); if (!UseCounter[iinfo]) items = items.Prepend(BooleanDataViewType.Instance.GetPair(AnnotationUtils.Kinds.IsNormalized)); @@ -204,7 +204,7 @@ protected override DataViewType GetColumnTypeCore(int iinfo) protected override DataViewType GetAnnotationTypeCore(string kind, int iinfo) { - Contracts.Assert(0 <= iinfo & iinfo < InfoCount); + Contracts.Assert(0 <= iinfo && iinfo < InfoCount); if (kind == AnnotationUtils.Kinds.IsNormalized && !UseCounter[iinfo]) return BooleanDataViewType.Instance; return base.GetAnnotationTypeCore(kind, iinfo); @@ -212,7 +212,7 @@ protected override DataViewType GetAnnotationTypeCore(string kind, int iinfo) protected override void GetAnnotationCore(string kind, int iinfo, ref TValue value) { - Contracts.Assert(0 <= iinfo & iinfo < InfoCount); + Contracts.Assert(0 <= iinfo && iinfo < InfoCount); if (kind == AnnotationUtils.Kinds.IsNormalized && !UseCounter[iinfo]) { AnnotationUtils.Marshal(IsNormalized, iinfo, ref value); @@ -224,7 +224,7 @@ protected override void GetAnnotationCore(string kind, int iinfo, ref TV private void IsNormalized(int iinfo, ref bool dst) { - Contracts.Assert(0 <= iinfo & iinfo < InfoCount); + Contracts.Assert(0 <= iinfo && iinfo < InfoCount); dst = true; } diff --git a/src/Microsoft.ML.Data/Transforms/LabelConvertTransform.cs b/src/Microsoft.ML.Data/Transforms/LabelConvertTransform.cs index e14c07d2ab..4116b4eeff 100644 --- a/src/Microsoft.ML.Data/Transforms/LabelConvertTransform.cs +++ b/src/Microsoft.ML.Data/Transforms/LabelConvertTransform.cs @@ -136,7 +136,7 @@ private protected override void SaveModel(ModelSaveContext ctx) protected override DataViewType GetColumnTypeCore(int iinfo) { - Contracts.Assert(0 <= iinfo & iinfo < Infos.Length); + Contracts.Assert(0 <= iinfo && iinfo < Infos.Length); return NumberDataViewType.Single; } diff --git a/src/Microsoft.ML.Data/Transforms/NormalizeColumnDbl.cs b/src/Microsoft.ML.Data/Transforms/NormalizeColumnDbl.cs index 7a5d42518c..805efdeeaa 100644 --- a/src/Microsoft.ML.Data/Transforms/NormalizeColumnDbl.cs +++ b/src/Microsoft.ML.Data/Transforms/NormalizeColumnDbl.cs @@ -111,7 +111,7 @@ internal static partial class AffineNormSerializationUtils numFeatures = size; int morphCount = ctx.Reader.ReadInt32(); - Contracts.CheckDecode(-1 <= morphCount & morphCount < size); + Contracts.CheckDecode(-1 <= morphCount && morphCount < size); if (indicesShift != null) indicesShift.Clear(); @@ -164,7 +164,7 @@ internal static partial class AffineNormSerializationUtils for (int iiv = 0; iiv < indicesMorph.Length; iiv++) { int iv = indicesMorph[iiv]; - Contracts.CheckDecode(ivPrev < iv & iv < numFeatures); + Contracts.CheckDecode(ivPrev < iv && iv < numFeatures); ivPrev = iv; TFloat scale = scales[iv] = scalesSparse[iiv]; Contracts.CheckDecode(!TFloat.IsNaN(scale)); @@ -356,7 +356,7 @@ public void ProcessValue(in VBuffer value) Contracts.Check(value.Length == size); _trainCount++; var values = value.GetValues(); - Contracts.Assert(0 <= values.Length & values.Length <= size); + Contracts.Assert(0 <= values.Length && values.Length <= size); if (values.Length == 0) return; @@ -457,7 +457,7 @@ public void ProcessValue(in VBuffer value) _trainCount++; var size = _mean.Length; var values = value.GetValues(); - Contracts.Assert(0 <= values.Length & values.Length <= size); + Contracts.Assert(0 <= values.Length && values.Length <= size); if (values.Length == 0) return; @@ -815,7 +815,7 @@ private static void FillValues(in VBuffer input, BufferBuilder b Contracts.Assert(input.Length == scale.Length); int size = scale.Length; var values = input.GetValues(); - Contracts.Assert(0 <= values.Length & values.Length <= size); + Contracts.Assert(0 <= values.Length && values.Length <= size); // We always start with sparse, since we may make things sparser than the source. bldr.Reset(size, dense: false); @@ -834,7 +834,7 @@ private static void FillValues(in VBuffer input, BufferBuilder b for (int ii = 0; ii < values.Length; ii++) { int i = indices[ii]; - Contracts.Assert(0 <= i & i < size); + Contracts.Assert(0 <= i && i < size); bldr.AddFeature(i, values[ii] * scale[i]); } } @@ -845,7 +845,7 @@ private static void FillValues(in VBuffer input, BufferBuilder b Contracts.Assert(input.Length == scale.Length); int size = scale.Length; var values = input.GetValues(); - Contracts.Assert(0 <= values.Length & values.Length <= size); + Contracts.Assert(0 <= values.Length && values.Length <= size); // We always start with sparse, since we may make things sparser than the source. bldr.Reset(size, dense: false); @@ -871,7 +871,7 @@ private static void FillValues(in VBuffer input, BufferBuilder b Contracts.Assert(ivSrc < size); for (int ivDst = 0; ivDst < size; ivDst++) { - Contracts.Assert(ivDst <= ivSrc & ivSrc <= size); + Contracts.Assert(ivDst <= ivSrc && ivSrc <= size); if (ivDst == ivSrc) { bldr.AddFeature(ivDst, (values[ii] - offset[ivDst]) * scale[ivDst]); @@ -890,7 +890,7 @@ private static void FillValues(in VBuffer input, BufferBuilder b int size = scale.Length; var values = input.GetValues(); - Contracts.Assert(0 <= values.Length & values.Length <= size); + Contracts.Assert(0 <= values.Length && values.Length <= size); // We always start with sparse, since we may make things sparser than the source. bldr.Reset(size, dense: false); @@ -917,8 +917,8 @@ private static void FillValues(in VBuffer input, BufferBuilder b int ivDst = nz[inz]; for (; ; ) { - Contracts.Assert(0 <= ivDst & ivDst <= size); - Contracts.Assert(0 <= ivSrc & ivSrc <= size); + Contracts.Assert(0 <= ivDst && ivDst <= size); + Contracts.Assert(0 <= ivSrc && ivSrc <= size); Contracts.Assert(ii < values.Length && ivSrc == indices[ii] || ii == values.Length && ivSrc == size); Contracts.Assert(inz < nz.Length && ivDst == nz[inz] || inz == nz.Length && ivDst == size); @@ -1089,7 +1089,7 @@ public override Delegate GetGetter(DataViewRow input, int icol) Contracts.Assert(input.Length == mean.Length); int size = mean.Length; var values = input.GetValues(); - Contracts.Assert(0 <= values.Length & values.Length <= size); + Contracts.Assert(0 <= values.Length && values.Length <= size); // We always start with sparse, since we may make things sparser than the source. bldr.Reset(size, dense: false); @@ -1157,7 +1157,7 @@ public ImplOne(IHost host, TFloat[] binUpperBounds, bool fixZero) _den = Math.Max(1, _binUpperBounds.Length - 1); if (fixZero) _offset = _binUpperBounds.FindIndexSorted(0) / _den; - Host.Assert(0 <= _offset & _offset <= 1); + Host.Assert(0 <= _offset && _offset <= 1); } public static new ImplOne Create(ModelLoadContext ctx, IHost host, DataViewType typeSrc) @@ -1237,7 +1237,7 @@ public ImplVec(IHost host, TFloat[][] binUpperBounds, bool fixZero) for (int i = 0; i < _binUpperBounds.Length; i++) { _offset[i] = _binUpperBounds[i].FindIndexSorted(0) / _den[i]; - Host.Assert(0 <= _offset[i] & _offset[i] <= 1); + Host.Assert(0 <= _offset[i] && _offset[i] <= 1); any |= _offset[i] != 0; } if (!any) @@ -1300,7 +1300,7 @@ private void GetResult(in VBuffer input, ref VBuffer value, Buff Contracts.Assert(input.Length == _binUpperBounds.Length); int size = _binUpperBounds.Length; var values = input.GetValues(); - Contracts.Assert(0 <= values.Length & values.Length <= size); + Contracts.Assert(0 <= values.Length && values.Length <= size); // We always start with sparse, since we may make things sparser than the source. bldr.Reset(size, dense: false); @@ -1336,7 +1336,7 @@ private void GetResult(in VBuffer input, ref VBuffer value, Buff TFloat zero = 0; for (int ivDst = 0; ivDst < size; ivDst++) { - Contracts.Assert(ivDst <= ivSrc & ivSrc <= size); + Contracts.Assert(ivDst <= ivSrc && ivSrc <= size); if (ivDst == ivSrc) { bldr.AddFeature(ivDst, @@ -1355,7 +1355,7 @@ private void GetResult(in VBuffer input, ref VBuffer value, Buff for (int ii = 0; ii < values.Length; ii++) { int i = indices[ii]; - Contracts.Assert(0 <= i & i < size); + Contracts.Assert(0 <= i && i < size); bldr.AddFeature(i, BinUtils.GetValue(in values[ii], _binUpperBounds[i], _den[i])); } } @@ -1403,7 +1403,7 @@ private static void ComputeScaleAndOffset(TFloat max, TFloat min, out TFloat sca offset = 0; else offset = min; - Contracts.Assert(0 <= scale & scale < TFloat.PositiveInfinity); + Contracts.Assert(0 <= scale && scale < TFloat.PositiveInfinity); } private static void ComputeScaleAndOffsetFixZero(TFloat max, TFloat min, out TFloat scale, out TFloat offset) @@ -1424,7 +1424,7 @@ private static void ComputeScaleAndOffsetFixZero(TFloat max, TFloat min, out TFl scale = 0; else scale = 1 / Math.Max(Math.Abs(max), Math.Abs(min)); - Contracts.Assert(0 <= scale & scale < TFloat.PositiveInfinity); + Contracts.Assert(0 <= scale && scale < TFloat.PositiveInfinity); } } @@ -1444,7 +1444,7 @@ public static void ComputeScaleAndOffset(Double mean, Double stddev, out TFloat offset = 0; else offset = (TFloat)mean; - Contracts.Assert(0 <= scale & scale < TFloat.PositiveInfinity); + Contracts.Assert(0 <= scale && scale < TFloat.PositiveInfinity); } public static void ComputeScaleAndOffsetFixZero(Double mean, Double meanSquaredError, out TFloat scale, out TFloat offset) @@ -1460,7 +1460,7 @@ public static void ComputeScaleAndOffsetFixZero(Double mean, Double meanSquaredE scale = 0; else scale = 1 / (TFloat)Math.Sqrt(meanSquaredError + mean * mean); - Contracts.Assert(0 <= scale & scale < TFloat.PositiveInfinity); + Contracts.Assert(0 <= scale && scale < TFloat.PositiveInfinity); } } @@ -1489,7 +1489,7 @@ public static TFloat GetValue(in TFloat input, TFloat[] binUpperBounds, TFloat d int binIdx = binUpperBounds.FindIndexSorted(0, binUpperBounds.Length - 1, input); Contracts.Check(binIdx < binUpperBounds.Length); var value = binIdx / den - offset; - Contracts.Assert(-1 <= value & value <= 1); + Contracts.Assert(-1 <= value && value <= 1); return value; } @@ -1910,7 +1910,7 @@ protected override bool ProcessValue(in VBuffer buffer) Host.Check(buffer.Length == size); var values = buffer.GetValues(); - Host.Assert(0 <= values.Length & values.Length <= size); + Host.Assert(0 <= values.Length && values.Length <= size); if (values.Length == 0) return true; diff --git a/src/Microsoft.ML.Data/Transforms/NormalizeColumnSng.cs b/src/Microsoft.ML.Data/Transforms/NormalizeColumnSng.cs index 7944f7d296..c4094c2876 100644 --- a/src/Microsoft.ML.Data/Transforms/NormalizeColumnSng.cs +++ b/src/Microsoft.ML.Data/Transforms/NormalizeColumnSng.cs @@ -111,7 +111,7 @@ internal static partial class AffineNormSerializationUtils numFeatures = size; int morphCount = ctx.Reader.ReadInt32(); - Contracts.CheckDecode(-1 <= morphCount & morphCount < size); + Contracts.CheckDecode(-1 <= morphCount && morphCount < size); if (indicesShift != null) indicesShift.Clear(); @@ -164,7 +164,7 @@ internal static partial class AffineNormSerializationUtils for (int iiv = 0; iiv < indicesMorph.Length; iiv++) { int iv = indicesMorph[iiv]; - Contracts.CheckDecode(ivPrev < iv & iv < numFeatures); + Contracts.CheckDecode(ivPrev < iv && iv < numFeatures); ivPrev = iv; TFloat scale = scales[iv] = scalesSparse[iiv]; Contracts.CheckDecode(!TFloat.IsNaN(scale)); @@ -357,7 +357,7 @@ public void ProcessValue(in VBuffer value) _trainCount++; var values = value.GetValues(); var count = values.Length; - Contracts.Assert(0 <= count & count <= size); + Contracts.Assert(0 <= count && count <= size); if (count == 0) return; @@ -459,7 +459,7 @@ public void ProcessValue(in VBuffer value) var size = _mean.Length; var values = value.GetValues(); var count = values.Length; - Contracts.Assert(0 <= count & count <= size); + Contracts.Assert(0 <= count && count <= size); if (count == 0) return; @@ -973,7 +973,7 @@ private static void FillValues(in VBuffer input, BufferBuilder b int size = scale.Length; var values = input.GetValues(); int count = values.Length; - Contracts.Assert(0 <= count & count <= size); + Contracts.Assert(0 <= count && count <= size); // We always start with sparse, since we may make things sparser than the source. bldr.Reset(size, dense: false); @@ -992,7 +992,7 @@ private static void FillValues(in VBuffer input, BufferBuilder b for (int ii = 0; ii < count; ii++) { int i = indices[ii]; - Contracts.Assert(0 <= i & i < size); + Contracts.Assert(0 <= i && i < size); bldr.AddFeature(i, values[ii] * scale[i]); } } @@ -1004,7 +1004,7 @@ private static void FillValues(in VBuffer input, BufferBuilder b int size = scale.Length; var values = input.GetValues(); int count = values.Length; - Contracts.Assert(0 <= count & count <= size); + Contracts.Assert(0 <= count && count <= size); // We always start with sparse, since we may make things sparser than the source. bldr.Reset(size, dense: false); @@ -1030,7 +1030,7 @@ private static void FillValues(in VBuffer input, BufferBuilder b Contracts.Assert(ivSrc < size); for (int ivDst = 0; ivDst < size; ivDst++) { - Contracts.Assert(ivDst <= ivSrc & ivSrc <= size); + Contracts.Assert(ivDst <= ivSrc && ivSrc <= size); if (ivDst == ivSrc) { bldr.AddFeature(ivDst, (values[ii] - offset[ivDst]) * scale[ivDst]); @@ -1050,7 +1050,7 @@ private static void FillValues(in VBuffer input, BufferBuilder b int size = scale.Length; var values = input.GetValues(); int count = values.Length; - Contracts.Assert(0 <= count & count <= size); + Contracts.Assert(0 <= count && count <= size); // We always start with sparse, since we may make things sparser than the source. bldr.Reset(size, dense: false); @@ -1077,8 +1077,8 @@ private static void FillValues(in VBuffer input, BufferBuilder b int ivDst = nz[inz]; for (; ; ) { - Contracts.Assert(0 <= ivDst & ivDst <= size); - Contracts.Assert(0 <= ivSrc & ivSrc <= size); + Contracts.Assert(0 <= ivDst && ivDst <= size); + Contracts.Assert(0 <= ivSrc && ivSrc <= size); Contracts.Assert(ii < count && ivSrc == indices[ii] || ii == count && ivSrc == size); Contracts.Assert(inz < nz.Length && ivDst == nz[inz] || inz == nz.Length && ivDst == size); @@ -1250,7 +1250,7 @@ public override Delegate GetGetter(DataViewRow input, int icol) int size = mean.Length; var values = input.GetValues(); int count = values.Length; - Contracts.Assert(0 <= count & count <= size); + Contracts.Assert(0 <= count && count <= size); // We always start with sparse, since we may make things sparser than the source. bldr.Reset(size, dense: false); @@ -1318,7 +1318,7 @@ public ImplOne(IHost host, TFloat[] binUpperBounds, bool fixZero) _den = Math.Max(1, _binUpperBounds.Length - 1); if (fixZero) _offset = _binUpperBounds.FindIndexSorted(0) / _den; - Host.Assert(0 <= _offset & _offset <= 1); + Host.Assert(0 <= _offset && _offset <= 1); } public static new ImplOne Create(ModelLoadContext ctx, IHost host, DataViewType typeSrc) @@ -1398,7 +1398,7 @@ public ImplVec(IHost host, TFloat[][] binUpperBounds, bool fixZero) for (int i = 0; i < _binUpperBounds.Length; i++) { _offset[i] = _binUpperBounds[i].FindIndexSorted(0) / _den[i]; - Host.Assert(0 <= _offset[i] & _offset[i] <= 1); + Host.Assert(0 <= _offset[i] && _offset[i] <= 1); any |= _offset[i] != 0; } if (!any) @@ -1462,7 +1462,7 @@ private void GetResult(in VBuffer input, ref VBuffer value, Buff int size = _binUpperBounds.Length; var values = input.GetValues(); int count = values.Length; - Contracts.Assert(0 <= count & count <= size); + Contracts.Assert(0 <= count && count <= size); // We always start with sparse, since we may make things sparser than the source. bldr.Reset(size, dense: false); @@ -1498,7 +1498,7 @@ private void GetResult(in VBuffer input, ref VBuffer value, Buff TFloat zero = 0; for (int ivDst = 0; ivDst < size; ivDst++) { - Contracts.Assert(ivDst <= ivSrc & ivSrc <= size); + Contracts.Assert(ivDst <= ivSrc && ivSrc <= size); if (ivDst == ivSrc) { bldr.AddFeature(ivDst, @@ -1517,7 +1517,7 @@ private void GetResult(in VBuffer input, ref VBuffer value, Buff for (int ii = 0; ii < count; ii++) { int i = indices[ii]; - Contracts.Assert(0 <= i & i < size); + Contracts.Assert(0 <= i && i < size); bldr.AddFeature(i, BinUtils.GetValue(values[ii], _binUpperBounds[i], _den[i])); } } @@ -1566,7 +1566,7 @@ private static void ComputeScaleAndOffset(TFloat max, TFloat min, out TFloat sca offset = 0; else offset = min; - Contracts.Assert(0 <= scale & scale < TFloat.PositiveInfinity); + Contracts.Assert(0 <= scale && scale < TFloat.PositiveInfinity); } private static void ComputeScaleAndOffsetFixZero(TFloat max, TFloat min, out TFloat scale, out TFloat offset) @@ -1587,7 +1587,7 @@ private static void ComputeScaleAndOffsetFixZero(TFloat max, TFloat min, out TFl scale = 0; else scale = 1 / Math.Max(Math.Abs(max), Math.Abs(min)); - Contracts.Assert(0 <= scale & scale < TFloat.PositiveInfinity); + Contracts.Assert(0 <= scale && scale < TFloat.PositiveInfinity); } } @@ -1607,7 +1607,7 @@ public static void ComputeScaleAndOffset(Double mean, Double stddev, out TFloat offset = 0; else offset = (TFloat)mean; - Contracts.Assert(0 <= scale & scale < TFloat.PositiveInfinity); + Contracts.Assert(0 <= scale && scale < TFloat.PositiveInfinity); } public static void ComputeScaleAndOffsetFixZero(Double mean, Double meanSquaredError, out TFloat scale, out TFloat offset) @@ -1623,7 +1623,7 @@ public static void ComputeScaleAndOffsetFixZero(Double mean, Double meanSquaredE scale = 0; else scale = 1 / (TFloat)Math.Sqrt(meanSquaredError + mean * mean); - Contracts.Assert(0 <= scale & scale < TFloat.PositiveInfinity); + Contracts.Assert(0 <= scale && scale < TFloat.PositiveInfinity); } } @@ -1652,7 +1652,7 @@ public static TFloat GetValue(TFloat input, TFloat[] binUpperBounds, TFloat den, int binIdx = binUpperBounds.FindIndexSorted(0, binUpperBounds.Length - 1, input); Contracts.Check(binIdx < binUpperBounds.Length); var value = binIdx / den - offset; - Contracts.Assert(-1 <= value & value <= 1); + Contracts.Assert(-1 <= value && value <= 1); return value; } @@ -2074,7 +2074,7 @@ protected override bool ProcessValue(in VBuffer buffer) var values = buffer.GetValues(); int count = values.Length; - Host.Assert(0 <= count & count <= size); + Host.Assert(0 <= count && count <= size); if (count == 0) return true; diff --git a/src/Microsoft.ML.Data/Transforms/RowShufflingTransformer.cs b/src/Microsoft.ML.Data/Transforms/RowShufflingTransformer.cs index d68e306970..6f70d4ff0c 100644 --- a/src/Microsoft.ML.Data/Transforms/RowShufflingTransformer.cs +++ b/src/Microsoft.ML.Data/Transforms/RowShufflingTransformer.cs @@ -583,11 +583,11 @@ private async Task ProduceAsync() int numRows; for (numRows = 0; numRows < requested; ++numRows) { - Ch.Assert(0 <= circularIndex & circularIndex < _pipeIndices.Length); + Ch.Assert(0 <= circularIndex && circularIndex < _pipeIndices.Length); if (!_input.MoveNext()) break; int pipeIndex = _pipeIndices[circularIndex++]; - Ch.Assert(0 <= pipeIndex & pipeIndex < _pipeIndices.Length); + Ch.Assert(0 <= pipeIndex && pipeIndex < _pipeIndices.Length); for (int c = 0; c < _pipes.Length; ++c) _pipes[c].Fill(pipeIndex); if (circularIndex == _pipeIndices.Length) @@ -617,7 +617,7 @@ protected override bool MoveNextCore() { Ch.Assert(_liveCount > 0); Ch.Assert(_deadCount < _blockSize || _doneConsuming); - Ch.Assert(0 <= _circularIndex & _circularIndex < _pipeIndices.Length); + Ch.Assert(0 <= _circularIndex && _circularIndex < _pipeIndices.Length); if (++_circularIndex == _pipeIndices.Length) _circularIndex = 0; diff --git a/src/Microsoft.ML.Data/Transforms/ValueToKeyMappingTransformerImpl.cs b/src/Microsoft.ML.Data/Transforms/ValueToKeyMappingTransformerImpl.cs index 78c467ef15..7b3b8d5599 100644 --- a/src/Microsoft.ML.Data/Transforms/ValueToKeyMappingTransformerImpl.cs +++ b/src/Microsoft.ML.Data/Transforms/ValueToKeyMappingTransformerImpl.cs @@ -657,7 +657,7 @@ public override void GetTerms(ref VBuffer> dst) int slot = 0; foreach (var nstr in _pool) { - Contracts.Assert(0 <= nstr.Id & nstr.Id < editor.Values.Length); + Contracts.Assert(0 <= nstr.Id && nstr.Id < editor.Values.Length); Contracts.Assert(nstr.Id == slot); editor.Values[nstr.Id] = nstr.Value; slot++; diff --git a/src/Microsoft.ML.ImageAnalytics/ImagePixelExtractor.cs b/src/Microsoft.ML.ImageAnalytics/ImagePixelExtractor.cs index 3fa71d2983..c91d3b1380 100644 --- a/src/Microsoft.ML.ImageAnalytics/ImagePixelExtractor.cs +++ b/src/Microsoft.ML.ImageAnalytics/ImagePixelExtractor.cs @@ -725,7 +725,7 @@ internal ColumnOptions(string name, string inputColumnName, ModelLoadContext ctx planes = (planes & 0x05) + ((planes >> 1) & 0x05); planes = (planes & 0x03) + ((planes >> 2) & 0x03); Planes = (byte)planes; - Contracts.Assert(0 < Planes & Planes <= 4); + Contracts.Assert(0 < Planes && Planes <= 4); OutputAsFloatArray = ctx.Reader.ReadBoolByte(); OffsetImage = ctx.Reader.ReadFloat(); diff --git a/src/Microsoft.ML.ImageAnalytics/VectorToImageTransform.cs b/src/Microsoft.ML.ImageAnalytics/VectorToImageTransform.cs index 5747fc16a4..ab69e889eb 100644 --- a/src/Microsoft.ML.ImageAnalytics/VectorToImageTransform.cs +++ b/src/Microsoft.ML.ImageAnalytics/VectorToImageTransform.cs @@ -546,7 +546,7 @@ internal ColumnOptions(string outputColumnName, string inputColumnName, ModelLoa planes = (planes & 0x05) + ((planes >> 1) & 0x05); planes = (planes & 0x03) + ((planes >> 2) & 0x03); Planes = (byte)planes; - Contracts.Assert(0 < Planes & Planes <= 4); + Contracts.Assert(0 < Planes && Planes <= 4); if (ctx.Header.ModelVerWritten <= VectorToImageConvertingTransformer.BeforeOrderVersion) Order = ImagePixelExtractingEstimator.ColorsOrder.ARGB; diff --git a/src/Microsoft.ML.Mkl.Components/OlsLinearRegression.cs b/src/Microsoft.ML.Mkl.Components/OlsLinearRegression.cs index 1455679b76..96b52fb8f2 100644 --- a/src/Microsoft.ML.Mkl.Components/OlsLinearRegression.cs +++ b/src/Microsoft.ML.Mkl.Components/OlsLinearRegression.cs @@ -633,8 +633,8 @@ private static VersionInfo GetVersionInfo() Contracts.Assert(!Double.IsNaN(rSquaredAdjusted) || standardErrors == null); // Nullity or not must be consistent between the statistics. Contracts.Assert((standardErrors == null) == (tValues == null) && (tValues == null) == (pValues == null)); - Contracts.Assert(0 <= rSquared & rSquared <= 1); - Contracts.Assert(Double.IsNaN(rSquaredAdjusted) | (0 <= rSquaredAdjusted & rSquaredAdjusted <= 1)); + Contracts.Assert(0 <= rSquared && rSquared <= 1); + Contracts.Assert(Double.IsNaN(rSquaredAdjusted) || (0 <= rSquaredAdjusted && rSquaredAdjusted <= 1)); if (standardErrors != null) { // If not null, the input arrays must have one value for each parameter. @@ -713,15 +713,15 @@ private protected override void SaveCore(ModelSaveContext ctx) // double[#parameters]: t-statistics per parameter // double[#parameters]: p-values per parameter - Contracts.Assert(0 <= RSquared & RSquared <= 1); + Contracts.Assert(0 <= RSquared && RSquared <= 1); ctx.Writer.Write(RSquared); - Contracts.Assert(Double.IsNaN(RSquaredAdjusted) | (0 <= RSquaredAdjusted && RSquaredAdjusted <= 1)); + Contracts.Assert(Double.IsNaN(RSquaredAdjusted) || (0 <= RSquaredAdjusted && RSquaredAdjusted <= 1)); ctx.Writer.Write(RSquaredAdjusted); - Contracts.Assert(!Double.IsNaN(RSquaredAdjusted) | !HasStatistics); + Contracts.Assert(!Double.IsNaN(RSquaredAdjusted) || !HasStatistics); ctx.Writer.WriteBoolByte(HasStatistics); if (!HasStatistics) { - Contracts.Assert(_standardErrors == null & _tValues == null & _pValues == null); + Contracts.Assert(_standardErrors == null && _tValues == null && _pValues == null); return; } Contracts.Assert(Weight.Length + 1 == _standardErrors.Length); diff --git a/src/Microsoft.ML.StandardTrainers/Standard/LinearModelParameters.cs b/src/Microsoft.ML.StandardTrainers/Standard/LinearModelParameters.cs index e59096bc88..0276ae46a2 100644 --- a/src/Microsoft.ML.StandardTrainers/Standard/LinearModelParameters.cs +++ b/src/Microsoft.ML.StandardTrainers/Standard/LinearModelParameters.cs @@ -172,7 +172,7 @@ private protected LinearModelParameters(IHostEnvironment env, string name, Model Host.Assert(len > 0); int cind = ctx.Reader.ReadInt32(); - Host.CheckDecode(0 <= cind & cind < len); + Host.CheckDecode(0 <= cind && cind < len); var indices = ctx.Reader.ReadIntArray(cind); // Verify monotonicity of indices. diff --git a/src/Microsoft.ML.StandardTrainers/Standard/SdcaBinary.cs b/src/Microsoft.ML.StandardTrainers/Standard/SdcaBinary.cs index 260fc51b83..049745e799 100644 --- a/src/Microsoft.ML.StandardTrainers/Standard/SdcaBinary.cs +++ b/src/Microsoft.ML.StandardTrainers/Standard/SdcaBinary.cs @@ -559,7 +559,7 @@ private protected sealed override TModel TrainCore(IChannel ch, RoleMappedData d // If we favor storing the invariants, precompute the invariants now. if (invariants != null) { - Contracts.Assert((idToIdx == null & ((long)idLoMax + 1) * weightSetCount <= Utils.ArrayMaxSize) | (idToIdx != null & count * weightSetCount <= Utils.ArrayMaxSize)); + Contracts.Assert((idToIdx == null && ((long)idLoMax + 1) * weightSetCount <= Utils.ArrayMaxSize) || (idToIdx != null && count * weightSetCount <= Utils.ArrayMaxSize)); Func getIndexFromIdAndRow = GetIndexFromIdAndRowGetter(idToIdx, biasReg.Length); int invariantCoeff = weightSetCount == 1 ? 1 : 2; using (var cursor = cursorFactory.Create()) @@ -571,7 +571,7 @@ private protected sealed override TModel TrainCore(IChannel ch, RoleMappedData d { Host.CheckAlive(); long longIdx = getIndexFromIdAndRow(cursor.Id, row); - Contracts.Assert(0 <= longIdx & longIdx < invariants.Length, $"longIdx={longIdx}, invariants.Length={invariants.Length}"); + Contracts.Assert(0 <= longIdx && longIdx < invariants.Length, $"longIdx={longIdx}, invariants.Length={invariants.Length}"); int idx = (int)longIdx; var features = cursor.Features; var normSquared = VectorUtils.NormSquared(features); @@ -1251,7 +1251,7 @@ private long GetIit(long hash) /// private long GetIndexCore(DataViewRowId val, long iit) { - Contracts.Assert(0 <= iit & iit < _rgit.Length); + Contracts.Assert(0 <= iit && iit < _rgit.Length); long it = _rgit[iit]; while (it >= 0) { @@ -1330,11 +1330,11 @@ private void AssertValid() { Contracts.AssertValue(_rgit); Contracts.Assert(_rgit.Length > 0); - Contracts.Assert(0 <= _count & _count <= _entries.Length); + Contracts.Assert(0 <= _count && _count <= _entries.Length); // The number of buckets should be at least the number of items, unless we're reached the // biggest number of buckets allowed. - Contracts.Assert(_rgit.Length >= _count | _rgit.Length == HashHelpers.MaxPrime); + Contracts.Assert(_rgit.Length >= _count || _rgit.Length == HashHelpers.MaxPrime); } [Conditional("DUMP_STATS")] diff --git a/src/Microsoft.ML.StandardTrainers/Standard/SdcaMulticlass.cs b/src/Microsoft.ML.StandardTrainers/Standard/SdcaMulticlass.cs index df56cf4940..a68bf1751b 100644 --- a/src/Microsoft.ML.StandardTrainers/Standard/SdcaMulticlass.cs +++ b/src/Microsoft.ML.StandardTrainers/Standard/SdcaMulticlass.cs @@ -471,7 +471,7 @@ private protected override void CheckLabel(RoleMappedData examples, out int weig private protected override float[] InitializeFeatureNormSquared(int length) { - Contracts.Assert(0 < length & length <= Utils.ArrayMaxSize); + Contracts.Assert(0 < length && length <= Utils.ArrayMaxSize); return new float[length]; } diff --git a/src/Microsoft.ML.TimeSeries/TimeSeriesUtils.cs b/src/Microsoft.ML.TimeSeries/TimeSeriesUtils.cs index 810840f38a..7ab764b534 100644 --- a/src/Microsoft.ML.TimeSeries/TimeSeriesUtils.cs +++ b/src/Microsoft.ML.TimeSeries/TimeSeriesUtils.cs @@ -32,7 +32,7 @@ internal static FixedSizeQueue DeserializeFixedSizeQueueSingle(BinaryRea var q = new FixedSizeQueue(capacity); int count = reader.ReadInt32(); - host.CheckDecode(0 <= count & count <= capacity); + host.CheckDecode(0 <= count && count <= capacity); for (int index = 0; index < count; index++) q.AddLast(reader.ReadSingle()); @@ -61,7 +61,7 @@ internal static FixedSizeQueue DeserializeFixedSizeQueueDouble(BinaryRea var q = new FixedSizeQueue(capacity); int count = reader.ReadInt32(); - host.CheckDecode(0 <= count & count <= capacity); + host.CheckDecode(0 <= count && count <= capacity); for (int index = 0; index < count; index++) q.AddLast(reader.ReadDouble()); diff --git a/src/Microsoft.ML.Transforms/Expression/CodeGen.cs b/src/Microsoft.ML.Transforms/Expression/CodeGen.cs index 6ebd4d59fb..b46dd736b7 100644 --- a/src/Microsoft.ML.Transforms/Expression/CodeGen.cs +++ b/src/Microsoft.ML.Transforms/Expression/CodeGen.cs @@ -37,7 +37,7 @@ public static Delegate Compile(out List errors, LambdaNode node) private LambdaCompiler(LambdaNode node) { Contracts.AssertValue(node); - Contracts.Assert(1 <= node.Vars.Length & node.Vars.Length <= MaxParams); + Contracts.Assert(1 <= node.Vars.Length && node.Vars.Length <= MaxParams); Contracts.Assert(MaxParams <= 16); Contracts.AssertValue(node.ResultType); @@ -63,7 +63,7 @@ private LambdaCompiler(LambdaNode node) var types = new Type[node.Vars.Length + 1]; foreach (var v in node.Vars) { - Contracts.Assert(0 <= v.Index & v.Index < node.Vars.Length); + Contracts.Assert(0 <= v.Index && v.Index < node.Vars.Length); Contracts.Assert(types[v.Index] == null); types[v.Index] = v.Type.RawType; } @@ -355,7 +355,7 @@ public override void Visit(IdentNode node) } else { - Contracts.Assert(0 <= loc.Index & loc.Index < _cacheWith.Count); + Contracts.Assert(0 <= loc.Index && loc.Index < _cacheWith.Count); var cache = _cacheWith[loc.Index]; Contracts.Assert(cache.Value != null); if (cache.Flag != null) @@ -903,7 +903,7 @@ public override bool PreVisit(CompareNode node) { // NotEqual is special - it means that the values are all distinct, so comparing adjacent // items is not enough. - Contracts.Assert(node.Op == CompareOp.NotEqual & items.Length > 2); + Contracts.Assert(node.Op == CompareOp.NotEqual && items.Length > 2); // We need a local for each item. var locals = new MethodGenerator.Temporary[items.Length]; diff --git a/src/Microsoft.ML.Transforms/Expression/LambdaBinder.cs b/src/Microsoft.ML.Transforms/Expression/LambdaBinder.cs index 8a756fec08..bc5b438dd4 100644 --- a/src/Microsoft.ML.Transforms/Expression/LambdaBinder.cs +++ b/src/Microsoft.ML.Transforms/Expression/LambdaBinder.cs @@ -364,7 +364,7 @@ private void ApplyBoolBinOp(BinaryOpNode node) { case BinaryOp.Or: if (v1 != null && v2 != null) - node.SetValue(v1.Value | v2.Value); + node.SetValue(v1.Value || v2.Value); else if (v1 != null && v1.Value || v2 != null && v2.Value) node.SetValue(true); else if (v1 != null && !v1.Value) @@ -375,7 +375,7 @@ private void ApplyBoolBinOp(BinaryOpNode node) case BinaryOp.And: if (v1 != null && v2 != null) - node.SetValue(v1.Value & v2.Value); + node.SetValue(v1.Value && v2.Value); else if (v1 != null && !v1.Value || v2 != null && !v2.Value) node.SetValue(false); else if (v1 != null && v1.Value) @@ -1400,7 +1400,7 @@ public override void PostVisit(CallNode node) best = candidates[0]; else { - _host.Assert(0 <= icandMinBad & icandMinBad < candidates.Count); + _host.Assert(0 <= icandMinBad && icandMinBad < candidates.Count); best = candidates[icandMinBad]; PostError(node, "The best overload of '{0}' has some invalid arguments", node.Head.Value); } @@ -1468,8 +1468,8 @@ public override void PostVisit(CallNode node) private static bool CanConvert(ExprTypeKind src, ExprTypeKind dst) { // src can be Error, but dst should not be. - Contracts.Assert(ExprTypeKind.Error <= src & src < ExprTypeKind._Lim); - Contracts.Assert(ExprTypeKind.Error < dst & dst < ExprTypeKind._Lim); + Contracts.Assert(ExprTypeKind.Error <= src && src < ExprTypeKind._Lim); + Contracts.Assert(ExprTypeKind.Error < dst && dst < ExprTypeKind._Lim); if (src == ExprTypeKind.Error) return true; @@ -1700,13 +1700,13 @@ internal static bool CanPromote(bool precise, ExprTypeKind k1, ExprTypeKind k2, return false; } - Contracts.Assert(0 <= i1 & i1 < 4); - Contracts.Assert(0 <= i2 & i2 < 4); + Contracts.Assert(0 <= i1 && i1 < 4); + Contracts.Assert(0 <= i2 && i2 < 4); Contracts.Assert(i1 != i2); // Combine the two two-bit values. int index = i1 | (i2 << 2); - Contracts.Assert(0 <= index & index < 16); + Contracts.Assert(0 <= index && index < 16); switch (index) { // Only integer types -> I8 diff --git a/src/Microsoft.ML.Transforms/Expression/LambdaParser.cs b/src/Microsoft.ML.Transforms/Expression/LambdaParser.cs index c003265bc6..b6d4167a94 100644 --- a/src/Microsoft.ML.Transforms/Expression/LambdaParser.cs +++ b/src/Microsoft.ML.Transforms/Expression/LambdaParser.cs @@ -71,7 +71,7 @@ private static int FindIndex(List map, int value, int ivMin) else ivLim = iv; } - Contracts.Assert(0 <= ivMin & ivMin <= map.Count); + Contracts.Assert(0 <= ivMin && ivMin <= map.Count); Contracts.Assert(ivMin == map.Count || value < map[ivMin]); Contracts.Assert(ivMin == 0 || value >= map[ivMin - 1]); return ivMin; diff --git a/src/Microsoft.ML.Transforms/Expression/Lexer.cs b/src/Microsoft.ML.Transforms/Expression/Lexer.cs index 4ff07e70f6..b1c8f87e04 100644 --- a/src/Microsoft.ML.Transforms/Expression/Lexer.cs +++ b/src/Microsoft.ML.Transforms/Expression/Lexer.cs @@ -298,7 +298,7 @@ private Token LexNumLit() } } - bool fReal = fDot | fExp; + bool fReal = fDot || fExp; char chSuf = LexRealSuffix(fReal); if (fReal || chSuf != '\0') return LexRealNum(chSuf); diff --git a/src/Microsoft.ML.Transforms/GroupTransform.cs b/src/Microsoft.ML.Transforms/GroupTransform.cs index 7301fa692d..4ea05ba921 100644 --- a/src/Microsoft.ML.Transforms/GroupTransform.cs +++ b/src/Microsoft.ML.Transforms/GroupTransform.cs @@ -618,7 +618,7 @@ private bool IsSameGroup() { // Even if the result is false, we need to call every checker so that they can memorize // the current key value. - result = checker.IsSameKey() & result; + result = checker.IsSameKey() && result; } return result; } diff --git a/src/Microsoft.ML.Transforms/MissingValueIndicatorTransform.cs b/src/Microsoft.ML.Transforms/MissingValueIndicatorTransform.cs index b0bc8c3604..c188bef3cf 100644 --- a/src/Microsoft.ML.Transforms/MissingValueIndicatorTransform.cs +++ b/src/Microsoft.ML.Transforms/MissingValueIndicatorTransform.cs @@ -168,7 +168,7 @@ private VectorDataViewType[] GetTypesAndMetadata() protected override DataViewType GetColumnTypeCore(int iinfo) { - Host.Assert(0 <= iinfo & iinfo < Infos.Length); + Host.Assert(0 <= iinfo && iinfo < Infos.Length); return _types[iinfo]; } @@ -294,7 +294,7 @@ private static void FillValues(float input, ref VBuffer result) private static void FillValues(IExceptionContext ectx, ref VBuffer buffer) { int size = buffer.Length; - ectx.Check(0 <= size & size < int.MaxValue / 2); + ectx.Check(0 <= size && size < int.MaxValue / 2); var values = buffer.GetValues(); var editor = VBufferEditor.Create(ref buffer, size * 2, values.Length); @@ -335,7 +335,7 @@ private static void FillValues(IExceptionContext ectx, ref VBuffer buffer if (val == 0) continue; int iv = indices[iivSrc]; - ectx.Assert(ivPrev < iv & iv < size); + ectx.Assert(ivPrev < iv && iv < size); ivPrev = iv; if (float.IsNaN(val)) { @@ -351,7 +351,7 @@ private static void FillValues(IExceptionContext ectx, ref VBuffer buffer } } - ectx.Assert(0 <= iivDst & iivDst <= values.Length); + ectx.Assert(0 <= iivDst && iivDst <= values.Length); buffer = editor.CommitTruncated(iivDst); } } diff --git a/src/Microsoft.ML.Transforms/MissingValueReplacing.cs b/src/Microsoft.ML.Transforms/MissingValueReplacing.cs index 6407992274..df27d8f3a0 100644 --- a/src/Microsoft.ML.Transforms/MissingValueReplacing.cs +++ b/src/Microsoft.ML.Transforms/MissingValueReplacing.cs @@ -750,7 +750,7 @@ private void FillValues(in VBuffer src, ref VBuffer dst, InPredicate Host.Assert(iivDst <= iivSrc); var srcVal = srcValues[iivSrc]; int iv = srcIndices[iivSrc]; - Host.Assert(ivPrev < iv & iv < srcSize); + Host.Assert(ivPrev < iv && iv < srcSize); ivPrev = iv; if (!isNA(in srcVal)) @@ -824,7 +824,7 @@ private void FillValues(in VBuffer src, ref VBuffer dst, InPredicate Host.Assert(iivDst <= iivSrc); var srcVal = srcValues[iivSrc]; int iv = srcIndices[iivSrc]; - Host.Assert(ivPrev < iv & iv < srcSize); + Host.Assert(ivPrev < iv && iv < srcSize); ivPrev = iv; if (!isNA(in srcVal)) diff --git a/src/Microsoft.ML.Transforms/OptionalColumnTransform.cs b/src/Microsoft.ML.Transforms/OptionalColumnTransform.cs index 48b4c82c09..e063fe8caa 100644 --- a/src/Microsoft.ML.Transforms/OptionalColumnTransform.cs +++ b/src/Microsoft.ML.Transforms/OptionalColumnTransform.cs @@ -179,7 +179,7 @@ private void SetMetadata() protected override DataViewType GetColumnTypeCore(int iinfo) { - Contracts.Assert(0 <= iinfo & iinfo < InfoCount); + Contracts.Assert(0 <= iinfo && iinfo < InfoCount); return ColumnTypes[iinfo]; } diff --git a/src/Microsoft.ML.Transforms/Text/NgramHashingTransformer.cs b/src/Microsoft.ML.Transforms/Text/NgramHashingTransformer.cs index 476441c742..8b75eb0d3c 100644 --- a/src/Microsoft.ML.Transforms/Text/NgramHashingTransformer.cs +++ b/src/Microsoft.ML.Transforms/Text/NgramHashingTransformer.cs @@ -691,7 +691,7 @@ private sealed class NGram : IEquatable public NGram(uint[] ngram, int lim, int srcCol) { Contracts.AssertValue(ngram); - Contracts.Assert(1 <= lim & lim <= ngram.Length); + Contracts.Assert(1 <= lim && lim <= ngram.Length); Contracts.Assert(0 <= srcCol); Grams = ngram; diff --git a/src/Microsoft.ML.Transforms/Text/StopWordsRemovingTransformer.cs b/src/Microsoft.ML.Transforms/Text/StopWordsRemovingTransformer.cs index 6bc00b9a30..43f5576025 100644 --- a/src/Microsoft.ML.Transforms/Text/StopWordsRemovingTransformer.cs +++ b/src/Microsoft.ML.Transforms/Text/StopWordsRemovingTransformer.cs @@ -318,7 +318,7 @@ private void CheckResources() private static void AddResourceIfNotPresent(StopWordsRemovingEstimator.Language lang) { - Contracts.Assert(0 <= (int)lang & (int)lang < Utils.Size(StopWords)); + Contracts.Assert(0 <= (int)lang && (int)lang < Utils.Size(StopWords)); if (StopWords[(int)lang] == null) { @@ -498,7 +498,7 @@ private void UpdateLanguage(ref StopWordsRemovingEstimator.Language langToUse, V private bool ResourceExists(StopWordsRemovingEstimator.Language lang) { int langVal = (int)lang; - Contracts.Assert(0 <= langVal & langVal < Utils.Size(StopWords)); + Contracts.Assert(0 <= langVal && langVal < Utils.Size(StopWords)); // Note: Updating values in _resourcesExist does not have to be an atomic operation return StopWords[langVal] != null || (_resourcesExist[langVal] ?? (_resourcesExist[langVal] = GetResourceFileStreamOrNull(lang) != null).Value);