10 #include <unordered_map>
12 #include <unordered_map>
32 template<
typename state>
47 template<
typename state,
typename CmpKey0,
typename CmpKey1,
class dataStructure = BDOpenClosedData<state> >
56 void Remove(uint64_t objKey);
60 inline const dataStructure &
Lookat(uint64_t objKey)
const {
return elements[objKey]; }
71 void Reopen(uint64_t objKey);
89 int child1 = index * 2 + 1;
90 int child2 = index * 2 + 2;
106 int child1 = index * 2 + 1;
107 int child2 = index * 2 + 2;
127 std::unordered_map<uint64_t, size_t>
table;
134 template<
typename state,
typename CmpKey0,
typename CmpKey1,
class dataStructure>
137 assert(elements[objKey].where ==
kClosed);
138 elements[objKey].reopened =
true;
140 elements[objKey].openLocation = priorityQueues[
kOpenWaiting].size();
149 template<
typename state,
typename CmpKey0,
typename CmpKey1,
class dataStructure>
152 std::vector<uint64_t> queue;
154 priorityQueues.push_back(queue);
155 priorityQueues.push_back(queue);
160 template<
typename state,
typename CmpKey0,
typename CmpKey1,
class dataStructure>
168 template<
typename state,
typename CmpKey0,
typename CmpKey1,
class dataStructure>
173 priorityQueues[0].resize(0);
174 priorityQueues[1].resize(0);
182 template<
typename state,
typename CmpKey0,
typename CmpKey1,
class dataStructure>
186 if (table.find(hash) != table.end())
193 elements.push_back(dataStructure(val, g, h, parent, priorityQueues[0].size() ,
kOpenReady));
198 elements.push_back(dataStructure(val, g, h, parent, priorityQueues[1].size(),
kOpenWaiting));
202 elements.back().parentID = elements.size()-1;
203 table[hash] = elements.size()-1;
205 priorityQueues[whichQueue].push_back(elements.size() - 1);
206 HeapifyUp(priorityQueues[whichQueue].size() - 1,whichQueue);
208 return elements.size()-1;
214 template<
typename state,
typename CmpKey0,
typename CmpKey1,
class dataStructure>
218 assert(table.find(hash) == table.end());
219 elements.push_back(dataStructure(val, g, h, parent, 0,
kClosed));
221 elements.back().parentID = elements.size()-1;
222 table[hash] = elements.size()-1;
223 return elements.size()-1;
229 template<
typename state,
typename CmpKey0,
typename CmpKey1,
class dataStructure>
238 if (!HeapifyUp(elements[val].openLocation,
kOpenReady))
239 HeapifyDown(elements[val].openLocation,
kOpenReady);
243 if (!HeapifyUp(elements[val].openLocation,
kOpenWaiting))
248 template<
typename state,
typename CmpKey0,
typename CmpKey1,
class dataStructure>
252 int index = elements[val].openLocation;
255 priorityQueues[whichQueue][index] = priorityQueues[whichQueue][priorityQueues[whichQueue].size() - 1];
256 elements[priorityQueues[whichQueue][index]].openLocation = index;
257 priorityQueues[whichQueue].pop_back();
259 if (!HeapifyUp(index, whichQueue))
260 HeapifyDown(index, whichQueue);
281 template<
typename state,
typename CmpKey0,
typename CmpKey1,
class dataStructure>
284 auto it = table.find(hashKey);
285 if (it == table.end())
289 return elements[objKey].where;
296 template<
typename state,
typename CmpKey0,
typename CmpKey1,
class dataStructure>
301 assert(OpenReadySize() != 0);
305 assert(OpenWaitingSize() != 0);
307 return priorityQueues[whichQueue][0];
313 template<
typename state,
typename CmpKey0,
typename CmpKey1,
class dataStructure>
318 assert(OpenReadySize() != 0);
322 assert(OpenWaitingSize() != 0);
324 return elements[priorityQueues[whichQueue][0]];
332 template<
typename state,
typename CmpKey0,
typename CmpKey1,
class dataStructure>
335 assert(OpenReadySize() != 0);
337 uint64_t ans = priorityQueues[0][0];
339 priorityQueues[0][0] = priorityQueues[0][OpenReadySize()-1];
340 elements[priorityQueues[0][0]].openLocation = 0;
341 priorityQueues[0].pop_back();
348 template<
typename state,
typename CmpKey0,
typename CmpKey1,
class dataStructure>
351 assert(OpenWaitingSize() != 0);
359 elements[back].openLocation = 0;
368 elements[ans].openLocation = priorityQueues[
kOpenReady].size()-1;
381 template<
typename state,
typename CmpKey0,
typename CmpKey1,
class dataStructure>
384 if (index == 0)
return false;
385 int parent = (index-1)/2;
390 if (compare(elements[priorityQueues[whichQueue][parent]], elements[priorityQueues[whichQueue][index]]))
392 unsigned int tmp = priorityQueues[whichQueue][parent];
393 priorityQueues[whichQueue][parent] = priorityQueues[whichQueue][index];
394 priorityQueues[whichQueue][index] = tmp;
395 elements[priorityQueues[whichQueue][parent]].openLocation = parent;
396 elements[priorityQueues[whichQueue][index]].openLocation = index;
397 HeapifyUp(parent, whichQueue);
404 if (compare(elements[priorityQueues[whichQueue][parent]], elements[priorityQueues[whichQueue][index]]))
406 unsigned int tmp = priorityQueues[whichQueue][parent];
407 priorityQueues[whichQueue][parent] = priorityQueues[whichQueue][index];
408 priorityQueues[whichQueue][index] = tmp;
409 elements[priorityQueues[whichQueue][parent]].openLocation = parent;
410 elements[priorityQueues[whichQueue][index]].openLocation = index;
411 HeapifyUp(parent, whichQueue);
420 template<
typename state,
typename CmpKey0,
typename CmpKey1,
class dataStructure>
424 unsigned int child1 = index*2+1;
425 unsigned int child2 = index*2+2;
433 unsigned int count = priorityQueues[whichQueue].size();
437 else if (child2 >= count)
439 else if (!(compare(elements[priorityQueues[whichQueue][child1]], elements[priorityQueues[whichQueue][child2]])))
445 if (!(compare(elements[priorityQueues[whichQueue][which]], elements[priorityQueues[whichQueue][index]])))
447 unsigned int tmp = priorityQueues[whichQueue][which];
448 priorityQueues[whichQueue][which] = priorityQueues[whichQueue][index];
450 priorityQueues[whichQueue][index] = tmp;
451 elements[priorityQueues[whichQueue][which]].openLocation = which;
452 elements[priorityQueues[whichQueue][index]].openLocation = index;
456 HeapifyDown(which,whichQueue);
463 unsigned int count = priorityQueues[whichQueue].size();
467 else if (child2 >= count)
470 else if (!(compare(elements[priorityQueues[whichQueue][child1]], elements[priorityQueues[whichQueue][child2]])))
476 if (!(compare(elements[priorityQueues[whichQueue][which]], elements[priorityQueues[whichQueue][index]])))
478 unsigned int tmp = priorityQueues[whichQueue][which];
479 priorityQueues[whichQueue][which] = priorityQueues[whichQueue][index];
480 priorityQueues[whichQueue][index] = tmp;
484 elements[priorityQueues[whichQueue][which]].openLocation = which;
485 elements[priorityQueues[whichQueue][index]].openLocation = index;
486 HeapifyDown(which, whichQueue);