-
Notifications
You must be signed in to change notification settings - Fork 4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Patterns: Allow non-negative and full integer sets to merge (intersect or union) #71968
Changes from 12 commits
45bd785
9f74cdb
af16224
1bb85ae
e6da7f4
e0597e4
25de777
ac2f593
b8eacd1
b725762
31e9af7
af60453
d617b80
aa29183
355aa2c
78678e6
043cebb
768d63f
58ce1ee
8bb84f9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,35 +8,38 @@ namespace Microsoft.CodeAnalysis.CSharp | |
{ | ||
internal static partial class ValueSetFactory | ||
{ | ||
private sealed class FloatingValueSetFactory<TFloating, TFloatingTC> : IValueSetFactory<TFloating> where TFloatingTC : struct, FloatingTC<TFloating> | ||
private sealed class FloatingValueSetFactory<TFloating, TFloatingTC> : IValueSetFactory<TFloating> where TFloatingTC : class, FloatingTC<TFloating> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
{ | ||
public static readonly FloatingValueSetFactory<TFloating, TFloatingTC> Instance = new FloatingValueSetFactory<TFloating, TFloatingTC>(); | ||
private readonly TFloatingTC _tc; | ||
|
||
private FloatingValueSetFactory() { } | ||
public FloatingValueSetFactory(TFloatingTC tc) | ||
{ | ||
_tc = tc; | ||
} | ||
|
||
IValueSet IValueSetFactory.AllValues => FloatingValueSet<TFloating, TFloatingTC>.AllValues; | ||
IValueSet IValueSetFactory.AllValues => FloatingValueSet<TFloating, TFloatingTC>.AllValues(_tc); | ||
|
||
IValueSet IValueSetFactory.NoValues => FloatingValueSet<TFloating, TFloatingTC>.NoValues; | ||
IValueSet IValueSetFactory.NoValues => FloatingValueSet<TFloating, TFloatingTC>.NoValues(_tc); | ||
|
||
public IValueSet<TFloating> Related(BinaryOperatorKind relation, TFloating value) => | ||
FloatingValueSet<TFloating, TFloatingTC>.Related(relation, value); | ||
FloatingValueSet<TFloating, TFloatingTC>.Related(relation, value, _tc); | ||
|
||
IValueSet IValueSetFactory.Random(int expectedSize, Random random) => | ||
FloatingValueSet<TFloating, TFloatingTC>.Random(expectedSize, random); | ||
FloatingValueSet<TFloating, TFloatingTC>.Random(expectedSize, random, _tc); | ||
|
||
ConstantValue IValueSetFactory.RandomValue(Random random) | ||
{ | ||
TFloatingTC tc = default; | ||
return tc.ToConstantValue(tc.Random(random)); | ||
return _tc.ToConstantValue(_tc.Random(random)); | ||
} | ||
|
||
IValueSet IValueSetFactory.Related(BinaryOperatorKind relation, ConstantValue value) => | ||
value.IsBad ? FloatingValueSet<TFloating, TFloatingTC>.AllValues : FloatingValueSet<TFloating, TFloatingTC>.Related(relation, default(TFloatingTC).FromConstantValue(value)); | ||
value.IsBad | ||
? FloatingValueSet<TFloating, TFloatingTC>.AllValues(_tc) | ||
: FloatingValueSet<TFloating, TFloatingTC>.Related(relation, _tc.FromConstantValue(value), _tc); | ||
|
||
bool IValueSetFactory.Related(BinaryOperatorKind relation, ConstantValue left, ConstantValue right) | ||
{ | ||
TFloatingTC tc = default; | ||
return tc.Related(relation, tc.FromConstantValue(left), tc.FromConstantValue(right)); | ||
return _tc.Related(relation, _tc.FromConstantValue(left), _tc.FromConstantValue(right)); | ||
} | ||
} | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,9 +11,21 @@ namespace Microsoft.CodeAnalysis.CSharp | |
|
||
internal static partial class ValueSetFactory | ||
{ | ||
private struct IntTC : INumericTC<int> | ||
private class IntTC : INumericTC<int> | ||
{ | ||
int INumericTC<int>.MinValue => int.MinValue; | ||
// Note: whenever we intersect or union two sets of IntTCs, | ||
// we just keep the nonNegative flag of the set we're merging into. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
If we are uncomfortable relying on this behavior, we could add Intersect/Union APIs to |
||
public bool nonNegative; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
|
||
private IntTC(bool nonNegative) | ||
{ | ||
this.nonNegative = nonNegative; | ||
} | ||
|
||
public static readonly IntTC DefaultInstance = new IntTC(nonNegative: false); | ||
public static readonly IntTC NonNegativeInstance = new IntTC(nonNegative: true); | ||
|
||
int INumericTC<int>.MinValue => nonNegative ? 0 : int.MinValue; | ||
|
||
int INumericTC<int>.MaxValue => int.MaxValue; | ||
|
||
|
@@ -46,7 +58,7 @@ int INumericTC<int>.Next(int value) | |
|
||
int INumericTC<int>.Prev(int value) | ||
{ | ||
Debug.Assert(value != int.MinValue); | ||
Debug.Assert(value != (nonNegative ? 0 : int.MinValue)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. MinValue is a private interface implementation There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
It doesn't have to be private. And we can avoid code duplication. |
||
return value - 1; | ||
} | ||
|
||
|
@@ -58,7 +70,14 @@ int INumericTC<int>.Prev(int value) | |
|
||
public int Random(Random random) | ||
{ | ||
return (random.Next() << 10) ^ random.Next(); | ||
if (nonNegative) | ||
{ | ||
return Math.Abs((random.Next() << 10) ^ random.Next()); | ||
} | ||
else | ||
{ | ||
return (random.Next() << 10) ^ random.Next(); | ||
} | ||
} | ||
} | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we should be able to get rid of this type parameter #Closed