#ifndef HEAP_INCLUDED // -*- C++ -*-
#define HEAP_INCLUDED

#include "Geom.hpp"
#include "Array.hpp"

#define NOT_IN_HEAP -47

//
//
// This file extracted from ~/anim/lab/mlab
//
//

class heap_node {
	public:
		real import;
		real factor;

		Labelled *obj;

		heap_node() { obj=NULL; import=0.0; factor = 1.0f; }
		heap_node(Labelled *t, double i=0.0) { obj=t; import=i; factor = 1.0f; }
		heap_node(const heap_node& h) { import=h.import; obj=h.obj; factor = h.factor; }

		void redo(void *ptr) { factor = obj->redo(ptr); }
};



class Heap : public arrayX<heap_node> {

    //
    // The actual size of the heap.  array::length()
    // simply returns the amount of allocated space
    int size;

    void swap(int i, int j);

    int parent(int i) { return (i-1)/2; }
    int left(int i) { return 2*i+1; }
    int right(int i) { return 2*i+2; }

    void upheap(int i);
    void downheap(int i);

public:

    Heap() { size=0; }
    Heap(int s) : arrayX<heap_node>(s) { size=0; }

	int GetSize() { return size; }

    void insert(Labelled *, real, real);
    void update(Labelled *, real, real);
	void redo(void *ptr) { for(int i=0;i<size;i++) ref(i).redo(ptr); }

    heap_node *extract();
    heap_node *top() { return size<1 ? (heap_node *)NULL : &ref(0); }
    heap_node *kill(int i);
};



#endif
