System.Drawing: added email to icon and test file headers
[mono.git] / mcs / class / dlr / Runtime / Microsoft.Scripting.Core / Utils / Helpers.cs
1
2 /* ****************************************************************************
3  *
4  * Copyright (c) Microsoft Corporation. 
5  *
6  * This source code is subject to terms and conditions of the Apache License, Version 2.0. A 
7  * copy of the license can be found in the License.html file at the root of this distribution. If 
8  * you cannot locate the  Apache License, Version 2.0, please send an email to 
9  * dlr@microsoft.com. By using this source code in any fashion, you are agreeing to be bound 
10  * by the terms of the Apache License, Version 2.0.
11  *
12  * You must not remove this notice, or any other, from this software.
13  *
14  *
15  * ***************************************************************************/
16
17 #if CLR2
18 using Microsoft.Scripting.Ast;
19 using Microsoft.Scripting.Utils;
20 #else
21 using System.Linq.Expressions;
22 #endif
23
24 using System.Collections.Generic;
25
26 namespace System.Dynamic.Utils {
27     // Miscellaneous helpers that don't belong anywhere else
28     internal static class Helpers {
29
30         internal static T CommonNode<T>(T first, T second, Func<T, T> parent) where T : class {
31             var cmp = EqualityComparer<T>.Default;
32             if (cmp.Equals(first, second)) {
33                 return first;
34             }
35             var set = new Set<T>(cmp);
36             for (T t = first; t != null; t = parent(t)) {
37                 set.Add(t);
38             }
39             for (T t = second; t != null; t = parent(t)) {
40                 if (set.Contains(t)) {
41                     return t;
42                 }
43             }
44             return null;
45         }
46
47         internal static void IncrementCount<T>(T key, Dictionary<T, int> dict) {
48             int count;
49             dict.TryGetValue(key, out count);
50             dict[key] = count + 1;
51         }
52     }
53 }