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.

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

When compar<strong>in</strong>g two lists, the physical storage of the list needs to be compared, rather than the access values<br />

which po<strong>in</strong>t to the storage for the list. Remember, two lists may conta<strong>in</strong> equal contents yet be represented by<br />

different physical lists.<br />

function "=" ( F:<strong>in</strong> List; S:<strong>in</strong> List ) return Boolean is<br />

F_Node : P_Node := F.First_Node; --First list<br />

S_Node : P_Node := S.First_Node; --Second list<br />

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

while F_Node /= null and S_Node /= null loop<br />

if F_Node.Item /= S_Node.Item then<br />

return False; --Different items<br />

end if;<br />

F_Node := F_Node.Next; S_Node := S_Node.Next;<br />

end loop;<br />

return F_Node = S_Node; --Both NULL if equal<br />

end "=";<br />

Note: When = is overloaded, /= is also overloaded with the def<strong>in</strong>ition of not =.<br />

This is true for = which returns a Boolean value.<br />

17.3.1 The list iterator<br />

The specification for the iterator, which is implemented as a child package of the package Class_List, is:<br />

generic<br />

package Class_List.Iterator is<br />

type List_Iter is limited private;<br />

procedure First( The:<strong>in</strong> out List_Iter; L:<strong>in</strong> out List );<br />

procedure Last( The:<strong>in</strong> out List_Iter; L:<strong>in</strong> out List );<br />

function Deliver( The:<strong>in</strong> List_Iter) return T;<br />

procedure Insert( The:<strong>in</strong> out List_Iter; Data:<strong>in</strong> T );<br />

procedure Delete( The:<strong>in</strong> out List_Iter );<br />

function Is_End( The:<strong>in</strong> List_Iter ) return Boolean;<br />

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

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

private<br />

type P_P_Node is access all P_Node;<br />

type List_Iter is record<br />

Cur_List_First: P_P_Node := null; --First <strong>in</strong> cha<strong>in</strong><br />

Cur_List_Last : P_P_Node := null; --Last <strong>in</strong> cha<strong>in</strong><br />

Cur_Node : P_Node := null; --Current item<br />

end record;<br />

end Class_List.Iterator;<br />

Note:<br />

The child package Class_List.Iterator must be generic as its parent is generic.<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!