II. Notes on Data Structuring * - Cornell University
II. Notes on Data Structuring * - Cornell University
II. Notes on Data Structuring * - Cornell University
Create successful ePaper yourself
Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.
NOTES ON DATA STRUCTURING 113<br />
ways in accordance with which of the alternative types it came from. Then<br />
<strong>on</strong>e may write<br />
with sv do {al"sl,<br />
a2;$2~<br />
a n "a n };<br />
where S~ is the statement to be selected for executi<strong>on</strong> whenever the value of<br />
the tag field of sv is a~. Within S~ it is guaranteed safe to assume that the<br />
value came from the corresp<strong>on</strong>ding alternative type, provided that the value<br />
of sv remains unchanged. C<strong>on</strong>sequently it is safe to use the comp<strong>on</strong>ent<br />
selectors which are defined for that alternative type by themselves to refer<br />
to the comp<strong>on</strong>ents of sv, just as in the case of a simple with statement<br />
described previously for a Cartesian product.<br />
If it is desired to regard a uni<strong>on</strong> type as ordered, the most natural ordering<br />
is that defined by taking all values corresp<strong>on</strong>ding to earlier alternatives in<br />
the list before any of the values of the later alternatives.<br />
Exercise<br />
Write a functi<strong>on</strong> that will compute the area of a figure as defined above.<br />
5.2. REPRESENTATION<br />
In representing a value from a discriminated uni<strong>on</strong> it is necessary first to<br />
represent the tag as an integer between zero and n - 1, where n is the number<br />
of alternative types. The tag is followed directly by the representati<strong>on</strong> of the<br />
value of the original type. As with the Cartesian product, there is a choice of<br />
the degree of packing used in a representati<strong>on</strong>.<br />
In the unpacked representati<strong>on</strong> the tag occupies a complete word, and the<br />
space occupied by each value of a uni<strong>on</strong> type is <strong>on</strong>e word more than that<br />
occupied by values from the largest alternative type. In a packed representa-<br />
ti<strong>on</strong>, this overhead can be reduced to a few bits. In the minimal representa-<br />
ti<strong>on</strong>, each value is obtained by adding its minimal representati<strong>on</strong> in the<br />
original type to the sum of the cardinalities of all preceding types in the<br />
uni<strong>on</strong>. Thus a value originating from the first type, for example (diam<strong>on</strong>d,<br />
four), has exactly the same value as it has in the original type, namely 16.<br />
But joker 1, with value zero in the original enumerati<strong>on</strong> type, has added to it<br />
the cardinality of the card type.<br />
The choice between unpacked, packed and tight packed representati<strong>on</strong>s<br />
is based <strong>on</strong> the same c<strong>on</strong>siderati<strong>on</strong>s as for Cartesian products; however the<br />
runtime speed penalty for the minimal representati<strong>on</strong> is a great deal less,