36 #ifndef REPLACEMENT_QUEUE_H
37 #define REPLACEMENT_QUEUE_H
43 #define RHEAP_DEBUG if (0)
58 return s <<
"[" << p.
value <<
"]";
71 template <
class T,
class Compare>
110 int empty()
const {
return (size == 0); }
120 s <<
"Replacementheap " <<
this <<
": " << size <<
" runs";
121 for (
size_t i = 0; i < size; i++) {
122 s << endl <<
" <- i=" << i <<
": " << mergeHeap[i].
run;
124 mergeHeap[i].
run->name(&runname);
125 runlen = mergeHeap[i].
run->stream_len();
126 s <<
", " << runname <<
", len=" << runlen;
135 template <
class T,
class Compare>
141 assert(runList && g_arity > 0);
143 RHEAP_DEBUG cerr <<
"ReplacementHeap arity=" << g_arity <<
"\n";
149 for (
unsigned int i = 0; i < arity; i++) {
161 template <
class T,
class Compare>
166 cerr <<
"warning: ~ReplacementHeap: heap not empty!\n";
169 for (
size_t i = 0; i < size; i++) {
170 if (mergeHeap[i].run)
171 delete mergeHeap[i].run;
179 template <
class T,
class Compare>
186 cerr <<
"ReplacementHeap::addRun size =" << size <<
",arity=" << arity
187 <<
" full, cannot add another run.\n";
193 mergeHeap[size].run =
r;
200 cerr <<
"ReplacementHeap::addRun added run " << strname
201 <<
" (rheap size=" << size <<
")" << endl;
213 template <
class T,
class Compare>
217 assert(i < size && mergeHeap[i].run);
221 cerr <<
"ReplacementHeap::deleteRun deleting run " << i <<
", "
222 << mergeHeap[i].run << endl;
227 delete mergeHeap[i].run;
230 mergeHeap[i].value = mergeHeap[size - 1].value;
231 mergeHeap[i].run = mergeHeap[size - 1].run;
240 template <
class T,
class Compare>
255 err = mergeHeap[i].run->seek(0);
257 cerr <<
"ReplacementHeap::Init(): cannot seek run " << i <<
"\n";
262 err = mergeHeap[i].run->read_item(&elt);
269 cerr <<
"ReplacementHeap::Init(): cannot read run " << i
277 mergeHeap[i].value = *elt;
298 static inline size_t rheap_lchild(
size_t index)
303 static inline size_t rheap_rchild(
size_t index)
305 return 2 * index + 1;
309 static inline size_t rheap_parent(
size_t index)
316 template <
class T,
class Compare>
319 size_t min_index = i;
320 size_t lc = rheap_lchild(i);
321 size_t rc = rheap_rchild(i);
325 if ((lc < size) && (cmpobj.compare(mergeHeap[lc].value,
326 mergeHeap[min_index].value) == -1)) {
329 if ((rc < size) && (cmpobj.compare(mergeHeap[rc].value,
330 mergeHeap[min_index].value) == -1)) {
334 if (min_index != i) {
337 mergeHeap[min_index] = mergeHeap[i];
347 template <
class T,
class Compare>
352 for (
int i = rheap_parent(size - 1); i >= 0; i--) {
361 template <
class T,
class Compare>
368 min = mergeHeap[0].value;
372 err = mergeHeap[0].run->read_item(&elt);
376 RHEAP_DEBUG cerr <<
"rheap extract_min: run " << mergeHeap[0].run
377 <<
" empty. deleting\n ";
381 cerr <<
"ReplacementHeap::extract_min: cannot read\n";
388 mergeHeap[0].value = *elt;
@ AMI_ERROR_END_OF_STREAM
friend ostream & operator<<(ostream &s, const HeapElement &p)
ReplacementHeap(size_t arity, queue< char * > *runList)
ostream & print(ostream &s) const
void addRun(AMI_STREAM< T > *run)
#define assert(condition)
SYMBOL * err(FILE *fp, SYMBOL *s, char *msg)