忽略
This commit is contained in:
@@ -1,74 +1,74 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Hncore.Infrastructure.Tree
|
||||
{
|
||||
public class DataNode<TData>
|
||||
{
|
||||
public DataNode(TData data)
|
||||
{
|
||||
this.Data = data;
|
||||
}
|
||||
|
||||
public DataNode<TData> Parent { get; set; }
|
||||
public TData Data { get; set; }
|
||||
|
||||
public List<DataNode<TData>> Children { get; set; } = new List<DataNode<TData>>();
|
||||
|
||||
public bool IsLeaf { get { return this.Children.Count()==0; } }
|
||||
|
||||
public void Traverse(Action<DataNode<TData>> act)
|
||||
{
|
||||
act(this);
|
||||
this.Children.ForEach(item =>
|
||||
{
|
||||
item.Traverse(act);
|
||||
});
|
||||
}
|
||||
|
||||
public string GetFullPath(Func<TData, string> func, string separator = ".")
|
||||
{
|
||||
var parent = this.Parent;
|
||||
var names = new List<string> { func(this.Data) };
|
||||
while (parent != null)
|
||||
{
|
||||
names.Add(func(parent.Data));
|
||||
parent = parent.Parent;
|
||||
}
|
||||
names.Reverse();
|
||||
return string.Join(separator, names).TrimStart(separator.ToArray());
|
||||
}
|
||||
|
||||
public DataNode<TData> SortAsc<Tkey>(Func<TData, Tkey> exp)
|
||||
{
|
||||
if (this.Children.Count > 0)
|
||||
{
|
||||
Func<DataNode<TData>, Tkey> iierExp = m => exp(m.Data);
|
||||
this.Children.OrderBy(iierExp);
|
||||
this.Children.ForEach(item =>
|
||||
{
|
||||
item.SortAsc(exp);
|
||||
});
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
public DataNode<TData> SortDesc<Tkey>(Func<TData, Tkey> exp)
|
||||
{
|
||||
if (this.Children.Count > 0)
|
||||
{
|
||||
Func<DataNode<TData>, Tkey> iierExp = m => exp(m.Data);
|
||||
this.Children.OrderByDescending(iierExp);
|
||||
this.Children.ForEach(item =>
|
||||
{
|
||||
item.SortDesc(exp);
|
||||
});
|
||||
}
|
||||
|
||||
return this;
|
||||
}
|
||||
}
|
||||
}
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Hncore.Infrastructure.Tree
|
||||
{
|
||||
public class DataNode<TData>
|
||||
{
|
||||
public DataNode(TData data)
|
||||
{
|
||||
this.Data = data;
|
||||
}
|
||||
|
||||
public DataNode<TData> Parent { get; set; }
|
||||
public TData Data { get; set; }
|
||||
|
||||
public List<DataNode<TData>> Children { get; set; } = new List<DataNode<TData>>();
|
||||
|
||||
public bool IsLeaf { get { return this.Children.Count()==0; } }
|
||||
|
||||
public void Traverse(Action<DataNode<TData>> act)
|
||||
{
|
||||
act(this);
|
||||
this.Children.ForEach(item =>
|
||||
{
|
||||
item.Traverse(act);
|
||||
});
|
||||
}
|
||||
|
||||
public string GetFullPath(Func<TData, string> func, string separator = ".")
|
||||
{
|
||||
var parent = this.Parent;
|
||||
var names = new List<string> { func(this.Data) };
|
||||
while (parent != null)
|
||||
{
|
||||
names.Add(func(parent.Data));
|
||||
parent = parent.Parent;
|
||||
}
|
||||
names.Reverse();
|
||||
return string.Join(separator, names).TrimStart(separator.ToArray());
|
||||
}
|
||||
|
||||
public DataNode<TData> SortAsc<Tkey>(Func<TData, Tkey> exp)
|
||||
{
|
||||
if (this.Children.Count > 0)
|
||||
{
|
||||
Func<DataNode<TData>, Tkey> iierExp = m => exp(m.Data);
|
||||
this.Children.OrderBy(iierExp);
|
||||
this.Children.ForEach(item =>
|
||||
{
|
||||
item.SortAsc(exp);
|
||||
});
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
public DataNode<TData> SortDesc<Tkey>(Func<TData, Tkey> exp)
|
||||
{
|
||||
if (this.Children.Count > 0)
|
||||
{
|
||||
Func<DataNode<TData>, Tkey> iierExp = m => exp(m.Data);
|
||||
this.Children.OrderByDescending(iierExp);
|
||||
this.Children.ForEach(item =>
|
||||
{
|
||||
item.SortDesc(exp);
|
||||
});
|
||||
}
|
||||
|
||||
return this;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,60 +1,60 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
||||
namespace Hncore.Infrastructure.Tree
|
||||
{
|
||||
public class DataTree
|
||||
{
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
/// <param name="topPredicate">得到第一级节点条件</param>
|
||||
/// <param name="childPredicate">得到孩子得条件</param>
|
||||
public static DataNode<TData> Load<TData>(IEnumerable<TData> datas, Func<TData, bool> topPredicate, Func<TData, TData, bool> childPredicate) where TData : new()
|
||||
{
|
||||
var root = new DataNode<TData>(new TData() { });
|
||||
if (datas == null || datas.Count() == 0)
|
||||
return root;
|
||||
var tops = datas.Where(topPredicate);
|
||||
|
||||
foreach (var p in tops)
|
||||
{
|
||||
LoadChildren(datas, root, p, childPredicate);
|
||||
}
|
||||
return root;
|
||||
}
|
||||
private static void LoadChildren<TData>(IEnumerable<TData> datas,DataNode<TData> topNode, TData p, Func<TData,TData, bool> childPredicate)
|
||||
{
|
||||
var pNode = new DataNode<TData>(p);
|
||||
pNode.Parent = topNode;
|
||||
topNode.Children.Add(pNode);
|
||||
|
||||
var childDatas = datas.Where(item=>childPredicate(p,item)) ;
|
||||
|
||||
if (childDatas.Count() > 0)
|
||||
{
|
||||
foreach (var childData in childDatas)
|
||||
{
|
||||
LoadChildren(datas,pNode, childData, childPredicate);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static void Traverse<TData>(DataNode<TData> rootNode, Action<DataNode<TData>> act, bool root = true)
|
||||
{
|
||||
if (root)
|
||||
{
|
||||
rootNode.Traverse(act);
|
||||
}
|
||||
else
|
||||
{
|
||||
rootNode.Children.ForEach(item =>
|
||||
{
|
||||
item.Traverse(act);
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
||||
namespace Hncore.Infrastructure.Tree
|
||||
{
|
||||
public class DataTree
|
||||
{
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
/// <param name="topPredicate">得到第一级节点条件</param>
|
||||
/// <param name="childPredicate">得到孩子得条件</param>
|
||||
public static DataNode<TData> Load<TData>(IEnumerable<TData> datas, Func<TData, bool> topPredicate, Func<TData, TData, bool> childPredicate) where TData : new()
|
||||
{
|
||||
var root = new DataNode<TData>(new TData() { });
|
||||
if (datas == null || datas.Count() == 0)
|
||||
return root;
|
||||
var tops = datas.Where(topPredicate);
|
||||
|
||||
foreach (var p in tops)
|
||||
{
|
||||
LoadChildren(datas, root, p, childPredicate);
|
||||
}
|
||||
return root;
|
||||
}
|
||||
private static void LoadChildren<TData>(IEnumerable<TData> datas,DataNode<TData> topNode, TData p, Func<TData,TData, bool> childPredicate)
|
||||
{
|
||||
var pNode = new DataNode<TData>(p);
|
||||
pNode.Parent = topNode;
|
||||
topNode.Children.Add(pNode);
|
||||
|
||||
var childDatas = datas.Where(item=>childPredicate(p,item)) ;
|
||||
|
||||
if (childDatas.Count() > 0)
|
||||
{
|
||||
foreach (var childData in childDatas)
|
||||
{
|
||||
LoadChildren(datas,pNode, childData, childPredicate);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static void Traverse<TData>(DataNode<TData> rootNode, Action<DataNode<TData>> act, bool root = true)
|
||||
{
|
||||
if (root)
|
||||
{
|
||||
rootNode.Traverse(act);
|
||||
}
|
||||
else
|
||||
{
|
||||
rootNode.Children.ForEach(item =>
|
||||
{
|
||||
item.Traverse(act);
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user