소스 검색

树形实体接口

懒得勤快 5 년 전
부모
커밋
11ef41b536

+ 0 - 1
Masuit.Tools.Core/Masuit.Tools.Core.csproj

@@ -159,7 +159,6 @@ github:https://github.com/ldqk/Masuit.Tools</Description>
     <Folder Include="Mapping\Helper\" />
     <Folder Include="Mapping\Visitor\" />
     <Folder Include="Media\" />
-    <Folder Include="Models\" />
     <Folder Include="RandomSelector\" />
     <Folder Include="Reflection\" />
     <Folder Include="Security\" />

+ 80 - 0
Masuit.Tools.Core/Models/ITree.cs

@@ -0,0 +1,80 @@
+using System.Collections.Generic;
+using System.Linq;
+
+namespace Masuit.Tools.Core.Models
+{
+    /// <summary>
+    /// 树形实体接口
+    /// </summary>
+    /// <typeparam name="T"></typeparam>
+    public interface ITree<T> where T : ITree<T>
+    {
+        /// <summary>
+        /// 名字
+        /// </summary>
+        public string Name { get; set; }
+
+        /// <summary>
+        /// 父节点
+        /// </summary>
+        public T Parent { get; set; }
+
+        /// <summary>
+        /// 子级
+        /// </summary>
+        public ICollection<T> Children { get; set; }
+
+        /// <summary>
+        /// 所有子级
+        /// </summary>
+        public ICollection<T> AllChildren => GetChildren(this);
+
+        /// <summary>
+        /// 所有父级
+        /// </summary>
+        public ICollection<T> AllParent => GetParents(this);
+
+        /// <summary>
+        /// 是否是根节点
+        /// </summary>
+        public bool IsRoot => Parent == null;
+
+        /// <summary>
+        /// 是否是叶子节点
+        /// </summary>
+        public bool IsLeaf => Children.Count == 0;
+
+        /// <summary>
+        /// 深度
+        /// </summary>
+        public int Level => IsRoot ? 0 : Parent.Level + 1;
+
+        /// <summary>
+        /// 节点路径(UNIX路径格式,以“/”分隔)
+        /// </summary>
+        public string Path => GetFullPath(this);
+
+        private string GetFullPath(ITree<T> c) => c.Parent != null ? GetFullPath(c.Parent) + "/" + c.Name : c.Name;
+
+        /// <summary>
+        /// 递归取出所有下级
+        /// </summary>
+        /// <param name="t"></param>
+        /// <returns></returns>
+        private List<T> GetChildren(ITree<T> t)
+        {
+            return t.Children.Union(t.Children.Where(c => c.Children.Any()).SelectMany(tree => GetChildren(tree))).ToList();
+        }
+
+        /// <summary>
+        /// 递归取出所有下级
+        /// </summary>
+        /// <param name="t"></param>
+        /// <returns></returns>
+        private List<T> GetParents(ITree<T> t)
+        {
+            var list = new List<T>() { t.Parent };
+            return t.Parent != null ? list.Union(GetParents(t.Parent)).ToList() : list;
+        }
+    }
+}

+ 62 - 0
Masuit.Tools.Core/Models/TreeExtensions.cs

@@ -0,0 +1,62 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+
+namespace Masuit.Tools.Core.Models
+{
+    /// <summary>
+    /// 树形数据扩展
+    /// </summary>
+    public static class TreeExtensions
+    {
+        /// <summary>
+        /// 过滤
+        /// </summary>
+        /// <typeparam name="T"></typeparam>
+        /// <param name="items"></param>
+        /// <param name="func"></param>
+        /// <returns></returns>
+        public static IEnumerable<T> Filter<T>(this IEnumerable<T> items, Func<T, bool> func) where T : class, ITree<T>
+        {
+            var results = new List<T>();
+            foreach (var item in items.Where(i => i != null))
+            {
+                item.Children = item.Children.Filter(func).ToList();
+                if (item.Children.Any() || func(item))
+                {
+                    results.Add(item);
+                }
+            }
+            return results;
+        }
+
+        /// <summary>
+        /// 过滤
+        /// </summary>
+        /// <typeparam name="T"></typeparam>
+        /// <param name="item"></param>
+        /// <param name="func"></param>
+        /// <returns></returns>
+        public static IEnumerable<T> Filter<T>(this T item, Func<T, bool> func) where T : class, ITree<T>
+        {
+            return (new[] { item }).Filter(func);
+        }
+
+        /// <summary>
+        /// 平铺开
+        /// </summary>
+        /// <typeparam name="T"></typeparam>
+        /// <param name="items"></param>
+        /// <returns></returns>
+        public static IEnumerable<T> Flatten<T>(this IEnumerable<T> items) where T : ITree<T>
+        {
+            var result = new List<T>();
+            foreach (var item in items)
+            {
+                result.Add(item);
+                result.AddRange(item.Children.Flatten());
+            }
+            return result;
+        }
+    }
+}

+ 0 - 2
Masuit.Tools/Masuit.Tools.csproj.DotSettings

@@ -1,2 +0,0 @@
-<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
-	<s:String x:Key="/Default/CodeInspection/CSharpLanguageProject/LanguageLevel/@EntryValue">CSharp80</s:String></wpf:ResourceDictionary>