110 IndexInHeapPropertyMap index_in_heap,
111 const Compare& compare = Compare(),
const Container& data = Container())
115 , index_in_heap(index_in_heap)
123 bool empty()
const {
return data.empty(); }
129 put(index_in_heap, v, index);
130 preserve_heap_property_up(index);
150 if (data.size() != 1)
152 data[0] = data.back();
155 preserve_heap_property_down();
171 preserve_heap_property_up(index);
188 put(index_in_heap, v, index);
190 preserve_heap_property_up(index);
194 DistanceMap
keys()
const {
return distance; }
199 DistanceMap distance;
200 IndexInHeapPropertyMap index_in_heap;
206 typedef typename std::remove_reference<DistanceMap>::type::mapped_type distance_type;
214 return index * Arity + child_idx + 1;
221 Value value_a = data[index_a];
222 Value value_b = data[index_b];
223 data[index_a] = value_b;
224 data[index_b] = value_a;
225 put(index_in_heap, value_a, index_b);
226 put(index_in_heap, value_b, index_a);
230 bool compare_indirect(
const Value& a,
const Value& b)
const
232 return compare(
get(distance, a),
get(distance, b));
236 void verify_heap()
const
241 for (
size_t i = 1; i < data.size(); ++i) {
242 if (compare_indirect(data[i], data[parent(i)])) {
251 void preserve_heap_property_up(
size_type index)
260 Value currently_being_moved = data[index];
261 distance_type currently_being_moved_dist
262 =
get(distance, currently_being_moved);
268 Value parent_value = data[parent_index];
270 currently_being_moved_dist,
get(distance, parent_value)))
273 index = parent_index;
284 for (
size_type i = 0; i < num_levels_moved; ++i)
287 Value parent_value = data[parent_index];
288 put(index_in_heap, parent_value, index);
289 data[index] = parent_value;
290 index = parent_index;
292 data[index] = currently_being_moved;
293 put(index_in_heap, currently_being_moved, index);
299 void preserve_heap_property_down()
304 Value currently_being_moved = data[0];
305 distance_type currently_being_moved_dist
306 =
get(distance, currently_being_moved);
308 Value* data_ptr = &data[0];
311 size_type first_child_index = child(index, 0);
312 if (first_child_index >= heap_size)
314 Value* child_base_ptr = data_ptr + first_child_index;
316 distance_type smallest_child_dist
317 =
get(distance, child_base_ptr[smallest_child_index]);
318 if (first_child_index + Arity <= heap_size)
321 for (
size_t i = 1; i < Arity; ++i)
323 Value i_value = child_base_ptr[i];
324 distance_type i_dist =
get(distance, i_value);
325 if (compare(i_dist, smallest_child_dist))
327 smallest_child_index = i;
328 smallest_child_dist = i_dist;
334 for (
size_t i = 1; i < heap_size - first_child_index; ++i)
336 distance_type i_dist =
get(distance, child_base_ptr[i]);
337 if (compare(i_dist, smallest_child_dist))
339 smallest_child_index = i;
340 smallest_child_dist = i_dist;
344 if (compare(smallest_child_dist, currently_being_moved_dist))
347 smallest_child_index + first_child_index, index);
348 index = smallest_child_index + first_child_index;