summaryrefslogtreecommitdiff
path: root/Client/Assets/Scripts/XUtliPoolLib/XHeap.cs
blob: 16c29954c9c18d97b14d3ed8e776a42c85022e0b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
using System;
using System.Collections.Generic;

namespace XUtliPoolLib
{
	internal class XHeap<T> where T : IComparable<T>, IHere
	{
		public int HeapSize
		{
			get
			{
				return this._heapSize;
			}
		}

		private List<T> _heap = null;

		private int _heapSize = 0;

		public XHeap()
		{
			this._heap = new List<T>();
			this._heapSize = 0;
		}

		public void PushHeap(T item)
		{
			int count = this._heap.Count;
			bool flag = this._heapSize < count;
			if (flag)
			{
				this._heap[this._heapSize] = item;
			}
			else
			{
				this._heap.Add(item);
			}
			item.Here = this._heapSize;
			XHeap<T>.HeapifyUp(this._heap, this._heapSize);
			this._heapSize++;
		}

		public T PopHeap()
		{
			T result = default(T);
			bool flag = this._heapSize > 0;
			if (flag)
			{
				this._heapSize--;
				XHeap<T>.Swap(this._heap, 0, this._heapSize);
				XHeap<T>.HeapifyDown(this._heap, 0, this._heapSize);
				result = this._heap[this._heapSize];
				result.Here = -1;
				this._heap[this._heapSize] = default(T);
			}
			return result;
		}

		public void PopHeapAt(int Idx)
		{
			bool flag = this._heapSize > 0 && Idx >= 0 && Idx < this._heapSize;
			if (flag)
			{
				this._heapSize--;
				XHeap<T>.Swap(this._heap, Idx, this._heapSize);
				T t = this._heap[this._heapSize];
				bool flag2 = t.CompareTo(this._heap[Idx]) < 0;
				if (flag2)
				{
					XHeap<T>.HeapifyDown(this._heap, Idx, this._heapSize);
				}
				else
				{
					t = this._heap[this._heapSize];
					bool flag3 = t.CompareTo(this._heap[Idx]) > 0;
					if (flag3)
					{
						XHeap<T>.HeapifyUp(this._heap, Idx);
					}
				}
				t = this._heap[this._heapSize];
				t.Here = -1;
				this._heap[this._heapSize] = default(T);
			}
		}

		public void Adjust(T item, bool downwords)
		{
			bool flag = this._heapSize > 1;
			if (flag)
			{
				if (downwords)
				{
					XHeap<T>.HeapifyDown(this._heap, item.Here, this._heapSize);
				}
				else
				{
					XHeap<T>.HeapifyUp(this._heap, item.Here);
				}
			}
		}

		public static void MakeHeap(List<T> list)
		{
			for (int i = list.Count / 2 - 1; i >= 0; i--)
			{
				XHeap<T>.HeapifyDown(list, i, list.Count);
			}
		}

		public static void HeapSort(List<T> list)
		{
			bool flag = list.Count < 2;
			if (!flag)
			{
				XHeap<T>.MakeHeap(list);
				for (int i = list.Count - 1; i > 0; i--)
				{
					XHeap<T>.Swap(list, 0, i);
					XHeap<T>.HeapifyDown(list, 0, i);
				}
			}
		}

		public T Peek()
		{
			bool flag = this._heapSize > 0;
			T result;
			if (flag)
			{
				result = this._heap[0];
			}
			else
			{
				result = default(T);
			}
			return result;
		}

		public void Clear()
		{
			this._heap.Clear();
			this._heapSize = 0;
		}

		private static void HeapifyDown(List<T> heap, int curIdx, int heapSize)
		{
			while (curIdx < heapSize)
			{
				int num = 2 * curIdx + 1;
				int num2 = 2 * curIdx + 2;
				T other = heap[curIdx];
				int num3 = curIdx;
				bool flag;
				if (num < heapSize)
				{
					T t = heap[num];
					flag = (t.CompareTo(other) < 0);
				}
				else
				{
					flag = false;
				}
				bool flag2 = flag;
				if (flag2)
				{
					other = heap[num];
					num3 = num;
				}
				bool flag3;
				if (num2 < heapSize)
				{
					T t = heap[num2];
					flag3 = (t.CompareTo(other) < 0);
				}
				else
				{
					flag3 = false;
				}
				bool flag4 = flag3;
				if (flag4)
				{
					other = heap[num2];
					num3 = num2;
				}
				bool flag5 = num3 != curIdx;
				if (!flag5)
				{
					break;
				}
				XHeap<T>.Swap(heap, curIdx, num3);
				curIdx = num3;
			}
		}

		private static void HeapifyUp(List<T> heap, int curIdx)
		{
			while (curIdx > 0)
			{
				int num = (curIdx - 1) / 2;
				T other = heap[num];
				int num2 = num;
				bool flag;
				if (num >= 0)
				{
					T t = heap[curIdx];
					flag = (t.CompareTo(other) < 0);
				}
				else
				{
					flag = false;
				}
				bool flag2 = flag;
				if (flag2)
				{
					num2 = curIdx;
				}
				bool flag3 = num2 != num;
				if (!flag3)
				{
					break;
				}
				XHeap<T>.Swap(heap, num, num2);
				curIdx = num;
			}
		}

		private static void Swap(List<T> heap, int x, int y)
		{
			T value = heap[x];
			heap[x] = heap[y];
			T t = heap[x];
			t.Here = x;
			heap[y] = value;
			t = heap[y];
			t.Here = y;
		}
	}
}