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

Create successful ePaper yourself

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

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

The procedure Next and Prev move the iterator on to the next / previous item <strong>in</strong> the list. If the iterator is not<br />

currently po<strong>in</strong>t<strong>in</strong>g at an item, the iterator is unmodified. The end of the list is <strong>in</strong>dicated by the iterator po<strong>in</strong>t<strong>in</strong>g to a<br />

null value. By <strong>in</strong>spect<strong>in</strong>g the list this case can be dist<strong>in</strong>guished from the case of an empty list.<br />

procedure Next( The:<strong>in</strong> out List_Iter ) is<br />

beg<strong>in</strong><br />

if The.Cur_Node /= null then --<br />

The.Cur_Node := The.Cur_Node.Next;<br />

end if;<br />

end Next;<br />

procedure Prev( The:<strong>in</strong> out List_Iter ) is<br />

beg<strong>in</strong><br />

if The.Cur_Node /= null then --<br />

The.Cur_Node := The.Cur_Node.Prev;<br />

end if;<br />

end Prev;<br />

end Class_List.Iterator;<br />

--Next<br />

--Previous<br />

Note:<br />

If you move the iterator to beyond the first element with prev then it is your responsibility to reset the<br />

iterator’s position. The class list will consider the position at the end of the list.<br />

17.3.2 Relationship between a list and its iterator<br />

The list on which the iterator navigates must be writable. This is because the iterator may be used to <strong>in</strong>sert or<br />

delete an item <strong>in</strong> the list. Another solution would have been to have two dist<strong>in</strong>ct iterators for read and write<br />

operations on the list.<br />

17.4 Limitations of the list implementation<br />

A limitation of this implementation is that a list object is physically duplicated when it is assigned. This is referred<br />

to as a deep copy of an object. A deep copy of an object can <strong>in</strong>volve the use of considerable time and storage<br />

space.<br />

There are two options for the implementation of assignment. These options are summarized <strong>in</strong> the table below:<br />

Type of copy<br />

Deep copy<br />

Shallow copy<br />

Commentary<br />

The whole physical data structure is duplicated.<br />

Only the po<strong>in</strong>ter held directly <strong>in</strong> the object is duplicated.<br />

For example, consider the data structure Orig<strong>in</strong>al represent<strong>in</strong>g a list of colours held as a l<strong>in</strong>ked list. This is<br />

illustrated <strong>in</strong> Figure 17.5 which shows the memory layout for the list conta<strong>in</strong>er Orig<strong>in</strong>al which holds the three<br />

colours Red, Green and Blue.<br />

Orig<strong>in</strong>al<br />

Red<br />

Green<br />

Blue<br />

Figure 17.5 Illustration of the memory layout for a l<strong>in</strong>ked list of three colours.<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!