11.07.2015 Views

Advanced Programming Guide

Advanced Programming Guide

Advanced Programming Guide

SHOW MORE
SHOW LESS
  • No tags were found...

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

108 • Chapter 3: <strong>Programming</strong> with Modules> # priority queue.> delete := proc()> local val;> val := heap[ 1 ]; # val := top()> # move bottom to the top> heap[ 1 ] := heap[ nitems ];> # allow expression to be collected> heap[ nitems ] := evaln( heap[ nitems ] );> # decrement the bottom of heap counter> nitems := nitems - 1;> # heapify the array> bubbledown( 1 );> # return the value> val> end proc;>> # Insert an item into the priority queue.> insert := proc( v )> if nargs > 1 then> op( map( procname, [ args ] ) )> else> nitems := 1 + nitems;> heap[ nitems ] := v;> bubbleup( nitems )> end if> end proc;>> # Insert any intially specified items.> if lnargs > 1 then> insert( op( largs ) )> end if> end module> end proc:The constructor takes a Maple procedure priority as its argument. Foreach expression placed on the queue, this procedure returns a numericmeasure of its priority. Items on the queue are maintained in a prioritizedorder so that the highest priority items are removed first.In this sample computation with a priority queue, use the Maple builtinprocedure length as the priority of an expression. Here, the randomlygenerated expressions are all polynomials.> pq := PriorityQueue( x -> length( x ) );pq := module()local heap, nitems, bubbleup, bubbledown;export empty, top, insert, size, delete, init;description “a priority queue”;end module

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!