% File "leaf.tu" % This file defines the leafNode class. % An leaf node is a single OOT string, with length up the the maximum % length of a string in OOT. It may be part of a bigger rope. % An internalNode is a specialization of a general Node. unit class leafNode % Parent class % ------------------------------------------------------------------ % An leafNode is a specialization of a general Node. % It inherits the constant and the subprogram headers from the Node % class. inherit Node in "node.tu" % Imports and exports % ------------------------------------------------------------------ % In addition to the imports and exports of its parent class, Node, % the leafNode class exports these two subprograms: export SetLeafValue, GetLeafValue % Instance variables % ------------------------------------------------------------------ % This is the variable that stores the actual node information. % Every instance of the class leafNode has its own instance of % this variable. var data : string := "" % Deallocate % ------------------------------------------------------------------ % This procedure has an empty body because there is nothing to do. % The memory used by a leaf node (just its `data' variable) can only % be deallocated by freeing something that points to it. Since it % is a leaf node, its parent points to it and takes care of the % freeing. body procedure Deallocate % Nothing to do! end Deallocate % Get % ------------------------------------------------------------------ % Sets this node containing the OOT string obtained by reading % `numCharsToRead' characters from the given input stream. % The stream number -2 denotes input from the the standard input. % Carriage returns in the input are ignored. (Thus the user % is able to break long strings up into separate lines of input.) % Hint: Do the reading a single character at a time. This % can be accomplished using % var c : string(1) <-- might as well save memory % get c : 1 <-- gets one character and % doesn't expect white space body procedure Get(inStream : int, numCharsToRead : int) % end Get % Put % ------------------------------------------------------------------ % Prints the string represented by this node to the given stream. % (The special stream number -1 denotes output to the standard output.) % For readability, line breaks are printed every `lineWidth' characters. % Zero is a special case; if `lineWidth' is zero, no line breaks are % printed. % `linePos' indicates at what point we are currently in the output % line. This will be 1 if we haven't printed anything yet, and may % be any value between 1 and lineWidth if we are in the middle of % doing some printing. `linePos' is returned with its value updated % appropriately, based on the printing that we do. % Hint: Although this is just a leaf node (and hence only contains % an ordinary OOT string), it could still be long enough to require % line breaks. % Hint: a loop can handle this nicely; recursion is not needed. body procedure Put(outStream: int, var linePos : int, lineWidth : int) % end Put % SetLeafValue % ------------------------------------------------------------------ % Sets the value of this leafNode to the string `s'. % This is accomplished by setting the `data' instance variable % to `s'. procedure SetLeafValue(s : string) % end SetLeafValue % Size % ------------------------------------------------------------------ % Returns the number of characters in this leafNode. body function Size : int % end Size % GetLeafValue % ------------------------------------------------------------------ % Returns the value of this leafNode. % This is accomplished by returning the value of the `data' % instance variable. function GetLeafValue : string % end GetLeafValue % SubRope % ------------------------------------------------------------------ % Returns a leafNode holding a copy of the subrope between positions % `pos1' and `pos2' in this node. New memory is allocated to hold % the copy. % Hint: Since we extracting from a leaf node, which holds just % an ordinary OOT string, the extraction part is easy. body function SubRope(pos1, pos2 : int) : ^Node % end SubRope % Height % ------------------------------------------------------------------ % Returns the height of the tree that this node is root of. % Hint: Since it is a leaf node, the height is always just 1. body function Height : int % end Height end leafNode