klao@39: /* FIXME: Copyright ... klao@39: * klao@39: * This implementation is heavily based on STL's heap functions and klao@39: * the similar class by Alpar Juttner in IKTA... klao@39: */ klao@39: klao@39: /****** klao@39: * klao@172: * BinHeap klao@39: * klao@172: * Ez az osztaly item-prioritas parok tarolasara alkalmas binaris kupacot klao@39: * valosit meg. klao@172: * A kupacban legfolul mindig az a par talalhato, amiben a prioritas a klao@39: * legkisebb. (Gondolj a Dijkstra pont-tavolsag kupacara; igazabol ahhoz klao@39: * lett keszitve...) klao@39: * klao@172: * Megjegyzes: a kupacos temakorokben a prioritast kulcsnak szoktak nevezni, klao@172: * de mivel ez zavaro tud lenni a property-map-es kulcs-ertek szohasznalata klao@172: * miatt, megprobaltunk valami semleges elnevezeseket kitalalni. klao@39: * klao@39: * A hasznalatahoz szukseg van egy irhato/olvashato property_map-re, ami klao@172: * az itemekhez egy int-et tud tarolni (ezzel tudom megkeresni az illeto klao@39: * elemet a kupacban a csokkentes es hasonlo muveletekhez). klao@39: * A map-re csak referenciat tarol, ugy hogy a kupac elete folyan a map-nek klao@39: * is elnie kell. (???) klao@39: * klao@39: * Ketfele modon hasznalhato: klao@39: * Lusta mod: klao@172: * set(Item, Prio) metodussal pakolunk a kupacba, klao@39: * aztan o majd eldonti, hogy ez az elem mar benne van-e es ha igen, akkor klao@39: * csokkentettunk-e rajta, vagy noveltunk. klao@39: * Ehhez nagyon fontos, hogy az atadott property map inicializalva legyen klao@39: * minden szobajovo kulcs ertekre, -1 -es ertekkel! klao@39: * Es ilyen esetben a kulcsokrol lekerdezheto az allapotuk a state metodussal: klao@39: * (nem jart meg a kupacban PRE_HEAP=-1, epp a kupacban van IN_HEAP=0, klao@39: * mar kikerult a kupacbol POST_HEAP=-2). klao@39: * Szoval ebben a modban a kupac nagyjabol hasznalhato property_map-kent, csak klao@172: * meg meg tudja mondani a "legkisebb" prioritasu elemet. De csak nagyjabol, klao@39: * hiszen a kupacbol kikerult elemeknek elvesz az ertekuk... klao@39: * klao@39: * Kozvetlen mod: klao@172: * push(Item, Prio) metodussal belerakunk a kupacba (ha az illeto kulcs mar klao@39: * benn volt, akkor gaz). klao@172: * increase/decrease(Item i, Prio new_prio) metodusokkal lehet klao@172: * novelni/csokkenteni az illeto elemhez tartozo prioritast. (Ha nem volt klao@172: * megbenne a kupacban az illeto elem, vagy nem abba az iranyba valtoztattad klao@39: * az erteket, amerre mondtad -- gaz). klao@39: * klao@39: * Termeszetesen a fenti ket modot ertelemszeruen lehet keverni. klao@39: * Ja es mindig nagyon gaz, ha belepiszkalsz a map-be, amit a kupac klao@39: * hasznal. :-)) klao@39: * klao@39: * klao@39: * Bocs, most faradt vagyok, majd egyszer leforditom. (Misi) klao@39: * klao@39: */ klao@39: klao@39: klao@37: #ifndef BIN_HEAP_HH klao@37: #define BIN_HEAP_HH klao@37: klao@37: #include klao@37: #include klao@37: #include klao@37: alpar@114: namespace hugo { klao@37: klao@172: template > klao@37: class BinHeap { klao@37: klao@37: public: klao@172: typedef Item ItemType; klao@37: // FIXME: stl-ben nem ezt hivjak value_type -nak, hanem a kovetkezot... klao@172: typedef Prio PrioType; klao@172: typedef std::pair PairType; klao@172: typedef ItemIntMap ItemIntMapType; klao@172: typedef Compare PrioCompare; klao@37: klao@37: /** klao@172: * Each Item element have a state associated to it. It may be "in heap", klao@37: * "pre heap" or "post heap". The later two are indifferent from the klao@37: * heap's point of view, but may be useful to the user. klao@37: * klao@172: * The ItemIntMap _should_ be initialized in such way, that it maps klao@37: * PRE_HEAP (-1) to any element to be put in the heap... klao@37: */ klao@39: enum state_enum { klao@37: IN_HEAP = 0, klao@37: PRE_HEAP = -1, klao@37: POST_HEAP = -2 klao@37: }; klao@37: klao@37: private: klao@37: std::vector data; klao@37: Compare comp; klao@37: // FIXME: jo ez igy??? klao@172: ItemIntMap &iim; klao@37: klao@37: public: klao@172: BinHeap(ItemIntMap &_iim) : iim(_iim) {} klao@172: BinHeap(ItemIntMap &_iim, const Compare &_comp) : comp(_comp), iim(_iim) {} klao@37: klao@37: klao@37: int size() const { return data.size(); } klao@41: bool empty() const { return data.empty(); } klao@37: klao@37: private: klao@37: static int parent(int i) { return (i-1)/2; } klao@37: static int second_child(int i) { return 2*i+2; } klao@214: bool less(const PairType &p1, const PairType &p2) const { klao@37: return comp(p1.second, p2.second); klao@37: } klao@37: klao@37: int bubble_up(int hole, PairType p); klao@37: int bubble_down(int hole, PairType p, int length); klao@37: klao@37: void move(const PairType &p, int i) { klao@37: data[i] = p; klao@172: iim.set(p.first, i); klao@37: } klao@37: klao@41: void rmidx(int h) { klao@41: int n = data.size()-1; klao@41: if( h>=0 && h<=n ) { klao@172: iim.set(data[h].first, POST_HEAP); klao@41: if ( h0 ? klao@37: return data[0].first; klao@37: } klao@172: Prio topPrio() const { klao@37: // FIXME: test size>0 ? klao@37: return data[0].second; klao@37: } klao@37: klao@37: void pop() { klao@41: rmidx(0); klao@41: } klao@41: klao@172: void erase(const Item &i) { klao@172: rmidx(iim.get(i)); klao@37: } klao@37: klao@214: Prio get(const Item &i) const { klao@172: int idx = iim.get(i); klao@37: return data[idx].second; klao@37: } klao@214: Prio operator[](const Item &i) const { klao@214: return get(i); klao@214: } klao@172: void set(const Item &i, const Prio &p) { klao@172: int idx = iim.get(i); klao@37: if( idx < 0 ) { klao@172: push(i,p); klao@37: } klao@172: else if( comp(p, data[idx].second) ) { klao@172: bubble_up(idx, PairType(i,p)); klao@37: } klao@37: else { klao@172: bubble_down(idx, PairType(i,p), data.size()); klao@37: } klao@37: } klao@37: klao@172: void decrease(const Item &i, const Prio &p) { klao@172: int idx = iim.get(i); klao@172: bubble_up(idx, PairType(i,p)); klao@37: } klao@172: void increase(const Item &i, const Prio &p) { klao@172: int idx = iim.get(i); klao@172: bubble_down(idx, PairType(i,p), data.size()); klao@37: } klao@37: klao@172: state_enum state(const Item &i) const { klao@172: int s = iim.get(i); klao@39: if( s>=0 ) klao@39: s=0; klao@39: return state_enum(s); klao@39: } klao@39: klao@37: }; // class BinHeap klao@37: klao@37: klao@37: template klao@37: int BinHeap::bubble_up(int hole, PairType p) { klao@37: int par = parent(hole); klao@37: while( hole>0 && less(p,data[par]) ) { klao@37: move(data[par],hole); klao@37: hole = par; klao@37: par = parent(hole); klao@37: } klao@37: move(p, hole); klao@37: return hole; klao@37: } klao@37: klao@37: template klao@37: int BinHeap::bubble_down(int hole, PairType p, int length) { klao@37: int child = second_child(hole); klao@37: while(child < length) { klao@37: if( less(data[child-1], data[child]) ) { klao@37: --child; klao@37: } klao@37: if( !less(data[child], p) ) klao@37: goto ok; klao@37: move(data[child], hole); klao@37: hole = child; klao@37: child = second_child(hole); klao@37: } klao@37: child--; klao@37: if( child