home *** CD-ROM | disk | FTP | other *** search
- /*******************************************************************************
- +
- + LEDA 2.1.1 11-15-1991
- +
- +
- + PRIO_M.h
- +
- +
- + Copyright (c) 1991 by Max-Planck-Institut fuer Informatik
- + Im Stadtwald, 6600 Saarbruecken, FRG
- + All rights reserved.
- +
- *******************************************************************************/
-
-
-
-
- #ifndef PRIO_H
- #define PRIO_H
-
-
- #include <LEDA/f_heap.h>
-
-
- typedef f_heap_item pq_item;
-
- #define prio(ktype,itype) name3(itype,ktype,prio)
-
-
- #define priodeclare2(ktype,itype)\
- \
- class prio(ktype,itype) : public f_heap\
- {\
- int cmp(ent& x, ent& y) const { return compare(*(itype*)&x,*(itype*)&y); }\
- void print_key(ent& x) const { Print(*(itype*)&x); }\
- void print_inf(ent& x) const { Print(*(ktype*)&x); }\
- void clear_key(ent& x) const { Clear(*(itype*)&x); }\
- void clear_inf(ent& x) const { Clear(*(ktype*)&x); }\
- void copy_key(ent& x) const { Copy(*(itype*)&x); }\
- void copy_inf(ent& x) const { Copy(*(ktype*)&x); }\
- \
- public:\
- \
- virtual pq_item insert(ktype k,itype i) { return f_heap::insert(Ent(i),Ent(k));}\
- virtual pq_item find_min() const { return f_heap::find_min();}\
- virtual ktype del_min() { ktype x = key(find_min()); f_heap::del_min(); return x; }\
- virtual ktype key(pq_item x) const { return ktype(f_heap::inf(x)); }\
- virtual itype inf(pq_item x) const { return itype(f_heap::key(x)); }\
- virtual void change_key(pq_item x, ktype k) { f_heap::change_inf(x,Ent(k)); }\
- virtual void decrease_inf(pq_item x,itype i){ f_heap::decrease_key(x,Ent(i)); }\
- virtual void del_item(pq_item x) { f_heap::del_item(x); }\
- \
- virtual bool empty() const { return f_heap::empty(); }\
- virtual int size() const { return f_heap::size(); }\
- \
- virtual pq_item first_item() const { return f_heap::first_item(); }\
- virtual pq_item next_item(pq_item it) const { return f_heap::next_item(it); }\
- \
- virtual prio(ktype,itype)& operator=(const prio(ktype,itype)& Q) { return (prio(ktype,itype)&)f_heap::operator=((f_heap&)Q); }\
- \
- prio(ktype,itype)() {}\
- prio(ktype,itype)(const prio(ktype,itype)& Q) : f_heap((f_heap&)Q) {}\
- ~prio(ktype,itype)() { f_heap::clear(); }\
- };
-
- #define forall_pq_items(i,Q) for(i = (Q).first_item(); i; i=(Q).next_item(i))
-
-
-
- #define PRIO_ITEM(T) name2(T,_item)
-
- #define PRIO(ktype,itype,impl) name4(itype,ktype,impl,PRIO)
-
-
- #define PRIOdeclare3(ktype,itype,impl)\
- \
- class PRIO(ktype,itype,impl) : public prio(ktype,itype), public impl\
- {\
- int cmp(ent& x, ent& y) const { int b = compare(*(itype*)x,*(itype*)y);\
- return (b==0) ? int(x)-int(y) : b;}\
- \
- void print_key(ent& x) const { Print(*(itype*)x); }\
- void print_inf(ent& x) const { Print(*(ktype*)&x); }\
- void clear_key(ent&) const {}\
- void clear_inf(ent& x) const { Clear(*(ktype*)&x); }\
- void copy_key(ent&) const {}\
- void copy_inf(ent& x) const { Copy(*(ktype*)&x); }\
- \
- public:\
- \
- pq_item insert(ktype k,itype i) { itype* p=new itype; *p=i; Copy(*p);\
- return pq_item(impl::insert(Ent(p),Ent(k)));}\
- \
- pq_item find_min() const { return pq_item(impl::find_min());}\
- \
- ktype del_min() { pq_item it = find_min(); ktype x = key(it);\
- del_item(it); return x; }\
- \
- ktype key(pq_item x) const { return ktype(impl::inf(PRIO_ITEM(impl)(x))); }\
- itype inf(pq_item x) const { return *(itype*)impl::key(PRIO_ITEM(impl)(x));}\
- \
- void change_key(pq_item x, ktype k)\
- { impl::change_inf(PRIO_ITEM(impl)(x),Ent(k)); }\
- \
- void decrease_inf(pq_item x,itype i)\
- { itype* p=new itype; *p=i; Copy(*p);\
- impl::decrease_key(PRIO_ITEM(impl)(x),Ent(p));}\
- \
- void del_item(pq_item x) { delete (itype*)impl::key(PRIO_ITEM(impl)(x));\
- impl::del_item(PRIO_ITEM(impl)(x)); }\
- \
- bool empty() const { return impl::empty(); }\
- int size() const { return impl::size(); }\
- \
- pq_item first_item() const { return pq_item(impl::first_item()); }\
- pq_item next_item(pq_item it) const { return pq_item(impl::next_item(PRIO_ITEM(impl)(it))); }\
- \
- prio(ktype,itype)& operator=(const prio(ktype,itype)& Q)\
- { return (PRIO(ktype,itype,impl)&)impl::operator=(*(impl*)&Q); }\
- \
- PRIO(ktype,itype,impl)(const PRIO(ktype,itype,impl)& Q):impl((impl&)Q) {}\
- \
- PRIO(ktype,itype,impl)() {}\
- \
- ~PRIO(ktype,itype,impl)() { impl::clear(); }\
- };
-
-
-
- //------------------------------------------------------------------------------
- // Bounded Priority Queues
- //------------------------------------------------------------------------------
-
-
- #define B_PRIO_ITEM(T) name2(T,_item)
-
- #define B_PRIO(ktype,impl) name3(ktype,impl,B_PRIO)
-
-
- #define B_PRIOdeclare2(ktype,impl)\
- \
- class B_PRIO(ktype,impl) : public prio(ktype,int), public impl\
- {\
- int cmp(ent& x, ent& y) const { return int(x) - int(y); }\
- void print_inf(ent& x) const { Print(int(x)); }\
- void print_key(ent& x) const { Print(*(ktype*)&x);}\
- void clear_key(ent& x) const { Clear(*(ktype*)&x);}\
- void copy_key(ent& x) const { Copy(*(ktype*)&x); }\
- \
- public:\
- \
- pq_item insert(ktype k,int i) { return pq_item(impl::insert(Ent(k),i));}\
- \
- pq_item find_min() const { return pq_item(impl::find_min());}\
- \
- ktype del_min() { return ktype(impl::del_min()); }\
- \
- ktype key(pq_item x) const { return ktype(impl::key(B_PRIO_ITEM(impl)(x))); }\
- int inf(pq_item x) const { return impl::inf(B_PRIO_ITEM(impl)(x));}\
- \
- void change_key(pq_item x, ktype k)\
- { impl::change_key(B_PRIO_ITEM(impl)(x),Ent(k)); }\
- \
- void decrease_inf(pq_item x, int i)\
- { impl::decrease_inf(B_PRIO_ITEM(impl)(x),i);}\
- \
- void del_item(pq_item x) { impl::del_item(B_PRIO_ITEM(impl)(x)); }\
- \
- bool empty() const { return impl::empty(); }\
- int size() const { return impl::size(); }\
- \
- B_PRIO(ktype,impl)(int N) : impl(N) {}\
- ~B_PRIO(ktype,impl)() { impl::clear(); }\
- };
-
-
- #endif
-