18.10.2014 Views

Object-oriented Software in Ada 95

Object-oriented Software in Ada 95

Object-oriented Software in Ada 95

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Conta<strong>in</strong>ers 251<br />

This mirrors closely the mechanism used to access sequentially the elements of an array <strong>in</strong> <strong>Ada</strong>.<br />

The list is implemented as a generic class list and its generic child Iterator. The two classes allow the<br />

elaboration of:<br />

• The object numbers which is the list of natural numbers.<br />

• The object num_it, an iterator which is used to step through the objects held <strong>in</strong> the list.<br />

17.1.1 List vs. array<br />

Criteria List Array<br />

The number of items held can be <strong>in</strong>creased √<br />

r<br />

at run-time.<br />

Deletion of an item leaves no gap when the √<br />

r<br />

items are iterated through.<br />

Random access is very efficient. r √<br />

Note:<br />

<strong>Ada</strong> array’s bounds are fixed once the declaration is elaborated.<br />

17.2 Methods implemented <strong>in</strong> a list<br />

The methods that are implemented <strong>in</strong> an <strong>in</strong>stance of the class list are as follows:<br />

Method Responsibility<br />

Initialize Initialize the conta<strong>in</strong>er.<br />

F<strong>in</strong>alize F<strong>in</strong>ish us<strong>in</strong>g the conta<strong>in</strong>er object.<br />

Adjust Used to facilitate a deep copy.<br />

= Comparison of a list for equality.<br />

Note: A full explanation of adjust can be found <strong>in</strong> Section 17.3.<br />

Whilst the methods that are implemented <strong>in</strong> an <strong>in</strong>stance of the class List_Iter are:<br />

Method Responsibility<br />

Initialize Initialize the iterator.<br />

F<strong>in</strong>alize F<strong>in</strong>ish us<strong>in</strong>g the iterator object.<br />

Deliver Deliver the object held at the position <strong>in</strong>dicated by the iterator.<br />

First Set the current position of the iterator to the first object <strong>in</strong> the<br />

list.<br />

Last<br />

Set the current position of the iterator to the last object <strong>in</strong> the list.<br />

Insert Insert <strong>in</strong>to the list an object before the current position of the<br />

iterator.<br />

Delete Remove and dispose of the object <strong>in</strong> the list which is specified<br />

by the current position of the iterator.<br />

Is_end Deliver true if the iteration on the conta<strong>in</strong>er has reached the end.<br />

Next<br />

Prev<br />

Move to the next item <strong>in</strong> the conta<strong>in</strong>er and make that the current<br />

position.<br />

Move to the previous item <strong>in</strong> the conta<strong>in</strong>er and make that the<br />

current position.<br />

17.2.1 Example of use<br />

The follow<strong>in</strong>g program illustrates the use of a list. In this program natural numbers are read and <strong>in</strong>serted <strong>in</strong><br />

ascend<strong>in</strong>g order <strong>in</strong>to a list. The contents of the list are then pr<strong>in</strong>ted.<br />

© M A Smith - May not be reproduced without permission

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

Saved successfully!

Ooh no, something went wrong!