115 IndexInHeapPropertyMap index_in_heap,
116 const Compare& compare = Compare(),
const Container& data = Container())
120 , index_in_heap(index_in_heap)
128 bool empty()
const {
return data.empty(); }
134 put(index_in_heap, v, index);
135 preserve_heap_property_up(index);
155 if (data.size() != 1)
157 data[0] = data.back();
160 preserve_heap_property_down();
176 preserve_heap_property_up(index);
193 put(index_in_heap, v, index);
195 preserve_heap_property_up(index);
199 DistanceMap
keys()
const {
return distance; }
204 DistanceMap distance;
205 IndexInHeapPropertyMap index_in_heap;
213 typedef typename std::remove_cv<
typename std::remove_reference<
decltype(
get(
214 std::declval<
typename std::remove_reference<DistanceMap>::type &>(),
215 std::declval<Value>()))>::type>::type distance_type;
223 return index * Arity + child_idx + 1;
230 Value value_a = data[index_a];
231 Value value_b = data[index_b];
232 data[index_a] = value_b;
233 data[index_b] = value_a;
234 put(index_in_heap, value_a, index_b);
235 put(index_in_heap, value_b, index_a);
239 bool compare_indirect(
const Value& a,
const Value& b)
const
241 return compare(
get(distance, a),
get(distance, b));
245 void verify_heap()
const
250 for (
size_t i = 1; i < data.size(); ++i) {
251 if (compare_indirect(data[i], data[parent(i)])) {
260 void preserve_heap_property_up(
size_type index)
269 Value currently_being_moved = data[index];
270 distance_type currently_being_moved_dist
271 =
get(distance, currently_being_moved);
277 Value parent_value = data[parent_index];
279 currently_being_moved_dist,
get(distance, parent_value)))
282 index = parent_index;
293 for (
size_type i = 0; i < num_levels_moved; ++i)
296 Value parent_value = data[parent_index];
297 put(index_in_heap, parent_value, index);
298 data[index] = parent_value;
299 index = parent_index;
301 data[index] = currently_being_moved;
302 put(index_in_heap, currently_being_moved, index);
308 void preserve_heap_property_down()
313 Value currently_being_moved = data[0];
314 distance_type currently_being_moved_dist
315 =
get(distance, currently_being_moved);
317 Value* data_ptr = &data[0];
320 size_type first_child_index = child(index, 0);
321 if (first_child_index >= heap_size)
323 Value* child_base_ptr = data_ptr + first_child_index;
325 distance_type smallest_child_dist
326 =
get(distance, child_base_ptr[smallest_child_index]);
327 if (first_child_index + Arity <= heap_size)
330 for (
size_t i = 1; i < Arity; ++i)
332 Value i_value = child_base_ptr[i];
333 distance_type i_dist =
get(distance, i_value);
334 if (compare(i_dist, smallest_child_dist))
336 smallest_child_index = i;
337 smallest_child_dist = i_dist;
343 for (
size_t i = 1; i < heap_size - first_child_index; ++i)
345 distance_type i_dist =
get(distance, child_base_ptr[i]);
346 if (compare(i_dist, smallest_child_dist))
348 smallest_child_index = i;
349 smallest_child_dist = i_dist;
353 if (compare(smallest_child_dist, currently_being_moved_dist))
356 smallest_child_index + first_child_index, index);
357 index = smallest_child_index + first_child_index;