TNode.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. namespace SAGA.RevitUtils.MEP
  5. {
  6. public class TNode
  7. {
  8. public TNode()
  9. {
  10. this.Nodes = new List<TNode>();
  11. }
  12. public void AddChild(TNode node)
  13. {
  14. node.Parent = this;
  15. this.Nodes.Add(node);
  16. }
  17. public void AddChildren(List<TNode> nodes)
  18. {
  19. nodes.ForEach(n => n.Parent = this);
  20. this.Nodes.AddRange(nodes);
  21. }
  22. private int GetDepth(TNode node, int currentDepth)
  23. {
  24. int num = currentDepth;
  25. if (!node.IsLeaf)
  26. {
  27. foreach (TNode node2 in node.Nodes)
  28. {
  29. num = Math.Max(this.GetDepth(node2, currentDepth + 1), num);
  30. }
  31. }
  32. return num;
  33. }
  34. public List<TNode> GetLeaves()
  35. {
  36. return GetLeaves(new List<TNode> { this });
  37. }
  38. public static List<TNode> GetLeaves(List<TNode> nodes)
  39. {
  40. List<TNode> list = new List<TNode>();
  41. for (int i = 0; i < nodes.Count; i++)
  42. {
  43. TNode item = nodes[i];
  44. if (item.IsLeaf)
  45. {
  46. list.Add(item);
  47. }
  48. else
  49. {
  50. list.AddRange(GetLeaves(item.Nodes));
  51. }
  52. }
  53. return list;
  54. }
  55. public int Depth
  56. {
  57. get
  58. {
  59. return this.GetDepth(this, 1);
  60. }
  61. }
  62. public bool IsLeaf
  63. {
  64. get
  65. {
  66. return (this.Nodes.Count == 0);
  67. }
  68. }
  69. public int LeafCount
  70. {
  71. get
  72. {
  73. int num = 0;
  74. List<TNode> list = new List<TNode> {
  75. this
  76. };
  77. for (int i = 0; i < list.Count; i++)
  78. {
  79. TNode node = list[i];
  80. if (node.IsLeaf)
  81. {
  82. num++;
  83. }
  84. else
  85. {
  86. list.AddRange(node.Nodes);
  87. }
  88. }
  89. return num;
  90. }
  91. }
  92. public int Level
  93. {
  94. get
  95. {
  96. int num = 1;
  97. for (TNode node = this.Parent; node != null; node = node.Parent)
  98. {
  99. num++;
  100. }
  101. return num;
  102. }
  103. }
  104. public string Name { get; set; }
  105. public List<TNode> Nodes { get; private set; }
  106. public TNode Parent { get; internal set; }
  107. public object Tag { get; set; }
  108. }
  109. public class TNode<T> where T : TNode<T>
  110. {
  111. public TNode()
  112. {
  113. this.Nodes = new List<T>();
  114. }
  115. public void AddChild(T node)
  116. {
  117. node.Parent = this as T;
  118. this.Nodes.Add(node);
  119. }
  120. public void AddChildren(List<T> nodes)
  121. {
  122. T p = this as T;
  123. this.Nodes.ForEach(n => n.Parent = p);
  124. this.Nodes.AddRange(nodes);
  125. }
  126. /// <summary>
  127. /// 获取所有节点
  128. /// </summary>
  129. /// <returns></returns>
  130. public List<T> GetAllNodes()
  131. {
  132. T local = this as T;
  133. if (local == null)
  134. {
  135. return new List<T>();
  136. }
  137. List<T> list = new List<T> {
  138. local
  139. };
  140. for (int i = 0; i < list.Count; i++)
  141. {
  142. T local2 = list[i];
  143. list.AddRange(local2.Nodes);
  144. }
  145. return list;
  146. }
  147. private int GetDepth(TNode<T> node, int currentDepth)
  148. {
  149. int num = currentDepth;
  150. if (!node.IsLeaf)
  151. {
  152. foreach (T local in node.Nodes)
  153. {
  154. num = Math.Max(this.GetDepth(local, currentDepth + 1), num);
  155. }
  156. }
  157. return num;
  158. }
  159. /// <summary>
  160. /// 获取所有叶节点
  161. /// </summary>
  162. /// <returns></returns>
  163. public List<T> GetLeaves()
  164. {
  165. return TNode<T>.GetLeaves(new List<T> { this as T });
  166. }
  167. public static List<T> GetLeaves(List<T> nodes)
  168. {
  169. List<T> list = new List<T>();
  170. for (int i = 0; i < nodes.Count; i++)
  171. {
  172. T item = nodes[i];
  173. if (item.IsLeaf)
  174. {
  175. list.Add(item);
  176. }
  177. else
  178. {
  179. list.AddRange(TNode<T>.GetLeaves(item.Nodes));
  180. }
  181. }
  182. return list;
  183. }
  184. /// <summary>
  185. /// 获取节点深度
  186. /// </summary>
  187. public int Depth
  188. {
  189. get
  190. {
  191. return this.GetDepth((TNode<T>)this, 1);
  192. }
  193. }
  194. /// <summary>
  195. /// 是否为叶节点
  196. /// </summary>
  197. public bool IsLeaf
  198. {
  199. get
  200. {
  201. return (this.Nodes.Count == 0);
  202. }
  203. }
  204. public int LeafCount
  205. {
  206. get
  207. {
  208. int num = 0;
  209. List<TNode<T>> list = new List<TNode<T>> {
  210. this
  211. };
  212. for (int i = 0; i < list.Count; i++)
  213. {
  214. TNode<T> node = list[i];
  215. if (node.IsLeaf)
  216. {
  217. num++;
  218. }
  219. else
  220. {
  221. list.AddRange(node.Nodes);
  222. }
  223. }
  224. return num;
  225. }
  226. }
  227. /// <summary>
  228. /// 当前node的级别
  229. /// </summary>
  230. public int Level
  231. {
  232. get
  233. {
  234. int num = 1;
  235. for (TNode<T> node = this.Parent; node != null; node = node.Parent)
  236. {
  237. num++;
  238. }
  239. return num;
  240. }
  241. }
  242. public string Name { get; set; }
  243. public List<T> Nodes { get; private set; }
  244. public T Parent { get; set; }
  245. public object Tag { get; set; }
  246. #region 便捷查找
  247. public T FirstOrDefault()
  248. {
  249. return this as T;
  250. }
  251. /// <summary>
  252. /// 查找第一个满足条件的节点
  253. /// </summary>
  254. /// <param name="predicateNode"></param>
  255. /// <returns></returns>
  256. public T FirstOrDefault(Predicate<T> predicateNode)
  257. {
  258. if (predicateNode == null)
  259. return this as T;
  260. var usePredicate = predicateNode;
  261. var queueNodes = new Queue<T>();
  262. queueNodes.Enqueue(this as T);
  263. while (queueNodes.Any())
  264. {
  265. var useNode = queueNodes.Dequeue();
  266. if (usePredicate(useNode))
  267. {
  268. return useNode;
  269. }
  270. useNode.Nodes.ToList().ForEach(queueNodes.Enqueue);
  271. }
  272. return null;
  273. }
  274. #endregion
  275. }
  276. }