36 #ifndef REPLACEMENT_HEAPBLOCK_H
37 #define REPLACEMENT_HEAPBLOCK_H
44 #define RBHEAP_DEBUG if (0)
59 return s <<
"[" << p.
value <<
"]";
72 template <
class T,
class Compare>
111 int empty()
const {
return (size == 0); }
119 s <<
"ReplacementheapBlock " <<
this <<
": " << size <<
" runs";
123 for (
size_t i = 0; i < size; i++) {
124 s << endl <<
" <- i=" << i <<
": " << mergeHeap[i].
run;
126 mergeHeap[i].
run->name(&runname);
127 runlen = mergeHeap[i].
run->stream_len();
128 s <<
", " << runname <<
", len=" << runlen;
139 template <
class T,
class Compare>
146 arity = runList->length();
152 for (
unsigned int i = 0; i < arity; i++) {
155 runList->dequeue(&str);
163 template <
class T,
class Compare>
168 cerr <<
"warning: ~ReplacementHeapBlock: heap not empty!\n";
171 for (
size_t i = 0; i < size; i++) {
172 if (mergeHeap[i].run)
173 delete mergeHeap[i].run;
181 template <
class T,
class Compare>
188 cerr <<
"ReplacementHeapBlockBlock::addRun size =" << size
189 <<
",arity=" << arity <<
" full, cannot add another run.\n";
195 mergeHeap[size].run =
r;
202 cerr <<
"ReplacementHeapBlock::addRun added run " << strname
203 <<
" (rheap size=" << size <<
")" << endl;
215 template <
class T,
class Compare>
219 assert(i < size && mergeHeap[i].run);
223 cerr <<
"ReplacementHeapBlock::deleteRun deleting run " << i <<
", "
224 << mergeHeap[i].run << endl;
229 delete mergeHeap[i].run;
232 mergeHeap[i].value = mergeHeap[size - 1].value;
233 mergeHeap[i].run = mergeHeap[size - 1].run;
242 template <
class T,
class Compare>
257 err = mergeHeap[i].run->seek(0);
259 cerr <<
"ReplacementHeapBlock::Init(): cannot seek run " << i
265 err = mergeHeap[i].run->read_item(&elt);
272 cerr <<
"ReplacementHeapBlock::Init(): cannot read run " << i
280 mergeHeap[i].value = *elt;
289 template <
class T,
class Compare>
292 size_t min_index = i;
293 size_t lc = rheap_lchild(i);
294 size_t rc = rheap_rchild(i);
298 if ((lc < size) && (cmpobj.compare(mergeHeap[lc].value,
299 mergeHeap[min_index].value) == -1)) {
302 if ((rc < size) && (cmpobj.compare(mergeHeap[rc].value,
303 mergeHeap[min_index].value) == -1)) {
307 if (min_index != i) {
310 mergeHeap[min_index] = mergeHeap[i];
320 template <
class T,
class Compare>
325 for (
int i = rheap_parent(size - 1); i >= 0; i--) {
334 template <
class T,
class Compare>
341 min = mergeHeap[0].value;
345 err = mergeHeap[0].run->read_item(&elt);
349 RBHEAP_DEBUG cerr <<
"rheap extract_min: run " << mergeHeap[0].run
350 <<
" empty. deleting\n ";
354 cerr <<
"ReplacementHeapBlock::extract_min: cannot read\n";
361 mergeHeap[0].value = *elt;
@ AMI_ERROR_END_OF_STREAM
friend ostream & operator<<(ostream &s, const BlockHeapElement &p)
void addRun(MEM_STREAM< T > *run)
ReplacementHeapBlock(queue< MEM_STREAM< T > * > *runList)
ostream & print(ostream &s) const
#define assert(condition)
SYMBOL * err(FILE *fp, SYMBOL *s, char *msg)