37 #ifndef REPLACEMENT_QUEUE_H 38 #define REPLACEMENT_QUEUE_H 44 #define RHEAP_DEBUG if(0) 60 return s <<
"[" << p.
value <<
"]";
78 template<
class T,
class Compare>
92 void heapify(
size_t i);
108 void deleteRun(
size_t i);
130 s <<
"Replacementheap " <<
this <<
": " << size <<
" runs";
131 for(
size_t i=0; i<size; i++) {
132 s << endl <<
" <- i=" << i<<
": " << mergeHeap[i].
run;
134 mergeHeap[i].
run->name(&runname);
135 runlen = mergeHeap[i].
run->stream_len();
136 s <<
", " << runname <<
", len=" << runlen;
149 template<
class T,
class Compare>
154 assert(runList && g_arity > 0);
156 RHEAP_DEBUG cerr <<
"ReplacementHeap arity=" << g_arity <<
"\n";
162 for (
unsigned int i=0; i< arity; i++) {
175 template<
class T,
class Compare>
179 cerr <<
"warning: ~ReplacementHeap: heap not empty!\n";
182 for(
size_t i=0; i<size; i++) {
183 if (mergeHeap[i].
run)
184 delete mergeHeap[i].run;
194 template<
class T,
class Compare>
201 cerr <<
"ReplacementHeap::addRun size =" << size <<
",arity=" << arity
202 <<
" full, cannot add another run.\n";
208 mergeHeap[size].run =
r;
214 cerr <<
"ReplacementHeap::addRun added run " << strname
215 <<
" (rheap size=" << size <<
")" << endl;
231 template<
class T,
class Compare>
235 assert(i >= 0 && i < size && mergeHeap[i].
run);
239 cerr <<
"ReplacementHeap::deleteRun deleting run " << i <<
", " 240 << mergeHeap[i].run << endl;
246 delete mergeHeap[i].run;
249 mergeHeap[i].value = mergeHeap[size-1].value;
250 mergeHeap[i].run = mergeHeap[size-1].run;
262 template<
class T,
class Compare>
277 err = mergeHeap[i].run->seek(0);
279 cerr <<
"ReplacementHeap::Init(): cannot seek run " << i <<
"\n";
284 err = mergeHeap[i].run->read_item(&elt);
290 cerr <<
"ReplacementHeap::Init(): cannot read run " << i <<
"\n";
296 mergeHeap[i].value = *elt;
317 static inline size_t rheap_lchild(
size_t index) {
321 static inline size_t rheap_rchild(
size_t index) {
322 return 2 * index + 1;
326 static inline size_t rheap_parent(
size_t index) {
334 template<
class T,
class Compare>
337 size_t min_index = i;
338 size_t lc = rheap_lchild(i);
339 size_t rc = rheap_rchild(i);
342 assert(i >= 0 && i < size);
344 (cmpobj.compare(mergeHeap[lc].value, mergeHeap[min_index].value) == -1)) {
348 (cmpobj.compare(mergeHeap[rc].
value, mergeHeap[min_index].value) == -1)) {
352 if (min_index != i) {
355 mergeHeap[min_index] = mergeHeap[i];
366 template<
class T,
class Compare>
371 for (
int i = rheap_parent(size-1); i>=0; i--) {
381 template<
class T,
class Compare>
388 min = mergeHeap[0].value;
393 err = mergeHeap[0].run->read_item(&elt);
397 RHEAP_DEBUG cerr <<
"rheap extract_min: run " << mergeHeap[0].run
398 <<
" empty. deleting\n ";
401 cerr <<
"ReplacementHeap::extract_min: cannot read\n";
407 mergeHeap[0].value = *elt;
411 if (size > 0) heapify(0);
ostream & print(ostream &s) const
friend ostream & operator<<(ostream &s, const HeapElement &p)
SYMBOL * err(FILE *fp, SYMBOL *s, char *msg)
#define assert(condition)
void addRun(AMI_STREAM< T > *run)
AMI_err name(char **stream_name)
ReplacementHeap(size_t arity, queue< char *> *runList)