一个简单的Linq to TreeNode

  最近两天写单元测试,碰到须要验证一个树是不是指望的,可是树这个结构要验证起来还真是有点烦。。。ide

  个人树大概是这样的:单元测试

 1 class TreeNode<T>
 2 {
 3         private static readonly TreeNode<T>[] Empty = new TreeNode<T>[0];
 4         public TreeNode() : this(default(T), Empty) { }
 5         public TreeNode(T value, IReadOnlyList<TreeNode<T>> children)
 6         {
 7             Value = value;
 8             Children = children;
 9         }
10         public T Value { get; set; }
11         public IReadOnlyList<TreeNode<T>> Children { get; set; }
12 }
View Code

  为了判等,这个树实现了IEquatable<TreeNode<T>>接口:测试

 1         public override bool Equals(object obj)
 2         {
 3             var other = obj as TreeNode<T>;
 4             if (other == null)
 5             {
 6                 return false;
 7             }
 8             return Equals(other);
 9         }
10 
11         public bool Equals(TreeNode<T> other)
12         {
13             return object.Equals(Value, other.Value) && Children.SequenceEqual(other.Children);
14         }
15 
16         public override int GetHashCode()
17         {
18             return Value.GetHashCode() ^ Children.Aggregate(Children.Count, (seed, x) => seed ^ x.GetHashCode());
19         }
View Code

  看着还不错,不过因为T实际是个复杂类型,每次重写Equals也是个不轻松的事情,并且还要把整个指望的树给构造出来,呵呵,仍是烦啊。。。this

  可是,若是只须要断定个把简单属性,事情就方便了许多,因此,TreeNode须要一个方法来转换T的类型:spa

1         public TreeNode<TResult> Select<TResult>(Func<T, TResult> selector)
2         {
3             return new TreeNode<TResult>(selector(Value), (from c in Children select c.Select(selector)).ToList());
4         }
View Code

  看起来不错,这样就能够有这样的code来玩转tree了:code

1 TreeNode<int> intTree = ...
2 TreeNode<string> stringTree = intTree.Select(i => i.ToString());
View Code

  等等,咱们能够把这代码写的跟linq:blog

1 TreeNode<int> intTree = ...
2 TreeNode<string> stringTree = from i in intTree
3                               select i.ToString();
View Code

  测试代码继续啪啦啪啦的写,唉,测试失败了,什么状况,仔细一看,哦,tree下面节点的顺序错了,而tree的equals方法要求顺序,可是这个测试恰好不要求顺序,因而我有了两个选择:排序

1. 改写equals方法 (不想折腾集合操做)接口

2. 让节点排序 (对测试用例而言,构建一个有顺序的树但是很简单的事情)get

  因此,我须要个OrderBy:

1         public TreeNode<T> OrderBy<TKey>(Func<T, TKey> keySelector)
2         {
3             return new TreeNode<T>(Value, (from c in Children
4                                            orderby keySelector(c.Value)
5                                            select c.OrderBy(keySelector)).ToList());
6         }
View Code

  这下就能够随便折腾这个树了:

1 TreeNode<int> intTree = ...
2 TreeNode<string> stringTree = from i in intTree
3                               let m = i % 3
4                               order by m
5                               select i.ToString();
View Code
相关文章
相关标签/搜索