BinaryHeap.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  1. /****************************************************************************
  2. * Copyright (c) 2017 snowcold
  3. * Copyright (c) 2017 liangxie
  4. *
  5. * http://qframework.io
  6. * https://github.com/liangxiegame/QFramework
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documentation files (the "Software"), to deal
  10. * in the Software without restriction, including without limitation the rights
  11. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in
  16. * all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. * THE SOFTWARE.
  25. ****************************************************************************/
  26. namespace QFramework
  27. {
  28. using System;
  29. public enum BinaryHeapBuildMode
  30. {
  31. kNLog = 1,
  32. kN = 2,
  33. }
  34. public enum BinaryHeapSortMode
  35. {
  36. kMin = 0,
  37. kMax = 1,
  38. }
  39. //优先队列&二叉堆
  40. public class BinaryHeap<T> where T : IBinaryHeapElement
  41. {
  42. protected T[] mArray;
  43. protected float mGrowthFactor = 1.6f;
  44. protected int mLastChildIndex; //最后子节点的位置
  45. protected BinaryHeapSortMode mSortMode;
  46. public BinaryHeap(int minSize, BinaryHeapSortMode sortMode)
  47. {
  48. mSortMode = sortMode;
  49. mArray = new T[minSize];
  50. mLastChildIndex = 0;
  51. }
  52. public BinaryHeap(T[] dataArray, BinaryHeapSortMode sortMode)
  53. {
  54. mSortMode = sortMode;
  55. int minSize = 10;
  56. if (dataArray != null)
  57. {
  58. minSize = dataArray.Length + 1;
  59. }
  60. mArray = new T[minSize];
  61. mLastChildIndex = 0;
  62. Insert(dataArray, BinaryHeapBuildMode.kN);
  63. }
  64. #region 公开方法
  65. #region 清空
  66. public void Clear()
  67. {
  68. mArray = new T[10];
  69. mLastChildIndex = 0;
  70. }
  71. #endregion
  72. #region 插入
  73. public void Insert(T[] dataArray, BinaryHeapBuildMode buildMode)
  74. {
  75. if (dataArray == null)
  76. {
  77. throw new NullReferenceException("BinaryHeap Not Support Insert Null Object");
  78. }
  79. int totalLength = mLastChildIndex + dataArray.Length + 1;
  80. if (mArray.Length < totalLength)
  81. {
  82. ResizeArray(totalLength);
  83. }
  84. if (buildMode == BinaryHeapBuildMode.kNLog)
  85. {
  86. //方式1:直接添加,每次添加都会上浮
  87. for (int i = 0; i < dataArray.Length; ++i)
  88. {
  89. Insert(dataArray[i]);
  90. }
  91. }
  92. else
  93. {
  94. //数量比较大的情况下会快一些
  95. //方式2:先添加完,然后排序
  96. for (int i = 0; i < dataArray.Length; ++i)
  97. {
  98. mArray[++mLastChildIndex] = dataArray[i];
  99. }
  100. SortAsCurrentMode();
  101. }
  102. }
  103. public void Insert(T element)
  104. {
  105. if (element == null)
  106. {
  107. throw new NullReferenceException("BinaryHeap Not Support Insert Null Object");
  108. }
  109. int index = ++mLastChildIndex;
  110. if (index == mArray.Length)
  111. {
  112. ResizeArray();
  113. }
  114. mArray[index] = element;
  115. ProcolateUp(index);
  116. }
  117. #endregion
  118. #region 弹出
  119. public T Pop()
  120. {
  121. if (mLastChildIndex < 1)
  122. {
  123. return default(T);
  124. }
  125. T result = mArray[1];
  126. mArray[1] = mArray[mLastChildIndex--];
  127. ProcolateDown(1);
  128. return result;
  129. }
  130. public T Top()
  131. {
  132. if (mLastChildIndex < 1)
  133. {
  134. return default(T);
  135. }
  136. return mArray[1];
  137. }
  138. #endregion
  139. #region 重新排序
  140. public void Sort(BinaryHeapSortMode sortMode)
  141. {
  142. if (mSortMode == sortMode)
  143. {
  144. return;
  145. }
  146. mSortMode = sortMode;
  147. SortAsCurrentMode();
  148. }
  149. public void RebuildAtIndex(int index)
  150. {
  151. if (index > mLastChildIndex)
  152. {
  153. return;
  154. }
  155. //1.首先找父节点,是否比父节点小,如果满足则上浮,不满足下沉
  156. var element = mArray[index];
  157. int parentIndex = index >> 1;
  158. if (parentIndex > 0)
  159. {
  160. if (mSortMode == BinaryHeapSortMode.kMin)
  161. {
  162. if (element.SortScore < mArray[parentIndex].SortScore)
  163. {
  164. ProcolateUp(index);
  165. }
  166. else
  167. {
  168. ProcolateDown(index);
  169. }
  170. }
  171. else
  172. {
  173. if (element.SortScore > mArray[parentIndex].SortScore)
  174. {
  175. ProcolateUp(index);
  176. }
  177. else
  178. {
  179. ProcolateDown(index);
  180. }
  181. }
  182. }
  183. else
  184. {
  185. ProcolateDown(index);
  186. }
  187. }
  188. private void SortAsCurrentMode()
  189. {
  190. int startChild = mLastChildIndex >> 1;
  191. for (int i = startChild; i > 0; --i)
  192. {
  193. ProcolateDown(i);
  194. }
  195. }
  196. #endregion
  197. #region 指定位置删除
  198. public void RemoveAt(int index)
  199. {
  200. if (index > mLastChildIndex || index < 1)
  201. {
  202. return;
  203. }
  204. if (index == mLastChildIndex)
  205. {
  206. --mLastChildIndex;
  207. mArray[index] = default(T);
  208. return;
  209. }
  210. mArray[index] = mArray[mLastChildIndex--];
  211. mArray[index].HeapIndex = index;
  212. RebuildAtIndex(index);
  213. }
  214. #endregion
  215. #region 索引查找
  216. //这个索引和大小排序之间没有任何关系
  217. public T GetElement(int index)
  218. {
  219. if (index > mLastChildIndex)
  220. {
  221. return default(T);
  222. }
  223. return mArray[index];
  224. }
  225. #endregion
  226. #region 判定辅助
  227. public bool HasValue()
  228. {
  229. return mLastChildIndex > 0;
  230. }
  231. #endregion
  232. #region 内部方法
  233. protected void ResizeArray(int newSize = -1)
  234. {
  235. if (newSize < 0)
  236. {
  237. newSize = System.Math.Max(mArray.Length + 4, (int) System.Math.Round(mArray.Length * mGrowthFactor));
  238. }
  239. if (newSize > 1 << 30)
  240. {
  241. throw new System.Exception(
  242. "Binary Heap Size really large (2^18). A heap size this large is probably the cause of pathfinding running in an infinite loop. " +
  243. "\nRemove this check (in BinaryHeap.cs) if you are sure that it is not caused by a bug");
  244. }
  245. T[] tmp = new T[newSize];
  246. for (int i = 0; i < mArray.Length; i++)
  247. {
  248. tmp[i] = mArray[i];
  249. }
  250. mArray = tmp;
  251. }
  252. //上浮:空穴思想
  253. protected void ProcolateUp(int index)
  254. {
  255. var element = mArray[index];
  256. if (element == null)
  257. {
  258. return;
  259. }
  260. float sortScore = element.SortScore;
  261. int parentIndex = index >> 1;
  262. if (mSortMode == BinaryHeapSortMode.kMin)
  263. {
  264. while (parentIndex >= 1 && sortScore < mArray[parentIndex].SortScore)
  265. {
  266. mArray[index] = mArray[parentIndex];
  267. mArray[index].HeapIndex = index;
  268. index = parentIndex;
  269. parentIndex = index >> 1;
  270. }
  271. }
  272. else
  273. {
  274. while (parentIndex >= 1 && sortScore > mArray[parentIndex].SortScore)
  275. {
  276. mArray[index] = mArray[parentIndex];
  277. mArray[index].HeapIndex = index;
  278. index = parentIndex;
  279. parentIndex = index >> 1;
  280. }
  281. }
  282. mArray[index] = element;
  283. mArray[index].HeapIndex = index;
  284. }
  285. protected void ProcolateDown(int index)
  286. {
  287. var element = mArray[index];
  288. if (element == null)
  289. {
  290. return;
  291. }
  292. int childIndex = index << 1;
  293. if (mSortMode == BinaryHeapSortMode.kMin)
  294. {
  295. while (childIndex <= mLastChildIndex)
  296. {
  297. if (childIndex != mLastChildIndex)
  298. {
  299. if (mArray[childIndex + 1].SortScore < mArray[childIndex].SortScore)
  300. {
  301. childIndex = childIndex + 1;
  302. }
  303. }
  304. if (mArray[childIndex].SortScore < element.SortScore)
  305. {
  306. mArray[index] = mArray[childIndex];
  307. mArray[index].HeapIndex = index;
  308. }
  309. else
  310. {
  311. break;
  312. }
  313. index = childIndex;
  314. childIndex = index << 1;
  315. }
  316. }
  317. else
  318. {
  319. while (childIndex <= mLastChildIndex)
  320. {
  321. if (childIndex != mLastChildIndex)
  322. {
  323. if (mArray[childIndex + 1].SortScore > mArray[childIndex].SortScore)
  324. {
  325. childIndex = childIndex + 1;
  326. }
  327. }
  328. if (mArray[childIndex].SortScore > element.SortScore)
  329. {
  330. mArray[index] = mArray[childIndex];
  331. mArray[index].HeapIndex = index;
  332. }
  333. else
  334. {
  335. break;
  336. }
  337. index = childIndex;
  338. childIndex = index << 1;
  339. }
  340. }
  341. mArray[index] = element;
  342. mArray[index].HeapIndex = index;
  343. }
  344. #endregion
  345. #endregion
  346. }
  347. }