[runtime] Disable some tests in full-aot mode which cannot be AOTed because of type...
[mono.git] / mcs / tools / mdoc / Mono.Rocks / ObjectRocks.cs
1 //
2 // Object.cs: C# extension methods on object.
3 //
4 // Author:
5 //   Jonathan Pryor  <jpryor@novell.com>
6 //   leppie  (http://xacc.wordpress.com/)
7 //
8 // Copyright (c) 2008-2009 Novell, Inc. (http://www.novell.com)
9 // Copyright (c) 2009 leppie (http://xacc.wordpress.com/)
10 //
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
18 //
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 //
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 //
30 using System;
31 using System.Collections.Generic;
32 using System.Linq;
33 using System.Linq.Expressions;
34
35 namespace Mono.Rocks {
36
37         static class Check {
38                 public static void ChildrenSelector (object childrenSelector)
39                 {
40                         if (childrenSelector == null)
41                                 throw new ArgumentNullException ("childrenSelector");
42                 }
43
44                 public static void Destination (object destination)
45                 {
46                         if (destination == null)
47                                 throw new ArgumentNullException ("destination");
48                 }
49
50                 public static void Self (object self)
51                 {
52                         if (self == null)
53                                 throw new ArgumentNullException ("self");
54                 }
55
56                 public static void ValueSelector (object valueSelector)
57                 {
58                         if (valueSelector == null)
59                                 throw new ArgumentNullException ("valueSelector");
60                 }
61         }
62
63         public static class ObjectRocks {
64
65                 #region Tree Traversal Methods
66
67                 /*
68                  * Tree Traversal Methods courtesy of:
69                  * http://xacc.wordpress.com/2009/03/05/tree-traversal-extension-methods/
70                  */
71
72                 public static IEnumerable<TResult> TraverseDepthFirst<TSource, TResult>(
73                                 this TSource self,
74                                 Func<TSource, TResult> valueSelector,
75                                 Func<TSource, IEnumerable<TSource>> childrenSelector)
76                 {
77                         return self.TraverseDepthFirstWithParent (valueSelector, childrenSelector)
78                                 .Select(x => x.Value);
79                 }
80
81                 public static IEnumerable<KeyValuePair<TSource, TResult>> TraverseDepthFirstWithParent<TSource, TResult>(
82                                 this TSource self,
83                                 Func<TSource, TResult> valueSelector,
84                                 Func<TSource, IEnumerable<TSource>> childrenSelector)
85                 {
86                         return self.TraverseDepthFirstWithParent (default (TSource), valueSelector, childrenSelector);
87                 }
88
89                 static IEnumerable<KeyValuePair<TSource, TResult>> TraverseDepthFirstWithParent<TSource, TResult>(
90                                 this TSource self,
91                                 TSource parent,
92                                 Func<TSource, TResult> valueSelector,
93                                 Func<TSource, IEnumerable<TSource>> childrenSelector)
94                 {
95                         Check.Self (self);
96                         Check.ValueSelector (valueSelector);
97                         Check.ChildrenSelector (childrenSelector);
98
99                         return CreateTraverseDepthFirstWithParentIterator (self, parent, valueSelector, childrenSelector);
100                 }
101
102                 static IEnumerable<KeyValuePair<TSource, TResult>> CreateTraverseDepthFirstWithParentIterator<TSource, TResult>(
103                                 this TSource self,
104                                 TSource parent,
105                                 Func<TSource, TResult> valueSelector,
106                                 Func<TSource, IEnumerable<TSource>> childrenSelector)
107                 {
108                         yield return new KeyValuePair<TSource, TResult>(parent, valueSelector (self));
109
110                         foreach (var c in childrenSelector (self))
111                         {
112                                 foreach (var item in c.TraverseDepthFirstWithParent(c, valueSelector, childrenSelector))
113                                 {
114                                         yield return item;
115                                 }
116                         }
117                 }
118                 #endregion
119         }
120 }
121