/* * lftp - file transfer program * * Copyright (c) 1996-2013 by Alexander V. Lukyanov (lav@yars.free.net) * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ #ifndef XHEAP_H #define XHEAP_H 1 // min-heap implementation with random remove #include #include "xarray.h" #define costly_assert(x) /*assert(x)*/ template class xheap { public: class node { T *obj; int heap_index; friend class xheap; public: node(T *t) : obj(t), heap_index(0) {} }; private: xarray heap; int count() const { return heap.count(); } node*& ptr(int i) { return heap[i-1]; } T& elem(int i) { return *(ptr(i)->obj); } void swap(int a,int b) { ptr(a)=replace_value(ptr(b),ptr(a)); ptr(a)->heap_index=a; ptr(b)->heap_index=b; } void siftup(int i) { while(i>1 && elem(i)heap_index=0; heap.chop(); } void fix(int i) { siftdown(i); siftup(i); } void remove(int i) { if(i==count()) { chop(); return; } assert(i>0 && i0 && i<=count()); assert(ptr(i)==&n); return; } heap.append(&n); siftup(n.heap_index=count()); costly_assert(is_heap(1,count())); } void fix(node& n) { fix(n.heap_index); } T *get_min() { return count()>0?ptr(1)->obj:0; } T *pop_min() { T *m=get_min(); if(!m) return 0; remove(1); return m; } void remove(node& x) { if(!x.heap_index) return; assert(ptr(x.heap_index)==&x); remove(x.heap_index); assert(!x.heap_index); } }; #endif//XHEAP_H