HOG2
DVCBSOpenClosed.h
Go to the documentation of this file.
1 //
2 // DVCBSOpenClosed.h
3 // hog2 mac native demos
4 //
5 // Created by Shahaf S. Shperberg
6 //
7 
8 #ifndef DVCBSOpenClosed_h
9 #define DVCBSOpenClosed_h
10 
11 #include <cassert>
12 #include <vector>
13 #include <unordered_map>
14 #include <stdint.h>
15 #include "AStarOpenClosed.h"
16 #include <unordered_map>
17 #include <map>
18 #include <set>
19 
20 
21 template<typename state>
23 public:
25  DVCBSOpenClosedData(const state &theData, double gCost, double hCost, uint64_t parent, stateLocation location)
26  :data(theData), g(gCost), h(hCost), parentID(parent), where(location) { reopened = false; }
27  state data;
28  double g;
29  double h;
30  uint64_t parentID;
31  uint64_t openLocation;
32  bool reopened;
34 };
35 
36 template<typename state, class dataStructure = DVCBSOpenClosedData<state> >
38 public:
41  void Reset();
42  uint64_t AddOpenNode(const state &val, uint64_t hash, double g, double h, uint64_t parent=kTBDNoNode, stateLocation whichQueue = kOpenWaiting);
43  uint64_t AddClosedNode(state &val, uint64_t hash, double g, double h, uint64_t parent=kTBDNoNode);
44  void KeyChanged(uint64_t objKey,double oldVal);
45  void eraseFromNodesMap(stateLocation whichQueue,uint64_t val);
46  void eraseFromNodesMap(stateLocation whichQueue,uint64_t val,double oldKey);
48  void putInNodesMap(stateLocation whichQueue,uint64_t val);
49  void Remove(uint64_t objKey);
50  //void IncreaseKey(uint64_t objKey);
51  stateLocation Lookup(uint64_t hashKey, uint64_t &objKey) const;
52  inline dataStructure &Lookup(uint64_t objKey) { return elements[objKey]; }
53  inline const dataStructure &Lookat(uint64_t objKey) const { return elements[objKey]; }
54  double getFirstKey(stateLocation whichQueue) const { return nodesMap[whichQueue].begin()->first; }
55 
56  //if exist min pair, return true, else(one queue is empty) return false;
57  //if it returns true, left and right should be set to the pair that is supposed to be returned.
58  //bool ExtractMinPair(uint64_t& left, uint64_t& right) ;
59  uint64_t Close();
60  uint64_t CloseAtIndex(uint64_t index);
61  void PutToReady();
62  //void Reopen(uint64_t objKey);
63 
64  size_t OpenReadySize() const { return nodesMapSize[kOpenReady]; }
65  size_t OpenWaitingSize() const { return nodesMapSize[kOpenWaiting]; }
66  size_t OpenSize() const { return OpenReadySize()+OpenWaitingSize(); }
67 
68  std::map<double,std::set<uint64_t> >::iterator getNodesMapBegin(stateLocation whichQueue) {return nodesMap[whichQueue].begin();}
69  std::map<double,std::set<uint64_t> >::iterator getNodesMapEnd(stateLocation whichQueue) {return nodesMap[whichQueue].end();}
70  std::set<uint64_t> getNodesMapElements(stateLocation whichQueue,double key) {return nodesMap[whichQueue][key];}
71 
72 
73  size_t ClosedSize() const { return size()-OpenReadySize()-OpenWaitingSize(); }
74  size_t size() const { return elements.size(); }
75  void verifyData();
76 private:
77 
78  //2 queues:
79  //priorityQueues[0] is openReady, priorityQueues[1] is openWaiting
80  std::vector<std::map<double,std::set<uint64_t> > > nodesMap;
81  std::vector<double> nodesMapSize;
82  //std::vector<uint64_t> readyQueue;
83  //std::vector<uint64_t> waitingQueue;
84 
85  // storing the element id; looking up with...hash?
86  typedef std::unordered_map<uint64_t, uint64_t, AHash64> IndexTable;
87 
89  //all the elements, open or closed
90  std::vector<dataStructure> elements;
91 };
92 
93 
94 template<typename state, class dataStructure>
96 {
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);
103  //readyQueue.resize(0);
104  //waitingQueue.resize(0);
105 }
106 
107 template<typename state, class dataStructure>
109 {
110 }
111 
115 template<typename state, class dataStructure>
117 {
118  table.clear();
119  elements.clear();
120  nodesMap[0].clear();
121  nodesMap[1].clear();
122  nodesMapSize[0]=0;
123  nodesMapSize[1]=0;
124  //readyQueue.resize(0);
125  //waitingQueue.resize(0);
126 }
127 
131 template<typename state, class dataStructure>
132 uint64_t DVCBSOpenClosed<state, dataStructure>::AddOpenNode(const state &val, uint64_t hash, double g, double h, uint64_t parent, stateLocation whichQueue)
133 {
134  // should do lookup here...
135  if (table.find(hash) != table.end())
136  {
137  //return -1; // TODO: find correct id and return
138  assert(false);
139  }
140  if (whichQueue == kOpenReady)
141  {
142  elements.push_back(dataStructure(val, g, h, parent, kOpenReady));
143 
144  }
145  else if (whichQueue == kOpenWaiting)
146  {
147  elements.push_back(dataStructure(val, g, h, parent, kOpenWaiting));
148  }
149 
150  if (parent == kTBDNoNode)
151  elements.back().parentID = elements.size()-1;
152  table[hash] = elements.size()-1; // hashing to element list location
153 
154  putInNodesMap(whichQueue,elements.size() - 1);
155 
156  return elements.size()-1;
157 }
158 
162 template<typename state, class dataStructure>
163 uint64_t DVCBSOpenClosed<state, dataStructure>::AddClosedNode(state &val, uint64_t hash, double g, double h, uint64_t parent)
164 {
165  // should do lookup here...
166  assert(table.find(hash) == table.end());
167  elements.push_back(dataStructure(val, g, h, parent,kClosed));
168  if (parent == kTBDNoNode)
169  elements.back().parentID = elements.size()-1;
170  table[hash] = elements.size()-1; // hashing to element list location
171  return elements.size()-1;
172 }
173 
178 template<typename state, class dataStructure>
180 {
181  eraseFromNodesMap(elements[val].where,val,oldKey);
182  putInNodesMap(elements[val].where,val);
183 }
184 
185 template<typename state, class dataStructure>
187 {
188  double key;
189  if (whichQueue == kOpenReady){
190  key = elements[val].g;
191  }
192  else if (whichQueue == kOpenWaiting){
193  key = elements[val].g+elements[val].h;
194  }
195  else{
196  return;
197  }
198  if (nodesMap[whichQueue][key].insert(val).second){
199  nodesMapSize[whichQueue] += 1;
200  }
201 }
202 
203 template<typename state, class dataStructure>
205 {
206  double key;
207  if (whichQueue == kOpenReady){
208  key = elements[val].g;
209  }
210  else if (whichQueue == kOpenWaiting){
211  key = elements[val].g+elements[val].h;
212  }
213  else{
214  return;
215  }
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);
224  }
225  }
226  }
227 }
228 
229 template<typename state, class dataStructure>
231 {
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);
240  }
241  }
242  }
243 }
244 
245 template<typename state, class dataStructure>
247 {
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);
253  }
254 }
255 
256 template<typename state, class dataStructure>
258 {
259  stateLocation whichQueue = elements[val].where;
260  elements[val].where = kClosed;
261  eraseFromNodesMap(whichQueue,val);
262 }
263 
264 template<typename state, class dataStructure>
265 stateLocation DVCBSOpenClosed<state, dataStructure>::Lookup(uint64_t hashKey, uint64_t &objKey) const
266 {
267  typename IndexTable::const_iterator it;
268  it = table.find(hashKey);
269  if (it != table.end())
270  {
271  objKey = (*it).second;
272  return elements[objKey].where;
273  }
274  return kUnseen;
275 }
276 
277 template<typename state, class dataStructure>
279 {
280 
281  stateLocation whichQueue = elements[val].where;
282  elements[val].where = kClosed;
283  eraseFromNodesMap(whichQueue,val);
284  return val;
285 }
286 
287 template<typename state, class dataStructure>
289 {
290  assert(OpenWaitingSize() != 0);
291  auto container = nodesMap[kOpenWaiting].begin()->second;
292  for (auto i = container.begin();i != container.end(); i++){
293  putInNodesMap(kOpenReady,*i);
294  elements[*i].where = kOpenReady;
295  }
296  eraseFirstClusterFromNodesMap(kOpenWaiting);
297 }
298 
299 
300 
301 #endif /* DVCBSOpenClosed_h */
DVCBSOpenClosed
Definition: DVCBSOpenClosed.h:37
kOpenWaiting
@ kOpenWaiting
Definition: BDOpenClosed.h:24
DVCBSOpenClosedData::where
stateLocation where
Definition: DVCBSOpenClosed.h:33
DVCBSOpenClosed::nodesMap
std::vector< std::map< double, std::set< uint64_t > > > nodesMap
Definition: DVCBSOpenClosed.h:80
DVCBSOpenClosed::Remove
void Remove(uint64_t objKey)
Definition: DVCBSOpenClosed.h:257
DVCBSOpenClosedData::data
state data
Definition: DVCBSOpenClosed.h:27
DVCBSOpenClosedData
Definition: DVCBSOpenClosed.h:22
DVCBSOpenClosed::AddClosedNode
uint64_t AddClosedNode(state &val, uint64_t hash, double g, double h, uint64_t parent=kTBDNoNode)
Add object into closed list.
Definition: DVCBSOpenClosed.h:163
DVCBSOpenClosed::getNodesMapElements
std::set< uint64_t > getNodesMapElements(stateLocation whichQueue, double key)
Definition: DVCBSOpenClosed.h:70
kOpenReady
@ kOpenReady
Definition: BDOpenClosed.h:23
DVCBSOpenClosedData::reopened
bool reopened
Definition: DVCBSOpenClosed.h:32
AStarOpenClosed.h
DVCBSOpenClosed::eraseFirstClusterFromNodesMap
void eraseFirstClusterFromNodesMap(stateLocation whichQueue)
Definition: DVCBSOpenClosed.h:246
kClosed
@ kClosed
Definition: BDOpenClosed.h:25
DVCBSOpenClosed::PutToReady
void PutToReady()
Definition: DVCBSOpenClosed.h:288
DVCBSOpenClosed::OpenSize
size_t OpenSize() const
Definition: DVCBSOpenClosed.h:66
DVCBSOpenClosed::AddOpenNode
uint64_t AddOpenNode(const state &val, uint64_t hash, double g, double h, uint64_t parent=kTBDNoNode, stateLocation whichQueue=kOpenWaiting)
Add object into open list.
Definition: DVCBSOpenClosed.h:132
DVCBSOpenClosed::verifyData
void verifyData()
kUnseen
@ kUnseen
Definition: BDOpenClosed.h:26
DVCBSOpenClosed::OpenReadySize
size_t OpenReadySize() const
Definition: DVCBSOpenClosed.h:64
DVCBSOpenClosedData::DVCBSOpenClosedData
DVCBSOpenClosedData(const state &theData, double gCost, double hCost, uint64_t parent, stateLocation location)
Definition: DVCBSOpenClosed.h:25
DVCBSOpenClosed::elements
std::vector< dataStructure > elements
Definition: DVCBSOpenClosed.h:90
kTBDNoNode
const uint64_t kTBDNoNode
Definition: BDOpenClosed.h:30
DVCBSOpenClosedData::openLocation
uint64_t openLocation
Definition: DVCBSOpenClosed.h:31
DVCBSOpenClosedData::g
double g
Definition: DVCBSOpenClosed.h:28
stateLocation
stateLocation
Definition: BDOpenClosed.h:22
DVCBSOpenClosed::size
size_t size() const
Definition: DVCBSOpenClosed.h:74
DVCBSOpenClosed::Close
uint64_t Close()
DVCBSOpenClosed::DVCBSOpenClosed
DVCBSOpenClosed()
Definition: DVCBSOpenClosed.h:95
DVCBSOpenClosed::KeyChanged
void KeyChanged(uint64_t objKey, double oldVal)
Indicate that the key for a particular object has changed.
Definition: DVCBSOpenClosed.h:179
DVCBSOpenClosed::getNodesMapBegin
std::map< double, std::set< uint64_t > >::iterator getNodesMapBegin(stateLocation whichQueue)
Definition: DVCBSOpenClosed.h:68
DVCBSOpenClosed::eraseFromNodesMap
void eraseFromNodesMap(stateLocation whichQueue, uint64_t val)
Definition: DVCBSOpenClosed.h:204
DVCBSOpenClosed::getFirstKey
double getFirstKey(stateLocation whichQueue) const
Definition: DVCBSOpenClosed.h:54
DVCBSOpenClosed::putInNodesMap
void putInNodesMap(stateLocation whichQueue, uint64_t val)
Definition: DVCBSOpenClosed.h:186
DVCBSOpenClosed::~DVCBSOpenClosed
~DVCBSOpenClosed()
Definition: DVCBSOpenClosed.h:108
DVCBSOpenClosed::table
IndexTable table
Definition: DVCBSOpenClosed.h:88
DVCBSOpenClosed::Lookat
const dataStructure & Lookat(uint64_t objKey) const
Definition: DVCBSOpenClosed.h:53
DVCBSOpenClosedData::h
double h
Definition: DVCBSOpenClosed.h:29
DVCBSOpenClosed::getNodesMapEnd
std::map< double, std::set< uint64_t > >::iterator getNodesMapEnd(stateLocation whichQueue)
Definition: DVCBSOpenClosed.h:69
DVCBSOpenClosed::Reset
void Reset()
Remove all objects from queue.
Definition: DVCBSOpenClosed.h:116
DVCBSOpenClosed::ClosedSize
size_t ClosedSize() const
Definition: DVCBSOpenClosed.h:73
DVCBSOpenClosed::CloseAtIndex
uint64_t CloseAtIndex(uint64_t index)
Definition: DVCBSOpenClosed.h:278
DVCBSOpenClosedData::parentID
uint64_t parentID
Definition: DVCBSOpenClosed.h:30
DVCBSOpenClosed::Lookup
dataStructure & Lookup(uint64_t objKey)
Definition: DVCBSOpenClosed.h:52
DVCBSOpenClosed::IndexTable
std::unordered_map< uint64_t, uint64_t, AHash64 > IndexTable
Definition: DVCBSOpenClosed.h:86
DVCBSOpenClosed::nodesMapSize
std::vector< double > nodesMapSize
Definition: DVCBSOpenClosed.h:81
DVCBSOpenClosed::Lookup
stateLocation Lookup(uint64_t hashKey, uint64_t &objKey) const
Definition: DVCBSOpenClosed.h:265
DVCBSOpenClosedData::DVCBSOpenClosedData
DVCBSOpenClosedData()
Definition: DVCBSOpenClosed.h:24
DVCBSOpenClosed::OpenWaitingSize
size_t OpenWaitingSize() const
Definition: DVCBSOpenClosed.h:65