/**************************************************************************** * Copyright (c) 2020.10 ~ 2022 liangxiegame UNDER MIT LICENSE * * https://qframework.cn * https://github.com/liangxiegame/QFramework * https://gitee.com/liangxiegame/QFramework ****************************************************************************/ using System; using System.Collections; using System.Collections.Generic; using System.Linq; // ReSharper disable once CheckNamespace namespace QFramework { public abstract class Table : IEnumerable, IDisposable { public void Add(TDataItem item) { OnAdd(item); } public void Remove(TDataItem item) { OnRemove(item); } public void Clear() { OnClear(); } // 改,由于 TDataItem 是引用类型,所以直接改值即可。 public void Update() { } protected abstract void OnAdd(TDataItem item); protected abstract void OnRemove(TDataItem item); protected abstract void OnClear(); public abstract IEnumerator GetEnumerator(); IEnumerator IEnumerable.GetEnumerator() { return GetEnumerator(); } public void Dispose() { OnDispose(); } protected abstract void OnDispose(); } public class TableIndex: IDisposable { private Dictionary> mIndex = new Dictionary>(); private Func mGetKeyByDataItem = null; public TableIndex(Func keyGetter) { mGetKeyByDataItem = keyGetter; } public IDictionary> Dictionary { get { return mIndex; } } public void Add(TDataItem dataItem) { var key = mGetKeyByDataItem(dataItem); if (mIndex.ContainsKey(key)) { mIndex[key].Add(dataItem); } else { var list = ListPool.Get(); list.Add(dataItem); mIndex.Add(key, list); } } public void Remove(TDataItem dataItem) { var key = mGetKeyByDataItem(dataItem); mIndex[key].Remove(dataItem); } public IEnumerable Get(TKeyType key) { List retList = null; if (mIndex.TryGetValue(key, out retList)) { return retList; } // 返回一个空的集合 return Enumerable.Empty(); } public void Clear() { foreach (var value in mIndex.Values) { value.Clear(); } mIndex.Clear(); } public void Dispose() { foreach (var value in mIndex.Values) { value.Release2Pool(); } mIndex.Release2Pool(); mIndex = null; } } /// /// 链表对象池:存储相关对象 /// /// internal static class ListPool { /// /// 栈对象:存储多个List /// static Stack> mListStack = new Stack>(8); /// /// 出栈:获取某个List对象 /// /// public static List Get() { if (mListStack.Count == 0) { return new List(8); } return mListStack.Pop(); } /// /// 入栈:将List对象添加到栈中 /// /// public static void Release(List toRelease) { toRelease.Clear(); mListStack.Push(toRelease); } } /// /// 链表对象池 拓展方法类 /// internal static class ListPoolExtensions { /// /// 给List拓展 自身入栈 的方法 /// /// /// public static void Release2Pool(this List toRelease) { ListPool.Release(toRelease); } } /// /// 字典对象池:用于存储相关对象 /// /// /// internal class DictionaryPool { /// /// 栈对象:存储多个字典 /// static Stack> mListStack = new Stack>(8); /// /// 出栈:从栈中获取某个字典数据 /// /// public static Dictionary Get() { if (mListStack.Count == 0) { return new Dictionary(8); } return mListStack.Pop(); } /// /// 入栈:将字典数据存储到栈中 /// /// public static void Release(Dictionary toRelease) { toRelease.Clear(); mListStack.Push(toRelease); } } /// /// 对象池字典 拓展方法类 /// internal static class DictionaryPoolExtensions { /// /// 对字典拓展 自身入栈 的方法 /// /// /// /// public static void Release2Pool(this Dictionary toRelease) { DictionaryPool.Release(toRelease); } } }