static void remove_from_heap(const RandomAccessIterator &first, const RandomAccessIterator &item, const RandomAccessIterator &last, const LessComparer &less_comparer) { assert(last > first); assert(item >= first); assert(item < last); assert(is_heap(first, last, less_comparer)); typedef typename std::iterator_traits<RandomAccessIterator>::value_type value_type; const size_t new_heap_size = last - first - 1; const size_t hole_index = item - first; if (hole_index < new_heap_size) { value_type tmp = std::move(first[new_heap_size]); _move(first[new_heap_size], *item); if (less_comparer(tmp, first[new_heap_size])) { _sift_down(first, less_comparer, new_heap_size, hole_index, tmp); } else { _sift_up(first, less_comparer, 0, hole_index, tmp); } } assert(is_heap(first, last - 1, less_comparer)); }
void _heapify() { int start = (_data.size() - 2) / 2; while (start >= 0) { _sift_down(start); start--; } }
static void _pop_max_item(const RandomAccessIterator &first, const LessComparer &less_comparer, const size_t heap_size) { assert(heap_size > 0); typedef typename std::iterator_traits<RandomAccessIterator>::value_type value_type; value_type tmp = std::move(first[heap_size]); _move(first[heap_size], first[0]); _sift_down(first, less_comparer, heap_size, 0, tmp); }
static void restore_heap_after_item_decrease( const RandomAccessIterator &first, const RandomAccessIterator &item, const RandomAccessIterator &last, const LessComparer &less_comparer) { assert(last > first); assert(item >= first); assert(item < last); assert(is_heap(first, item, less_comparer)); typedef typename std::iterator_traits<RandomAccessIterator>::value_type value_type; const size_t heap_size = last - first; const size_t hole_index = item - first; value_type tmp = std::move(*item); _sift_down(first, less_comparer, heap_size, hole_index, tmp); assert(is_heap(first, last, less_comparer)); }
static void swap_max_item(const RandomAccessIterator &first, const RandomAccessIterator &last, typename std::iterator_traits<RandomAccessIterator>::value_type &item, const LessComparer &less_comparer) { assert(first < last); assert(is_heap(first, last, less_comparer)); typedef typename std::iterator_traits<RandomAccessIterator>::value_type value_type; const size_t heap_size = last - first; value_type tmp = std::move(item); _move(item, first[0]); _sift_down(first, less_comparer, heap_size, 0, tmp); assert(is_heap(first, last, less_comparer)); }
static void make_heap(const RandomAccessIterator &first, const RandomAccessIterator &last, const LessComparer &less_comparer) { assert(last >= first); typedef typename std::iterator_traits<RandomAccessIterator>::value_type value_type; const size_t heap_size = last - first; if (heap_size > 1) { // Skip leaf nodes without children. This is easy to do for non-paged // heap, i.e. when page_chunks = 1, but it is difficult for paged heaps. // So leaf nodes in paged heaps are visited anyway. size_t i = (PageChunks == 1) ? ((heap_size - 2) / Fanout) : (heap_size - 2); do { value_type item = std::move(first[i]); _sift_down(first, less_comparer, heap_size, i, item); } while (i-- > 0); } assert(is_heap(first, last, less_comparer)); }