20 #include <unordered_map>
27 template<
typename OBJ,
class HashKey,
class EqKey,
class CmpKey,
class SpecialKey,
class CmpKeyStrictExtract>
51 typedef std::unordered_map<OBJ, unsigned int, HashKey, EqKey >
IndexTable;
56 template<
typename OBJ,
class HashKey,
class EqKey,
class CmpKey,
class SpecialKey,
class CmpKeyStrictExtract>
61 template<
typename OBJ,
class HashKey,
class EqKey,
class CmpKey,
class SpecialKey,
class CmpKeyStrictExtract>
69 template<
typename OBJ,
class HashKey,
class EqKey,
class CmpKey,
class SpecialKey,
class CmpKeyStrictExtract>
79 template<
typename OBJ,
class HashKey,
class EqKey,
class CmpKey,
class SpecialKey,
class CmpKeyStrictExtract>
82 assert(table.find(val) == table.end());
83 table[val] = _elts.size();
88 HeapifyUp(table[val]);
94 template<
typename OBJ,
class HashKey,
class EqKey,
class CmpKey,
class SpecialKey,
class CmpKeyStrictExtract>
98 assert(eq(_elts[table[val]], val));
100 _elts[table[val]] = val;
101 HeapifyUp(table[val]);
107 template<
typename OBJ,
class HashKey,
class EqKey,
class CmpKey,
class SpecialKey,
class CmpKeyStrictExtract>
111 assert(eq(_elts[table[val]], val));
112 _elts[table[val]] = val;
113 HeapifyDown(table[val]);
119 template<
typename OBJ,
class HashKey,
class EqKey,
class CmpKey,
class SpecialKey,
class CmpKeyStrictExtract>
123 if ((table.find(val) != table.end()) && (table[val] < _elts.size()) &&
124 (eq(_elts[table[val]], val)))
132 template<
typename OBJ,
class HashKey,
class EqKey,
class CmpKey,
class SpecialKey,
class CmpKeyStrictExtract>
139 _elts[0] = _elts[_elts.size()-1];
152 template<
typename OBJ,
class HashKey,
class EqKey,
class CmpKey,
class SpecialKey,
class CmpKeyStrictExtract>
157 return _elts[table[val]];
163 template<
typename OBJ,
class HashKey,
class EqKey,
class CmpKey,
class SpecialKey,
class CmpKeyStrictExtract>
166 return _elts.size() == 0;
184 template<
typename OBJ,
class HashKey,
class EqKey,
class CmpKey,
class SpecialKey,
class CmpKeyStrictExtract>
187 if (index == 0)
return;
188 int parent = (index-1)/2;
191 if (compare(_elts[parent], _elts[index]))
193 OBJ tmp = _elts[parent];
194 _elts[parent] = _elts[index];
196 table[_elts[parent]] = parent;
197 table[_elts[index]] = index;
199 assert(!eq(_elts[parent], _elts[index]));
204 template<
typename OBJ,
class HashKey,
class EqKey,
class CmpKey,
class SpecialKey,
class CmpKeyStrictExtract>
208 unsigned int child1 = index*2+1;
209 unsigned int child2 = index*2+2;
211 unsigned int count = _elts.size();
215 else if (child2 >= count)
218 else if (!(compare(_elts[child1], _elts[child2])))
224 if (!(compare(_elts[which], _elts[index])))
226 OBJ tmp = _elts[which];
227 _elts[which] = _elts[index];
228 table[_elts[which]] = which;
230 table[_elts[index]] = index;
238 template<
typename OBJ,
class HashKey,
class EqKey,
class CmpKey,
class SpecialKey,
class CmpKeyStrictExtract>
241 std::deque<unsigned int> candidates;
242 CmpKeyStrictExtract extractor;
245 if (!
fless(extractor(_elts[0]), F))
248 unsigned int resultIndex = 0;
249 candidates.push_back(0);
251 while(candidates.size())
253 unsigned int current = candidates.front();
254 candidates.pop_front();
256 if (spk(_elts[resultIndex],_elts[current]))
258 resultIndex = current;
261 unsigned int count = _elts.size();
263 unsigned int child1 = current*2+1;
264 unsigned int child2 = current*2+2;
266 if (child1 < count &&
fless(extractor(_elts[child1]),F))
268 candidates.push_back(child1);
271 if (child2 < count &&
fless(extractor(_elts[child2]),F))
273 candidates.push_back(child2);
277 return _elts[resultIndex];
281 template<
typename OBJ,
class HashKey,
class EqKey,
class CmpKey,
class SpecialKey,
class CmpKeyStrictExtract>
284 std::deque<unsigned int> candidates;
285 CmpKeyStrictExtract extractor;
288 if (!
fequal(extractor(_elts[0]), F))
291 unsigned int resultIndex = 0;
292 candidates.push_back(0);
294 while(candidates.size())
296 unsigned int current = candidates.front();
297 candidates.pop_front();
299 if (spk(_elts[resultIndex],_elts[current]))
301 resultIndex = current;
304 unsigned int count = _elts.size();
306 unsigned int child1 = current*2+1;
307 unsigned int child2 = current*2+2;
309 if (child1 < count &&
fequal(extractor(_elts[child1]),F))
311 candidates.push_back(child1);
314 if (child2 < count &&
fequal(extractor(_elts[child2]),F))
316 candidates.push_back(child2);
320 return _elts[resultIndex];