123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- namespace SAGA.RevitUtils.MEP
- {
- public class TNode
- {
- public TNode()
- {
- this.Nodes = new List<TNode>();
- }
- public void AddChild(TNode node)
- {
- node.Parent = this;
- this.Nodes.Add(node);
- }
- public void AddChildren(List<TNode> nodes)
- {
- nodes.ForEach(n => n.Parent = this);
- this.Nodes.AddRange(nodes);
- }
- private int GetDepth(TNode node, int currentDepth)
- {
- int num = currentDepth;
- if (!node.IsLeaf)
- {
- foreach (TNode node2 in node.Nodes)
- {
- num = Math.Max(this.GetDepth(node2, currentDepth + 1), num);
- }
- }
- return num;
- }
- public List<TNode> GetLeaves()
- {
- return GetLeaves(new List<TNode> { this });
- }
- public static List<TNode> GetLeaves(List<TNode> nodes)
- {
- List<TNode> list = new List<TNode>();
- for (int i = 0; i < nodes.Count; i++)
- {
- TNode item = nodes[i];
- if (item.IsLeaf)
- {
- list.Add(item);
- }
- else
- {
- list.AddRange(GetLeaves(item.Nodes));
- }
- }
- return list;
- }
- public int Depth
- {
- get
- {
- return this.GetDepth(this, 1);
- }
- }
- public bool IsLeaf
- {
- get
- {
- return (this.Nodes.Count == 0);
- }
- }
- public int LeafCount
- {
- get
- {
- int num = 0;
- List<TNode> list = new List<TNode> {
- this
- };
- for (int i = 0; i < list.Count; i++)
- {
- TNode node = list[i];
- if (node.IsLeaf)
- {
- num++;
- }
- else
- {
- list.AddRange(node.Nodes);
- }
- }
- return num;
- }
- }
- public int Level
- {
- get
- {
- int num = 1;
- for (TNode node = this.Parent; node != null; node = node.Parent)
- {
- num++;
- }
- return num;
- }
- }
- public string Name { get; set; }
- public List<TNode> Nodes { get; private set; }
- public TNode Parent { get; internal set; }
- public object Tag { get; set; }
- }
- public class TNode<T> where T : TNode<T>
- {
- public TNode()
- {
- this.Nodes = new List<T>();
- }
- public void AddChild(T node)
- {
- node.Parent = this as T;
- this.Nodes.Add(node);
- }
- public void AddChildren(List<T> nodes)
- {
- T p = this as T;
- this.Nodes.ForEach(n => n.Parent = p);
- this.Nodes.AddRange(nodes);
- }
- /// <summary>
- /// 获取所有节点
- /// </summary>
- /// <returns></returns>
- public List<T> GetAllNodes()
- {
- T local = this as T;
- if (local == null)
- {
- return new List<T>();
- }
- List<T> list = new List<T> {
- local
- };
- for (int i = 0; i < list.Count; i++)
- {
- T local2 = list[i];
- list.AddRange(local2.Nodes);
- }
- return list;
- }
- private int GetDepth(TNode<T> node, int currentDepth)
- {
- int num = currentDepth;
- if (!node.IsLeaf)
- {
- foreach (T local in node.Nodes)
- {
- num = Math.Max(this.GetDepth(local, currentDepth + 1), num);
- }
- }
- return num;
- }
- /// <summary>
- /// 获取所有叶节点
- /// </summary>
- /// <returns></returns>
- public List<T> GetLeaves()
- {
- return TNode<T>.GetLeaves(new List<T> { this as T });
- }
- public static List<T> GetLeaves(List<T> nodes)
- {
- List<T> list = new List<T>();
- for (int i = 0; i < nodes.Count; i++)
- {
- T item = nodes[i];
- if (item.IsLeaf)
- {
- list.Add(item);
- }
- else
- {
- list.AddRange(TNode<T>.GetLeaves(item.Nodes));
- }
- }
- return list;
- }
- /// <summary>
- /// 获取节点深度
- /// </summary>
- public int Depth
- {
- get
- {
- return this.GetDepth((TNode<T>)this, 1);
- }
- }
- /// <summary>
- /// 是否为叶节点
- /// </summary>
- public bool IsLeaf
- {
- get
- {
- return (this.Nodes.Count == 0);
- }
- }
- public int LeafCount
- {
- get
- {
- int num = 0;
- List<TNode<T>> list = new List<TNode<T>> {
- this
- };
- for (int i = 0; i < list.Count; i++)
- {
- TNode<T> node = list[i];
- if (node.IsLeaf)
- {
- num++;
- }
- else
- {
- list.AddRange(node.Nodes);
- }
- }
- return num;
- }
- }
- /// <summary>
- /// 当前node的级别
- /// </summary>
- public int Level
- {
- get
- {
- int num = 1;
- for (TNode<T> node = this.Parent; node != null; node = node.Parent)
- {
- num++;
- }
- return num;
- }
- }
- public string Name { get; set; }
- public List<T> Nodes { get; private set; }
- public T Parent { get; set; }
- public object Tag { get; set; }
- #region 便捷查找
- public T FirstOrDefault()
- {
- return this as T;
- }
- /// <summary>
- /// 查找第一个满足条件的节点
- /// </summary>
- /// <param name="predicateNode"></param>
- /// <returns></returns>
- public T FirstOrDefault(Predicate<T> predicateNode)
- {
- if (predicateNode == null)
- return this as T;
- var usePredicate = predicateNode;
- var queueNodes = new Queue<T>();
- queueNodes.Enqueue(this as T);
- while (queueNodes.Any())
- {
- var useNode = queueNodes.Dequeue();
- if (usePredicate(useNode))
- {
- return useNode;
- }
- useNode.Nodes.ToList().ForEach(queueNodes.Enqueue);
- }
- return null;
- }
- #endregion
- }
- }
|