3 // Copyright (c) Microsoft Corporation. All rights reserved.
6 // =+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+
10 // <OWNER>Microsoft</OWNER>
12 // =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
14 using System
.Collections
.Generic
;
16 namespace System
.Linq
.Parallel
19 /// Common miscellaneous utility methods used throughout the code-base.
21 internal static class Util
24 //-----------------------------------------------------------------------------------
25 // Simple helper that returns a constant depending on the sign of the argument. I.e.
26 // if the argument is negative, the result is -1; if it's positive, the result is 1;
27 // otherwise, if it's zero, the result is 0.
30 internal static int Sign(int x
)
32 return x
< 0 ? -1 : x
== 0 ? 0 : 1;
35 //-----------------------------------------------------------------------------------
36 // This is a temporary workaround for a VSWhidbey bug 601998 in the X64 JIT compiler.
37 // Unlike the X86 JIT, null checks on value types aren't optimized away in Whidbey.
38 // That means using the GenericComparer<K> infrastructure results in boxing value
39 // types. This kills performance all over the place. This bug has been fixed in
40 // Orcas (2.0 SP1), so once we're on the SP1 runtime, this can be removed.
43 internal static Comparer
<TKey
> GetDefaultComparer
<TKey
>()
45 if (typeof(TKey
) == typeof(int))
47 return (Comparer
<TKey
>)(object)s_fastIntComparer
;
49 else if (typeof(TKey
) == typeof(long))
51 return (Comparer
<TKey
>)(object)s_fastLongComparer
;
53 else if (typeof(TKey
) == typeof(float))
55 return (Comparer
<TKey
>)(object)s_fastFloatComparer
;
57 else if (typeof(TKey
) == typeof(double))
59 return (Comparer
<TKey
>)(object)s_fastDoubleComparer
;
61 else if (typeof(TKey
) == typeof(DateTime
))
63 return (Comparer
<TKey
>)(object)s_fastDateTimeComparer
;
66 return Comparer
<TKey
>.Default
;
69 private static FastIntComparer s_fastIntComparer
= new FastIntComparer();
71 class FastIntComparer
: Comparer
<int>
73 public override int Compare(int x
, int y
)
75 return x
.CompareTo(y
);
79 private static FastLongComparer s_fastLongComparer
= new FastLongComparer();
81 class FastLongComparer
: Comparer
<long>
83 public override int Compare(long x
, long y
)
85 return x
.CompareTo(y
);
89 private static FastFloatComparer s_fastFloatComparer
= new FastFloatComparer();
91 class FastFloatComparer
: Comparer
<float>
93 public override int Compare(float x
, float y
)
95 return x
.CompareTo(y
);
99 private static FastDoubleComparer s_fastDoubleComparer
= new FastDoubleComparer();
101 class FastDoubleComparer
: Comparer
<double>
103 public override int Compare(double x
, double y
)
105 return x
.CompareTo(y
);
109 private static FastDateTimeComparer s_fastDateTimeComparer
= new FastDateTimeComparer();
111 class FastDateTimeComparer
: Comparer
<DateTime
>
113 public override int Compare(DateTime x
, DateTime y
)
115 return x
.CompareTo(y
);