2 // Object.cs: C# extension methods on object.
5 // Jonathan Pryor <jpryor@novell.com>
6 // leppie (http://xacc.wordpress.com/)
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.
31 using System
.Collections
.Generic
;
33 using System
.Linq
.Expressions
;
35 namespace Mono
.Rocks
{
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
)
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
>(
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
>(
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
>(
92 Func
<TSource
, TResult
> valueSelector
,
93 Func
<TSource
, IEnumerable
<TSource
>> childrenSelector
)
96 Check
.ValueSelector (valueSelector
);
97 Check
.ChildrenSelector (childrenSelector
);
99 return CreateTraverseDepthFirstWithParentIterator (self
, parent
, valueSelector
, childrenSelector
);
102 static IEnumerable
<KeyValuePair
<TSource
, TResult
>> CreateTraverseDepthFirstWithParentIterator
<TSource
, TResult
>(
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
))