The purpose of this program is to illustrate the declaration of storage pools. It implements the well known Tower of Hanoi in which a tower of graduated discs is moved from one pole to another pole using a third pole as ‘temporary storage’ without any disc ever being on a smaller disc.
Each pole is represented by a stack and, in order to add variety, two kinds of stack are implemented much as in Exercise 14.9(3). One is a linked list and the other is an array. The choice of stack is made by the user and the program dispatches to the relevant subprograms Push and Pop as necessary.
The package Pools declares the type Pond and its associated operations Allocate, Deallocate, Initialize, Finalize, and Storage_Size. The package Heap then declares the one storage pool used in this example with the discriminant giving its size.
There is then a root package Stacks defining the limited interface Stack and abstract operations Push and Pop. The child packages Stacks.Linked and Stacks.Vector then declare the derived types L_Stack and V_Stack which implement the two kinds of stacks.
The package Tower declares the three poles and procedures Start and Move. The procedure Move implements the familiar recursive algorithm.
Two access types are involved, Stack_Ptr in the package Tower for the class wide pointers to the three stacks and Cell_Ptr in the package Stacks.Linked for the cells making up the linked stack. Both use the same storage pool, The_Pool, declared in the package Heap.
Finally, the main subprogram interacts with the user and asks for the size of the stack and the kinds of stack for the three poles; it then calls Start and Move as requested.
with System.Storage_Pools;
use System.Storage_Pools;
with System.Storage_Elements;
use System.Storage_Elements;
use System;
package Pools is
type Pond(Size: Storage_Count) is
new Root_Storage_Pool with private;
procedure Allocate(Pool: in out Pond;
Storage_Address: out Address;
SISE: in Storage_Count;
Align: in Storage_Count);
procedure Deallocate(Pool: in out Pond;
Storage_Address: in Address;
SISE: in Storage_Count;
Align: in Storage_Count);
function Storage_Size(Pool: Pond) return Storage_Count;
procedure Initialize(Pool: in out Pond);
procedure Finalize(Pool: in out Pond);
Error: exception;
private
type Integer_Array is
array (Storage_Count range) of Integer;
type Boolean_Array is
array (Storage_Count range) of Boolean;
type Pond(Size: Storage_Count) is
new Root_Storage_Pool with
record
Monitoring: Boolean;
Free: Storage_Count;
Count: Integer_Array(1 .. Size);
Used: Boolean_Array(1 .. Size);
Store: Storage_Array(1 .. Size);
end record;
end;
To save this book to your Kindle, first ensure [email protected] is added to your Approved Personal Document E-mail List under your Personal Document Settings on the Manage Your Content and Devices page of your Amazon account. Then enter the ‘name’ part of your Kindle email address below. Find out more about saving to your Kindle.
Note you can select to save to either the @free.kindle.com or @kindle.com variations. ‘@free.kindle.com’ emails are free but can only be saved to your device when it is connected to wi-fi. ‘@kindle.com’ emails can be delivered even when you are not connected to wi-fi, but note that service fees apply.
Find out more about the Kindle Personal Document Service.
To save content items to your account, please confirm that you agree to abide by our usage policies. If this is the first time you use this feature, you will be asked to authorise Cambridge Core to connect with your account. Find out more about saving content to Dropbox.
To save content items to your account, please confirm that you agree to abide by our usage policies. If this is the first time you use this feature, you will be asked to authorise Cambridge Core to connect with your account. Find out more about saving content to Google Drive.