File size: 2,957 Bytes
ce81a16
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
using System;
using System.Collections;
using System.Collections.Generic;

namespace Quantum
{
	public class GOAPHeap : IEnumerable, IEnumerable<GOAPNode>
	{
		private GOAPNode[] _heap;
		private int _size;

		public GOAPHeap(int capacity)
		{
			_heap = new GOAPNode[capacity];
		}

		public GOAPHeap() : this(1024)
		{
		}

		public int Size { get { return _size; } }

		public void Clear()
		{
			_size = 0;

			// remove all stuff from heap
			Array.Clear(_heap, 0, _heap.Length);
		}

		public void Update(GOAPNode updateNode)
		{
			int bubbleIndex = -1;
			for (int i = 0; i < _size; i++)
			{
				var node = _heap[i];
				if (node.Hash == updateNode.Hash)
				{
					bubbleIndex = i;
					break;
				}
			}

			if (bubbleIndex < 0)
			{
				Log.Error($"Cannot update node: Node with hash {updateNode.Hash} is not present in the heap");
				return;
			}

			_heap[bubbleIndex] = updateNode;

			while (bubbleIndex != 0)
			{
				int parentIndex = (bubbleIndex - 1) / 2;

				if (_heap[parentIndex].F <= updateNode.F)
					break;

				_heap[bubbleIndex] = _heap[parentIndex];
				_heap[parentIndex] = updateNode;

				bubbleIndex = parentIndex;
			}
		}

		public void Push(GOAPNode node)
		{
			if (_size == _heap.Length)
			{
				ExpandHeap();
			}

			int bubbleIndex = _size;
			_heap[bubbleIndex] = node;

			_size++;

			while (bubbleIndex != 0)
			{
				int parentIndex = (bubbleIndex - 1) / 2;
				if (_heap[parentIndex].F <= node.F)
					break;

				_heap[bubbleIndex] = _heap[parentIndex];
				_heap[parentIndex] = node;

				bubbleIndex = parentIndex;
			}
		}

		public GOAPNode Pop()
		{
			GOAPNode returnItem = _heap[0];
			_heap[0] = _heap[_size - 1];

			_size--;

			int swapItem = 0;
			int parent = 0;

			do
			{
				parent = swapItem;

				int leftChild = 2 * parent + 1;
				int rightChild = 2 * parent + 2;

				if (rightChild <= _size)
				{
					int smallerChild = _heap[leftChild].F < _heap[rightChild].F ? leftChild : rightChild;

					if (_heap[parent].F >= _heap[smallerChild].F)
					{
						swapItem = smallerChild;
					}
				}
				else if (leftChild <= _size)
				{
					// Only one child exists
					if (_heap[parent].F >= _heap[leftChild].F)
					{
						swapItem = leftChild;
					}
				}

				// One if the parent's children are smaller or equal, swap them
				if (parent != swapItem)
				{
					GOAPNode tmpIndex = _heap[parent];

					_heap[parent] = _heap[swapItem];
					_heap[swapItem] = tmpIndex;
				}
			}
			while (parent != swapItem);

			return returnItem;
		}

		private void ExpandHeap()
		{
			// Double the size
			GOAPNode[] newHeap = new GOAPNode[_heap.Length * 2];

			Array.Copy(_heap, newHeap, _heap.Length);
			_heap = newHeap;
		}

		IEnumerator<GOAPNode> IEnumerable<GOAPNode>.GetEnumerator()
		{
			for (int i = 0; i < _size; i++)
			{
				yield return _heap[i];
			}
		}

		IEnumerator IEnumerable.GetEnumerator()
		{
			return (this as IEnumerable<GOAPNode>).GetEnumerator();
		}
	}
}