12 #ifndef OpenClosedList_H
13 #define OpenClosedList_H
19 #include <unordered_map>
26 template<
typename OBJ,
class HashKey,
class EqKey,
class CmpKey>
35 bool IsIn(
const OBJ val)
const;
48 typedef std::unordered_map<OBJ, unsigned int, HashKey, EqKey >
IndexTable;
53 template<
typename OBJ,
class HashKey,
class EqKey,
class CmpKey>
58 template<
typename OBJ,
class HashKey,
class EqKey,
class CmpKey>
66 template<
typename OBJ,
class HashKey,
class EqKey,
class CmpKey>
76 template<
typename OBJ,
class HashKey,
class EqKey,
class CmpKey>
79 if ((table.find(val) != table.end()))
81 printf(
"Trying to add duplicate node.\n");
82 assert(table.find(val) == table.end());
84 table[val] = _elts.size();
89 HeapifyUp(table[val]);
95 template<
typename OBJ,
class HashKey,
class EqKey,
class CmpKey>
99 assert(eq(_elts[table[val]], val));
101 _elts[table[val]] = val;
102 HeapifyUp(table[val]);
108 template<
typename OBJ,
class HashKey,
class EqKey,
class CmpKey>
112 assert(eq(_elts[table[val]], val));
113 _elts[table[val]] = val;
114 HeapifyDown(table[val]);
120 template<
typename OBJ,
class HashKey,
class EqKey,
class CmpKey>
125 typename IndexTable::const_iterator it;
126 it = table.find(val);
127 if (it != table.end())
129 const unsigned int stored = (*it).second;
130 if (stored < _elts.size())
132 if (eq(_elts[stored], val))
142 template<
typename OBJ,
class HashKey,
class EqKey,
class CmpKey>
149 _elts[0] = _elts[_elts.size()-1];
162 template<
typename OBJ,
class HashKey,
class EqKey,
class CmpKey>
167 return _elts[table[val]];
173 template<
typename OBJ,
class HashKey,
class EqKey,
class CmpKey>
176 return _elts.size() == 0;
194 template<
typename OBJ,
class HashKey,
class EqKey,
class CmpKey>
197 if (index == 0)
return;
198 int parent = (index-1)/2;
201 if (compare(_elts[parent], _elts[index]))
203 OBJ tmp = _elts[parent];
204 _elts[parent] = _elts[index];
206 table[_elts[parent]] = parent;
207 table[_elts[index]] = index;
209 assert(!eq(_elts[parent], _elts[index]));
214 template<
typename OBJ,
class HashKey,
class EqKey,
class CmpKey>
218 unsigned int child1 = index*2+1;
219 unsigned int child2 = index*2+2;
221 unsigned int count = _elts.size();
225 else if (child2 >= count)
228 else if (!(compare(_elts[child1], _elts[child2])))
234 if (!(compare(_elts[which], _elts[index])))
236 OBJ tmp = _elts[which];
237 _elts[which] = _elts[index];
238 table[_elts[which]] = which;
240 table[_elts[index]] = index;