7ccdf8f5e4a11d8bfded239c0aacef0adba0cd7a
[mono.git] / mcs / class / referencesource / System.Core / System / Linq / Parallel / Utils / Util.cs
1 // ==++==
2 //
3 //   Copyright (c) Microsoft Corporation.  All rights reserved.
4 // 
5 // ==--==
6 // =+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+
7 //
8 // Util.cs
9 //
10 // <OWNER>Microsoft</OWNER>
11 //
12 // =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
13
14 using System.Collections.Generic;
15
16 namespace System.Linq.Parallel
17 {
18     /// <summary>
19     /// Common miscellaneous utility methods used throughout the code-base.
20     /// </summary>
21     internal static class Util
22     {
23
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.
28         //
29
30         internal static int Sign(int x)
31         {
32             return x < 0 ? -1 : x == 0 ? 0 : 1;
33         }
34
35         //-----------------------------------------------------------------------------------
36         // This is a temporary workaround for a VSWhidbey 
37
38
39
40
41
42
43         internal static Comparer<TKey> GetDefaultComparer<TKey>()
44         {
45             if (typeof(TKey) == typeof(int))
46             {
47                 return (Comparer<TKey>)(object)s_fastIntComparer;
48             }
49             else if (typeof(TKey) == typeof(long))
50             {
51                 return (Comparer<TKey>)(object)s_fastLongComparer;
52             }
53             else if (typeof(TKey) == typeof(float))
54             {
55                 return (Comparer<TKey>)(object)s_fastFloatComparer;
56             }
57             else if (typeof(TKey) == typeof(double))
58             {
59                 return (Comparer<TKey>)(object)s_fastDoubleComparer;
60             }
61             else if (typeof(TKey) == typeof(DateTime))
62             {
63                 return (Comparer<TKey>)(object)s_fastDateTimeComparer;
64             }
65
66             return Comparer<TKey>.Default;
67         }
68
69         private static FastIntComparer s_fastIntComparer = new FastIntComparer();
70
71         class FastIntComparer : Comparer<int>
72         {
73             public override int Compare(int x, int y)
74             {
75                 return x.CompareTo(y);
76             }
77         }
78
79         private static FastLongComparer s_fastLongComparer = new FastLongComparer();
80
81         class FastLongComparer : Comparer<long>
82         {
83             public override int Compare(long x, long y)
84             {
85                 return x.CompareTo(y);
86             }
87         }
88
89         private static FastFloatComparer s_fastFloatComparer = new FastFloatComparer();
90
91         class FastFloatComparer : Comparer<float>
92         {
93             public override int Compare(float x, float y)
94             {
95                 return x.CompareTo(y);
96             }
97         }
98
99         private static FastDoubleComparer s_fastDoubleComparer = new FastDoubleComparer();
100
101         class FastDoubleComparer : Comparer<double>
102         {
103             public override int Compare(double x, double y)
104             {
105                 return x.CompareTo(y);
106             }
107         }
108
109         private static FastDateTimeComparer s_fastDateTimeComparer = new FastDateTimeComparer();
110
111         class FastDateTimeComparer : Comparer<DateTime>
112         {
113             public override int Compare(DateTime x, DateTime y)
114             {
115                 return x.CompareTo(y);
116             }
117         }
118
119     }
120 }