X-Git-Url: http://p2p-next.cs.pub.ro/gitweb/?a=blobdiff_plain;f=src%2Flibswift%2Fbinheap.cpp;fp=src%2Flibswift%2Fbinheap.cpp;h=58054d4d3609f6941a4b38629fc606f24919e779;hb=45963a7511531cd1656ad5d3847d2dafd015c54d;hp=0000000000000000000000000000000000000000;hpb=d069796805ad79542fd7e4406d1e9c6d2d8c2ef7;p=swifty.git diff --git a/src/libswift/binheap.cpp b/src/libswift/binheap.cpp new file mode 100644 index 0000000..58054d4 --- /dev/null +++ b/src/libswift/binheap.cpp @@ -0,0 +1,71 @@ +/* + * sbit.cpp + * serp++ + * + * Created by Victor Grishchenko on 4/1/09. + * Copyright 2009 Delft University of Technology. All rights reserved. + * + */ + +#include +#include + +#include "binheap.h" + + +binheap::binheap() { + size_ = 32; + heap_ = (bin_t*) malloc(size_*sizeof(bin_t)); + filled_ = 0; +} + +bool bincomp (const bin_t& a, const bin_t& b) { + register uint64_t ab = a.base_offset(), bb = b.base_offset(); + if (ab==bb) + return a.layer_bits() < b.layer_bits(); + else + return ab > bb; +} + +bool bincomp_rev (const bin_t& a, const bin_t& b) { + register uint64_t ab = a.base_offset(), bb = b.base_offset(); + if (ab==bb) + return a.layer_bits() > b.layer_bits(); + else + return ab < bb; +} + +bin_t binheap::pop() { + if (!filled_) + return bin_t::NONE; + bin_t ret = heap_[0]; + std::pop_heap(heap_, heap_+filled_--,bincomp); + while (filled_ && ret.contains(heap_[0])) + std::pop_heap(heap_, heap_+filled_--,bincomp); + return ret; +} + +void binheap::extend() { + std::sort(heap_,heap_+filled_,bincomp_rev); + int solid = 0; + for(int i=1; isize_) { + size_ <<= 1; + heap_ = (bin_t*) realloc(heap_,size_*sizeof(bin_t)); + } +} + +void binheap::push(bin_t val) { + if (filled_==size_) + extend(); + heap_[filled_++] = val; + std::push_heap(heap_, heap_+filled_,bincomp); +} + +binheap::~binheap() { + free(heap_); +} +