Summary
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;
- Type
- Chapter
- Information
- Programming in Ada 2012 , pp. 803 - 806Publisher: Cambridge University PressPrint publication year: 2014