List 的 Diff 功能

cs 复制代码
    public interface IListComparer
    {
        bool Contain<T>(T t) where T: ListCompareData;
    }
    public class ListCompareData : IListComparer
    {
        public int mId;
        public int mCompareId;

        public bool Contain<T>(T t) where T : ListCompareData
        {
            return (mId == t.mId && mCompareId == t.mCompareId);
        }
    }

    public static List<T> Diff<T>(this List<T> rList1, List<T> rList2, EListDiffType rDiffType) where T : ListCompareData
    {
        List<T> resultList = new List<T>();
        ///新增的数据, 2有1没有
        if (rDiffType == EListDiffType.Add)
        {
            foreach (T item2 in rList2)
            {
                bool isNew = true;
                foreach(T item1 in rList1)
                {
                    if (item1.Contain(item2))
                    {
                        isNew = false;
                        break;
                    }
                }
                if (isNew) resultList.Add(item2);
            }
        }
        //减少的数据,1有2没有
        else if (rDiffType == EListDiffType.Sub)
        {
            foreach (T item1 in rList1)
            {
                bool isRemoved = true;
                foreach (T item2 in rList2)
                {
                    if (item1.Contain(item2))
                    {
                        isRemoved = false;
                        break;  
                    }
                }
                if(isRemoved) resultList.Add(item1);
            }
        }
        return resultList;
    }
}

使用:

列表新增的内容:

var newEquips = list1.Diff<ListCompareData>(list2, Util.EListDiffType.Add);

列表减少的内容:

var oldEquips = list1.Diff<ListCompareData>(list2, Util.EListDiffType.Sub);

可以自行扩展ListCompareData的比较项,这里是使用了id 和 一个整形比较

相关推荐
心之所向,自强不息7 小时前
【Unity Shader编程】之让画面动起来
unity·游戏引擎
不伤欣1 天前
游戏设计模式 - 子类沙箱
游戏·unity·设计模式
Magnum Lehar1 天前
vulkan游戏引擎test文件memory实现
游戏引擎
Magnum Lehar1 天前
vulkan游戏引擎test_manager实现
java·算法·游戏引擎
快乐觉主吖1 天前
Unity的日志管理类
android·unity·游戏引擎
WarPigs2 天前
Unity性能优化笔记
笔记·unity·游戏引擎
T.D.C2 天前
【业务框架】3C-相机-Cinemachine
unity
一线灵2 天前
跨平台游戏引擎 Axmol-2.6.1 发布
游戏引擎
Clank的游戏栈2 天前
Unity基于GraphView的可视化关卡编辑器开发指南
unity·编辑器·游戏引擎
海尔辛3 天前
Unity UI 性能优化--Sprite 篇
ui·unity·性能优化