HOG2
OpenClosedList.h
Go to the documentation of this file.
1 /*
2  * $Id: OpenClosedList.h
3  * hog2
4  *
5  * Created by Nathan Sturtevant on 1/14/06.
6  * Modified by Nathan Sturtevant on 02/29/20.
7  *
8  * This file is part of HOG2. See https://github.com/nathansttt/hog2 for licensing information.
9  *
10  */
11 
12 #ifndef OpenClosedList_H
13 #define OpenClosedList_H
14 
15 #include <cassert>
16 #include <vector>
17 #include <stdio.h>
18 #include <stdint.h>
19 #include <unordered_map>
20 
26 template<typename OBJ, class HashKey, class EqKey, class CmpKey>
28 public:
31  void reset();
32  void Add(OBJ val);
33  void DecreaseKey(OBJ val);
34  void IncreaseKey(OBJ val);
35  bool IsIn(const OBJ val) const;
36  OBJ Remove();
37  void pop() { Remove(); }
38  const OBJ peek() const { return _elts[0]; }
39  OBJ top() { return _elts[0]; }
40  OBJ find(OBJ val);
41  bool Empty();
42  unsigned size() { return _elts.size(); }
43  // void verifyData();
44 private:
45  std::vector<OBJ> _elts;
46  void HeapifyUp(unsigned int index);
47  void HeapifyDown(unsigned int index);
48  typedef std::unordered_map<OBJ, unsigned int, HashKey, EqKey > IndexTable;
50 };
51 
52 
53 template<typename OBJ, class HashKey, class EqKey, class CmpKey>
55 {
56 }
57 
58 template<typename OBJ, class HashKey, class EqKey, class CmpKey>
60 {
61 }
62 
66 template<typename OBJ, class HashKey, class EqKey, class CmpKey>
68 {
69  table.clear();
70  _elts.resize(0);
71 }
72 
76 template<typename OBJ, class HashKey, class EqKey, class CmpKey>
78 {
79  if ((table.find(val) != table.end()))
80  {
81  printf("Trying to add duplicate node.\n");
82  assert(table.find(val) == table.end());
83  }
84  table[val] = _elts.size();
85  // val->key = count;
86  _elts.push_back(val);
87  //count++;
88  // HeapifyUp(val->key);
89  HeapifyUp(table[val]);
90 }
91 
95 template<typename OBJ, class HashKey, class EqKey, class CmpKey>
97 {
98  EqKey eq;
99  assert(eq(_elts[table[val]], val));
100  //HeapifyUp(val->key);
101  _elts[table[val]] = val;
102  HeapifyUp(table[val]);
103 }
104 
108 template<typename OBJ, class HashKey, class EqKey, class CmpKey>
110 {
111  EqKey eq;
112  assert(eq(_elts[table[val]], val));
113  _elts[table[val]] = val;
114  HeapifyDown(table[val]);
115 }
116 
120 template<typename OBJ, class HashKey, class EqKey, class CmpKey>
122 {
123  EqKey eq;
124  // typedef std::unordered_map<OBJ, unsigned int, HashKey, EqKey > IndexTable;
125  typename IndexTable::const_iterator it;
126  it = table.find(val);
127  if (it != table.end())
128  {
129  const unsigned int stored = (*it).second;
130  if (stored < _elts.size())
131  {
132  if (eq(_elts[stored], val))
133  return true;
134  }
135  }
136  return false;
137 }
138 
142 template<typename OBJ, class HashKey, class EqKey, class CmpKey>
144 {
145  if (Empty())
146  return OBJ();
147  // count--;
148  OBJ ans = _elts[0];
149  _elts[0] = _elts[_elts.size()-1];
150  table[_elts[0]] = 0;
151  //_elts[0]->key = 0;
152  _elts.pop_back();
153  table.erase(ans);
154  HeapifyDown(0);
155 
156  return ans;
157 }
158 
162 template<typename OBJ, class HashKey, class EqKey, class CmpKey>
164 {
165  if (!IsIn(val))
166  return OBJ();
167  return _elts[table[val]];
168 }
169 
173 template<typename OBJ, class HashKey, class EqKey, class CmpKey>
175 {
176  return _elts.size() == 0;
177 }
178 
180 //* Verify that the Heap is internally consistent. Fails assertion if not.
181 // */
182 //template<typename OBJ, class HashKey, class EqKey, class CmpKey>
183 //void OpenClosedList<OBJ, HashKey, EqKey, CmpKey>::verifyData()
184 //{
185 // assert(_elts.size() == table.size());
186 // OpenClosedList::IndexTable::iterator iter;
187 // for (iter = table.begin(); iter != table.end(); iter++)
188 // {
189 // EqKey eq;
190 // assert(eq(iter->first, _elts[iter->second]));
191 // }
192 //}
193 
194 template<typename OBJ, class HashKey, class EqKey, class CmpKey>
196 {
197  if (index == 0) return;
198  int parent = (index-1)/2;
199  CmpKey compare;
200 
201  if (compare(_elts[parent], _elts[index]))
202  {
203  OBJ tmp = _elts[parent];
204  _elts[parent] = _elts[index];
205  _elts[index] = tmp;
206  table[_elts[parent]] = parent;
207  table[_elts[index]] = index;
208  EqKey eq;
209  assert(!eq(_elts[parent], _elts[index]));
210  HeapifyUp(parent);
211  }
212 }
213 
214 template<typename OBJ, class HashKey, class EqKey, class CmpKey>
216 {
217  CmpKey compare;
218  unsigned int child1 = index*2+1;
219  unsigned int child2 = index*2+2;
220  int which;
221  unsigned int count = _elts.size();
222  // find smallest child
223  if (child1 >= count)
224  return;
225  else if (child2 >= count)
226  which = child1;
227  //else if (fless(_elts[child1]->GetKey(), _elts[child2]->GetKey()))
228  else if (!(compare(_elts[child1], _elts[child2])))
229  which = child1;
230  else
231  which = child2;
232 
233  //if (fless(_elts[which]->GetKey(), _elts[index]->GetKey()))
234  if (!(compare(_elts[which], _elts[index])))
235  {
236  OBJ tmp = _elts[which];
237  _elts[which] = _elts[index];
238  table[_elts[which]] = which;
239  _elts[index] = tmp;
240  table[_elts[index]] = index;
241 // _elts[which]->key = which;
242 // _elts[index]->key = index;
243  HeapifyDown(which);
244  }
245 }
246 
247 #endif
OpenClosedList::DecreaseKey
void DecreaseKey(OBJ val)
Indicate that the key for a particular object has decreased.
Definition: OpenClosedList.h:96
OpenClosedList::top
OBJ top()
Definition: OpenClosedList.h:39
OpenClosedList::IsIn
bool IsIn(const OBJ val) const
Returns true if the object is in the OpenClosedList.
Definition: OpenClosedList.h:121
OpenClosedList::HeapifyDown
void HeapifyDown(unsigned int index)
Definition: OpenClosedList.h:215
OpenClosedList::Add
void Add(OBJ val)
Add object into OpenClosedList.
Definition: OpenClosedList.h:77
OpenClosedList::IndexTable
std::unordered_map< OBJ, unsigned int, HashKey, EqKey > IndexTable
Definition: OpenClosedList.h:48
OpenClosedList::pop
void pop()
Definition: OpenClosedList.h:37
OpenClosedList
A simple Heap class.
Definition: OpenClosedList.h:27
OpenClosedList::Remove
OBJ Remove()
Remove the item with the lowest key from the OpenClosedList & re-heapify.
Definition: OpenClosedList.h:143
OpenClosedList::HeapifyUp
void HeapifyUp(unsigned int index)
‍**
Definition: OpenClosedList.h:195
OpenClosedList::table
IndexTable table
Definition: OpenClosedList.h:49
OpenClosedList::OpenClosedList
OpenClosedList()
Definition: OpenClosedList.h:54
OpenClosedList::_elts
std::vector< OBJ > _elts
Definition: OpenClosedList.h:45
OpenClosedList::find
OBJ find(OBJ val)
find this object in the Heap and return
Definition: OpenClosedList.h:163
OpenClosedList::~OpenClosedList
~OpenClosedList()
Definition: OpenClosedList.h:59
OpenClosedList::IncreaseKey
void IncreaseKey(OBJ val)
Indicate that the key for a particular object has increased.
Definition: OpenClosedList.h:109
OpenClosedList::size
unsigned size()
Definition: OpenClosedList.h:42
OpenClosedList::peek
const OBJ peek() const
Definition: OpenClosedList.h:38
OpenClosedList::Empty
bool Empty()
Returns true if no items are in the OpenClosedList.
Definition: OpenClosedList.h:174
OpenClosedList::reset
void reset()
Remove all objects from queue.
Definition: OpenClosedList.h:67