Priority Queue
Instructions
Copy and paste the below code into a compatible MUSH or MUX.
MUSHCode for Priority Queue
@create Priority Queue Routines
@lock Priority Queue Routines==me
@set Priority Queue Routines = VISUAL
@set Priority Queue Routines = DEBUG
@set Priority Queue Routines = NO_COMMAND
&DESCRIBE Priority Queue Routines=A PQueue with a heap implementation
@set Priority Queue Routines/DESCRIBE=no_command visual public nearby
&EXCHANGE Priority Queue Routines=setq(T, extract(%0, %1, 1, %3))[replace(replace(%0, %1, extract(%0, %2, 1, %3), %3), %2, %qT, %3)]
&FIXDOWN Priority Queue Routines=switch(mul(%1, 2), <[inc(words(r(%0), %3))], u(fixdown2, %0, %1, %2, %3, if(and(lt(#$, words(r(%0), %3)), ulocal(%2, extract(r(%0), #$, 1, %3), extract(r(%0), inc(#$), 1, %3))), inc(#$), #$)))
&FIXDOWN2 Priority Queue Routines=if(ulocal(%2, extract(r(%0), %1, 1, %3), extract(r(%0), %4, 1, %3)), setq(%0, ulocal(exchange, r(%0), %1, %4, %3))[u(fixdown, %0, %4, %2, %3)])
&FIXUP Priority Queue Routines=if(cand(gt(%1, 1), ulocal(%2, extract(r(%0), div(%1, 2), 1, %3), extract(r(%0), %1, 1, %3))), setq(%0, u(exchange, r(%0), %1, div(%1, 2), %3))[u(fixdown, %0, div(%1, 2), %2, %3)])
< Priority Queue Routines=lt(%0, %1)
&POPMAX Priority Queue Routines=first(r(%0), %3)[setq(%0, u(fixdown, r(%0), 1, %1/%2, %3))]
&POPMAX_HELP Priority Queue Routines=%0 = q-register queue is in. %1 = object comparitor is on. %2 = comparitor attribute. Returns max element from pqueue. %3 = Delimiter
&PUSH_ITEM Priority Queue Routines=setq(%0, squish(r(%0)[if(strlen(%4), %4, %b)]%1, if(strlen(%4), %4, %b))[u(fixup, %0, words(r(%0), %4), %2/%3, %4)]
&PUSH_ITEM_HELP Priority Queue Routines=%0 = q-register %1 new element %2 = obj %3 = attr %4 = delim
@lock Priority Queue Routines==me
@set Priority Queue Routines = VISUAL
@set Priority Queue Routines = DEBUG
@set Priority Queue Routines = NO_COMMAND
&DESCRIBE Priority Queue Routines=A PQueue with a heap implementation
@set Priority Queue Routines/DESCRIBE=no_command visual public nearby
&EXCHANGE Priority Queue Routines=setq(T, extract(%0, %1, 1, %3))[replace(replace(%0, %1, extract(%0, %2, 1, %3), %3), %2, %qT, %3)]
&FIXDOWN Priority Queue Routines=switch(mul(%1, 2), <[inc(words(r(%0), %3))], u(fixdown2, %0, %1, %2, %3, if(and(lt(#$, words(r(%0), %3)), ulocal(%2, extract(r(%0), #$, 1, %3), extract(r(%0), inc(#$), 1, %3))), inc(#$), #$)))
&FIXDOWN2 Priority Queue Routines=if(ulocal(%2, extract(r(%0), %1, 1, %3), extract(r(%0), %4, 1, %3)), setq(%0, ulocal(exchange, r(%0), %1, %4, %3))[u(fixdown, %0, %4, %2, %3)])
&FIXUP Priority Queue Routines=if(cand(gt(%1, 1), ulocal(%2, extract(r(%0), div(%1, 2), 1, %3), extract(r(%0), %1, 1, %3))), setq(%0, u(exchange, r(%0), %1, div(%1, 2), %3))[u(fixdown, %0, div(%1, 2), %2, %3)])
< Priority Queue Routines=lt(%0, %1)
&POPMAX Priority Queue Routines=first(r(%0), %3)[setq(%0, u(fixdown, r(%0), 1, %1/%2, %3))]
&POPMAX_HELP Priority Queue Routines=%0 = q-register queue is in. %1 = object comparitor is on. %2 = comparitor attribute. Returns max element from pqueue. %3 = Delimiter
&PUSH_ITEM Priority Queue Routines=setq(%0, squish(r(%0)[if(strlen(%4), %4, %b)]%1, if(strlen(%4), %4, %b))[u(fixup, %0, words(r(%0), %4), %2/%3, %4)]
&PUSH_ITEM_HELP Priority Queue Routines=%0 = q-register %1 new element %2 = obj %3 = attr %4 = delim