4
4
#endif
5
5
using System . Collections ;
6
6
using System . Collections . Generic ;
7
+ using System . Diagnostics ;
7
8
using System . Globalization ;
8
9
using System . Linq ;
9
10
using System . Runtime . CompilerServices ;
@@ -1017,12 +1018,13 @@ private static Polynom MultiplyAlphaPolynoms(Polynom polynomBase, Polynom polyno
1017
1018
}
1018
1019
1019
1020
// Identify and merge terms with the same exponent.
1020
- var toGlue = GetNotUniqueExponents ( resultPolynom ) ;
1021
1021
#if NETCOREAPP
1022
+ var toGlue = GetNotUniqueExponents ( resultPolynom , resultPolynom . Count <= 128 ? stackalloc int [ 128 ] . Slice ( 0 , resultPolynom . Count ) : new int [ resultPolynom . Count ] ) ;
1022
1023
var gluedPolynoms = toGlue . Length <= 128
1023
1024
? stackalloc PolynomItem [ 128 ] . Slice ( 0 , toGlue . Length )
1024
1025
: new PolynomItem [ toGlue . Length ] ;
1025
1026
#else
1027
+ var toGlue = GetNotUniqueExponents ( resultPolynom ) ;
1026
1028
var gluedPolynoms = new PolynomItem [ toGlue . Length ] ;
1027
1029
#endif
1028
1030
var gluedPolynomsIndex = 0 ;
@@ -1042,7 +1044,11 @@ private static Polynom MultiplyAlphaPolynoms(Polynom polynomBase, Polynom polyno
1042
1044
1043
1045
// Remove duplicated exponents and add the corrected ones back.
1044
1046
for ( int i = resultPolynom . Count - 1 ; i >= 0 ; i -- )
1047
+ #if NETCOREAPP
1048
+ if ( toGlue . Contains ( resultPolynom [ i ] . Exponent ) )
1049
+ #else
1045
1050
if ( Array . IndexOf ( toGlue , resultPolynom [ i ] . Exponent ) >= 0 )
1051
+ #endif
1046
1052
resultPolynom . RemoveAt ( i ) ;
1047
1053
foreach ( var polynom in gluedPolynoms )
1048
1054
resultPolynom . Add ( polynom ) ;
@@ -1052,20 +1058,55 @@ private static Polynom MultiplyAlphaPolynoms(Polynom polynomBase, Polynom polyno
1052
1058
return resultPolynom ;
1053
1059
1054
1060
// Auxiliary function to identify exponents that appear more than once in the polynomial.
1055
- int [ ] GetNotUniqueExponents ( Polynom list )
1061
+ #if NETCOREAPP
1062
+ static ReadOnlySpan < int > GetNotUniqueExponents ( Polynom list , Span < int > buffer )
1056
1063
{
1057
- var dic = new Dictionary < int , bool > ( list . Count ) ;
1064
+ Debug . Assert ( list . Count == buffer . Length ) ;
1065
+
1066
+ int idx = 0 ;
1058
1067
foreach ( var row in list )
1059
1068
{
1060
- #if NETCOREAPP
1061
- if ( dic . TryAdd ( row . Exponent , false ) )
1062
- dic [ row . Exponent ] = true ;
1069
+ buffer [ idx ++ ] = row . Exponent ;
1070
+ }
1071
+
1072
+ buffer . Sort ( ) ;
1073
+
1074
+ idx = 0 ;
1075
+ int expCount = 0 ;
1076
+ int last = buffer [ 0 ] ;
1077
+
1078
+ for ( int i = 1 ; i < buffer . Length ; ++ i )
1079
+ {
1080
+ if ( buffer [ i ] == last )
1081
+ {
1082
+ expCount ++ ;
1083
+ }
1084
+ else
1085
+ {
1086
+ if ( expCount > 0 )
1087
+ {
1088
+ Debug . Assert ( idx <= i - 1 ) ;
1089
+
1090
+ buffer [ idx ++ ] = last ;
1091
+ expCount = 0 ;
1092
+ }
1093
+ }
1094
+
1095
+ last = buffer [ i ] ;
1096
+ }
1097
+
1098
+ return buffer . Slice ( 0 , idx ) ;
1099
+ }
1063
1100
#else
1101
+ static int [ ] GetNotUniqueExponents ( Polynom list )
1102
+ {
1103
+ var dic = new Dictionary < int , bool > ( list . Count ) ;
1104
+ foreach ( var row in list )
1105
+ {
1064
1106
if ( ! dic . ContainsKey ( row . Exponent ) )
1065
1107
dic . Add ( row . Exponent , false ) ;
1066
1108
else
1067
1109
dic [ row . Exponent ] = true ;
1068
- #endif
1069
1110
}
1070
1111
1071
1112
// Collect all exponents that appeared more than once.
@@ -1086,6 +1127,7 @@ int[] GetNotUniqueExponents(Polynom list)
1086
1127
1087
1128
return result ;
1088
1129
}
1130
+ #endif
1089
1131
}
1090
1132
1091
1133
/// <inheritdoc cref="IDisposable.Dispose"/>
0 commit comments