12 #ifndef ASTAROPENCLOSED_H
13 #define ASTAROPENCLOSED_H
19 #include <unordered_map>
24 {
return (
size_t)(x); }
35 template<
typename state>
51 template<
typename state>
73 template<
typename state,
typename CmpKey,
class dataStructure = AStarOpenClosedData<state> >
78 void Reset(
int val=0);
87 inline const dataStructure &
Lookat(uint64_t objKey)
const {
return elements[objKey]; }
88 void Remove(uint64_t hash);
89 uint64_t
Peek()
const;
90 uint64_t
Close(uint64_t objKey);
92 void Reopen(uint64_t objKey);
96 for (
int x = 0; x <
theHeap.size(); x++)
115 typedef std::unordered_map<uint64_t, uint64_t, AHash64>
IndexTable;
121 template<
typename state,
typename CmpKey,
class dataStructure>
126 template<
typename state,
typename CmpKey,
class dataStructure>
134 template<
typename state,
typename CmpKey,
class dataStructure>
145 template<
typename state,
typename CmpKey,
class dataStructure>
151 auto i = table.find(hash);
152 if (i != table.end())
156 uint64_t index = i->second;
157 if (
fless(g, elements[index].g))
159 elements[index].parentID = parent;
160 elements[index].g = g;
161 elements[index].f = f;
166 elements.push_back(dataStructure(val, f, g, h, parent, theHeap.size(),
kOpenList));
168 elements.back().parentID = elements.size()-1;
169 table[hash] = elements.size()-1;
170 theHeap.push_back(elements.size()-1);
171 HeapifyUp(theHeap.size()-1);
172 return elements.size()-1;
178 template<
typename state,
typename CmpKey,
class dataStructure>
183 auto i = table.find(hash);
184 if (i != table.end())
188 uint64_t index = i->second;
189 if (
fless(g, elements[index].g))
191 elements[index].parentID = parent;
192 elements[index].g = g;
197 elements.push_back(dataStructure(val, g, h, parent, theHeap.size(),
kOpenList));
199 elements.back().parentID = elements.size()-1;
200 table[hash] = elements.size()-1;
201 theHeap.push_back(elements.size()-1);
202 HeapifyUp(theHeap.size()-1);
203 return elements.size()-1;
209 template<
typename state,
typename CmpKey,
class dataStructure>
213 assert(table.find(hash) == table.end());
214 elements.push_back(dataStructure(val, f, g, h, parent, 0,
kClosedList));
216 elements.back().parentID = elements.size()-1;
217 table[hash] = elements.size()-1;
218 return elements.size()-1;
221 template<
typename state,
typename CmpKey,
class dataStructure>
225 assert(table.find(hash) == table.end());
226 elements.push_back(dataStructure(val, g, h, parent, 0,
kClosedList));
228 elements.back().parentID = elements.size()-1;
229 table[hash] = elements.size()-1;
230 return elements.size()-1;
236 template<
typename state,
typename CmpKey,
class dataStructure>
239 uint64_t index = table[hash];
240 uint64_t openLoc = elements[index].openLocation;
241 uint64_t swappedItem = theHeap.back();
242 table.erase(table.find(hash));
243 theHeap[openLoc] = theHeap.back();
245 elements[swappedItem].openLocation = openLoc;
252 template<
typename state,
typename CmpKey,
class dataStructure>
255 if (!HeapifyUp(elements[val].openLocation))
256 HeapifyDown(elements[val].openLocation);
262 template<
typename state,
typename CmpKey,
class dataStructure>
265 typename IndexTable::const_iterator it;
266 it = table.find(hashKey);
267 if (it != table.end())
269 objKey = (*it).second;
270 return elements[objKey].where;
279 template<
typename state,
typename CmpKey,
class dataStructure>
282 assert(OpenSize() != 0);
290 template<
typename state,
typename CmpKey,
class dataStructure>
293 assert(OpenSize() != 0);
294 uint64_t index = elements[objKey].openLocation;
295 uint64_t ans = theHeap[index];
296 assert(ans == objKey);
298 theHeap[index] = theHeap[theHeap.size()-1];
299 elements[theHeap[index]].openLocation = index;
301 if (!HeapifyUp(index))
310 template<
typename state,
typename CmpKey,
class dataStructure>
313 assert(OpenSize() != 0);
315 uint64_t ans = theHeap[0];
317 theHeap[0] = theHeap[theHeap.size()-1];
318 elements[theHeap[0]].openLocation = 0;
328 template<
typename state,
typename CmpKey,
class dataStructure>
332 elements[objKey].reopened =
true;
334 elements[objKey].openLocation = theHeap.size();
335 theHeap.push_back(objKey);
336 HeapifyUp(theHeap.size()-1);
342 template<
typename state,
typename CmpKey,
class dataStructure>
345 if (index == 0)
return false;
346 int parent = (index-1)/2;
349 if (compare(elements[theHeap[parent]], elements[theHeap[index]]))
351 unsigned int tmp = theHeap[parent];
352 theHeap[parent] = theHeap[index];
353 theHeap[index] = tmp;
354 elements[theHeap[parent]].openLocation = parent;
355 elements[theHeap[index]].openLocation = index;
362 template<
typename state,
typename CmpKey,
class dataStructure>
366 unsigned int child1 = index*2+1;
367 unsigned int child2 = index*2+2;
369 unsigned int count = theHeap.size();
373 else if (child2 >= count)
375 else if (!(compare(elements[theHeap[child1]], elements[theHeap[child2]])))
380 if (!(compare(elements[theHeap[which]], elements[theHeap[index]])))
382 unsigned int tmp = theHeap[which];
383 theHeap[which] = theHeap[index];
384 theHeap[index] = tmp;
385 elements[theHeap[which]].openLocation = which;
386 elements[theHeap[index]].openLocation = index;