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.

288 Persistence<br />

The procedure F<strong>in</strong>d uses a recursive descent of the tree to f<strong>in</strong>d the selected <strong>in</strong>dex. If the <strong>in</strong>dex is not found,<br />

then the exception Not_There is raised.<br />

function F<strong>in</strong>d( The:<strong>in</strong> Subtree; I:<strong>in</strong> Index) return Subtree is<br />

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

if The = null then raise Not_There; end if;<br />

if I = The.Rec.S_Index then<br />

return The;<br />

--Found<br />

else<br />

if I > The.Rec.S_Index<br />

then return F<strong>in</strong>d( The.Right, I ); --Try RHS<br />

else return F<strong>in</strong>d( The.Left, I ); --Try LHS<br />

end if;<br />

end if;<br />

end F<strong>in</strong>d;<br />

As the tree is built us<strong>in</strong>g dynamic storage, the storage must be released. The procedure Release_Storage<br />

carries out this task:<br />

procedure Dispose is<br />

new Unchecked_Deallocation( Leaf, Subtree );<br />

procedure Release_Storage( The:<strong>in</strong> out Subtree ) is<br />

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

if The /= null then --Not empty<br />

Release_Storage( The.Left ); --Free LHS<br />

Release_Storage( The.Right ); --Free RHS<br />

Dispose( The ); --Dispose of item<br />

end if;<br />

The := null;<br />

--Subtree root NULL<br />

end Release_Storage;<br />

end Class_Pic;<br />

Note:<br />

The implicit garbage collector could be used, but this would only be called when the <strong>in</strong>stance of the<br />

class Pic went out of scope. If this object is serially re-used then storage used by the program could<br />

become excessive.<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!