2010-05-19 Jb Evain <jbevain@novell.com>
[mcs.git] / tools / mdoc / Mono.Rocks / ObjectRocks.cs
blobc1014dce1971d3049b969f946bc6d78662756cf9
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/)
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:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
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.
30 using System;
31 using System.Collections.Generic;
32 using System.Linq;
33 using System.Linq.Expressions;
35 namespace Mono.Rocks {
37 static class Check {
38 public static void ChildrenSelector (object childrenSelector)
40 if (childrenSelector == null)
41 throw new ArgumentNullException ("childrenSelector");
44 public static void Destination (object destination)
46 if (destination == null)
47 throw new ArgumentNullException ("destination");
50 public static void Self (object self)
52 if (self == null)
53 throw new ArgumentNullException ("self");
56 public static void ValueSelector (object valueSelector)
58 if (valueSelector == null)
59 throw new ArgumentNullException ("valueSelector");
63 public static class ObjectRocks {
65 #region Tree Traversal Methods
68 * Tree Traversal Methods courtesy of:
69 * http://xacc.wordpress.com/2009/03/05/tree-traversal-extension-methods/
72 public static IEnumerable<TResult> TraverseDepthFirst<TSource, TResult>(
73 this TSource self,
74 Func<TSource, TResult> valueSelector,
75 Func<TSource, IEnumerable<TSource>> childrenSelector)
77 return self.TraverseDepthFirstWithParent (valueSelector, childrenSelector)
78 .Select(x => x.Value);
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)
86 return self.TraverseDepthFirstWithParent (default (TSource), valueSelector, childrenSelector);
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)
95 Check.Self (self);
96 Check.ValueSelector (valueSelector);
97 Check.ChildrenSelector (childrenSelector);
99 return CreateTraverseDepthFirstWithParentIterator (self, parent, valueSelector, childrenSelector);
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)
108 yield return new KeyValuePair<TSource, TResult>(parent, valueSelector (self));
110 foreach (var c in childrenSelector (self))
112 foreach (var item in c.TraverseDepthFirstWithParent(c, valueSelector, childrenSelector))
114 yield return item;
118 #endregion