8 #ifndef DVCBSOpenClosed_h
9 #define DVCBSOpenClosed_h
13 #include <unordered_map>
16 #include <unordered_map>
21 template<
typename state>
36 template<
typename state,
class dataStructure = DVCBSOpenClosedData<state> >
44 void KeyChanged(uint64_t objKey,
double oldVal);
49 void Remove(uint64_t objKey);
53 inline const dataStructure &
Lookat(uint64_t objKey)
const {
return elements[objKey]; }
80 std::vector<std::map<double,std::set<uint64_t> > >
nodesMap;
86 typedef std::unordered_map<uint64_t, uint64_t, AHash64>
IndexTable;
94 template<
typename state,
class dataStructure>
97 std::map<double,std::set<uint64_t> > mapReady;
98 std::map<double,std::set<uint64_t> > mapWaiting;
99 nodesMap.push_back(mapReady);
100 nodesMap.push_back(mapWaiting);
101 nodesMapSize.push_back(0);
102 nodesMapSize.push_back(0);
107 template<
typename state,
class dataStructure>
115 template<
typename state,
class dataStructure>
131 template<
typename state,
class dataStructure>
135 if (table.find(hash) != table.end())
142 elements.push_back(dataStructure(val, g, h, parent,
kOpenReady));
147 elements.push_back(dataStructure(val, g, h, parent,
kOpenWaiting));
151 elements.back().parentID = elements.size()-1;
152 table[hash] = elements.size()-1;
154 putInNodesMap(whichQueue,elements.size() - 1);
156 return elements.size()-1;
162 template<
typename state,
class dataStructure>
166 assert(table.find(hash) == table.end());
167 elements.push_back(dataStructure(val, g, h, parent,
kClosed));
169 elements.back().parentID = elements.size()-1;
170 table[hash] = elements.size()-1;
171 return elements.size()-1;
178 template<
typename state,
class dataStructure>
181 eraseFromNodesMap(elements[val].where,val,oldKey);
182 putInNodesMap(elements[val].where,val);
185 template<
typename state,
class dataStructure>
190 key = elements[val].g;
193 key = elements[val].g+elements[val].h;
198 if (nodesMap[whichQueue][key].insert(val).second){
199 nodesMapSize[whichQueue] += 1;
203 template<
typename state,
class dataStructure>
208 key = elements[val].g;
211 key = elements[val].g+elements[val].h;
216 auto containerIter = nodesMap[whichQueue].find(key);
217 if (containerIter != nodesMap[whichQueue].end()){
218 auto toDelete = containerIter->second.find(val);
219 if (toDelete != containerIter->second.end()){
220 containerIter->second.erase(toDelete);
221 nodesMapSize[whichQueue] -= 1;
222 if (containerIter->second.empty()){
223 nodesMap[whichQueue].erase(containerIter);
229 template<
typename state,
class dataStructure>
232 auto containerIter = nodesMap[whichQueue].find(oldKey);
233 if (containerIter != nodesMap[whichQueue].end()){
234 auto toDelete = containerIter->second.find(val);
235 if (toDelete != containerIter->second.end()){
236 containerIter->second.erase(toDelete);
237 nodesMapSize[whichQueue] -= 1;
238 if (containerIter->second.empty()){
239 nodesMap[whichQueue].erase(containerIter);
245 template<
typename state,
class dataStructure>
248 auto containerIter = nodesMap[whichQueue].begin();
249 auto oldsize = nodesMap[whichQueue].size();
250 if (containerIter != nodesMap[whichQueue].end()){
251 nodesMapSize[whichQueue] -= containerIter->second.size();
252 nodesMap[whichQueue].erase(containerIter);
256 template<
typename state,
class dataStructure>
261 eraseFromNodesMap(whichQueue,val);
264 template<
typename state,
class dataStructure>
267 typename IndexTable::const_iterator it;
268 it = table.find(hashKey);
269 if (it != table.end())
271 objKey = (*it).second;
272 return elements[objKey].where;
277 template<
typename state,
class dataStructure>
283 eraseFromNodesMap(whichQueue,val);
287 template<
typename state,
class dataStructure>
290 assert(OpenWaitingSize() != 0);
291 auto container = nodesMap[
kOpenWaiting].begin()->second;
292 for (
auto i = container.begin();i != container.end(); i++){