5 #ifndef BDIndexOpenClosed_H
6 #define BDIndexOpenClosed_H
10 #include <unordered_map>
12 #include <unordered_map>
21 template<
typename state>
37 template<
typename state,
typename CmpKey0,
typename CmpKey1,
class dataStructure = BDIndexOpenClosedData<state> >
45 void Remove(uint64_t objKey);
49 inline const dataStructure &
Lookat(uint64_t objKey)
const {
return elements[objKey]; }
60 void Reopen(uint64_t objKey);
78 int child1 = index * 2 + 1;
79 int child2 = index * 2 + 2;
95 int child1 = index * 2 + 1;
96 int child2 = index * 2 + 2;
118 template<
typename state,
typename CmpKey0,
typename CmpKey1,
class dataStructure>
121 assert(elements[objKey].where ==
kClosed);
122 elements[objKey].reopened =
true;
124 elements[objKey].openLocation = priorityQueues[
kOpenWaiting].size();
133 template<
typename state,
typename CmpKey0,
typename CmpKey1,
class dataStructure>
137 std::vector<uint64_t> queue;
139 priorityQueues.push_back(queue);
140 priorityQueues.push_back(queue);
148 template<
typename state,
typename CmpKey0,
typename CmpKey1,
class dataStructure>
151 elements.resize(maxHash);
152 priorityQueues[0].resize(0);
153 priorityQueues[1].resize(0);
162 template<
typename state,
typename CmpKey0,
typename CmpKey1,
class dataStructure>
165 if (elements[hash].round == currRound)
172 elements[hash] = dataStructure(val, g, h, parent, priorityQueues[0].size() ,
kOpenReady);
176 elements[hash] = dataStructure(val, g, h, parent, priorityQueues[1].size(),
kOpenWaiting);
178 elements[hash].round = currRound;
181 elements[hash].parentID = hash;
184 priorityQueues[whichQueue].push_back(hash);
185 HeapifyUp(priorityQueues[whichQueue].size() - 1,whichQueue);
193 template<
typename state,
typename CmpKey0,
typename CmpKey1,
class dataStructure>
196 assert(elements[hash].currRound < currRound);
198 elements[hash] = dataStructure(val, g, h, parent, 0,
kClosed);
199 elements[hash].round = currRound;
201 elements[hash].parentID = hash;
209 template<
typename state,
typename CmpKey0,
typename CmpKey1,
class dataStructure>
218 if (!HeapifyUp(elements[val].openLocation,
kOpenReady))
219 HeapifyDown(elements[val].openLocation,
kOpenReady);
223 if (!HeapifyUp(elements[val].openLocation,
kOpenWaiting))
228 template<
typename state,
typename CmpKey0,
typename CmpKey1,
class dataStructure>
232 int index = elements[val].openLocation;
235 priorityQueues[whichQueue][index] = priorityQueues[whichQueue][priorityQueues[whichQueue].size() - 1];
236 elements[priorityQueues[whichQueue][index]].openLocation = index;
237 priorityQueues[whichQueue].pop_back();
239 if (!HeapifyUp(index, whichQueue))
240 HeapifyDown(index, whichQueue);
261 template<
typename state,
typename CmpKey0,
typename CmpKey1,
class dataStructure>
264 if (elements[hashKey].round != currRound)
267 return elements[objKey].where;
274 template<
typename state,
typename CmpKey0,
typename CmpKey1,
class dataStructure>
279 assert(OpenReadySize() != 0);
283 assert(OpenWaitingSize() != 0);
285 return priorityQueues[whichQueue][0];
291 template<
typename state,
typename CmpKey0,
typename CmpKey1,
class dataStructure>
296 assert(OpenReadySize() != 0);
300 assert(OpenWaitingSize() != 0);
302 return elements[priorityQueues[whichQueue][0]];
310 template<
typename state,
typename CmpKey0,
typename CmpKey1,
class dataStructure>
313 assert(OpenReadySize() != 0);
315 uint64_t ans = priorityQueues[0][0];
317 priorityQueues[0][0] = priorityQueues[0][OpenReadySize()-1];
318 elements[priorityQueues[0][0]].openLocation = 0;
319 priorityQueues[0].pop_back();
326 template<
typename state,
typename CmpKey0,
typename CmpKey1,
class dataStructure>
329 assert(OpenWaitingSize() != 0);
337 elements[back].openLocation = 0;
346 elements[ans].openLocation = priorityQueues[
kOpenReady].size()-1;
359 template<
typename state,
typename CmpKey0,
typename CmpKey1,
class dataStructure>
362 if (index == 0)
return false;
363 int parent = (index-1)/2;
368 if (compare(elements[priorityQueues[whichQueue][parent]], elements[priorityQueues[whichQueue][index]]))
370 unsigned int tmp = priorityQueues[whichQueue][parent];
371 priorityQueues[whichQueue][parent] = priorityQueues[whichQueue][index];
372 priorityQueues[whichQueue][index] = tmp;
373 elements[priorityQueues[whichQueue][parent]].openLocation = parent;
374 elements[priorityQueues[whichQueue][index]].openLocation = index;
375 HeapifyUp(parent, whichQueue);
382 if (compare(elements[priorityQueues[whichQueue][parent]], elements[priorityQueues[whichQueue][index]]))
384 unsigned int tmp = priorityQueues[whichQueue][parent];
385 priorityQueues[whichQueue][parent] = priorityQueues[whichQueue][index];
386 priorityQueues[whichQueue][index] = tmp;
387 elements[priorityQueues[whichQueue][parent]].openLocation = parent;
388 elements[priorityQueues[whichQueue][index]].openLocation = index;
389 HeapifyUp(parent, whichQueue);
398 template<
typename state,
typename CmpKey0,
typename CmpKey1,
class dataStructure>
402 unsigned int child1 = index*2+1;
403 unsigned int child2 = index*2+2;
411 unsigned int count = priorityQueues[whichQueue].size();
415 else if (child2 >= count)
417 else if (!(compare(elements[priorityQueues[whichQueue][child1]], elements[priorityQueues[whichQueue][child2]])))
423 if (!(compare(elements[priorityQueues[whichQueue][which]], elements[priorityQueues[whichQueue][index]])))
425 unsigned int tmp = priorityQueues[whichQueue][which];
426 priorityQueues[whichQueue][which] = priorityQueues[whichQueue][index];
428 priorityQueues[whichQueue][index] = tmp;
429 elements[priorityQueues[whichQueue][which]].openLocation = which;
430 elements[priorityQueues[whichQueue][index]].openLocation = index;
434 HeapifyDown(which,whichQueue);
441 unsigned int count = priorityQueues[whichQueue].size();
445 else if (child2 >= count)
448 else if (!(compare(elements[priorityQueues[whichQueue][child1]], elements[priorityQueues[whichQueue][child2]])))
454 if (!(compare(elements[priorityQueues[whichQueue][which]], elements[priorityQueues[whichQueue][index]])))
456 unsigned int tmp = priorityQueues[whichQueue][which];
457 priorityQueues[whichQueue][which] = priorityQueues[whichQueue][index];
458 priorityQueues[whichQueue][index] = tmp;
462 elements[priorityQueues[whichQueue][which]].openLocation = which;
463 elements[priorityQueues[whichQueue][index]].openLocation = index;
464 HeapifyDown(which, whichQueue);