9 #ifndef hog2_glut_BucketOpenClosed_h
10 #define hog2_glut_BucketOpenClosed_h
15 template<
typename state,
typename CmpKey,
class dataStructure = AStarOpenClosedData<state> >
27 inline const dataStructure &
Lookat(uint64_t objKey)
const {
return elements[objKey]; }
28 uint64_t
Peek()
const;
30 void Reopen(uint64_t objKey);
40 uint64_t
Add(uint64_t key, uint64_t fCost);
41 void Remove(uint64_t key, uint64_t fCost);
51 typedef std::unordered_map<uint64_t, uint64_t, AHash64>
IndexTable;
56 template<
typename state,
typename CmpKey,
class dataStructure>
63 template<
typename state,
typename CmpKey,
class dataStructure>
71 template<
typename state,
typename CmpKey,
class dataStructure>
100 template<
typename state,
typename CmpKey,
class dataStructure>
104 if (table.find(hash) != table.end())
112 uint64_t
loc = Add(elements.size(), f);
113 elements.push_back(dataStructure(val, f, g, h, parent,
loc,
kOpenList));
115 elements.back().parentID = elements.size()-1;
116 table[hash] = elements.size()-1;
119 return elements.size()-1;
125 template<
typename state,
typename CmpKey,
class dataStructure>
129 assert(table.find(hash) == table.end());
130 elements.push_back(dataStructure(val, f, g, h, parent, 0,
kClosedList));
132 elements.back().parentID = elements.size()-1;
133 table[hash] = elements.size();
134 return elements.size()-1;
140 template<
typename state,
typename CmpKey,
class dataStructure>
148 Remove(val, elements[val].openLocation);
164 elements[val].openLocation = Add(val, elements[val].g+elements[val].h);
175 template<
typename state,
typename CmpKey,
class dataStructure>
178 typename IndexTable::const_iterator it;
179 it = table.find(hashKey);
180 if (it != table.end())
182 objKey = (*it).second;
183 return elements[objKey].where;
192 template<
typename state,
typename CmpKey,
class dataStructure>
195 assert(OpenSize() != 0);
197 assert(pQueue[minBucket].entries.size() > 0);
198 return pQueue[minBucket].entries.back();
207 template<
typename state,
typename CmpKey,
class dataStructure>
210 assert(OpenSize() != 0);
211 assert(pQueue[minBucket].entries.size() > 0);
214 uint64_t ans = pQueue[minBucket].entries.back();
215 pQueue[minBucket].entries.pop_back();
216 if (pQueue[minBucket].entries.size() == 0)
217 pQueue[minBucket].fCost = -1;
228 template<
typename state,
typename CmpKey,
class dataStructure>
233 g = elements[objKey].g;
234 h = elements[objKey].h;
236 elements[objKey].reopened =
true;
238 elements[objKey].openLocation = Add(objKey, g+h);
241 template<
typename state,
typename CmpKey,
class dataStructure>
246 { minBucket = -1;
return; }
249 for (
int x = 0; x < pQueue.size(); x++)
251 while (pQueue[x].entries.size() > 0 && pQueue[x].entries.back() == -1)
253 pQueue[x].entries.pop_back();
255 if (pQueue[x].entries.size() == 0)
257 pQueue[x].fCost = -1;
266 if (pQueue[x].fCost < pQueue[minBucket].fCost)
273 template<
typename state,
typename CmpKey,
class dataStructure>
276 for (
unsigned int x = 0; x < elements.size(); x++)
289 template<
typename state,
typename CmpKey,
class dataStructure>
293 printf(
"**pQueue[%d] items [%d, %d]:\n", OpenSize(), minBucket, minSubBucket);
294 for (
unsigned int x = 0; x < pQueue.size(); x++)
296 for (
unsigned int y = 0; y < pQueue[x].size(); y++)
298 if (pQueue[x][y].size() > 0)
299 printf(
"**[%d][%d] has %d elements\n", x, y, pQueue[x][y].size());
300 cnt += pQueue[x][y].size();
303 if (cnt != OpenSize())
305 assert(!
"ERROR; sizes inconsistent");
309 template<
typename state,
typename CmpKey,
class dataStructure>
312 for (
int x = 0; x < pQueue.size(); x++)
314 if (pQueue[x].fCost == fCost)
316 pQueue[x].entries.push_back(key);
318 return pQueue[x].entries.size()-1;
321 for (
int x = 0; x < pQueue.size(); x++)
323 if (pQueue[x].fCost == -1)
325 pQueue[x].fCost = fCost;
326 pQueue[x].entries.push_back(key);
328 return pQueue[x].entries.size()-1;
331 assert(!
"No space found to insert element");
334 template<
typename state,
typename CmpKey,
class dataStructure>
337 for (
int x = 0; x < pQueue.size(); x++)
339 if (pQueue[x].entries.size() > location && pQueue[x].entries[location] == key)
341 if (location == pQueue[x].entries.size()-1)
343 pQueue[x].entries.pop_back();
346 pQueue[x].entries[location] = -1;
347 if (pQueue[x].entries.size() == 1)
348 pQueue[x].fCost = -1;
354 assert(!
"Element not found to remove");