TableKit.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. /****************************************************************************
  2. * Copyright (c) 2020.10 ~ 2022 liangxiegame UNDER MIT LICENSE
  3. *
  4. * https://qframework.cn
  5. * https://github.com/liangxiegame/QFramework
  6. * https://gitee.com/liangxiegame/QFramework
  7. ****************************************************************************/
  8. using System;
  9. using System.Collections;
  10. using System.Collections.Generic;
  11. using System.Linq;
  12. // ReSharper disable once CheckNamespace
  13. namespace QFramework
  14. {
  15. public abstract class Table<TDataItem> : IEnumerable<TDataItem>, IDisposable
  16. {
  17. public void Add(TDataItem item)
  18. {
  19. OnAdd(item);
  20. }
  21. public void Remove(TDataItem item)
  22. {
  23. OnRemove(item);
  24. }
  25. public void Clear()
  26. {
  27. OnClear();
  28. }
  29. // 改,由于 TDataItem 是引用类型,所以直接改值即可。
  30. public void Update()
  31. {
  32. }
  33. protected abstract void OnAdd(TDataItem item);
  34. protected abstract void OnRemove(TDataItem item);
  35. protected abstract void OnClear();
  36. public abstract IEnumerator<TDataItem> GetEnumerator();
  37. IEnumerator IEnumerable.GetEnumerator()
  38. {
  39. return GetEnumerator();
  40. }
  41. public void Dispose()
  42. {
  43. OnDispose();
  44. }
  45. protected abstract void OnDispose();
  46. }
  47. public class TableIndex<TKeyType, TDataItem>: IDisposable
  48. {
  49. private Dictionary<TKeyType, List<TDataItem>> mIndex = new Dictionary<TKeyType, List<TDataItem>>();
  50. private Func<TDataItem, TKeyType> mGetKeyByDataItem = null;
  51. public TableIndex(Func<TDataItem, TKeyType> keyGetter)
  52. {
  53. mGetKeyByDataItem = keyGetter;
  54. }
  55. public IDictionary<TKeyType, List<TDataItem>> Dictionary
  56. {
  57. get { return mIndex; }
  58. }
  59. public void Add(TDataItem dataItem)
  60. {
  61. var key = mGetKeyByDataItem(dataItem);
  62. if (mIndex.ContainsKey(key))
  63. {
  64. mIndex[key].Add(dataItem);
  65. }
  66. else
  67. {
  68. var list = ListPool<TDataItem>.Get();
  69. list.Add(dataItem);
  70. mIndex.Add(key, list);
  71. }
  72. }
  73. public void Remove(TDataItem dataItem)
  74. {
  75. var key = mGetKeyByDataItem(dataItem);
  76. mIndex[key].Remove(dataItem);
  77. }
  78. public IEnumerable<TDataItem> Get(TKeyType key)
  79. {
  80. List<TDataItem> retList = null;
  81. if (mIndex.TryGetValue(key, out retList))
  82. {
  83. return retList;
  84. }
  85. // 返回一个空的集合
  86. return Enumerable.Empty<TDataItem>();
  87. }
  88. public void Clear()
  89. {
  90. foreach (var value in mIndex.Values)
  91. {
  92. value.Clear();
  93. }
  94. mIndex.Clear();
  95. }
  96. public void Dispose()
  97. {
  98. foreach (var value in mIndex.Values)
  99. {
  100. value.Release2Pool();
  101. }
  102. mIndex.Release2Pool();
  103. mIndex = null;
  104. }
  105. }
  106. /// <summary>
  107. /// 链表对象池:存储相关对象
  108. /// </summary>
  109. /// <typeparam name="T"></typeparam>
  110. internal static class ListPool<T>
  111. {
  112. /// <summary>
  113. /// 栈对象:存储多个List
  114. /// </summary>
  115. static Stack<List<T>> mListStack = new Stack<List<T>>(8);
  116. /// <summary>
  117. /// 出栈:获取某个List对象
  118. /// </summary>
  119. /// <returns></returns>
  120. public static List<T> Get()
  121. {
  122. if (mListStack.Count == 0)
  123. {
  124. return new List<T>(8);
  125. }
  126. return mListStack.Pop();
  127. }
  128. /// <summary>
  129. /// 入栈:将List对象添加到栈中
  130. /// </summary>
  131. /// <param name="toRelease"></param>
  132. public static void Release(List<T> toRelease)
  133. {
  134. toRelease.Clear();
  135. mListStack.Push(toRelease);
  136. }
  137. }
  138. /// <summary>
  139. /// 链表对象池 拓展方法类
  140. /// </summary>
  141. internal static class ListPoolExtensions
  142. {
  143. /// <summary>
  144. /// 给List拓展 自身入栈 的方法
  145. /// </summary>
  146. /// <typeparam name="T"></typeparam>
  147. /// <param name="toRelease"></param>
  148. public static void Release2Pool<T>(this List<T> toRelease)
  149. {
  150. ListPool<T>.Release(toRelease);
  151. }
  152. }
  153. /// <summary>
  154. /// 字典对象池:用于存储相关对象
  155. /// </summary>
  156. /// <typeparam name="TKey"></typeparam>
  157. /// <typeparam name="TValue"></typeparam>
  158. internal class DictionaryPool<TKey, TValue>
  159. {
  160. /// <summary>
  161. /// 栈对象:存储多个字典
  162. /// </summary>
  163. static Stack<Dictionary<TKey, TValue>> mListStack = new Stack<Dictionary<TKey, TValue>>(8);
  164. /// <summary>
  165. /// 出栈:从栈中获取某个字典数据
  166. /// </summary>
  167. /// <returns></returns>
  168. public static Dictionary<TKey, TValue> Get()
  169. {
  170. if (mListStack.Count == 0)
  171. {
  172. return new Dictionary<TKey, TValue>(8);
  173. }
  174. return mListStack.Pop();
  175. }
  176. /// <summary>
  177. /// 入栈:将字典数据存储到栈中
  178. /// </summary>
  179. /// <param name="toRelease"></param>
  180. public static void Release(Dictionary<TKey, TValue> toRelease)
  181. {
  182. toRelease.Clear();
  183. mListStack.Push(toRelease);
  184. }
  185. }
  186. /// <summary>
  187. /// 对象池字典 拓展方法类
  188. /// </summary>
  189. internal static class DictionaryPoolExtensions
  190. {
  191. /// <summary>
  192. /// 对字典拓展 自身入栈 的方法
  193. /// </summary>
  194. /// <typeparam name="TKey"></typeparam>
  195. /// <typeparam name="TValue"></typeparam>
  196. /// <param name="toRelease"></param>
  197. public static void Release2Pool<TKey,TValue>(this Dictionary<TKey, TValue> toRelease)
  198. {
  199. DictionaryPool<TKey,TValue>.Release(toRelease);
  200. }
  201. }
  202. }